summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2012-03-06 17:45:04 (GMT)
committer Jaromil <jaromil@dyne.org>2012-03-06 17:45:04 (GMT)
commit43189c9921b1aa0be0f1346441558ef4605d5bbe (patch)
tree5e6e35716a42e5cbdc00750ab313ac65351de12a
parent0a6f44a0abeec1a1b813d2bb64e177abceb1bfbd (diff)
test handshake in pgm broadcast
-rw-r--r--.gitignore6
-rw-r--r--src/Makefile.am6
-rw-r--r--src/handshake.cpp109
3 files changed, 118 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index a1c296b..808a8ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,7 +4,13 @@ src/avremote
src/hdsync
*~
*.o
+*.lo
+*.la
*.log
+libtool
+ltmain.sh
+m4
+.libs
.deps
Makefile
Makefile.in
diff --git a/src/Makefile.am b/src/Makefile.am
index 6826879..ff10dda 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,12 +2,12 @@ INCLUDES = -I$(top_srcdir)/src/include
SUBDIRS = uuid pgm 0mq
-bin_PROGRAMS = netcat broadcaster hdsync avremote hwclient
+bin_PROGRAMS = netcat broadcaster hdsync avremote handshake
-hwclient_SOURCES = hwclient.cpp
+handshake_SOURCES = handshake.cpp
-hwclient_LDADD = uuid/libuuid.la 0mq/libzmq.la pgm/libpgm.la -lpthread
+handshake_LDADD = uuid/libuuid.la 0mq/libzmq.la pgm/libpgm.la -lpthread
netcat_SOURCES = \
core.c \
diff --git a/src/handshake.cpp b/src/handshake.cpp
new file mode 100644
index 0000000..a369722
--- /dev/null
+++ b/src/handshake.cpp
@@ -0,0 +1,109 @@
+/* HDSync
+
+ (c) 2012 Denis Roio <jaromil@dyne.org>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program 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 Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>
+
+*/
+
+#include <zmq.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int main (int argc, char **argv)
+{
+ void *context = zmq_init (1);
+ void *sock_in = NULL;
+ void *sock_out = NULL;
+ int request_nbr;
+ zmq_msg_t request, reply;
+
+ if(argc<2) {
+ printf("Error: need two arguments: [ offer | listen ] [0mq address]\n");
+ printf("Example: handshake offer pgm://eth0;239.192.1.1:5555\n");
+ exit(1);
+ }
+
+ // create offer socket
+ sock_out = zmq_socket (context, ZMQ_PUB);
+ zmq_connect(sock_out, argv[2]);
+
+ // create listen socket
+ sock_in = zmq_socket (context, ZMQ_SUB);
+ zmq_bind (sock_in, argv[2]);
+
+ // listen to all broadcasts by default
+ zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"",0);
+
+ if(strcmp(argv[1],"offer")==0) {
+
+ printf ("Offering on %s\n",argv[2]);
+
+ zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"ACK",3);
+
+ while(1) {
+
+ zmq_msg_init_size (&request, 5);
+ memcpy (zmq_msg_data (&request), "OFFER", 5);
+ printf ("Sending offer...\n", request_nbr);
+ zmq_send (sock_out, &request, 0);
+ zmq_msg_close (&request);
+
+ sleep(1);
+
+ zmq_msg_init (&reply);
+ zmq_recv (sock_in, &reply, 0);
+ printf ("Received response\n");
+ printf("Data: %s\n",zmq_msg_data(&reply));
+ zmq_msg_close (&reply);
+
+ }
+
+ } else if(strcmp(argv[1],"listen")==0) {
+
+ printf ("Listening on %s\n",argv[2]);
+
+ while (1) {
+ // Wait for next request from client
+ zmq_msg_init (&request);
+ zmq_recv (sock_in, &request, 0);
+ printf ("Received offer\n");
+ printf ("Data: %s\n",zmq_msg_data(&request));
+ zmq_msg_close (&request);
+
+ // Do some 'work'
+ sleep (1);
+
+ // Send reply back to client
+ zmq_msg_init_size (&reply, 3);
+ memcpy (zmq_msg_data (&reply), "ACK", 3);
+ zmq_send (sock_out, &reply, 0);
+ zmq_msg_close (&reply);
+
+ }
+
+ } else {
+ printf("Command not recognized: %s\n",argv[1]);
+
+ }
+
+ if(sock_out) zmq_close (sock_out);
+ if(sock_in) zmq_close (sock_in);
+
+ zmq_term (context);
+
+ exit(0);
+}