diff options
author | Patrick Simianer <p@simianer.de> | 2015-03-04 17:50:46 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2015-03-04 17:50:46 +0100 |
commit | 717bead50c8b209dac3f6ac8cb2c081613850d26 (patch) | |
tree | 286b7a073670df59c55d2f9745678ac4c179c513 | |
parent | c0ece722bd7775af14c7f2ec18dcd1fd138607cb (diff) |
nanomsg examples
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | nanomsg/Makefile | 11 | ||||
-rw-r--r-- | nanomsg/README | 5 | ||||
m--------- | nanomsg/cppnanomsg | 0 | ||||
-rwxr-xr-x | nanomsg/pair | bin | 0 -> 13049 bytes | |||
-rw-r--r-- | nanomsg/pair.c | 73 | ||||
-rwxr-xr-x | nanomsg/pipeline | bin | 0 -> 12805 bytes | |||
-rw-r--r-- | nanomsg/pipeline.c | 52 | ||||
-rw-r--r-- | nanomsg/pipeline.cc | 54 |
9 files changed, 198 insertions, 0 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c0151b1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "nanomsg/cppnanomsg"] + path = nanomsg/cppnanomsg + url = https://github.com/nanomsg/cppnanomsg.git 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 +Subproject a36d44db1827a36bbd3868825c1b82d23f10e49 diff --git a/nanomsg/pair b/nanomsg/pair Binary files differnew file mode 100755 index 0000000..978ab21 --- /dev/null +++ b/nanomsg/pair 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 <assert.h> +#include <unistd.h> +#include <string.h> +#include <nanomsg/nn.h> +#include <nanomsg/pair.h> +#include <stdio.h> + +#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 <URL> <ARG> ...\n", + NODE0, NODE1); + return 1; + } +} + diff --git a/nanomsg/pipeline b/nanomsg/pipeline Binary files differnew file mode 100755 index 0000000..cacc226 --- /dev/null +++ b/nanomsg/pipeline 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 <assert.h> +#include <unistd.h> +#include <string.h> +#include <pthread.h> +#include <stdio.h> +#include <nanomsg/nn.h> +#include <nanomsg/pipeline.h> + +#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 <URL> <ARG> ...'\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 <p@simianer.de> + * YYYY-MM-DD + */ + +#include <iostream> +#include <nanomsg/nn.h> +#include <nanomsg/pipeline.h> +#include <nn.hpp> +#include <sstream> + +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; +} + |