3 * $Id: txport.h,v 1.1 2002/01/25 19:34:45 mdw Exp $
5 * Transport switch glue
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2002/01/25 19:34:45 mdw
44 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/darray.h>
56 #include <mLib/lbuf.h>
58 /*----- Data structures ---------------------------------------------------*/
60 /* --- Vector of bytes --- */
64 DA_DECL(uchar_v
, unsigned char);
67 /* --- Node for lines --- */
69 typedef struct txline
{
70 struct txline
*next
, *prev
; /* Next node in the list */
71 struct txport
*tx
; /* Owning transport */
72 char *s
; /* Pointer to the text */
73 size_t len
; /* Length of the string */
76 /* --- A transport context --- *
78 * The only members for which there is contention are the state @s@ and the
79 * raw incoming buffer @buf@. Other members may be accessed without locking
80 * the structure. Thus, the thread messing about is essentially isolated to
81 * the data- fetching thread and the line buffering code.
84 typedef struct txport
{
85 struct txport_ops
*ops
; /* Operations table */
86 pthread_t tid
; /* Fetching thread id */
87 pthread_mutex_t mx
; /* Lock for this structure */
88 pthread_cond_t cv
; /* `New data has arrived' */
89 unsigned s
; /* State of this transport */
90 uchar_v buf
; /* Buffer of incoming data */
91 lbuf lb
; /* Buffer for extracting lines */
92 txline
*ll
, *ll_tail
; /* List of waiting lines, in order */
96 TX_READY
, /* More data may arrive */
97 TX_CLOSE
, /* No more data will arrive */
98 TX_CLOSED
/* Done the closure thing already */
101 /* --- Transport operations --- */
103 struct txfile
{ const char *env
; const char *name
; };
105 typedef struct txport_ops
{
106 struct txport_ops
*next
;
108 const struct txfile
*fv
;
110 txport
*(*create
)(const char */
*file*/
, const char */
*config*/
);
111 void *(*fetch
)(void */
*txv*/
);
112 ssize_t (*write
)(txport */
*tx*/
, const void */
*p*/
, size_t /*sz*/);
113 void (*destroy
)(txport */
*tx*/
);
116 /*----- Global variables --------------------------------------------------*/
118 extern txport_ops
*txlist
;
119 extern const char *txname
;
120 extern const char *txfile
;
121 extern const char *txconf
;
123 #define DIRVAR "JOGDIR"
125 /*----- Functions provided ------------------------------------------------*/
127 /* --- @tx_create@ --- *
129 * Arguments: @const char *name@ = name of transport to instantiate
130 * @const char *file@ = filename for transport
131 * @const char *config@ = config string
133 * Returns: A pointer to the transport context, or null on error.
135 * Use: Creates a new transport.
138 extern txport
*tx_create(const char */
*name*/
, const char */
*file*/
,
139 const char */
*config*/
);
141 /* --- @tx_write@ --- *
143 * Arguments: @txport *tx@ = pointer to transport context
144 * @const void *p@ = pointer to buffer to write
145 * @size_t sz@ = size of buffer
147 * Returns: Zero if OK, or @-1@ on error.
149 * Use: Writes some data to a transport.
152 extern int tx_write(txport */
*tx*/
, const void */
*p*/
, size_t /*sz*/);
154 /* --- @tx_printf@ --- *
156 * Arguments: @txport *tx@ = pointer to transport context
157 * @const char *p@ = pointer to string to write
159 * Returns: The number of characters printed, or @EOF@ on error.
161 * Use: Writes a textual message to a transport.
164 extern int tx_vprintf(txport */
*tx*/
, const char */
*p*/
, va_list */
*ap*/
);
166 extern int tx_printf(txport */
*tx*/
, const char */
*p*/
, ...);
168 /* --- @tx_read@, @tx_readx@ --- *
170 * Arguments: @txport *tx@ = pointer to transport context
171 * @unsigned long t@ = time to wait for data (ms)
172 * @int (*filter)(const char *s, void *p)@ = filtering function
173 * @void *p@ = pointer argument for filter
175 * Returns: A pointer to a line block, which must be freed using
178 * Use: Fetches a line from the buffer. Each line is passed to the
179 * filter function in oldest-to-newest order; the filter
180 * function returns nonzero to choose a line. If no suitable
181 * line is waiting in the raw buffer, the program blocks while
182 * more data is fetched, until the time limit @t@ is exceeded,
183 * in which case a null pointer is returned. A null filter
184 * function is equivalent to one which always selects its line.
187 #define FOREVER (~0ul)
189 extern txline
*tx_readx(txport */
*tx*/
, unsigned long /*t*/,
190 int (*/
*filter*/
)(const char */
*s*/
, void */
*p*/
),
193 extern txline
*tx_read(txport */
*tx*/
, unsigned long /*t*/);
195 /* --- @tx_freeline@ --- *
197 * Arguments: @txline *l@ = pointer to line
201 * Use: Frees a line block.
204 extern void tx_freeline(txline */
*l*/
);
206 /* --- @tx_destroy@ --- *
208 * Arguments: @txport *tx@ = transport context
212 * Use: Destroys a transport.
215 extern void tx_destroy(txport */
*tx*/
);
217 /*----- That's all, folks -------------------------------------------------*/