summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaromil <jaromil@dyne.org>2012-05-08 11:03:22 (GMT)
committer Jaromil <jaromil@dyne.org>2012-05-08 11:03:22 (GMT)
commitec46a4a1ee35d2fe11cb0e7595c1cb31f4fbc736 (patch)
tree2bffe222fc5c8947d849db950700fd4dfef6b577
parentd500639fc8105df1a069586b5412f0c2cbe96d1f (diff)
code flow cleanup and clean exit
-rw-r--r--src/hdsync_cli.cpp186
1 files changed, 96 insertions, 90 deletions
diff --git a/src/hdsync_cli.cpp b/src/hdsync_cli.cpp
index 88163bc..0f6ec9e 100644
--- a/src/hdsync_cli.cpp
+++ b/src/hdsync_cli.cpp
@@ -63,8 +63,8 @@ int chanTOT = 0;
// upnp reply parser
parser_f *parser = NULL;
-// peers found
-char **listeners = NULL;
+// info on peers
+upnp_t **peers;
// own IPv4 address
@@ -91,7 +91,7 @@ int shout(const char *message, int flags) {
// zmq_connect(sock_out, pgmaddr);
zmq_msg_init_size (&request, strlen(message));
memcpy (zmq_msg_data (&request), message, strlen(message));
- // printf ("Shouting \"%s\"\n", message);
+ printf ("Shouting \"%s\"\n", message);
res = zmq_send (sock_shout, &request, flags);
zmq_msg_close (&request);
// zmq_close(sock_out);
@@ -113,7 +113,7 @@ int expect(const char *prefix, char *message, int flags) {
zmq_msg_init (&reply);
zmq_recv (sock_in, &reply, flags);
// printf ("Received response\n");
- snprintf(res,511,"%s",zmq_msg_data(&reply));
+ snprintf(res,511,"%s",(char*)zmq_msg_data(&reply));
zmq_msg_close (&reply);
// parse the response and if ACK insert it in uniq list
@@ -132,27 +132,26 @@ int expect(const char *prefix, char *message, int flags) {
}
int handshake() {
- int request_nbr;
char message[256];
- int listindex = 0;
+ int listindex = 1;
int listmax = 0;
char res[512];
- int c, i;
-
+ int c;
+
+ listmax = chanTOT; // number of desired peers
+ printf("looking to connect %u peers\n",listmax);
+
+ // allocate space for listener array
+ for(c=1; c<listmax; c++)
+ peers[c] = create_upnp();
+
if(chanID==1) { // first channel is the one to offer
-
+
////////
// OFFER
//////////////////////////////
printf ("Offering on %s from address %s\n",pgmaddr, IPv4);
-
- listmax = chanTOT; // 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(i=0; 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
snprintf(message,255,"OFFER;%s;",IPv4);
@@ -164,11 +163,14 @@ int handshake() {
// printf("Got ACK from %s\n",res);
// parse the response and if ACK insert it in uniq list
for(c=0; c<listindex; c++)
- if(strncmp(listeners[c], res, 16) == 0)
+ if(strncmp(peers[c]->hostname, res, 16) == 0)
break; // found a duplicate
if(c==listindex) { // there was no duplicate
- snprintf(listeners[c], 16, "%s", res);
- printf("New listener: %s\n",listeners[c]);
+ // parse and insert hostname and port
+ char *p = res; while(*p != ':') p++; *p='\0';
+ snprintf(peers[c]->hostname, 16, "%s", res);
+ sscanf(p+1, "%u", &peers[c]->port);
+ printf("New listener: %s : %u\n",peers[c]->hostname, peers[c]->port);
listindex++;
}
@@ -190,9 +192,11 @@ int handshake() {
/////////
// LISTEN
- printf ("Listening on %s from address %s\n",pgmaddr, IPv4);
+ printf ("Listening on %s from address %s\n",pgmaddr, peers[0]->hostname);
+
+
+ snprintf(message,255,"ACK;%s:%u;",peers[0]->hostname,peers[0]->port);
- snprintf(message,255,"ACK;%s;",IPv4);
while (1) {
// Wait for offer
@@ -212,9 +216,8 @@ int handshake() {
}
int main(int argc, char **argv) {
- upnp_t *upnp;
- int found;
char tmp[512];
+ int c;
//////////
///// INIT
@@ -229,31 +232,32 @@ int main(int argc, char **argv) {
fprintf(stderr,"HDSync starting for channel %u of %u\n",chanID, chanTOT);
fprintf(stderr,"will sync and play video: %s\n", filename);
- upnp = create_upnp();
-
- // no server specified, force localhost
- if(!server[0]) sprintf(server,"localhost");
+ if( ! get_address() ) {
+ fprintf(stderr,"Error getting own address.\n");
+ exit(ERR);
+ }
- // commandline or detection found explicit addresses
- snprintf(upnp->hostname, MAX_HOSTNAME_SIZE-1,"%s",server);
- upnp->port = port;
+ // we put ourselves in first
+ peers = (upnp_t**) calloc(chanTOT, sizeof(upnp_t*));
+ peers[0] = create_upnp();
+ snprintf(peers[0]->hostname, 16, "%s", IPv4);
+ peers[0]->port = port;
if(!dryrun) {
- if ( connect_upnp (upnp) < 0 ) {
- fprintf(stderr,"can't connect to %s:%u: operation aborted.\n", server, port);
- free_upnp(upnp);
+ if ( connect_upnp (peers[0]) < 0 ) {
+ fprintf(stderr,"can't connect to local UPNP: operation aborted.\n");
+ // free all peers array
+ for(c=0; c<chanTOT; c++)
+ free_upnp(peers[c]);
exit(ERR);
} else {
- fprintf(stderr,"UPNP connected to %s:%u\n",server, port);
- close(upnp->sockfd);
- upnp->sockfd = 0;
+ fprintf(stderr,"UPNP connected to %s:%u\n",
+ peers[0]->hostname, peers[0]->port);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
}
}
- if( ! get_address() ) {
- fprintf(stderr,"Error getting own address.\n");
- exit(ERR);
- }
// initialize 0mq
zcontext = zmq_init (1);
@@ -314,55 +318,60 @@ int main(int argc, char **argv) {
// stop player
- connect_upnp(upnp);
- render_upnp(upnp,"Stop","");
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
- close(upnp->sockfd);
- upnp->sockfd = 0;
+ connect_upnp(peers[0]);
+ render_upnp(peers[0],"Stop","");
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
zmq_sleep(1);
// load video into player
- connect_upnp(upnp);
- render_uri_meta(upnp,filename);
- render_upnp(upnp,"SetAVTransportURI", upnp->meta);
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
- close(upnp->sockfd);
- upnp->sockfd = 0;
+ connect_upnp(peers[0]);
+ render_uri_meta(peers[0],filename);
+ render_upnp(peers[0],"SetAVTransportURI", peers[0]->meta);
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
zmq_sleep(1);
// play
- connect_upnp(upnp);
- render_upnp(upnp,"Play","<Speed>1</Speed>");
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
- close(upnp->sockfd);
- upnp->sockfd = 0;
-
+ connect_upnp(peers[0]);
+ render_upnp(peers[0],"Play","<Speed>1</Speed>");
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
+
+ // offer checks if all are playing
+ // if(chanID==1) {
+ // int playing = 0;
+ // while(playing < listmax)
+
zmq_sleep(5);
- connect_upnp(upnp);
+ connect_upnp(peers[0]);
snprintf(tmp,511,"<Unit>REL_TIME</Unit><Target>%s</Target>",filename);
- render_upnp(upnp,"Seek","<Unit>REL_TIME</Unit><Target>00:00:00</Target>");
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
- close(upnp->sockfd);
- upnp->sockfd = 0;
+ render_upnp(peers[0],"Seek","<Unit>REL_TIME</Unit><Target>00:00:00</Target>");
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
// pause
- connect_upnp(upnp);
- render_upnp(upnp,"Pause","");
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
- close(upnp->sockfd);
- upnp->sockfd = 0;
+ connect_upnp(peers[0]);
+ render_upnp(peers[0],"Pause","");
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
+ close(peers[0]->sockfd);
+ peers[0]->sockfd = 0;
// prepare upnp play message
- connect_upnp(upnp);
- render_upnp(upnp,"Play","<Speed>1</Speed>");
+ connect_upnp(peers[0]);
+ render_upnp(peers[0],"Play","<Speed>1</Speed>");
if(chanID==1) { // offer should send sync
@@ -378,34 +387,31 @@ int main(int argc, char **argv) {
}
// start player
- send_upnp(upnp);
- recv_upnp(upnp, 1000);
+ send_upnp(peers[0]);
+ recv_upnp(peers[0], 1000);
zmq_sleep(1);
- free_upnp(upnp);
} // if(test|real)
fprintf(stderr,"Software shutdown\n");
- // if(sock_out) {
zmq_close (sock_shout);
- // sock_out = NULL;
- // }
- // if(sock_in) {
+
zmq_close (sock_in);
- // sock_in = NULL;
- // }
// somehow offer blocks here
+ printf("free 0mq context\n");
zmq_term (zcontext);
- if(listeners) {
- for(int c; c<chanTOT; c++)
- free(listeners[c]);
- free(listeners);
+ printf("free peer array\n");
+ // free all peers array
+ for(c=0; c<chanTOT; c++) {
+ if(peers[c] != NULL)
+ free_upnp(peers[c]);
}
-
+ free(peers);
+
fprintf(stderr,"Done.\n");
exit(0);
}
@@ -415,7 +421,7 @@ void cmdline(int argc, char **argv) {
filename[0] = 0;
server[0] = 0;
- int res, optlen;
+ int res;
do {
res = getopt(argc, argv, short_options);
switch(res) {