The Tcl_File structure is only manipulated in the Tk-event interface
[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.9  1996-03-20 13:54:05  adam
10  * The Tcl_File structure is only manipulated in the Tk-event interface
11  * in tkinit.c.
12  *
13  * Revision 1.8  1996/03/05  09:21:20  adam
14  * Bug fix: memory used by GRS records wasn't freed.
15  * Rewrote some of the error handling code - the connection is always
16  * closed before failback is called.
17  * If failback is defined the send APDU methods (init, search, ...) will
18  * return OK but invoke failback (as is the case if the write operation
19  * fails).
20  * Bug fix: ref_count in assoc object could grow if fraction of PDU was
21  * read.
22  *
23  * Revision 1.7  1996/02/19  15:41:55  adam
24  * Better log messages.
25  * Minor improvement of connect method.
26  *
27  * Revision 1.6  1996/02/06  09:22:54  adam
28  * Ported ir-tcl to use beta releases of tcl7.5/tk4.1.
29  *
30  * Revision 1.5  1995/11/28  13:53:40  quinn
31  * Windows port.
32  *
33  * Revision 1.4  1995/10/17  12:18:59  adam
34  * Bug fix: when target connection closed, the connection was not
35  * properly reestablished.
36  *
37  * Revision 1.3  1995/08/04  11:32:40  adam
38  * More work on output queue. Memory related routines moved
39  * to mem.c
40  *
41  * Revision 1.2  1995/08/03  13:23:01  adam
42  * Request queue.
43  *
44  * Revision 1.1  1995/07/28  10:28:39  adam
45  * First work on request queue.
46  *
47  */
48
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <ctype.h>
52 #include <assert.h>
53
54 #include "ir-tclp.h"
55
56 int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
57                       const char *msg, const char *object_name)
58 {
59     IrTcl_Request **rp;
60
61     if (!z_APDU (p->odr_out, &apdu, 0))
62     {
63         Tcl_AppendResult (interp, odr_errmsg (odr_geterror (p->odr_out)),
64                           NULL);
65         odr_reset (p->odr_out);
66         return TCL_ERROR;
67     }
68     rp = &p->request_queue;
69     while (*rp)
70         rp = &(*rp)->next;
71     *rp = ir_tcl_malloc (sizeof(**rp));
72     (*rp)->next = NULL;
73     
74     if (ir_tcl_strdup (interp, &(*rp)->object_name, object_name) == TCL_ERROR)
75         return TCL_ERROR;
76     if (ir_tcl_strdup (interp, &(*rp)->callback, p->callback) == TCL_ERROR)
77         return TCL_ERROR;
78     
79     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
80     odr_setbuf (p->odr_out, NULL, 0, 1);
81     odr_reset (p->odr_out);
82     if (p->state == IR_TCL_R_Idle)
83     {
84         logf (LOG_DEBUG, "send_apdu. Sending %s", msg);
85         if (ir_tcl_send_q (p, p->request_queue, msg) == TCL_ERROR)
86         {
87             if (p->failback)
88             {
89                 ir_tcl_disconnect (p);
90                 p->failInfo = IR_TCL_FAIL_WRITE;
91                 ir_tcl_eval (interp, p->failback);
92                 return TCL_OK;
93             }
94             else 
95             {
96                 sprintf (interp->result, "cs_put failed in %s", msg);
97                 return TCL_ERROR;
98             }
99         } 
100     }
101     else
102         logf (LOG_DEBUG, "send_apdu. Not idle (%s)", msg);
103     return TCL_OK;
104 }
105
106 int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rp, const char *msg)
107 {
108     int r;
109
110     assert (rp);
111     r = cs_put (p->cs_link, rp->buf_out, rp->len_out);
112     if (r < 0)
113         return TCL_ERROR;
114     else if (r == 1)
115     {
116         ir_select_add_write (cs_fileno (p->cs_link), p);
117         logf (LOG_DEBUG, "Send part of %s", msg);
118         p->state = IR_TCL_R_Writing;
119     }
120     else
121     {
122         logf (LOG_DEBUG, "Send %s (%d bytes) fd=%d", msg, rp->len_out,
123               cs_fileno(p->cs_link));
124         p->state = IR_TCL_R_Waiting;
125         free (rp->buf_out);
126         rp->buf_out = NULL;
127     }
128     return TCL_OK;
129 }
130
131 void ir_tcl_del_q (IrTcl_Obj *p)
132 {
133     IrTcl_Request *rp, *rp1;
134
135     p->state = IR_TCL_R_Idle;
136     for (rp = p->request_queue; rp; rp = rp1)
137     {
138         free (rp->object_name);
139         free (rp->callback);
140         free (rp->buf_out);
141         rp1 = rp->next;
142         free (rp);
143     }
144     p->request_queue = NULL;
145 }
146
147
148