Revision: 68845
          http://sourceforge.net/p/brlcad/code/68845
Author:   starseeker
Date:     2016-09-18 17:29:14 +0000 (Sun, 18 Sep 2016)
Log Message:
-----------
More simplification of the example, demonstrate data going back from client to 
server.

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

Modified: brlcad/trunk/src/libpkg/example/client.c
===================================================================
--- brlcad/trunk/src/libpkg/example/client.c    2016-09-18 02:21:16 UTC (rev 
68844)
+++ brlcad/trunk/src/libpkg/example/client.c    2016-09-18 17:29:14 UTC (rev 
68845)
@@ -41,34 +41,16 @@
 #include "pkg.h"
 #include "ntp.h"
 
-/* used by the client to pass the dbip and opened transfer file
- * descriptor.
- */
-typedef struct _my_data_ {
-    struct pkg_conn *connection;
-    const char *server;
-    int port;
-} my_data;
-
-
-/**
- * callback when a HELO message packet is received.
- *
- * We should not encounter this packet specifically since we listened
- * for it before beginning processing of packets as part of a simple
- * handshake setup.
- */
+/* callback when an unexpected message packet is received. */
 void
-client_helo(struct pkg_conn *UNUSED(connection), char *buf)
+client_unexpected(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("Unexpected HELO encountered\n");
+    bu_log("Unexpected message package encountered\n");
     free(buf);
 }
 
 
-/**
- * callback when a DATA message packet is received
- */
+/* callback when a DATA message packet is received */
 void
 client_data(struct pkg_conn *connection, char *buf)
 {
@@ -77,71 +59,66 @@
     free(buf);
 }
 
-
-/**
- * callback when a CIAO message packet is received
- */
+/* callback when a CIAO message packet is received */
 void
 client_ciao(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("CIAO encountered: %s\n", buf);
+    bu_log("CIAO received from server: %s\n", buf);
     free(buf);
 }
 
-
-/**
- * start up a client that connects to the given server.
- */
-void
-run_client(const char *server, int port, struct bu_vls *all_msgs)
-{
-    my_data stash;
+int
+main() {
+    int port = 2000;
+    const char *server = "127.0.0.1";
+    struct bu_vls all_msgs = BU_VLS_INIT_ZERO;
+    struct pkg_conn *connection;
     char s_port[MAX_DIGITS + 1] = {0};
     long bytes = 0;
     int pkg_result = 0;
     /** our callbacks for each message type */
     struct pkg_switch callbacks[] = {
-       {MSG_HELO, client_helo, "HELO", NULL},
+       {MSG_HELO, client_unexpected, "HELO", NULL},
        {MSG_DATA, client_data, "DATA", NULL},
        {MSG_CIAO, client_ciao, "CIAO", NULL},
        {0, 0, (char *)0, (void*)0}
     };
 
-    callbacks[1].pks_user_data = (void *)all_msgs;
+    /* Collect data from more than one server communication for later use */
+    callbacks[1].pks_user_data = (void *)&all_msgs;
 
+    /* fire up the client */
+    bu_log("Connecting to %s, port %d\n", server, 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) {
+    connection = pkg_open(server, s_port, "tcp", NULL, NULL, NULL, NULL);
+    if (connection == PKC_ERROR) {
        bu_log("Connection to %s, port %d, failed.\n", server, port);
        bu_bomb("ERROR: Unable to open a connection to the server\n");
     }
-    stash.server = server;
-    stash.port = port;
-    stash.connection->pkc_switch = callbacks;
+    connection->pkc_switch = callbacks;
 
-
     /* let the server know we're cool. */
-    bytes = pkg_send(MSG_HELO, MAGIC_ID, strlen(MAGIC_ID) + 1, 
stash.connection);
+    bytes = pkg_send(MSG_HELO, MAGIC_ID, strlen(MAGIC_ID) + 1, connection);
     if (bytes < 0) {
-       pkg_close(stash.connection);
+       pkg_close(connection);
        bu_log("Connection to %s, port %d, seems faulty.\n", server, port);
        bu_bomb("ERROR: Unable to communicate with the server\n");
     }
 
     /* Server should have a message to send us.
-     */
+    */
     bu_log("Processing data from server\n");
     do {
        /* process packets potentially received in a processing callback */
-       pkg_result = pkg_process(stash.connection);
+       pkg_result = pkg_process(connection);
        if (pkg_result < 0) {
            bu_log("Unable to process packets? Weird.\n");
-       } else {
+       } else if (pkg_result > 0) {
            bu_log("Processed %d packet%s\n", pkg_result, pkg_result == 1 ? "" 
: "s");
        }
 
        /* suck in data from the network */
-       pkg_result = pkg_suckin(stash.connection);
+       pkg_result = pkg_suckin(connection);
        if (pkg_result < 0) {
            bu_log("Seemed to have trouble sucking in packets.\n");
            break;
@@ -151,47 +128,31 @@
        }
 
        /* process new packets received */
-       pkg_result = pkg_process(stash.connection);
+       pkg_result = pkg_process(connection);
        if (pkg_result < 0) {
            bu_log("Unable to process packets? Weird.\n");
-       } else {
+       } else if (pkg_result > 0) {
            bu_log("Processed %d packet%s\n", pkg_result, pkg_result == 1 ? "" 
: "s");
        }
 
-    } while (stash.connection->pkc_type != MSG_CIAO);
+    } while (connection->pkc_type != MSG_CIAO);
 
+    /* server's done, send our own message back to it */
+    bytes = pkg_send(MSG_DATA, "Message from client", 20, connection);
+
     /* let the server know we're done. */
-    bytes = pkg_send(MSG_CIAO, "BYE", 4, stash.connection);
+    bytes = pkg_send(MSG_CIAO, "DONE", 5, connection);
+    bytes = pkg_send(MSG_CIAO, "BYE", 4, connection);
     if (bytes < 0) {
        bu_log("Unable to cleanly disconnect from %s, port %d.\n", server, 
port);
     }
 
     /* flush output and close */
-    pkg_close(stash.connection);
+    pkg_close(connection);
 
-    return;
-}
+    bu_log("All messages: %s\n", bu_vls_addr(&all_msgs));
 
-
-/**
- * main application for both the client and server
- */
-int
-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);
-
-    /* fire up the client */
-    bu_log("Connecting to %s, port %d\n", server_name, port);
-    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);
+    bu_vls_free(&all_msgs);
     return 0;
 }
 

