Revision: 68844
          http://sourceforge.net/p/brlcad/code/68844
Author:   starseeker
Date:     2016-09-18 02:21:16 +0000 (Sun, 18 Sep 2016)
Log Message:
-----------
start simplifying my old attempt at understanding libpkg.  Can send basic data 
from server to client now and assemble data in the client as exmplified by 
all_msgs collecting vls.  Still need to demonstrate sending more interesting 
stuff from client back to server...

Modified Paths:
--------------
    brlcad/trunk/src/libpkg/example/client.c
    brlcad/trunk/src/libpkg/example/server.c

Modified: brlcad/trunk/src/libpkg/example/client.c
===================================================================
--- brlcad/trunk/src/libpkg/example/client.c    2016-09-15 00:22:44 UTC (rev 
68843)
+++ brlcad/trunk/src/libpkg/example/client.c    2016-09-18 02:21:16 UTC (rev 
68844)
@@ -37,6 +37,7 @@
 #include "bu/malloc.h"
 #include "bu/getopt.h"
 #include "bu/file.h"
+#include "bu/vls.h"
 #include "pkg.h"
 #include "ntp.h"
 
@@ -51,32 +52,6 @@
 
 
 /**
- * print a usage statement when invoked with bad, help, or no arguments
- */
-static void
-usage(const char *msg, const char *argv0)
-{
-    if (msg) {
-       bu_log("%s\n", msg);
-    }
-    bu_log("Client Usage: %s [-p#] [host] file\n\t-p#\tport number to send to 
(default 2000)\n\thost\thostname or IP address of receiving 
server\n\tfile\tsome file to transfer\n", argv0 ? argv0 : MAGIC_ID);
-
-    bu_log("\n%s", pkg_version());
-
-    exit(1);
-}
-
-/**
- * simple "valid" port number check used by client and server
- */
-static void
-validate_port(int port) {
-    if (port < 0 || port > 0xffff) {
-       bu_bomb("Invalid negative port range\n");
-    }
-}
-
-/**
  * callback when a HELO message packet is received.
  *
  * We should not encounter this packet specifically since we listened
@@ -84,7 +59,7 @@
  * handshake setup.
  */
 void
-server_helo(struct pkg_conn *UNUSED(connection), char *buf)
+client_helo(struct pkg_conn *UNUSED(connection), char *buf)
 {
     bu_log("Unexpected HELO encountered\n");
     free(buf);
@@ -95,9 +70,10 @@
  * callback when a DATA message packet is received
  */
 void
-server_data(struct pkg_conn *UNUSED(connection), char *buf)
+client_data(struct pkg_conn *connection, char *buf)
 {
-    bu_log("Received file data\n");
+    bu_log("Received file data: %s\n", buf);
+    bu_vls_printf((struct bu_vls *)connection->pkc_user_data, "\n%s\n", buf);
     free(buf);
 }
 
@@ -106,47 +82,33 @@
  * callback when a CIAO message packet is received
  */
 void
-server_ciao(struct pkg_conn *UNUSED(connection), char *buf)
+client_ciao(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("CIAO encountered\n");
+    bu_log("CIAO encountered: %s\n", buf);
     free(buf);
 }
 
 
 /**
- * start up a client that connects to the given server, and sends
- * serialized file data.
+ * start up a client that connects to the given server.
  */
 void
-run_client(const char *server, int port, const char *file)
+run_client(const char *server, int port, struct bu_vls *all_msgs)
 {
     my_data stash;
     char s_port[MAX_DIGITS + 1] = {0};
     long bytes = 0;
-    FILE *fp = (FILE *)NULL;
     int pkg_result = 0;
-    static const unsigned int TPKG_BUFSIZE = 2048;
-    char *buffer;
     /** our callbacks for each message type */
     struct pkg_switch callbacks[] = {
-       {MSG_HELO, server_helo, "HELO", NULL},
-       {MSG_DATA, server_data, "DATA", NULL},
-       {MSG_CIAO, server_ciao, "CIAO", NULL},
+       {MSG_HELO, client_helo, "HELO", NULL},
+       {MSG_DATA, client_data, "DATA", NULL},
+       {MSG_CIAO, client_ciao, "CIAO", NULL},
        {0, 0, (char *)0, (void*)0}
     };
 
-    buffer = (char *)bu_calloc(TPKG_BUFSIZE, 1, "buffer allocation");
+    callbacks[1].pks_user_data = (void *)all_msgs;
 
-    /* make sure the file can be opened */
-    fp = fopen(file, "rb");
-    if (fp == NULL) {
-       bu_log("Unable to open %s\n", file);
-       bu_bomb("Unable to read file\n");
-    }
-
-    /* open a connection to the server */
-    validate_port(port);
-
     snprintf(s_port, MAX_DIGITS, "%d", port);
     stash.connection = pkg_open(server, s_port, "tcp", NULL, NULL, NULL, NULL);
     if (stash.connection == PKC_ERROR) {
@@ -166,8 +128,7 @@
        bu_bomb("ERROR: Unable to communicate with the server\n");
     }
 
-
-    /* Server should have a file to send us.
+    /* Server should have a message to send us.
      */
     bu_log("Processing data from server\n");
     do {
@@ -196,12 +157,9 @@
        } else {
            bu_log("Processed %d packet%s\n", pkg_result, pkg_result == 1 ? "" 
: "s");
        }
-       bu_log("buffer: %s\n", stash.connection->pkc_inbuf);
+
     } while (stash.connection->pkc_type != MSG_CIAO);
 
-    /* print the CIAO message */
-    bu_log("buffer: %s\n", stash.connection->pkc_inbuf);
-
     /* let the server know we're done. */
     bytes = pkg_send(MSG_CIAO, "BYE", 4, stash.connection);
     if (bytes < 0) {
@@ -210,8 +168,6 @@
 
     /* flush output and close */
     pkg_close(stash.connection);
-    fclose(fp);
-    bu_free(buffer, "buffer release");
 
     return;
 }
@@ -221,61 +177,21 @@
  * main application for both the client and server
  */
 int
-main(int argc, char *argv[]) {
-    const char * const argv0 = argv[0];
-    int c;
+main() {
     int port = 2000;
+    const char *server_name = "127.0.0.1";
+    struct bu_vls *all_msgs;
+    BU_GET(all_msgs, struct bu_vls);
+    bu_vls_init(all_msgs);
 
-    /* client stuff */
-    const char *server_name = NULL;
-    const char *file = NULL;
-
-    /* process the command-line arguments after the application name */
-    while ((c = bu_getopt(argc, argv, "p:P:hH?")) != -1) {
-       if (bu_optopt == '?') c='h';
-       switch (c) {
-           case 'p':
-           case 'P':
-               port = atoi(bu_optarg);
-               break;
-           case 'h':
-           case 'H':
-               /* help */
-               usage(NULL, argv0);
-               break;
-           default:
-               usage("ERROR: Unknown argument\n", argv0);
-       }
-    }
-
-    argc -= bu_optind;
-    argv += bu_optind;
-
-
-    /* prep up the client */
-    if (argc < 1) {
-       usage("ERROR: Missing file argument\n", argv0);
-    } else if (argc > 2) {
-       usage("ERROR: Too many arguments provided\n", argv0);
-    }
-
-    if (argc < 2) {
-       server_name = "127.0.0.1";
-    } else {
-       server_name = *argv++;
-    }
-    file = *argv++;
-
-    /* make sure the file exists */
-    if (!bu_file_exists(file, NULL)) {
-       bu_log("File does not exist: %s\n", file);
-       return 1;
-    }
-
     /* fire up the client */
     bu_log("Connecting to %s, port %d\n", server_name, port);
-    run_client(server_name, port, file);
+    run_client(server_name, port, all_msgs);
 
+    bu_log("All messages: %s\n", bu_vls_addr(all_msgs));
+
+    bu_vls_free(all_msgs);
+    BU_PUT(all_msgs, struct bu_vls);
     return 0;
 }
 

Modified: brlcad/trunk/src/libpkg/example/server.c
===================================================================
--- brlcad/trunk/src/libpkg/example/server.c    2016-09-15 00:22:44 UTC (rev 
68843)
+++ brlcad/trunk/src/libpkg/example/server.c    2016-09-18 02:21:16 UTC (rev 
68844)
@@ -40,32 +40,6 @@
 #include "ntp.h"
 
 /**
- * print a usage statement when invoked with bad, help, or no arguments
- */
-static void
-usage(const char *msg, const char *argv0)
-{
-    if (msg) {
-       bu_log("%s\n", msg);
-    }
-    bu_log("Server Usage: %s [-p#]\n\t-p#\tport number to listen on (default 
2000)\n", argv0 ? argv0 : MAGIC_ID);
-
-    bu_log("\n%s", pkg_version());
-
-    exit(1);
-}
-
-/**
- * simple "valid" port number check used by client and server
- */
-static void
-validate_port(int port) {
-    if (port < 0 || port > 0xffff) {
-       bu_bomb("Invalid negative port range\n");
-    }
-}
-
-/**
  * callback when a HELO message packet is received.
  *
  * We should not encounter this packet specifically since we listened
@@ -86,7 +60,7 @@
 void
 server_data(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("Received file data\n");
+    bu_log("Received file data: %s\n", buf);
     free(buf);
 }
 
@@ -97,7 +71,7 @@
 void
 server_ciao(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("CIAO encountered\n");
+    bu_log("CIAO encountered: %s\n", buf);
     free(buf);
 }
 
@@ -113,7 +87,6 @@
     /* int pkg_result  = 0; */
     char *buffer, *msgbuffer;
     long bytes = 0;
-    FILE *fp;
 
     /** our server callbacks for each message type */
     struct pkg_switch callbacks[] = {
@@ -123,8 +96,6 @@
        {0, 0, (char *)0, (void*)0}
     };
 
-    validate_port(port);
-
     /* start up the server on the given port */
     snprintf(portname, MAX_DIGITS, "%d", port);
     netfd = pkg_permserver(portname, "tcp", 0, 0);
@@ -167,28 +138,26 @@
     } while (client == PKC_NULL);
 
     /* have client, will send file */
-    fp = fopen("lempar.c", "rb");
     buffer = (char *)bu_calloc(2048, 1, "buffer allocation");
+    snprintf(buffer, 2048, "This is a message from the server.");
 
-    if (fp == NULL) {
-       bu_log("Unable to open lempar.c\n");
-       bu_bomb("Unable to read file\n");
+    /* send the message to the server */
+    bytes = pkg_send(MSG_DATA, buffer, (size_t)strlen(buffer), client);
+    if (bytes < 0) {
+       pkg_close(client);
+       bu_log("Unable to successfully send message");
+       bu_free(buffer, "buffer release");
+       return;
     }
 
-    /* send the file data to the server */
-    while (!feof(fp) && !ferror(fp)) {
-       bytes = fread(buffer, 1, 2048, fp);
-       bu_log("Read %ld bytes from lempar.c\n", bytes);
-
-       if (bytes > 0) {
-           bytes = pkg_send(MSG_DATA, buffer, (size_t)bytes, client);
-           if (bytes < 0) {
-               pkg_close(client);
-               bu_log("Unable to successfully send data");
-               bu_free(buffer, "buffer release");
-               return;
-           }
-       }
+    /* send another message to the server */
+    snprintf(buffer, 2048, "Yet another message from the server.");
+    bytes = pkg_send(MSG_DATA, buffer, (size_t)strlen(buffer), client);
+    if (bytes < 0) {
+       pkg_close(client);
+       bu_log("Unable to successfully send message");
+       bu_free(buffer, "buffer release");
+       return;
     }
 
     /* Tell the client we're done */
@@ -198,7 +167,7 @@
     }
 
     /* Confirm the client is done */
-    buffer = pkg_bwaitfor (MSG_CIAO , client);
+    buffer = pkg_bwaitfor(MSG_CIAO , client);
     bu_log("buffer: %s\n", buffer);
 
     /* shut down the server, one-time use */
@@ -209,40 +178,9 @@
  * main application for both the client and server
  */
 int
-main(int argc, char *argv[]) {
-    const char * const argv0 = argv[0];
-    int c;
+main() {
     int port = 2000;
 
-    if (argc > 2) {
-       usage("ERROR: Incorrect number of arguments\n", argv[0]);
-    }
-
-    /* process the command-line arguments after the application name */
-    while ((c = bu_getopt(argc, argv, "p:P:hH?")) != -1) {
-       if (bu_optopt == '?') c='h';
-       switch (c) {
-           case 'p':
-           case 'P':
-               port = atoi(bu_optarg);
-               break;
-           case 'h':
-           case 'H':
-               /* help */
-               usage(NULL, argv0);
-               break;
-           default:
-               usage("ERROR: Unknown argument\n", argv0);
-       }
-    }
-
-    argc -= bu_optind;
-    argv += bu_optind;
-
-    if (argc > 0) {
-       usage("ERROR: Unexpected extra server arguments\n", argv0);
-    }
-
     /* ignore broken pipes, on platforms where we have SIGPIPE */
 #ifdef SIGPIPE
     (void)signal(SIGPIPE, SIG_IGN);

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
_______________________________________________
BRL-CAD Source Commits mailing list
brlcad-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to