Revision: 75834
http://sourceforge.net/p/brlcad/code/75834
Author: starseeker
Date: 2020-05-18 17:23:46 +0000 (Mon, 18 May 2020)
Log Message:
-----------
C->C++
Modified Paths:
--------------
brlcad/trunk/regress/pkg/CMakeLists.txt
Added Paths:
-----------
brlcad/trunk/regress/pkg/regress_pkg_client.cpp
brlcad/trunk/regress/pkg/regress_pkg_server.cpp
Removed Paths:
-------------
brlcad/trunk/regress/pkg/regress_pkg_client.c
brlcad/trunk/regress/pkg/regress_pkg_server.c
Modified: brlcad/trunk/regress/pkg/CMakeLists.txt
===================================================================
--- brlcad/trunk/regress/pkg/CMakeLists.txt 2020-05-18 17:13:06 UTC (rev
75833)
+++ brlcad/trunk/regress/pkg/CMakeLists.txt 2020-05-18 17:23:46 UTC (rev
75834)
@@ -5,11 +5,11 @@
${PKG_INCLUDE_DIRS}
)
-add_executable(regress_pkg_server regress_pkg_server.c)
+add_executable(regress_pkg_server regress_pkg_server.cpp)
target_link_libraries(regress_pkg_server libbu libpkg)
set_target_properties(regress_pkg_server PROPERTIES FOLDER "BRL-CAD Regression
Tests/libpkg")
-add_executable(regress_pkg_client regress_pkg_client.c)
+add_executable(regress_pkg_client regress_pkg_client.cpp)
target_link_libraries(regress_pkg_client libbu libpkg)
set_target_properties(regress_pkg_client PROPERTIES FOLDER "BRL-CAD Regression
Tests/libpkg")
@@ -38,9 +38,6 @@
CMAKEFILES(
CMakeLists.txt
- regress_pkg.cpp
- regress_pkg_server.c
- regress_pkg_client.c
regress_pkg_protocol.h
)
# Local Variables:
Deleted: brlcad/trunk/regress/pkg/regress_pkg_client.c
===================================================================
--- brlcad/trunk/regress/pkg/regress_pkg_client.c 2020-05-18 17:13:06 UTC
(rev 75833)
+++ brlcad/trunk/regress/pkg/regress_pkg_client.c 2020-05-18 17:23:46 UTC
(rev 75834)
@@ -1,168 +0,0 @@
-/* R E G R E S S _ P K G _ C L I E N T . C
- * BRL-CAD
- *
- * Copyright (c) 2020 United States Government as represented by
- * the U.S. Army Research Laboratory.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this file; see the file named COPYING for more
- * information.
- */
-/** @file regress_pkg_client.c
- *
- * Client application for libpkg regression test.
- *
- */
-
-#include "common.h"
-
-/* system headers */
-#include <stdlib.h>
-#include <signal.h>
-#include <string.h>
-#include "bio.h"
-
-/* interface headers */
-#include "bu/app.h"
-#include "bu/log.h"
-#include "bu/malloc.h"
-#include "bu/getopt.h"
-#include "bu/file.h"
-#include "bu/vls.h"
-#include "pkg.h"
-#include "regress_pkg_protocol.h"
-
-/* callback when an unexpected message packet is received. */
-void
-client_unexpected(struct pkg_conn *UNUSED(connection), char *UNUSED(buf))
-{
- bu_exit(-1, "Unexpected message package encountered\n");
-}
-
-/* callback when a DATA message packet is received */
-void
-client_data(struct pkg_conn *connection, char *buf)
-{
- bu_log("Received file data: %s\n", buf);
- bu_vls_printf((struct bu_vls *)connection->pkc_user_data, "\n%s\n", buf);
- free(buf);
-}
-
-/* callback when a CIAO message packet is received */
-void
-client_ciao(struct pkg_conn *UNUSED(connection), char *buf)
-{
- bu_log("CIAO received from server: %s\n", buf);
- free(buf);
-}
-
-int
-main(int UNUSED(argc), const char *argv[]) {
- 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_PORT_DIGITS + 1] = {0};
- long bytes = 0;
- int pkg_result = 0;
-
- /** our callbacks for each message type */
- struct pkg_switch callbacks[] = {
- {MSG_HELO, client_unexpected, "HELO", NULL},
- {MSG_DATA, client_data, "DATA", NULL},
- {MSG_CIAO, client_ciao, "CIAO", NULL},
- {0, 0, (char *)0, (void*)0}
- };
-
- bu_setprogname(argv[0]);
-
- /* 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_PORT_DIGITS, "%d", port);
- 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_exit(-1, "ERROR: Unable to open a connection to the server\n");
- }
- connection->pkc_switch = callbacks;
-
- /* let the server know we're cool. */
- bytes = pkg_send(MSG_HELO, MAGIC_ID, strlen(MAGIC_ID) + 1, connection);
- if (bytes < 0) {
- pkg_close(connection);
- bu_log("Connection to %s, port %d, seems faulty.\n", server, port);
- bu_exit(-1, "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(connection);
- if (pkg_result < 0) {
- bu_log("Unable to process packets? Weird.\n");
- } 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(connection);
- if (pkg_result < 0) {
- bu_log("Seemed to have trouble sucking in packets.\n");
- break;
- } else if (pkg_result == 0) {
- bu_log("Client closed the connection.\n");
- break;
- }
-
- /* process new packets received */
- pkg_result = pkg_process(connection);
- if (pkg_result < 0) {
- bu_log("Unable to process packets? Weird.\n");
- } else if (pkg_result > 0) {
- bu_log("Processed %d packet%s\n", pkg_result, pkg_result == 1 ? ""
: "s");
- }
-
- } 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, "DONE", 5, connection);
- bytes = pkg_send(MSG_CIAO, "BYE", 4, connection);
- if (bytes < 0) {
- bu_exit(-1, "Unable to cleanly disconnect from %s, port %d.\n", server,
port);
- }
-
- /* flush output and close */
- pkg_close(connection);
-
- bu_log("All messages: %s\n", bu_vls_addr(&all_msgs));
-
- bu_vls_free(&all_msgs);
- return 0;
-}
-
-/*
- * Local Variables:
- * mode: C
- * tab-width: 8
- * indent-tabs-mode: t
- * c-file-style: "stroustrup"
- * End:
- * ex: shiftwidth=4 tabstop=8
- */
Copied: brlcad/trunk/regress/pkg/regress_pkg_client.cpp (from rev 75833,
brlcad/trunk/regress/pkg/regress_pkg_client.c)
===================================================================
--- brlcad/trunk/regress/pkg/regress_pkg_client.cpp
(rev 0)
+++ brlcad/trunk/regress/pkg/regress_pkg_client.cpp 2020-05-18 17:23:46 UTC
(rev 75834)
@@ -0,0 +1,168 @@
+/* R E G R E S S _ P K G _ C L I E N T . C
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file regress_pkg_client.c
+ *
+ * Client application for libpkg regression test.
+ *
+ */
+
+#include "common.h"
+
+/* system headers */
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include "bio.h"
+
+/* interface headers */
+#include "bu/app.h"
+#include "bu/log.h"
+#include "bu/malloc.h"
+#include "bu/getopt.h"
+#include "bu/file.h"
+#include "bu/vls.h"
+#include "pkg.h"
+#include "regress_pkg_protocol.h"
+
+/* callback when an unexpected message packet is received. */
+void
+client_unexpected(struct pkg_conn *UNUSED(connection), char *UNUSED(buf))
+{
+ bu_exit(-1, "Unexpected message package encountered\n");
+}
+
+/* callback when a DATA message packet is received */
+void
+client_data(struct pkg_conn *connection, char *buf)
+{
+ bu_log("Received file data: %s\n", buf);
+ bu_vls_printf((struct bu_vls *)connection->pkc_user_data, "\n%s\n", buf);
+ free(buf);
+}
+
+/* callback when a CIAO message packet is received */
+void
+client_ciao(struct pkg_conn *UNUSED(connection), char *buf)
+{
+ bu_log("CIAO received from server: %s\n", buf);
+ free(buf);
+}
+
+int
+main(int UNUSED(argc), const char *argv[]) {
+ 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_PORT_DIGITS + 1] = {0};
+ long bytes = 0;
+ int pkg_result = 0;
+
+ /** our callbacks for each message type */
+ struct pkg_switch callbacks[] = {
+ {MSG_HELO, client_unexpected, "HELO", NULL},
+ {MSG_DATA, client_data, "DATA", NULL},
+ {MSG_CIAO, client_ciao, "CIAO", NULL},
+ {0, 0, (char *)0, (void*)0}
+ };
+
+ bu_setprogname(argv[0]);
+
+ /* 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_PORT_DIGITS, "%d", port);
+ 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_exit(-1, "ERROR: Unable to open a connection to the server\n");
+ }
+ connection->pkc_switch = callbacks;
+
+ /* let the server know we're cool. */
+ bytes = pkg_send(MSG_HELO, MAGIC_ID, strlen(MAGIC_ID) + 1, connection);
+ if (bytes < 0) {
+ pkg_close(connection);
+ bu_log("Connection to %s, port %d, seems faulty.\n", server, port);
+ bu_exit(-1, "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(connection);
+ if (pkg_result < 0) {
+ bu_log("Unable to process packets? Weird.\n");
+ } 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(connection);
+ if (pkg_result < 0) {
+ bu_log("Seemed to have trouble sucking in packets.\n");
+ break;
+ } else if (pkg_result == 0) {
+ bu_log("Client closed the connection.\n");
+ break;
+ }
+
+ /* process new packets received */
+ pkg_result = pkg_process(connection);
+ if (pkg_result < 0) {
+ bu_log("Unable to process packets? Weird.\n");
+ } else if (pkg_result > 0) {
+ bu_log("Processed %d packet%s\n", pkg_result, pkg_result == 1 ? ""
: "s");
+ }
+
+ } 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, "DONE", 5, connection);
+ bytes = pkg_send(MSG_CIAO, "BYE", 4, connection);
+ if (bytes < 0) {
+ bu_exit(-1, "Unable to cleanly disconnect from %s, port %d.\n", server,
port);
+ }
+
+ /* flush output and close */
+ pkg_close(connection);
+
+ bu_log("All messages: %s\n", bu_vls_addr(&all_msgs));
+
+ bu_vls_free(&all_msgs);
+ return 0;
+}
+
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
Deleted: brlcad/trunk/regress/pkg/regress_pkg_server.c
===================================================================
--- brlcad/trunk/regress/pkg/regress_pkg_server.c 2020-05-18 17:13:06 UTC
(rev 75833)
+++ brlcad/trunk/regress/pkg/regress_pkg_server.c 2020-05-18 17:23:46 UTC
(rev 75834)
@@ -1,189 +0,0 @@
-/* R E G R E S S _ P K G _ S E R V E R . C
- * BRL-CAD
- *
- * Copyright (c) 2020 United States Government as represented by
- * the U.S. Army Research Laboratory.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this file; see the file named COPYING for more
- * information.
- */
-/** @file regress_pkg_server.c
- *
- * Server program for the libpkg regression test. This is similar to the
- * libpkg example - the main difference being that it exits immediately with an
- * error when anything goes wrong.
- *
- */
-
-#include "common.h"
-
-/* system headers */
-#include <stdlib.h>
-#include <signal.h>
-#include <string.h>
-#include "bio.h"
-
-/* interface headers */
-#include "bu/app.h"
-#include "bu/log.h"
-#include "bu/str.h"
-#include "bu/malloc.h"
-#include "bu/getopt.h"
-#include "bu/vls.h"
-#include "bu/snooze.h"
-#include "pkg.h"
-#include "regress_pkg_protocol.h"
-
-/*
- * 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.
- */
-void
-server_helo(struct pkg_conn *UNUSED(connection), char *UNUSED(buf))
-{
- bu_exit(-1, "Unexpected HELO encountered\n");
-}
-
-/* callback when a DATA message packet is received */
-void
-server_data(struct pkg_conn *UNUSED(connection), char *buf)
-{
- bu_log("Received message from client: %s\n", buf);
- free(buf);
-}
-
-
-/* callback when a CIAO message packet is received */
-void
-server_ciao(struct pkg_conn *UNUSED(connection), char *buf)
-{
- bu_log("CIAO encountered: %s\n", buf);
- free(buf);
-}
-
-int
-main(int UNUSED(argc), const char *argv[]) {
- int port = 2000;
- struct pkg_conn *client;
- int netfd;
- char portname[MAX_PORT_DIGITS + 1] = {0};
- /* int pkg_result = 0; */
- 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},
- {MSG_DATA, server_data, "DATA", NULL},
- {MSG_CIAO, server_ciao, "CIAO", NULL},
- {0, 0, (char *)0, (void*)0}
- };
-
- bu_setprogname(argv[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_PORT_DIGITS, "%d", port);
- netfd = pkg_permserver(portname, "tcp", 0, 0);
- if (netfd < 0) {
- bu_exit(-1, "Unable to start the server");
- }
-
- /* listen for a good client indefinitely. this is a simple
- * 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) {
- bu_log("Connection seems to be busy, waiting...\n");
- bu_snooze(BU_SEC2USEC(10));
- continue;
- } else if (client == PKC_ERROR) {
- pkg_close(client);
- client = PKC_NULL;
- bu_exit(-1, "Fatal error accepting client connection.\n");
- continue;
- }
-
- /* got a connection, process it */
- msgbuffer = pkg_bwaitfor (MSG_HELO, client);
- if (msgbuffer == NULL) {
- bu_log("Failed to process the client connection, still waiting\n");
- pkg_close(client);
- client = PKC_NULL;
- } else {
- bu_log("msgbuffer: %s\n", msgbuffer);
- /* validate magic header that client should have sent */
- if (!BU_STR_EQUAL(msgbuffer, MAGIC_ID)) {
- bu_log("Bizarre corruption, received a HELO without at matching
MAGIC ID!\n");
- pkg_close(client);
- client = PKC_NULL;
- }
- }
- } while (client == PKC_NULL);
-
- /* 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 another message to the server */
- 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);
- if (bytes < 0) {
- bu_exit(-1, "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 */
- (void)pkg_bwaitfor(MSG_CIAO , client);
-
- /* shut down the server, one-time use */
- pkg_close(client);
- return 0;
-
-failure:
- pkg_close(client);
- bu_vls_free(&buffer);
- bu_exit(-1, "Unable to successfully send message.\n");
-}
-
-/*
- * Local Variables:
- * mode: C
- * tab-width: 8
- * indent-tabs-mode: t
- * c-file-style: "stroustrup"
- * End:
- * ex: shiftwidth=4 tabstop=8
- */
Copied: brlcad/trunk/regress/pkg/regress_pkg_server.cpp (from rev 75833,
brlcad/trunk/regress/pkg/regress_pkg_server.c)
===================================================================
--- brlcad/trunk/regress/pkg/regress_pkg_server.cpp
(rev 0)
+++ brlcad/trunk/regress/pkg/regress_pkg_server.cpp 2020-05-18 17:23:46 UTC
(rev 75834)
@@ -0,0 +1,189 @@
+/* R E G R E S S _ P K G _ S E R V E R . C
+ * BRL-CAD
+ *
+ * Copyright (c) 2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+/** @file regress_pkg_server.c
+ *
+ * Server program for the libpkg regression test. This is similar to the
+ * libpkg example - the main difference being that it exits immediately with an
+ * error when anything goes wrong.
+ *
+ */
+
+#include "common.h"
+
+/* system headers */
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include "bio.h"
+
+/* interface headers */
+#include "bu/app.h"
+#include "bu/log.h"
+#include "bu/str.h"
+#include "bu/malloc.h"
+#include "bu/getopt.h"
+#include "bu/vls.h"
+#include "bu/snooze.h"
+#include "pkg.h"
+#include "regress_pkg_protocol.h"
+
+/*
+ * 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.
+ */
+void
+server_helo(struct pkg_conn *UNUSED(connection), char *UNUSED(buf))
+{
+ bu_exit(-1, "Unexpected HELO encountered\n");
+}
+
+/* callback when a DATA message packet is received */
+void
+server_data(struct pkg_conn *UNUSED(connection), char *buf)
+{
+ bu_log("Received message from client: %s\n", buf);
+ free(buf);
+}
+
+
+/* callback when a CIAO message packet is received */
+void
+server_ciao(struct pkg_conn *UNUSED(connection), char *buf)
+{
+ bu_log("CIAO encountered: %s\n", buf);
+ free(buf);
+}
+
+int
+main(int UNUSED(argc), const char *argv[]) {
+ int port = 2000;
+ struct pkg_conn *client;
+ int netfd;
+ char portname[MAX_PORT_DIGITS + 1] = {0};
+ /* int pkg_result = 0; */
+ 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},
+ {MSG_DATA, server_data, "DATA", NULL},
+ {MSG_CIAO, server_ciao, "CIAO", NULL},
+ {0, 0, (char *)0, (void*)0}
+ };
+
+ bu_setprogname(argv[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_PORT_DIGITS, "%d", port);
+ netfd = pkg_permserver(portname, "tcp", 0, 0);
+ if (netfd < 0) {
+ bu_exit(-1, "Unable to start the server");
+ }
+
+ /* listen for a good client indefinitely. this is a simple
+ * 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) {
+ bu_log("Connection seems to be busy, waiting...\n");
+ bu_snooze(BU_SEC2USEC(10));
+ continue;
+ } else if (client == PKC_ERROR) {
+ pkg_close(client);
+ client = PKC_NULL;
+ bu_exit(-1, "Fatal error accepting client connection.\n");
+ continue;
+ }
+
+ /* got a connection, process it */
+ msgbuffer = pkg_bwaitfor (MSG_HELO, client);
+ if (msgbuffer == NULL) {
+ bu_log("Failed to process the client connection, still waiting\n");
+ pkg_close(client);
+ client = PKC_NULL;
+ } else {
+ bu_log("msgbuffer: %s\n", msgbuffer);
+ /* validate magic header that client should have sent */
+ if (!BU_STR_EQUAL(msgbuffer, MAGIC_ID)) {
+ bu_log("Bizarre corruption, received a HELO without at matching
MAGIC ID!\n");
+ pkg_close(client);
+ client = PKC_NULL;
+ }
+ }
+ } while (client == PKC_NULL);
+
+ /* 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 another message to the server */
+ 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);
+ if (bytes < 0) {
+ bu_exit(-1, "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 */
+ (void)pkg_bwaitfor(MSG_CIAO , client);
+
+ /* shut down the server, one-time use */
+ pkg_close(client);
+ return 0;
+
+failure:
+ pkg_close(client);
+ bu_vls_free(&buffer);
+ bu_exit(-1, "Unable to successfully send message.\n");
+}
+
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits