#include #include #include #include #include #include #include #include #include "task.h" // receives results from the workers and displays them on the console // usage: // a.out portno emitterhost emitterport // number of the port for the result socket // then address of emitter (to send feedbacks on task completion) #define MAXHOSTNAME 80 int main(int argc, char * argv[]) { int s, ei, si, retcode, i; RESULT result; unsigned int salen; struct sockaddr_in sa,sai,eai; char hostname[MAXHOSTNAME]; char * emitterhost = argv[2]; int emitter_port = atoi(argv[3]); si = socket(AF_INET,SOCK_STREAM,0); // socket to receive the results if(si == -1) { perror("while opening socket"); return -1;} sai.sin_family = AF_INET; sai.sin_port = htons(atoi(argv[1])); gethostname(hostname,MAXHOSTNAME); memcpy(&sai.sin_addr, (gethostbyname(hostname)->h_addr), sizeof(sai.sin_addr)); retcode = bind(si,(struct sockaddr *) & sai, sizeof(sai)); if(retcode == -1) { perror("while binding socket to addr"); return -1; } retcode = listen(si,1); if(retcode == -1) { perror("while calling listen"); return -1; } while(1==1) { SERVICE feedback; salen = sizeof(sa); printf("Accepting connections\n"); s = accept(si,(struct sockaddr *)&sa,&salen); // accept a connection if(s == 1) { perror("while accepting a connection"); return -1; } // read a res from one of the Worker retcode = read(s,&result,sizeof(RESULT)); if(retcode != sizeof(RESULT)) { // AGAIN, should be corrected perror("while reading a result"); return -1; } if(result.tag < 0) { printf("EOS -> terminating\n"); break; } printf("Read result: <%d,%d> ",result.v,result.tag); fflush(stdout); // and print it on console close(s); // now send a feedback to emitter process with the tag of the // received task: in case it was selected for re-scheduling // it will be removed, otherwise it will be rescheduled after // completing "normal" tasks ei = socket(AF_INET,SOCK_STREAM,0); // socket to receive the results if(ei == -1) { perror("while opening socket"); return -1;} eai.sin_family = AF_INET; eai.sin_port = htons(emitter_port); memcpy(&eai.sin_addr, (gethostbyname(emitterhost)->h_addr), sizeof(eai.sin_addr)); retcode = connect(ei,(struct sockaddr *)&eai,sizeof(eai)); if(retcode == -1) { perror("while connecting to emitter"); return(-1);} // send feedback on computed task feedback.tag = TAG_COMPLETED; feedback.v = result.tag; retcode = write(ei,&feedback,sizeof(SERVICE)); if(retcode != sizeof(SERVICE)) { perror("while writing feedback to emitter"); return (-1); } close(ei); // one shot: TODO use permanent connection on dedicated ss } close(si); return 0; }