More work on output queue. Memory related routines moved
[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.3  1995-08-04 11:32:40  adam
10  * More work on output queue. Memory related routines moved
11  * to mem.c
12  *
13  * Revision 1.2  1995/08/03  13:23:01  adam
14  * Request queue.
15  *
16  * Revision 1.1  1995/07/28  10:28:39  adam
17  * First work on request queue.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <assert.h>
25
26 #include "ir-tclp.h"
27
28 int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
29                       const char *msg, const char *object_name)
30 {
31     IrTcl_Request **rp;
32
33     if (!z_APDU (p->odr_out, &apdu, 0))
34     {
35         Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
36                           NULL);
37         odr_reset (p->odr_out);
38         return TCL_ERROR;
39     }
40     rp = &p->request_queue;
41     while (*rp)
42         rp = &(*rp)->next;
43     *rp = ir_tcl_malloc (sizeof(**rp));
44     (*rp)->next = NULL;
45     
46     if (ir_tcl_strdup (interp, &(*rp)->object_name, object_name) == TCL_ERROR)
47         return TCL_ERROR;
48     if (ir_tcl_strdup (interp, &(*rp)->callback, p->callback) == TCL_ERROR)
49         return TCL_ERROR;
50     
51     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
52     odr_setbuf (p->odr_out, NULL, 0, 1);
53     odr_reset (p->odr_out);
54     if (p->state == IR_TCL_R_Idle)
55     {
56         if (ir_tcl_send_q (p, p->request_queue, msg) == TCL_ERROR)
57         {
58             sprintf (interp->result, "cs_put failed in %s", msg);
59             return TCL_ERROR;
60         } 
61     }
62     return TCL_OK;
63 }
64
65 int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rp, const char *msg)
66 {
67     int r;
68
69     assert (rp);
70     r = cs_put (p->cs_link, rp->buf_out, rp->len_out);
71     if (r < 0)
72         return TCL_ERROR;
73     else if (r == 1)
74     {
75         ir_select_add_write (cs_fileno (p->cs_link), p);
76         logf (LOG_DEBUG, "Send part of %s", msg);
77         p->state = IR_TCL_R_Writing;
78     }
79     else
80     {
81         logf (LOG_DEBUG, "Send %s (%d bytes)", msg, rp->len_out);
82         p->state = IR_TCL_R_Waiting;
83         free (rp->buf_out);
84         rp->buf_out = NULL;
85     }
86     return TCL_OK;
87 }
88
89 void ir_tcl_del_q (IrTcl_Obj *p)
90 {
91     IrTcl_Request *rp, *rp1;
92
93     for (rp = p->request_queue; rp; rp = rp1)
94     {
95         free (rp->object_name);
96         free (rp->callback);
97         free (rp->buf_out);
98         rp1 = rp->next;
99         free (rp);
100     }
101     p->request_queue = NULL;
102 }
103
104
105