From 717bead50c8b209dac3f6ac8cb2c081613850d26 Mon Sep 17 00:00:00 2001
From: Patrick Simianer
Date: Wed, 4 Mar 2015 17:50:46 +0100
Subject: nanomsg examples
---
nanomsg/Makefile | 11 ++++++++
nanomsg/README | 5 ++++
nanomsg/cppnanomsg | 1 +
nanomsg/pair | Bin 0 -> 13049 bytes
nanomsg/pair.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
nanomsg/pipeline | Bin 0 -> 12805 bytes
nanomsg/pipeline.c | 52 +++++++++++++++++++++++++++++++++++++
nanomsg/pipeline.cc | 54 ++++++++++++++++++++++++++++++++++++++
8 files changed, 196 insertions(+)
create mode 100644 nanomsg/Makefile
create mode 100644 nanomsg/README
create mode 160000 nanomsg/cppnanomsg
create mode 100755 nanomsg/pair
create mode 100644 nanomsg/pair.c
create mode 100755 nanomsg/pipeline
create mode 100644 nanomsg/pipeline.c
create mode 100644 nanomsg/pipeline.cc
(limited to 'nanomsg')
diff --git a/nanomsg/Makefile b/nanomsg/Makefile
new file mode 100644
index 0000000..634c8cc
--- /dev/null
+++ b/nanomsg/Makefile
@@ -0,0 +1,11 @@
+all: pipeline pipelinec pair
+
+pipeline:
+ g++ -I cppnanomsg pipeline.cc -o pipeline /usr/lib64/libnanomsg.so
+
+pipelinec: pipeline.c
+ gcc pipeline.c -o pipeline /usr/lib64/libnanomsg.so
+
+pair: pair.c
+ gcc pair.c -o pair /usr/lib64/libnanomsg.so
+
diff --git a/nanomsg/README b/nanomsg/README
new file mode 100644
index 0000000..31cc054
--- /dev/null
+++ b/nanomsg/README
@@ -0,0 +1,5 @@
+examples from [1]
+
+
+[1] http://tim.dysinger.net/posts/2013-09-16-getting-started-with-nanomsg.html
+
diff --git a/nanomsg/cppnanomsg b/nanomsg/cppnanomsg
new file mode 160000
index 0000000..a36d44d
--- /dev/null
+++ b/nanomsg/cppnanomsg
@@ -0,0 +1 @@
+Subproject commit a36d44db1827a36bbd3868825c1b82d23f10e491
diff --git a/nanomsg/pair b/nanomsg/pair
new file mode 100755
index 0000000..978ab21
Binary files /dev/null and b/nanomsg/pair differ
diff --git a/nanomsg/pair.c b/nanomsg/pair.c
new file mode 100644
index 0000000..f252518
--- /dev/null
+++ b/nanomsg/pair.c
@@ -0,0 +1,73 @@
+#include
+#include
+#include
+#include
+#include
+#include
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+
+int send_name(int sock, const char *name)
+{
+ printf ("%s: SENDING \"%s\"\n", name, name);
+ int sz_n = strlen (name) + 1; // '\0' too
+ return nn_send (sock, name, sz_n, 0);
+}
+
+int recv_name(int sock, const char *name)
+{
+ char *buf = NULL;
+ int result = nn_recv (sock, &buf, NN_MSG, 0);
+ if (result > 0)
+ {
+ printf ("%s: RECEIVED \"%s\"\n", name, buf);
+ nn_freemsg (buf);
+ }
+ return result;
+}
+
+int send_recv(int sock, const char *name)
+{
+ int to = 100;
+ assert (nn_setsockopt (sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)) >= 0);
+ while(1)
+ {
+ recv_name(sock, name);
+ sleep(1);
+ send_name(sock, name);
+ }
+}
+
+int node0 (const char *url)
+{
+ int sock = nn_socket (AF_SP, NN_PAIR);
+ assert (sock >= 0);
+ assert (nn_bind (sock, url) >= 0);
+ send_recv(sock, NODE0);
+ return nn_shutdown (sock, 0);
+}
+
+int node1 (const char *url)
+{
+ int sock = nn_socket (AF_SP, NN_PAIR);
+ assert (sock >= 0);
+ assert (nn_connect (sock, url) >= 0);
+ send_recv(sock, NODE1);
+ return nn_shutdown (sock, 0);
+}
+
+int main (const int argc, const char **argv)
+{
+ if (strncmp (NODE0, argv[1], strlen (NODE0)) == 0 && argc > 1)
+ return node0 (argv[2]);
+ else if (strncmp (NODE1, argv[1], strlen (NODE1)) == 0 && argc > 1)
+ return node1 (argv[2]);
+ else
+ {
+ fprintf (stderr, "Usage: pair %s|%s ...\n",
+ NODE0, NODE1);
+ return 1;
+ }
+}
+
diff --git a/nanomsg/pipeline b/nanomsg/pipeline
new file mode 100755
index 0000000..cacc226
Binary files /dev/null and b/nanomsg/pipeline differ
diff --git a/nanomsg/pipeline.c b/nanomsg/pipeline.c
new file mode 100644
index 0000000..37340fa
--- /dev/null
+++ b/nanomsg/pipeline.c
@@ -0,0 +1,52 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+
+int node0 (const char *url)
+{
+ int sock = nn_socket (AF_SP, NN_PULL);
+ assert (sock >= 0);
+ assert (nn_bind (sock, url) >= 0);
+ while (1)
+ {
+ char *buf = NULL;
+ int bytes = nn_recv (sock, &buf, NN_MSG, 0);
+ assert (bytes >= 0);
+ printf ("NODE0: RECEIVED \"%s\"\n", buf);
+ nn_freemsg (buf);
+ }
+}
+
+int node1 (const char *url, const char *msg)
+{
+ int sz_msg = strlen (msg) + 1; // '\0' too
+ int sock = nn_socket (AF_SP, NN_PUSH);
+ assert (sock >= 0);
+ assert (nn_connect (sock, url) >= 0);
+ printf ("NODE1: SENDING \"%s\"\n", msg);
+ int bytes = nn_send (sock, msg, sz_msg, 0);
+ assert (bytes == sz_msg);
+ return nn_shutdown (sock, 0);
+}
+
+int main (const int argc, const char **argv)
+{
+ if (strncmp (NODE0, argv[1], strlen (NODE0)) == 0 && argc > 1)
+ return node0 (argv[2]);
+ else if (strncmp (NODE1, argv[1], strlen (NODE1)) == 0 && argc > 2)
+ return node1 (argv[2], argv[3]);
+ else
+ {
+ fprintf (stderr, "Usage: pipeline %s|%s ...'\n",
+ NODE0, NODE1);
+ return 1;
+ }
+}
+
diff --git a/nanomsg/pipeline.cc b/nanomsg/pipeline.cc
new file mode 100644
index 0000000..704f24c
--- /dev/null
+++ b/nanomsg/pipeline.cc
@@ -0,0 +1,54 @@
+/*
+ * template.cpp
+ *
+ * Patrick Simianer
+ * YYYY-MM-DD
+ */
+
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+void
+receiver(const string url)
+{
+ nn::socket s(AF_SP, NN_PULL);
+ s.bind(url.c_str());
+ while (1) {
+ char *buf = NULL;
+ s.recv(&buf, NN_MSG, 0);
+ cout << "receiving " << buf << endl;
+ }
+}
+
+void
+send(const string url, const string msg)
+{
+
+ nn::socket s(AF_SP, NN_PUSH);
+ s.connect(url.c_str());
+ cout << "sending " << msg << endl;
+ s.send(msg.c_str(), msg.size()+1, 0);
+}
+
+int main(int argc, char const* argv[])
+{
+ string cmd(argv[1]);
+ if (cmd == "send") {
+ ostringstream msg;
+ string url(argv[2]);
+ for (size_t i = 3; i < argc; i++)
+ msg << argv[i];
+ send(url, msg.str());
+ } else {
+ string url(argv[1]);
+ receiver(url);
+ }
+
+ return 0;
+}
+
--
cgit v1.2.3