From 3f0a442799955f56b2c77aabd6bc7aa4458718b4 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 17 Jul 2016 14:21:26 +0200 Subject: API changes, pedandic fixes, dgrambuf stats & info field, recvmmsg() with alarm(), partial writev() support. --- mcastseed/src/mcastseed.c | 86 ++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 42 deletions(-) (limited to 'mcastseed/src/mcastseed.c') diff --git a/mcastseed/src/mcastseed.c b/mcastseed/src/mcastseed.c index 09cadac..6440fc6 100644 --- a/mcastseed/src/mcastseed.c +++ b/mcastseed/src/mcastseed.c @@ -82,31 +82,31 @@ int main(int argc, char *argv[]) { switch ( state ) { case 1: res = send_hello(); state = (res==0)?2:-1; break; case 2: res = accept_pending_clients_or_wait_a_bit(); - if (res==0) state = 2; // Some clients has just come in, try to get more - else if (res==1) state = 1; // Nothing new. Keep accepting clients after another hello - else if (res==2) state = 3; // Wanted clients are accepted - else state = -2; - break; + if (res==0) state = 2; /* Some clients has just come in, try to get more */ + else if (res==1) state = 1; /* Nothing new. Keep accepting clients after another hello */ + else if (res==2) state = 3; /* Wanted clients are accepted */ + else state = -2; + break; case 3: res = start_job(); - if (res==0) state = 3; // Keep trying to convince every client to start - else if (res==1) state = 4; // All clients have started the job pipe - else if (res==2) state = 4; // There is dead clients but all alive are ready to go - else state = -3; - break; + if (res==0) state = 3; /* Keep trying to convince every client to start */ + else if (res==1) state = 4; /* All clients have started the job pipe */ + else if (res==2) state = 4; /* There is dead clients but all alive are ready to go */ + else state = -3; + break; case 4: res = send_data(); - if (res==0) state = 4; - else if (res==1) state = 5; // All data sent - else state = -4; - break; + if (res==0) state = 4; + else if (res==1) state = 5; /* All data sent */ + else state = -4; + break; case 5: res = wait_all_finalize_job(); - if (res==0) state = 5; - else if (res==1) state = 6; - else state = -5; + if (res==0) state = 5; + else if (res==1) state = 6; + else state = -5; case 6: res = is_there_more_job(); - if (res==0) state = 0; - else if (res==1) state = 3; - else state = -6; - break; + if (res==0) state = 0; + else if (res==1) state = 3; + else state = -6; + break; } } fsm_trace(state); @@ -144,7 +144,7 @@ int accept_pending_clients_or_wait_a_bit() { FD_ZERO(&readfds); FD_ZERO(&exceptfds); - FD_SET(0,&readfds); // Read from stdin. Will never work as is on Windows, requires threads and so. + FD_SET(0,&readfds); FD_SET(ucast_sock,&readfds); FD_SET(ucast_sock,&exceptfds); timeout.tv_sec = 2; @@ -158,7 +158,7 @@ int accept_pending_clients_or_wait_a_bit() { if ( res > 0 ) { if (FD_ISSET(ucast_sock, &readfds)) { - //TODO : this assumes that the event is an accept() while ones could be send data there + /*TODO : this assumes that the event is an accept() while ones could be send data there */ if ( clients_next >= MAX_CLIENTS ) { fprintf(stderr, "%s\n", "Bouncing client, MAX_CLIENTS reached"); close(accept(ucast_sock, NULL, 0)); @@ -170,13 +170,13 @@ int accept_pending_clients_or_wait_a_bit() { clients_next++; } } - //TODO : drop this keybord read with accept(), this is not portable + /*TODO : drop this keybord read with accept(), this is not portable */ if ( FD_ISSET(0, &readfds)) { nread = read(0, readbuf, READ_BUF_LEN); if ( nread <= 0 ) { fprintf(stderr, "%s\n", "lost stdin"); } - // User wants to go now + /* User wants to go now */ return 2; } if (FD_ISSET(ucast_sock, &exceptfds)) { @@ -185,7 +185,7 @@ int accept_pending_clients_or_wait_a_bit() { } } if (res == 0 ) { - // nothing happened before timeout + /* Nothing happened before timeout */ return 1; } return 0; @@ -243,7 +243,7 @@ int start_job() { fprintf(stderr, "unexpected data from %i\n", i); clients[i].state = 2; } else { - // Received "ready" ack from client + /* Received "ready" ack from client */ clients[i].state = 1; } } @@ -256,7 +256,7 @@ int start_job() { all_non_dead_ready &= (clients[i].state == 1); } } - // (res == 0 ) nothing happened before timeout + /* (res == 0 ) nothing happened before timeout */ if ( all_ready ) return 1; @@ -270,33 +270,34 @@ void send_fake(char buf[], int paylen, int i) { *( (uint32_t *) buf+1 ) = htonl(i); snprintf(buf+29, 5, "%04i", i); *( (char *) buf+33 ) = ')'; - sendto(mcast_sock, buf, 8+paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); + sendto(mcast_sock, buf, paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); } int send_data() { ssize_t nwrite; char buf[] = "dataXXXXJe suis à la plage (XXXX).\n"; - int paylen = strlen(buf)-8; + int paylen = strlen(buf); int i; - //XXX Dummy + /* XXX Dummy */ send_fake(buf, paylen, 5); send_fake(buf, paylen, 4); -/* - for (i=6; i<=300; i+=2) { + send_fake(buf, paylen, 3); + + for (i=6; i<=300; i+=2) { send_fake(buf, paylen, i); } - for (i=7; i<=300; i+=2) { + for (i=7; i<=300; i+=2) { send_fake(buf, paylen, i); } -*/ + send_fake(buf, paylen, 1); send_fake(buf, paylen, 1); send_fake(buf, paylen, 2); *( (uint32_t *) buf+1 ) = htonl(3); - buf[22]='m', buf[23]='e', buf[24]='r'; buf[25]='.'; buf[26]='\n'; paylen = 19; - nwrite = sendto(mcast_sock, buf, 8+paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); + buf[22]='m', buf[23]='e', buf[24]='r'; buf[25]='.'; buf[26]='\n'; paylen = 27; + nwrite = sendto(mcast_sock, buf, paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); if ( nwrite < 0 ) { perror("sendto() failed"); return -1; @@ -316,10 +317,11 @@ int wait_all_finalize_job() { int all_non_dead_done; int i, res; SOCKET client_sock; - const char *payload = "final"; - int paylen = strlen(payload); + char buf[] = "end:XXXX"; + int paylen = strlen(buf); - nwrite = sendto(mcast_sock, payload, paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); + *( (uint32_t *) buf+1 ) = htonl(300); + nwrite = sendto(mcast_sock, buf, paylen, 0, mcast_addr->ai_addr, mcast_addr->ai_addrlen); if ( nwrite < 0 ) { perror("sendto() failed"); return -1; @@ -360,7 +362,7 @@ int wait_all_finalize_job() { fprintf(stderr, "unexpected data from %i\n", i); clients[i].state = 2; } else { - // Received "done." ack from client + /* Received "done." ack from client */ clients[i].state = 3; } } @@ -372,7 +374,7 @@ int wait_all_finalize_job() { all_non_dead_done &= (clients[i].state == 3); } } - // (res == 0 ) nothing happened before timeout + /* (res == 0 ) nothing happened before timeout */ if ( all_non_dead_done ) return 1; -- cgit v1.2.3