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