Modified: brlcad/trunk/src/libpkg/example/ntp.h
===================================================================
--- brlcad/trunk/src/libpkg/example/ntp.h       2016-09-18 02:21:16 UTC (rev 
68844)
+++ brlcad/trunk/src/libpkg/example/ntp.h       2016-09-18 17:29:14 UTC (rev 
68845)
@@ -1,4 +1,4 @@
-/*                          N T P . H
+/*                          N C P . H
  * BRL-CAD
  *
  * Copyright (c) 2006-2016 United States Government as represented by
@@ -17,16 +17,16 @@
  * License along with this file; see the file named COPYING for more
  * information.
  */
-/** @file libpkg/example/ntp.h
+/** @file libpkg/example/ncp.h
  *
- * Example file transfer protocol definition
+ * Example network communications protocol definition
  *
  */
 
 #ifndef LIBPKG_EXAMPLE_NTP_H
 #define LIBPKG_EXAMPLE_NTP_H
 
-/* simple network transport protocol. connection starts with a HELO,
+/* simple network communication protocol. connection starts with a HELO,
  * then a variable number of GEOM/ARGS messages, then a CIAO to end.
  */
 #define MAGIC_ID       "TPKG"
@@ -37,7 +37,7 @@
 /* maximum number of digits on a port number */
 #define MAX_DIGITS      5
 
-#endif /* LIBPKG_EXAMPLE_NTP_H */
+#endif /* LIBPKG_EXAMPLE_NCP_H */
 
 /*
  * Local Variables:

Modified: brlcad/trunk/src/libpkg/example/server.c
===================================================================
--- brlcad/trunk/src/libpkg/example/server.c    2016-09-18 02:21:16 UTC (rev 
68844)
+++ brlcad/trunk/src/libpkg/example/server.c    2016-09-18 17:29:14 UTC (rev 
68845)
@@ -36,10 +36,11 @@
 #include "bu/str.h"
 #include "bu/malloc.h"
 #include "bu/getopt.h"
+#include "bu/vls.h"
 #include "pkg.h"
 #include "ntp.h"
 
-/**
+/*
  * callback when a HELO message packet is received.
  *
  * We should not encounter this packet specifically since we listened
@@ -53,21 +54,16 @@
     free(buf);
 }
 
-
-/**
- * callback when a DATA message packet is received
- */
+/* callback when a DATA message packet is received */
 void
 server_data(struct pkg_conn *UNUSED(connection), char *buf)
 {
-    bu_log("Received file data: %s\n", buf);
+    bu_log("Received message from client: %s\n", buf);
     free(buf);
 }
 
 
