* IL communication protocol and appropriate changes to the Makefile
authorMichal Voců <michal@ruk.cuni.cz>
Thu, 10 Feb 2005 15:44:54 +0000 (15:44 +0000)
committerMichal Voců <michal@ruk.cuni.cz>
Thu, 10 Feb 2005 15:44:54 +0000 (15:44 +0000)
org.glite.lb.common/Makefile
org.glite.lb.common/src/il_msg.c

index bdb2abf..fbbb507 100644 (file)
@@ -82,7 +82,7 @@ INSTALL:=libtool --mode=install install
 
 OBJS:=lb_gss.o lb_plain_io.o escape.o events.o mini_http.o query_rec.o \
        status.o xml_conversions.o xml_parse.o ulm_parse.o param.o \
-       events_parse.o il_string.o il_int.o notifid.o \
+       events_parse.o il_string.o il_int.o il_msg.o notifid.o \
        il_log.o il_msg.o context.o trio.o strio.o
 LOBJS:=${OBJS:.o=.lo}
 
@@ -90,7 +90,7 @@ THROBJS:=${OBJS:.o=.thr.o}
 THRLOBJS:=${OBJS:.o=.thr.lo}
 
 HDRS:=context-int.h  lb_gss.h lb_plain_io.h mini_http.h authz.h xml_parse.h \
-       xml_conversions.h log_proto.h events_parse.h il_string.h escape.h \
+       xml_conversions.h log_proto.h events_parse.h il_string.h il_msg.h escape.h \
        ulm_parse.h trio.h
 
 STATICLIB:=libglite_lb_common_${nothrflavour}.a
index 30997f6..a93121a 100644 (file)
 #include <stdio.h>
 #include <unistd.h>
 
+
 int 
-receive_msg(int sd, char **ucs, char **event)
+encode_il_msg(char **buffer, const char *event)
 {
-  char buffer[17];
-  char *p, *q, *msg;
   int len;
+  char *p;
 
-  len = read(sd, buffer, 17);
 
-  if(buffer[16] != '\n') {
-    printf("Error in header!\n");
-    goto err;
+  /* allocate enough room to hold the message */
+  len = 17 + len_string(event);
+  if((*buffer = malloc(len)) == NULL) {
+    return(-1);
   }
 
-  sscanf(buffer, "%d", &len);
-  if(len > MAXLEN) {
-    printf("Message too long!\n");
-    goto err;
+  p = *buffer;
+
+  /* write header */
+  sprintf(p, "%16d\n", len - 17);
+  p += 17;
+
+  /* write rest of the message */
+  p = put_string(p, event);
+
+  return(p - *buffer);
+}
+
+
+int
+encode_il_reply(char **buffer, 
+               int err_code, int err_code_min, 
+               const char *err_msg)
+{
+  len = 17 + len_int(err_code) + len_int(err_code_min) + len_string(err_msg);
+  if((*buffer = malloc(len)) == NULL) {
+    return(-1);
+  }
+
+  sprintf(*buffer, "%16d\n", len - 17);
+  p = *buffer + 17;
+  p = put_int(p, err_code);
+  p = put_int(p, err_code_min);
+  p = put_string(p, err_msg);
+  return(p - *buffer);
+}
+
+
+int 
+decode_il_msg(char **event, const char *buf)
+{
+  char *p;
+
+  p = get_string(buf, event);
+  if(p == NULL) {
+    if(*event) { free(*event); *event = NULL; };
+    return(EINVAL);
   }
-  p = msg = malloc(len+1);
+  return(p - buf);
+}
+
+
+int
+decode_il_reply(int *maj, int *min, char **err, const char * buf)
+{
+  char *p = buf;
+
+  p = get_int(p, maj);
+  if(p == NULL) return(EINVAL);
+  p = get_int(p, min);
+  if(p == NULL) return(EINVAL);
+  p = get_string(p, err);
   if(p == NULL) {
-    printf("Error allocating %d bytes\n", len+1);
-    goto err;
+    if(*err) { free(*err); *err = NULL; };
+    return(EINVAL);
   }
+  return(p - buf);
+}
 
-  read(sd, p, len);
-  p[len] = 0;
 
-  if((q = get_string(p, ucs)) == NULL) {
-    printf("Protocol error at %s\n", p);
+int
+read_il_data(char **buffer, 
+            int (*reader)(char *, const int))
+{
+  char buffer[17];
+  int ret, len;
+
+  /* read 17 byte header */
+  len = (*reader)(buffer, 17);
+  if(len < 0) {
+    goto err;
+  }
+  if((len=atoi(buffer)) <= 0) {
+    len = EINVAL;
     goto err;
   }
-  p = q;
-  if((q = get_string(p, event)) == NULL) {
-    printf("Protocol error at %s\n", p);
+
+  /* allocate room for the body */
+  *buffer = malloc(len+1);
+  if(*buffer == NULL) {
+    len = ENOMEM;
+    goto err;
+  }
+
+  /* read body */
+  ret = (*reader)(*buffer, len);
+  if(ret < 0) {
+    free(*buffer);
+    *buffer = NULL;
+    len = ret;
     goto err;
   }
 
-  free(msg);
-  return(0);
+  (*buffer)[len] = 0;
 
- err: 
-  
-  return(-1);
+ err:
+  return(len);
 }