--- l2_channel.c 2001/09/05 19:58:44 1.15
+++ l2_channel.c 2001/09/06 14:37:53 1.16
@@ -33,159 +33,241 @@
#include <string.h>
+/* create channel */
l2_channel_t *l2_channel_create(l2_handler_t *h)
{
l2_channel_t *ch;
+ /* argument sanity check */
if (h == NULL)
return NULL;
+
+ /* allocate channel structure */
if ((ch = (l2_channel_t *)malloc(sizeof(l2_channel_t))) == NULL)
return NULL;
+
+ /* initialize channel structure */
ch->state = L2_CHSTATE_CREATED;
ch->downstream = NULL;
memset(&ch->context, 0, sizeof(l2_context_t));
memcpy(&ch->handler, h, sizeof(l2_handler_t));
- if (ch->handler.create(&ch->context, ch) == L2_ERROR) {
- free(ch);
- return NULL;
+
+ /* (optionally) perform create operation in handler */
+ if (ch->handler.create != NULL) {
+ if (ch->handler.create(&ch->context, ch) != L2_OK) {
+ free(ch);
+ return NULL;
+ }
}
+
return ch;
}
+/* configure channel */
l2_result_t l2_channel_configure(l2_channel_t *ch, const char *fmt, ...)
{
l2_result_t rv;
va_list ap;
+ /* argument sanity check */
if (ch == NULL || fmt == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure the channel is in state "created" */
if (ch->state != L2_CHSTATE_CREATED)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* pass operation to handler */
+ rv = L2_OK;
va_start(ap, fmt);
- rv = ch->handler.configure(&ch->context, ch, fmt, ap);
+ if (ch->handler.configure != NULL)
+ rv = ch->handler.configure(&ch->context, ch, fmt, ap);
va_end(ap);
+
return rv;
}
+/* open channel (stack) */
l2_result_t l2_channel_open(l2_channel_t *ch)
{
l2_result_t rv;
+ /* argument sanity check */
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure channel is in state "created" */
if (ch->state != L2_CHSTATE_CREATED)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* skip empty open handlers on channel stack */
while (ch != NULL && ch->handler.open == NULL) {
ch->state = L2_CHSTATE_OPENED;
ch = ch->downstream;
}
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* pass operation to handler */
rv = ch->handler.open(&ch->context, ch);
if (rv == L2_OK)
ch->state = L2_CHSTATE_OPENED;
+
return rv;
}
+/* write to channel (stack) */
l2_result_t l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize)
{
l2_result_t rv;
- if (ch == NULL)
- return L2_ERROR;
+ /* argument sanity check */
+ if (ch == NULL || buf == NULL)
+ return L2_ERR_ARG;
+
+ /* make sure channel is in state "opened" */
if (ch->state != L2_CHSTATE_OPENED)
- return L2_ERROR;
- if (buf == NULL)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* short circuiting */
if (bufsize == 0)
return L2_OK;
+
+ /* walk to next available write handler */
while (ch != NULL && ch->handler.write == NULL)
ch = ch->downstream;
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* pass operation to handler */
rv = ch->handler.write(&ch->context, ch, buf, bufsize);
+
return rv;
}
+/* flush channel (stack) */
l2_result_t l2_channel_flush(l2_channel_t *ch)
{
l2_result_t rv;
+ /* argument sanity check */
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure channel is in state "opened" */
if (ch->state != L2_CHSTATE_OPENED)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* walk to next available flush handler */
while (ch != NULL && ch->handler.flush == NULL)
ch = ch->downstream;
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* pass operation to handler */
rv = ch->handler.flush(&ch->context, ch);
+
return rv;
}
+/* close channel (stack) */
l2_result_t l2_channel_close(l2_channel_t *ch)
{
l2_result_t rv;
+ /* argument sanity check */
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure channel is in state "opened" */
if (ch->state != L2_CHSTATE_OPENED)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* walk to next available close handler */
while (ch != NULL && ch->handler.close == NULL) {
ch->state = L2_CHSTATE_CREATED;
ch = ch->downstream;
}
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* pass operation to handler */
rv = ch->handler.close(&ch->context, ch);
if (rv == L2_OK)
ch->state = L2_CHSTATE_CREATED;
+
return rv;
}
+/* destroy channel */
l2_result_t l2_channel_destroy(l2_channel_t *ch)
{
l2_result_t rv;
+ /* argument sanity check */
if (ch == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure channel is in state "opened" */
if (ch->state == L2_CHSTATE_OPENED)
if ((rv = l2_channel_close(ch)) != L2_OK)
return rv;
+
+ /* walk to next available destroy handler */
while (ch != NULL && ch->handler.destroy == NULL)
ch = ch->downstream;
+
+ /* pass operation to handler */
if (ch != NULL)
rv = ch->handler.destroy(&ch->context, ch);
else
rv = L2_OK;
+
+ /* free channel structure */
free(ch);
+
return rv;
}
+/* stack channel on top of another channel */
l2_result_t l2_channel_stack(l2_channel_t *ch, l2_channel_t *chTop)
{
+ /* argument sanity check */
if (ch == NULL || chTop == NULL)
- return L2_ERROR;
+ return L2_ERR_ARG;
+
+ /* make sure both channels are in state "created" */
if ( ch->state != L2_CHSTATE_CREATED
|| chTop->state != L2_CHSTATE_CREATED)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* make sure top channel is a filter channel */
if (chTop->handler.type != L2_CHANNEL_FILTER)
- return L2_ERROR;
+ return L2_ERR_USE;
+
+ /* stack the channels */
chTop->downstream = ch;
+
return L2_OK;
}
+/* return downstream channel */
l2_channel_t *l2_channel_downstream(l2_channel_t *ch)
{
+ /* argument sanity check */
if (ch == NULL)
return NULL;
+
return ch->downstream;
}
+/* return channel type */
l2_chtype_t l2_channel_type(l2_channel_t *ch)
{
+ /* argument sanity check */
if (ch == NULL)
return NULL;
+
return ch->handler.type;
}
|