summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2012-04-10 11:10:59 (GMT)
committer Jaromil <jaromil@dyne.org>2012-04-10 11:10:59 (GMT)
commitfdea689c7f0af52183f910b714ce4f04427c816c (patch)
tree961342186f3979c7bbcb0295ff28779ddea92644
parent43189c9921b1aa0be0f1346441558ef4605d5bbe (diff)
perfectioned handshake with auto generated pgm addresses
-rw-r--r--src/handshake.cpp78
1 files changed, 62 insertions, 16 deletions
diff --git a/src/handshake.cpp b/src/handshake.cpp
index a369722..f57fa4f 100644
--- a/src/handshake.cpp
+++ b/src/handshake.cpp
@@ -23,6 +23,14 @@
#include <stdio.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define ETHDEV "eth0"
+#define PGMPORT "5555"
+
int main (int argc, char **argv)
{
void *context = zmq_init (1);
@@ -30,34 +38,67 @@ int main (int argc, char **argv)
void *sock_out = NULL;
int request_nbr;
zmq_msg_t request, reply;
+ char message[256];
+ char pgmaddr[64];
+ // discover own IP address
+ struct ifaddrs * ifAddrStruct=NULL;
+ struct ifaddrs * ifa=NULL;
+ void * tmpAddrPtr=NULL;
+ char IPv4[24];
+ printf("Discovering self assigned IPv4 addresses\n");
+ getifaddrs(&ifAddrStruct);
+
+ pgmaddr[0] = NULL; // zero if iface not found
+
+ for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
+ if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
+ // is a valid IP4 Address
+ tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
+ char addressBuffer[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
+ // printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
+ if( strcmp(ifa->ifa_name, ETHDEV) == 0) {
+ strncpy(IPv4, addressBuffer, 16);
+ snprintf(pgmaddr,63,"pgm://%s:%s",ifa->ifa_name, PGMPORT);
+ }
+ }
+ }
+ if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
+
+ if(!pgmaddr[0]) {
+ printf("Error: ethernet interface %s is not configured\n",ETHDEV);
+ exit(1);
+ }
+
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");
+ printf("Error: need two arguments: [ offer | listen ] [ total number of peers ]\n");
+ printf("Example: handshake offer 3\n");
exit(1);
}
// create offer socket
sock_out = zmq_socket (context, ZMQ_PUB);
- zmq_connect(sock_out, argv[2]);
+ zmq_connect(sock_out, pgmaddr);
// create listen socket
sock_in = zmq_socket (context, ZMQ_SUB);
- zmq_bind (sock_in, argv[2]);
+ zmq_bind (sock_in, pgmaddr);
- // 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]);
+ printf ("Offering on %s from address %s\n",pgmaddr, IPv4);
+
+ // offer subscribes to all broadcasts by default
+ // somehow listening to ACK only doesn't works
+ zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"",0);
+ snprintf(message,255,"OFFER;%s;",IPv4);
- zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"ACK",3);
-
while(1) {
- zmq_msg_init_size (&request, 5);
- memcpy (zmq_msg_data (&request), "OFFER", 5);
+ zmq_msg_init_size (&request, strlen(message));
+ memcpy (zmq_msg_data (&request), message, strlen(message));
printf ("Sending offer...\n", request_nbr);
zmq_send (sock_out, &request, 0);
zmq_msg_close (&request);
@@ -74,7 +115,13 @@ int main (int argc, char **argv)
} else if(strcmp(argv[1],"listen")==0) {
- printf ("Listening on %s\n",argv[2]);
+ printf ("Listening on %s from address %s\n",pgmaddr, IPv4);
+
+ // listen subscribes only to offers
+ zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"OFFER",5);
+
+
+ snprintf(message,255,"ACK;%s;",IPv4);
while (1) {
// Wait for next request from client
@@ -84,12 +131,11 @@ int main (int argc, char **argv)
printf ("Data: %s\n",zmq_msg_data(&request));
zmq_msg_close (&request);
- // Do some 'work'
- sleep (1);
+ sleep (1);
// Send reply back to client
- zmq_msg_init_size (&reply, 3);
- memcpy (zmq_msg_data (&reply), "ACK", 3);
+ zmq_msg_init_size (&reply, strlen(message));
+ memcpy (zmq_msg_data (&reply), message, strlen(message));
zmq_send (sock_out, &reply, 0);
zmq_msg_close (&reply);