Better log messages.
[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.7  1996-02-19 15:41:55  adam
10  * Better log messages.
11  * Minor improvement of connect method.
12  *
13  * Revision 1.6  1996/02/06  09:22:54  adam
14  * Ported ir-tcl to use beta releases of tcl7.5/tk4.1.
15  *
16  * Revision 1.5  1995/11/28  13:53:40  quinn
17  * Windows port.
18  *
19  * Revision 1.4  1995/10/17  12:18:59  adam
20  * Bug fix: when target connection closed, the connection was not
21  * properly reestablished.
22  *
23  * Revision 1.3  1995/08/04  11:32:40  adam
24  * More work on output queue. Memory related routines moved
25  * to mem.c
26  *
27  * Revision 1.2  1995/08/03  13:23:01  adam
28  * Request queue.
29  *
30  * Revision 1.1  1995/07/28  10:28:39  adam
31  * First work on request queue.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <assert.h>
39
40 #include "ir-tclp.h"
41
42 int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
43                       const char *msg, const char *object_name)
44 {
45     IrTcl_Request **rp;
46
47     if (!z_APDU (p->odr_out, &apdu, 0))
48     {
49         Tcl_AppendResult (interp, odr_errmsg (odr_geterror (p->odr_out)),
50                           NULL);
51         odr_reset (p->odr_out);
52         return TCL_ERROR;
53     }
54     rp = &p->request_queue;
55     while (*rp)
56         rp = &(*rp)->next;
57     *rp = ir_tcl_malloc (sizeof(**rp));
58     (*rp)->next = NULL;
59     
60     if (ir_tcl_strdup (interp, &(*rp)->object_name, object_name) == TCL_ERROR)
61         return TCL_ERROR;
62     if (ir_tcl_strdup (interp, &(*rp)->callback, p->callback) == TCL_ERROR)
63         return TCL_ERROR;
64     
65     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
66     odr_setbuf (p->odr_out, NULL, 0, 1);
67     odr_reset (p->odr_out);
68     if (p->state == IR_TCL_R_Idle)
69     {
70         logf (LOG_DEBUG, "send_apdu. Sending %s", msg);
71         if (ir_tcl_send_q (p, p->request_queue, msg) == TCL_ERROR)
72         {
73             sprintf (interp->result, "cs_put failed in %s", msg);
74             return TCL_ERROR;
75         } 
76     }
77     else
78         logf (LOG_DEBUG, "send_apdu. Not idle (%s)", msg);
79     return TCL_OK;
80 }
81
82 int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rp, const char *msg)
83 {
84     int r;
85
86     assert (rp);
87     r = cs_put (p->cs_link, rp->buf_out, rp->len_out);
88     if (r < 0)
89         return TCL_ERROR;
90     else if (r == 1)
91     {
92 #if IRTCL_GENERIC_FILES
93         ir_select_add_write (p->csFile, p);
94 #else
95         ir_select_add_write (cs_fileno (p->cs_link), p);
96 #endif
97         logf (LOG_DEBUG, "Send part of %s", msg);
98         p->state = IR_TCL_R_Writing;
99     }
100     else
101     {
102         logf (LOG_DEBUG, "Send %s (%d bytes) fd=%d", msg, rp->len_out,
103               cs_fileno(p->cs_link));
104         p->state = IR_TCL_R_Waiting;
105         free (rp->buf_out);
106         rp->buf_out = NULL;
107     }
108     return TCL_OK;
109 }
110
111 void ir_tcl_del_q (IrTcl_Obj *p)
112 {
113     IrTcl_Request *rp, *rp1;
114
115     for (rp = p->request_queue; rp; rp = rp1)
116     {
117         free (rp->object_name);
118         free (rp->callback);
119         free (rp->buf_out);
120         rp1 = rp->next;
121         free (rp);
122     }
123     p->request_queue = NULL;
124 }
125
126
127