summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2012-04-10 12:22:49 (GMT)
committer Jaromil <jaromil@dyne.org>2012-04-10 12:22:49 (GMT)
commitf96e719f004cc40a4cc6e8821d4cd295d094b766 (patch)
treea8534e899543e9164ed0bcc5b983b13cc27d8b24
parentfdea689c7f0af52183f910b714ce4f04427c816c (diff)
handshake counts unique listeners until goal reached
-rw-r--r--src/handshake.cpp61
1 files changed, 50 insertions, 11 deletions
diff --git a/src/handshake.cpp b/src/handshake.cpp
index f57fa4f..d3a4a23 100644
--- a/src/handshake.cpp
+++ b/src/handshake.cpp
@@ -40,7 +40,10 @@ int main (int argc, char **argv)
zmq_msg_t request, reply;
char message[256];
char pgmaddr[64];
-
+ char **listeners = NULL;
+ int listindex = 0;
+ int listmax = 0;
+
// discover own IP address
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
@@ -71,7 +74,7 @@ int main (int argc, char **argv)
exit(1);
}
- if(argc<2) {
+ if(argc!=3) {
printf("Error: need two arguments: [ offer | listen ] [ total number of peers ]\n");
printf("Example: handshake offer 3\n");
exit(1);
@@ -85,21 +88,29 @@ int main (int argc, char **argv)
sock_in = zmq_socket (context, ZMQ_SUB);
zmq_bind (sock_in, pgmaddr);
-
+
if(strcmp(argv[1],"offer")==0) {
-
+ //////////////////////////////
printf ("Offering on %s from address %s\n",pgmaddr, IPv4);
+ sscanf(argv[2],"%u",&listmax); // number of desired peers
+ listmax--; // we count outrselves in
+ printf("looking to connect %u peers\n",listmax);
+ // allocate space for listener array
+ listeners = (char**) calloc(listmax,sizeof(char*));
+ for(int i; i<listmax; i++) listeners[i] = (char*)calloc(16, sizeof(char));
+
// 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);
while(1) {
+ char res[128];
zmq_msg_init_size (&request, strlen(message));
memcpy (zmq_msg_data (&request), message, strlen(message));
- printf ("Sending offer...\n", request_nbr);
+ // printf ("Sending offer...\n", request_nbr);
zmq_send (sock_out, &request, 0);
zmq_msg_close (&request);
@@ -107,18 +118,46 @@ int main (int argc, char **argv)
zmq_msg_init (&reply);
zmq_recv (sock_in, &reply, 0);
- printf ("Received response\n");
- printf("Data: %s\n",zmq_msg_data(&reply));
+ // printf ("Received response\n");
+ snprintf(res,127,"%s",zmq_msg_data(&reply));
zmq_msg_close (&reply);
- }
+ // parse the response and if ACK insert it in uniq list
+ if( strncmp(res,"ACK",3) == 0) {
+ int c;
+ char *p, *pp;
+ printf("%s\n",res);
+
+ p = res; while(*p != ';') p++; p++;
+ pp = p; while(*pp != ';') pp++; *pp='\0';
+
+ // *p has the ip string
+ for(c=0; c<listindex; c++)
+ if(strncmp(listeners[c], p, 16) == 0)
+ break; // found a duplicate
+ if(c==listindex) { // there was no duplicate
+ snprintf(listeners[c], 16, "%s", p);
+ printf("New listener: %s\n",p);
+ listindex++;
+ }
+ if(listindex==listmax) { // goal reached
+ printf("All listeners found:\n");
+ for(c=0;c<listmax;c++)
+ printf("%s\n",listeners[c]);
+ break;
+ }
+
+ } // if ACK response
+
+ } // offer while loop
} else if(strcmp(argv[1],"listen")==0) {
printf ("Listening on %s from address %s\n",pgmaddr, IPv4);
- // listen subscribes only to offers
+ // listen subscribes only to offers and ready
zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"OFFER",5);
+ zmq_setsockopt(sock_in,ZMQ_SUBSCRIBE,"READY",5);
snprintf(message,255,"ACK;%s;",IPv4);
@@ -127,8 +166,8 @@ int main (int argc, char **argv)
// 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));
+ // printf ("Received offer\n");
+ printf ("%s\n",zmq_msg_data(&request));
zmq_msg_close (&request);
sleep (1);