- changes to handling of error messages
authorDaniel Kouřil <kouril@ics.muni.cz>
Thu, 8 Dec 2011 08:55:28 +0000 (08:55 +0000)
committerDaniel Kouřil <kouril@ics.muni.cz>
Thu, 8 Dec 2011 08:55:28 +0000 (08:55 +0000)
- results of cleanup calls needn't be checked
- some cosmetics

emi.canl.canl-c/src/canl_err.c
emi.canl.canl-c/src/canl_sample_client.c
emi.canl.canl-c/src/canl_sample_server.c

index 645b14f..ba35c17 100644 (file)
@@ -188,10 +188,17 @@ char *
 canl_get_error_message(canl_ctx cc)
 {
     glb_ctx *ctx = (glb_ctx*) cc;
+    int ret;
+    char *msg = NULL;
 
-    if (ctx == NULL || ctx->err_msg == NULL)
-       return "No error available";
+    if (ctx == NULL)
+       return "Context is not initialized";
+
+    ret = canl_get_error(ctx, &msg);
+    if (ret)
+       return "No human-error available";
 
+    ctx->err_msg = msg;
     return ctx->err_msg;
 }
 
index b347c1b..4583cb0 100644 (file)
@@ -24,7 +24,7 @@ int main(int argc, char *argv[])
             case 'h':
                 fprintf(stderr, "Usage: %s [-p port]" 
                         "[-s server] [-h] \n", argv[0]);
-                break;
+                exit(0);
             case 'p':
                 port = atoi(optarg);
                 break;
@@ -58,7 +58,8 @@ int main(int argc, char *argv[])
 
     err = canl_io_connect(my_ctx, my_io_h, p_server, port, 0, NULL, &timeout);
     if (err) {
-        printf("[CLIENT] connection cannot be established\n");
+        printf("[CLIENT] connection to %s cannot be established: %s\n",
+              p_server, canl_get_error_message(my_ctx));
         goto end;
     }
     else {
@@ -97,10 +98,6 @@ int main(int argc, char *argv[])
     my_io_h = NULL;
 
 end:
-    canl_get_error(my_ctx, &err_msg);
-    if (err_msg != NULL)
-        printf("%s\n", err_msg);
-
     canl_free_ctx(my_ctx);
 
     return err;
index 5ca61fa..081f210 100644 (file)
@@ -28,7 +28,7 @@ int main(int argc, char *argv[])
             case 'h':
                 fprintf(stderr, "Usage: %s [-p port] [-c certificate]"
                         " [-k private key] [-h] \n", argv[0]);
-                break;
+                exit(0);
             case 'p':
                 port = atoi(optarg);
                 break;
@@ -108,12 +108,11 @@ int main(int argc, char *argv[])
         if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
                     sizeof(int)) == -1) {
             err = errno;
-            freeaddrinfo(servinfo); // all done with this structure
-            return -1;
+           continue;
         }
         if ((err = bind(sockfd, p->ai_addr, p->ai_addrlen))) {
-            close(sockfd);
             err = errno;
+            close(sockfd);
             continue;
         }
         if ((err = listen(sockfd, BACKLOG))) {
@@ -122,13 +121,14 @@ int main(int argc, char *argv[])
             continue;
         }
 
-
         break;
     }
 
     freeaddrinfo(servinfo); // all done with this structure
     if (p == NULL) {
-        printf("Failed to acquire a server socket");
+       /* Beware that only the last error is displayed here ... */
+        printf("Failed to acquire a server socket: %s\n",
+              strerror(err));
         return 1;
     }
 
@@ -168,33 +168,19 @@ int main(int argc, char *argv[])
     }
 
     err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, NULL);
-    if (err > 0) {
-        buf[err] = '\0';
-        printf ("[SERVER] received: %s\n", buf);
-    }
-    else
-        printf("[SERVER] Failed to receive reply from client: %s\n",
+    if (err <= 0) {
+       printf("[SERVER] Failed to receive reply from client: %s\n",
               canl_get_error_message(my_ctx));
-
-end:
-    print_error_from_canl(my_ctx);
-
-    if (my_io_h) {
-        err = canl_io_close(my_ctx, my_io_h);
-        if (err){
-            printf("[SERVER] Cannot close connection\n");
-            print_error_from_canl(my_ctx);
-        }
+       goto end;
     }
 
-    if (my_io_h) {
+    buf[err] = '\0';
+    printf ("[SERVER] received: %s\n", buf);
+    err = 0;
+
+end:
+    if (my_io_h)
         err = canl_io_destroy(my_ctx, my_io_h);
-        if (err){
-            printf("[SERVER] Cannot destroy connection\n");
-            print_error_from_canl(my_ctx);
-        }
-        my_io_h = NULL;
-    }
 
     canl_free_ctx(my_ctx);