Windows port.
[ir-tcl-moved-to-github.git] / queue.c
1
2 /*
3  * IR toolkit for tcl/tk
4  * (c) Index Data 1995
5  * See the file LICENSE for details.
6  * Sebastian Hammer, Adam Dickmeiss
7  *
8  * $Log: queue.c,v $
9  * Revision 1.5  1995-11-28 13:53:40  quinn
10  * Windows port.
11  *
12  * Revision 1.4  1995/10/17  12:18:59  adam
13  * Bug fix: when target connection closed, the connection was not
14  * properly reestablished.
15  *
16  * Revision 1.3  1995/08/04  11:32:40  adam
17  * More work on output queue. Memory related routines moved
18  * to mem.c
19  *
20  * Revision 1.2  1995/08/03  13:23:01  adam
21  * Request queue.
22  *
23  * Revision 1.1  1995/07/28  10:28:39  adam
24  * First work on request queue.
25  *
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <assert.h>
32
33 #include "ir-tclp.h"
34
35 int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
36                       const char *msg, const char *object_name)
37 {
38     IrTcl_Request **rp;
39
40     if (!z_APDU (p->odr_out, &apdu, 0))
41     {
42         Tcl_AppendResult (interp, odr_errmsg (odr_geterror (p->odr_out)),
43                           NULL);
44         odr_reset (p->odr_out);
45         return TCL_ERROR;
46     }
47     rp = &p->request_queue;
48     while (*rp)
49         rp = &(*rp)->next;
50     *rp = ir_tcl_malloc (sizeof(**rp));
51     (*rp)->next = NULL;
52     
53     if (ir_tcl_strdup (interp, &(*rp)->object_name, object_name) == TCL_ERROR)
54         return TCL_ERROR;
55     if (ir_tcl_strdup (interp, &(*rp)->callback, p->callback) == TCL_ERROR)
56         return TCL_ERROR;
57     
58     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
59     odr_setbuf (p->odr_out, NULL, 0, 1);
60     odr_reset (p->odr_out);
61     if (p->state == IR_TCL_R_Idle)
62     {
63         logf (LOG_DEBUG, "send_apdu. Sending %s", msg);
64         if (ir_tcl_send_q (p, p->request_queue, msg) == TCL_ERROR)
65         {
66             sprintf (interp->result, "cs_put failed in %s", msg);
67             return TCL_ERROR;
68         } 
69     }
70     else
71         logf (LOG_DEBUG, "send_apdu. Not idle (%s)", msg);
72     return TCL_OK;
73 }
74
75 int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rp, const char *msg)
76 {
77     int r;
78
79     assert (rp);
80     r = cs_put (p->cs_link, rp->buf_out, rp->len_out);
81     if (r < 0)
82         return TCL_ERROR;
83     else if (r == 1)
84     {
85         ir_select_add_write (cs_fileno (p->cs_link), p);
86         logf (LOG_DEBUG, "Send part of %s", msg);
87         p->state = IR_TCL_R_Writing;
88     }
89     else
90     {
91         logf (LOG_DEBUG, "Send %s (%d bytes)", msg, rp->len_out);
92         p->state = IR_TCL_R_Waiting;
93         free (rp->buf_out);
94         rp->buf_out = NULL;
95     }
96     return TCL_OK;
97 }
98
99 void ir_tcl_del_q (IrTcl_Obj *p)
100 {
101     IrTcl_Request *rp, *rp1;
102
103     for (rp = p->request_queue; rp; rp = rp1)
104     {
105         free (rp->object_name);
106         free (rp->callback);
107         free (rp->buf_out);
108         rp1 = rp->next;
109         free (rp);
110     }
111     p->request_queue = NULL;
112 }
113
114
115