-/**
- * callback when a CIAO message packet is received
- */
+/* callback when a CIAO message packet is received */
 void
 server_ciao(struct pkg_conn *UNUSED(connection), char *buf)
 {
@@ -75,19 +71,16 @@
     free(buf);
 }
 
-
-/**
- * start up a server that listens for a single client.
- */
-void
-run_server(int port) {
+int
+main() {
+    int port = 2000;
     struct pkg_conn *client;
     int netfd;
     char portname[MAX_DIGITS + 1] = {0};
     /* int pkg_result  = 0; */
-    char *buffer, *msgbuffer;
+    struct bu_vls buffer = BU_VLS_INIT_ZERO;
+    char *msgbuffer;
     long bytes = 0;
-
     /** our server callbacks for each message type */
     struct pkg_switch callbacks[] = {
        {MSG_HELO, server_helo, "HELO", NULL},
@@ -96,6 +89,11 @@
        {0, 0, (char *)0, (void*)0}
     };
 
+    /* ignore broken pipes, on platforms where we have SIGPIPE */
+#ifdef SIGPIPE
+    (void)signal(SIGPIPE, SIG_IGN);
+#endif
+
     /* start up the server on the given port */
     snprintf(portname, MAX_DIGITS, "%d", port);
     netfd = pkg_permserver(portname, "tcp", 0, 0);
@@ -107,6 +105,7 @@
      * handshake that waits for a HELO message from the client.  if it
      * doesn't get one, the server continues to wait.
      */
+    bu_log("Listening on port %d\n", port);
     do {
        client = pkg_getclient(netfd, callbacks, NULL, 0);
        if (client == PKC_NULL) {
@@ -137,28 +136,15 @@
        }
     } while (client == PKC_NULL);
 
-    /* have client, will send file */
-    buffer = (char *)bu_calloc(2048, 1, "buffer allocation");
-    snprintf(buffer, 2048, "This is a message from the server.");
+    /* send the first message to the server */
+    bu_vls_sprintf(&buffer, "This is a message from the server.");
+    bytes = pkg_send(MSG_DATA, bu_vls_addr(&buffer), 
(size_t)bu_vls_strlen(&buffer)+1, client);
+    if (bytes < 0) goto failure;
 
-    /* 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 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;
-    }
+    bu_vls_sprintf(&buffer, "Yet another message from the server.");
+    bytes = pkg_send(MSG_DATA, bu_vls_addr(&buffer), 
(size_t)bu_vls_strlen(&buffer)+1, client);
+    if (bytes < 0) goto failure;
 
     /* Tell the client we're done */
     bytes = pkg_send(MSG_CIAO, "DONE", 5, client);
@@ -166,31 +152,25 @@
        bu_log("Connection to client seems faulty.\n");
     }
 
+    /* Wait to hear from the client */
+    do {
+       (void)pkg_process(client);
+       (void)pkg_suckin(client);
+       (void)pkg_process(client);
+    } while (client->pkc_type != MSG_CIAO);
+
+
     /* Confirm the client is done */
-    buffer = pkg_bwaitfor(MSG_CIAO , client);
-    bu_log("buffer: %s\n", buffer);
+    (void)pkg_bwaitfor(MSG_CIAO , client);
 
     /* shut down the server, one-time use */
     pkg_close(client);
-}
-
-/**
- * main application for both the client and server
- */
-int
-main() {
-    int port = 2000;
-
-    /* ignore broken pipes, on platforms where we have SIGPIPE */
-#ifdef SIGPIPE
-    (void)signal(SIGPIPE, SIG_IGN);
-#endif
-
-    /* fire up the server */
-    bu_log("Listening on port %d\n", port);
-    run_server(port);
-
     return 0;
+failure:
+    pkg_close(client);
+    bu_log("Unable to successfully send message");
+    bu_vls_free(&buffer);
+    return 0;
 }
 
 /*

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