1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file parc_EventQueue.h
* @ingroup events
* @brief Queue buffer events
*
* Provides a facade implementing many regularly available event functions.
* This is an interface that software implementors may use to substitute
* different kinds of underlying implementations of these event management functions.
* Notable examples are libevent and libev.
*
*/
#ifndef libparc_parc_EventQueue_h
#define libparc_parc_EventQueue_h
#include <sys/types.h>
#include <sys/socket.h>
#include <parc/algol/parc_Event.h>
/**
* Current implementation based on top of libevent2
*/
/**
* @typedef PARCEventQueue
* @brief A structure containing private libevent state data variables
*/
typedef struct PARCEventQueue PARCEventQueue;
/**
* @typedef PARCEventQueuePair
* @brief A structure containing private libevent state data for connected queue pairs
*/
typedef struct PARCEventQueuePair PARCEventQueuePair;
/**
* @typedef PARCEventQueueEventType
* @brief An enumeration of queue event types
*/
typedef enum {
PARCEventQueueEventType_Reading = 0x01,
PARCEventQueueEventType_Writing = 0x02,
PARCEventQueueEventType_EOF = 0x10,
PARCEventQueueEventType_Error = 0x20,
PARCEventQueueEventType_Timeout = 0x40,
PARCEventQueueEventType_Connected = 0x80
} PARCEventQueueEventType;
/**
* @typedef PARCEventQueue_Options
* @brief A structure queue flags
*/
typedef enum {
PARCEventQueueOption_CloseOnFree = 0x01,
PARCEventQueueOption_DeferCallbacks = 0x04
} PARCEventQueueOption;
/**
* @typedef PARCEventQueue_Callback
* @brief A definition for callback function arguments
*/
typedef void (PARCEventQueue_Callback)(PARCEventQueue *event, PARCEventType type, void *user_data);
/**
* @typedef PARCEventQueue_EventCallback
* @brief A definition for callback function arguments
*/
typedef void (PARCEventQueue_EventCallback)(PARCEventQueue *event, PARCEventQueueEventType type, void *user_data);
/**
* Create a buffer event handler instance.
*
* The event instance is passed in. Options can be either, both or none of the following.
*
* PARCEventQueue_CloseOnFree
* The underlying file descriptor is closed when this event is freed.
*
* PARCEventQueue_DeferCallbacks
* Callbacks are run deferred in the scheduler.
*
* @param [in] eventScheduler - The scheduler instance base.
* @param [in] fd the file descriptor to monitor
* @param [in] options as described in flags above
* @returns A pointer to the a PARCEventQueue instance.
*
* Example:
* @code
* {
* PARCEventQueue *event = parcEventQueue_Create(eventScheduler, fd, PARCEventQueue_CloseOnFree);
* }
* @endcode
*
*/
PARCEventQueue *parcEventQueue_Create(PARCEventScheduler *eventScheduler, int fd, PARCEventQueueOption options);
/**
* Destroy a buffer event handler instance.
*
* The event instance is passed in.
*
* @param [in] eventQueue - The address of the instance to destroy.
*
* Example:
* @code
* {
* parcEventQueue_Destroy(&eventQueue);
* }
* @endcode
*
*/
void parcEventQueue_Destroy(PARCEventQueue **eventQueue);
/**
* Enable events on an instance.
*
* The event instance is passed in.
*
* @param [in] event - queue instance to enable.
* @param [in] types - the event(s) to enable.
*
* Example:
* @code
* {
* parcEventQueue_Enable(bufferEvent, PARCEvent_ReadEvent);
* }
* @endcode
*
*/
void parcEventQueue_Enable(PARCEventQueue *event, PARCEventType types);
/**
* Get enable events on an instance.
*
* The event instance is passed in.
*
* @param [in] queue - the instance to return enabled events from
* @returns mask of events which are enabled.
*
* Example:
* @code
* {
* PARCEventType *events = parcEventQueue_GetEnable(queue);
* }
* @endcode
*
*/
PARCEventType parcEventQueue_GetEnabled(PARCEventQueue *queue);
/**
* Disable events on an instance.
*
* The event instance is passed in.
*
* @param [in] queue - instance to disable event on.
* @param [in] types - the events to disable.
*
* Example:
* @code
* {
* parcEventQueue_Disable(queue, types);
* }
* @endcode
*
*/
void parcEventQueue_Disable(PARCEventQueue *queue, PARCEventType types);
/**
* Set callbacks on a buffer event instance.
*
* The event instance is passed in.
* You can disable a callback by passing NULL instead of the callback function.
* NB: all the callback functions on a bufferevent share a single user_data
* value, so changing user_data will affect all of them.
*
* @param [in] eventInstance - event instance
* @param [in] readCallback - callback for read events
* @param [in] writeCallback - callback for write events
* @param [in] eventCallback - callback for non-read/write events
* @param [in] user_data - data passed along in callback arguments
*
* Example:
* @code
* void Callback(PARCEventType type, void *user_data)
* {
* printf("Received event of type=%d\n", type);
* }
* ...
* {
* ...
* parcEventQueue_SetCallbacks(eventInstance, Callback, NULL, NULL, user_data);
* parcEventQueue_Enable(eventInstance, PARCEventType_Read);
* }
* @endcode
*
*/
void parcEventQueue_SetCallbacks(PARCEventQueue *eventInstance,
PARCEventQueue_Callback *readCallback,
PARCEventQueue_Callback *writeCallback,
PARCEventQueue_EventCallback *eventCallback,
void *user_data);
/**
* Flush events on a queue
*
* @param [in] queue instance to flush
* @param [in] types the type(s) of events to flush
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* int result = parcEventQueue_Flush(queue, PARCEventType_Write);
* }
* @endcode
*
*/
int parcEventQueue_Flush(PARCEventQueue *queue, PARCEventType types);
/**
* Finialized flush of events on a queue
*
* @param [in] queue instance to flush
* @param [in] types the type(s) of events to flush
* @returns 0 if no data was flushed, 1 if some data was flushed, -1 on failure
*
* Example:
* @code
* {
* int result = parcEventQueue_Finished(queue, PARCEventType_Write);
* }
* @endcode
*
*/
int parcEventQueue_Finished(PARCEventQueue *queue, PARCEventType types);
/**
* Set watermark boundries on a queue
*
* @param [in] queue - queue instance to set watermark on
* @param [in] types - the events to set watermark on
* @param [in] low - the low watermark value
* @param [in] high - the high watermark value
*
* Example:
* @code
* {
* parcEventQueue_SetWatermark(queue, PARCEventType_Read, 0, MAXPATHLEN);
* }
* @endcode
*
*/
void parcEventQueue_SetWatermark(PARCEventQueue *queue, PARCEventType types, size_t low, size_t high);
/**
* Add formatted text to the end of a queue
*
* @param [in] queue - queue instance to write to
* @param [in] fmt - printf arguments
*
* Example:
* @code
* {
* parcEventQueue_Printf(queue, "%s\n", "Hello world.");
* }
* @endcode
*
*/
int parcEventQueue_Printf(PARCEventQueue *queue, const char *fmt, ...);
/**
* Set the associated file descriptor on a queue
*
* @param [in] queue instance set to monitor this descriptor
* @param [in] fd file descriptor
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* int result = parcEventQueue_SetFileDescriptor(queue, STDIN_FILENO);
* }
* @endcode
*
*/
int parcEventQueue_SetFileDescriptor(PARCEventQueue *queue, int fd);
/**
* Get the associated file descriptor on a queue
*
* @param [in] queue instance set to monitor this descriptor
* @returns file descriptor on success, -1 on failure
*
* Example:
* @code
* {
* int fileDescriptor = parcEventQueue_GetFileDescriptor(queue);
* }
* @endcode
*
*/
int parcEventQueue_GetFileDescriptor(PARCEventQueue *queue);
/**
* Read data from the queue output
*
* @param [in] queue instance to read from
* @param [in] data to read into
* @param [in] dataLength length of data to read
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* int result = parcEventQueue_Read(queue, data, length);
* }
* @endcode
*
*/
int parcEventQueue_Read(PARCEventQueue *queue, void *data, size_t dataLength);
/**
* Add data to the queue output
*
* @param [in] queue instance to add to
* @param [in] data to write
* @param [in] dataLength length of data to write
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* int result = parcEventQueue_Write(queue, data, length);
* }
* @endcode
*
*/
int parcEventQueue_Write(PARCEventQueue *queue, void *data, size_t dataLength);
/**
* Attach an launch a socket on a queue
*
* @param [in] queue instance to attach socket to
* @param [in] address socket data
* @param [in] addressLength socket data length
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* struct sockaddr_un addr_unix;
* memset(&addr_unix, 0, sizeof(addr_unix));
* addr_unix.sun_family = AF_UNIX;
* strcpy(addr_unix.sun_path, sock_name);
* int result = parcEventQueue_ConnectSocket(queue, addr_unix, sizeof(addr_unix));
* }
* @endcode
*
*/
int parcEventQueue_ConnectSocket(PARCEventQueue *queue, struct sockaddr *address, int addressLength);
/**
* Set queue priority
*
* @param [in] queue instance to modify
* @param [in] priority queue priority
* @returns 0 on success, -1 on failure
*
* Example:
* @code
* {
* parcEvent_Enable(queue, PARCEventQueuePriority_Normal);
* }
* @endcode
*
*/
int parcEventQueue_SetPriority(PARCEventQueue *queue, PARCEventPriority priority);
/**
* Create a pair of connected queues
*
* @param [in] eventScheduler event scheduler instance
* @returns a queue pair instance
*
* Example:
* @code
* {
* PARCEventQueuePair *pair = parcEventQueue_CreateConnectedPair(eventScheduler);
* }
* @endcode
*
*/
PARCEventQueuePair *parcEventQueue_CreateConnectedPair(PARCEventScheduler *eventScheduler);
/**
* Destroy a connected queue pair
*
* @param [in] queuePair queue pair instance address to destroy
*
* Example:
* @code
* {
* parcEventQueuePairCreateparcEventQueue_DestroyConnectedPair(&queuePair);
* }
* @endcode
*
*/
void parcEventQueue_DestroyConnectedPair(PARCEventQueuePair **queuePair);
/**
* Return the downward queue of a pair
*
* @param [in] queuePair queue pair instance address to destroy
*
* Example:
* @code
* {
* PARCEventQueue *downQueue = parcEventQueue_GetConnectedDownQueue(queuePair);
* }
* @endcode
*
*/
PARCEventQueue *parcEventQueue_GetConnectedDownQueue(PARCEventQueuePair *queuePair);
/**
* Return the upward queue of a pair
*
* @param [in] queuePair queue pair instance address to destroy
*
* Example:
* @code
* {
* PARCEventQueue *upQueue = parcEventQueue_GetConnectedUpQueue(queuePair);
* }
* @endcode
*
*/
PARCEventQueue *parcEventQueue_GetConnectedUpQueue(PARCEventQueuePair *queuePair);
/**
* Private Internal Function - return internal input buffer of a queue
*
* The event instance is passed in.
*
* @param [in] queue the event queue
* @returns private evbuffer pointer
*
* Example:
* @code
* {
* struct evbuffer *evbuffer = internal_parcEventQueue_GetEvInputBuffer(queue);
* }
* @endcode
*
*/
struct evbuffer *internal_parcEventQueue_GetEvInputBuffer(PARCEventQueue *queue);
/**
* Private Internal Function - return internal output buffer of a queue
*
* The event instance is passed in.
*
* @param [in] queue the event queue
* @returns private evbuffer pointer
*
* Example:
* @code
* {
* struct evbuffer *evbuffer = internal_parcEventQueue_GetEvOutputBuffer(queue);
* }
* @endcode
*
*/
struct evbuffer *internal_parcEventQueue_GetEvOutputBuffer(PARCEventQueue *queue);
/**
* Turn on debugging flags and messages
*
* Example:
* @code
* {
* parcEventQueue_EnableDebug();
* }
* @endcode
*
*/
void parcEventQueue_EnableDebug(void);
/**
* Turn off debugging flags and messages
*
* Example:
* @code
* {
* parcEventQueue_DisableDebug();
* }
* @endcode
*
*/
void parcEventQueue_DisableDebug(void);
#endif // libparc_parc_EventQueue_h
|