OSSP CVS Repository

ossp - Check-in [490]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 490
Date: 2001-May-24 11:40:28 (local)
2001-May-24 09:40:28 (UTC)
User:rse
Branch:
Comment: o rename l2_error_t to l2_result_t o introduce internal channel state for API robustness
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2.h      1.4 -> 1.5     1 inserted, 1 deleted
ossp-pkg/l2/l2_ch_buffer.c      1.3 -> 1.4     1 inserted, 1 deleted
ossp-pkg/l2/l2_ch_file.c      1.5 -> 1.6     1 inserted, 1 deleted
ossp-pkg/l2/l2_channel.c      1.3 -> 1.4     57 inserted, 24 deleted
ossp-pkg/l2/l2_p.h      1.3 -> 1.4     7 inserted, 1 deleted

ossp-pkg/l2/l2.h 1.4 -> 1.5

--- l2.h 2001/05/22 18:47:31     1.4
+++ l2.h 2001/05/24 09:40:28     1.5
@@ -77,7 +77,7 @@
 typedef enum {
     L2_OK,
     L2_ERROR
-} l2_error_t;
+} l2_result_t;
 
 /* context union for storing data */
 union l2_context_un {


ossp-pkg/l2/l2_ch_buffer.c 1.3 -> 1.4

--- l2_ch_buffer.c       2001/05/22 20:00:12     1.3
+++ l2_ch_buffer.c       2001/05/24 09:40:28     1.4
@@ -55,7 +55,7 @@
 {
     l2_ch_buffer_t *cfg;
     l2_param_t pa[3]; 
-    l2_error_t rv;
+    l2_result_t rv;
 
     if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
         return L2_ERROR;


ossp-pkg/l2/l2_ch_file.c 1.5 -> 1.6

--- l2_ch_file.c 2001/05/22 18:47:31     1.5
+++ l2_ch_file.c 2001/05/24 09:40:28     1.6
@@ -68,7 +68,7 @@
 {
     l2_ch_file_t *cfg;
     l2_param_t pa[3]; 
-    l2_error_t rv;
+    l2_result_t rv;
 
     /* parameter checks */
     if ((cfg = (l2_ch_file_t *)ctx->vp) == NULL)


ossp-pkg/l2/l2_channel.c 1.3 -> 1.4

--- l2_channel.c 2001/05/22 20:00:12     1.3
+++ l2_channel.c 2001/05/24 09:40:28     1.4
@@ -31,6 +31,8 @@
 #include "l2.h"
 #include "l2_p.h"
 
+#include <string.h>
+
 l2_channel_t *l2_channel_create(l2_handler_t *h)
 {
     l2_channel_t *ch;
@@ -39,15 +41,10 @@
         return NULL;
     if ((ch = (l2_channel_t *)malloc(sizeof(l2_channel_t))) == NULL)
         return NULL;
-    ch->below              = NULL;
-    ch->context.vp         = NULL;
-    ch->handler.create     = h->create;
-    ch->handler.configure  = h->configure;
-    ch->handler.open       = h->open;
-    ch->handler.write      = h->write;
-    ch->handler.flush      = h->flush;
-    ch->handler.close      = h->close;
-    ch->handler.destroy    = h->destroy;
+    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) == L2_ERROR) {
         free(ch);
         return NULL;
@@ -59,8 +56,11 @@
 {
     if (ch1 == NULL || ch2 == NULL)
         return NULL;
-    ch2->below = ch1;
-    return ch2;
+    if (   ch1->state != L2_CHSTATE_CREATED 
+        || ch2->state != L2_CHSTATE_CREATED)
+        return NULL;
+    ch1->downstream = ch2;
+    return ch1;
 }
 
 int l2_channel_configure(l2_channel_t *ch, const char *fmt, ...)
@@ -70,6 +70,8 @@
 
     if (ch == NULL || fmt == NULL)
         return L2_ERROR;
+    if (ch->state != L2_CHSTATE_CREATED)
+        return L2_ERROR;
     va_start(ap, fmt);
     rv = ch->handler.configure(&ch->context, fmt, ap);
     va_end(ap);
@@ -78,62 +80,91 @@
 
 int l2_channel_open(l2_channel_t *ch)
 {
+    l2_result_t rv;
+
     if (ch == NULL)
         return L2_ERROR;
+    if (ch->state != L2_CHSTATE_CREATED)
+        return L2_ERROR;
     while (ch != NULL && ch->handler.open == NULL)
-        ch = ch->below;
+        ch = ch->downstream;
     if (ch == NULL)
         return L2_ERROR;
-    return ch->handler.open(&ch->context, ch->below);
+    rv = ch->handler.open(&ch->context, ch->downstream);
+    if (rv == L2_OK)
+        ch->state = L2_CHSTATE_OPENED;
+    return rv;
 }
 
 int l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize)
 {
-    if (ch == NULL || buf == NULL || bufsize <= 0)
-        return L2_ERROR;
-    while (ch != NULL && ch->handler.write == NULL)
-        ch = ch->below;
+    l2_result_t rv;
+
     if (ch == NULL)
         return L2_ERROR;
+    if (ch->state != L2_CHSTATE_OPENED)
+        return L2_ERROR;
     if (buf == NULL)
         return L2_ERROR;
     if (bufsize == 0)
         return L2_OK;
-    return ch->handler.write(&ch->context, ch->below, buf, bufsize);
+    while (ch != NULL && ch->handler.write == NULL)
+        ch = ch->downstream;
+    if (ch == NULL)
+        return L2_ERROR;
+    rv = ch->handler.write(&ch->context, ch->downstream, buf, bufsize);
+    return rv;
 }
 
 int l2_channel_flush(l2_channel_t *ch)
 {
+    l2_result_t rv;
+
     if (ch == NULL)
         return L2_ERROR;
+    if (ch->state != L2_CHSTATE_OPENED)
+        return L2_ERROR;
     while (ch != NULL && ch->handler.flush == NULL)
-        ch = ch->below;
+        ch = ch->downstream;
     if (ch == NULL)
         return L2_ERROR;
-    return ch->handler.flush(&ch->context, ch->below);
+    rv = ch->handler.flush(&ch->context, ch->downstream);
+    return rv;
 }
 
 int l2_channel_close(l2_channel_t *ch)
 {
+    l2_result_t rv;
+
     if (ch == NULL)
         return L2_ERROR;
+    if (ch->state != L2_CHSTATE_OPENED)
+        return L2_ERROR;
     while (ch != NULL && ch->handler.close == NULL)
-        ch = ch->below;
+        ch = ch->downstream;
     if (ch == NULL)
         return L2_ERROR;
-    return ch->handler.close(&ch->context, ch->below);
+    rv = ch->handler.close(&ch->context, ch->downstream);
+    if (rv == L2_OK)
+        ch->state = L2_CHSTATE_CREATED;
+    return rv;
 }
 
 int l2_channel_destroy(l2_channel_t *ch)
 {
-    int rv = L2_OK;
+    l2_result_t rv;
 
     if (ch == NULL)
         return L2_ERROR;
+    if (ch->state == L2_CHSTATE_OPENED)
+        if ((rv = l2_channel_close(ch)) != L2_OK)
+            return rv;
     while (ch != NULL && ch->handler.destroy == NULL)
-        ch = ch->below;
+        ch = ch->downstream;
     if (ch != NULL)
         rv = ch->handler.destroy(&ch->context);
+    else
+        rv = L2_OK;
     free(ch);
     return rv;
 }
@@ -145,6 +176,8 @@
     int ok;
     int i; 
 
+    if (pa == NULL || fmt == NULL || ap == NULL)
+        return L2_ERROR;
     cpE = fmt;
     while (*cpE != '\0') {
         /* determine begin of parameter name */


ossp-pkg/l2/l2_p.h 1.3 -> 1.4

--- l2_p.h       2001/05/19 20:08:30     1.3
+++ l2_p.h       2001/05/24 09:40:28     1.4
@@ -37,8 +37,14 @@
 #define L2_MAX_CHANNELS    128
 #define L2_MAX_FORMATTERS  128
 
+typedef enum {
+    L2_CHSTATE_CREATED,
+    L2_CHSTATE_OPENED
+} l2_chstate_t;
+
 struct l2_channel_st {
-    l2_channel_t *below;
+    l2_chstate_t  state;
+    l2_channel_t *downstream;
     l2_context_t  context;
     l2_handler_t  handler;
 };

CVSTrac 2.0.1