// connect to the server and get the next task
-long int get_next_task( const string& host, int port ) {
+long int get_next_task( const string& host, int port, long int last_tile ) {
long int tile;
int sock, len;
fd_set ready;
}
// build a command string from the argv[]'s
- strcpy(message, "hello world!\n");
+ sprintf(message, "%ld", last_tile);
// send command and arguments to remote server
- // if ( write(sock, message, sizeof(message)) < 0 ) {
- // perror("Cannot write to stream socket");
- // }
+ if ( write(sock, message, sizeof(message)) < 0 ) {
+ perror("Cannot write to stream socket");
+ }
// loop until remote program finishes
- cout << "looping ..." << endl;
+ cout << "querying server for next task ..." << endl;
FD_ZERO(&ready);
FD_SET(sock, &ready);
// block until input from sock
select(32, &ready, 0, 0, NULL);
- cout << "unblocking ... " << endl;
+ cout << " received reply" << endl;
if ( FD_ISSET(sock, &ready) ) {
/* input coming from socket */
if ( (len = read(sock, message, 1024)) > 0 ) {
message[len] = '\0';
tile = atoi(message);
- cout << "tile to construct = " << tile << endl;
+ cout << " tile to construct = " << tile << endl;
close(sock);
return tile;
} else {
}
-// build the specified tile
-void run_task( long int tile ) {
+// build the specified tile, return true if contruction completed
+// successfully
+bool construct_tile( long int tile ) {
+ return true;
}
main(int argc, char *argv[]) {
- long int tile;
+ long int tile, last_tile;
+ bool result;
// Check usage
if ( argc < 3 ) {
string host = argv[1];
int port = atoi( argv[2] );
- while (
- (tile = get_next_task( host, port )) >= 0
- )
- {
- run_task( tile );
+ last_tile = 0;
+
+ while ( (tile = get_next_task( host, port, last_tile )) >= 0 ) {
+ result = construct_tile( tile );
+
+ if ( result ) {
+ last_tile = tile;
+ } else {
+ last_tile = -tile;
+ }
}
}
}
-#if 0 // better move this to client
// return true if file exists
static bool file_exists( const string& file ) {
struct stat buf;
return false;
}
-#endif
+
// initialize the tile counting system
void init_tile_count() {
}
// reset lat
- lat = -89.0 + (shift_up*dy) - (dy*0.5);
+ // lat = -89.0 + (shift_up*dy) - (dy*0.5);
+ lat = 0.0 + (shift_up*dy) + (dy*0.5);
// reset lon
FGBucket tmp( 0.0, lat );
// printf("%d %d Incomming message --> ", getpid(), pid);
// get the next tile to work on
- next_tile = get_next_tile(work_base, output_base);
+ next_tile = get_next_tile( work_base, output_base );
+ while ( ! has_data( work_base, FGBucket(next_tile) ) ) {
+ next_tile = get_next_tile( work_base, output_base );
+ }
+
// cout << "next tile = " << next_tile << endl;;
msgsock = accept(sock, 0, 0);
// clean up all of our zombie children
int status;
while ( (pid = waitpid( WAIT_ANY, &status, WNOHANG )) > 0 ) {
- // cout << "waitpid() returned " << pid << endl;
+ cout << "waitpid(): pid = " << pid
+ << " status = " << status << endl;
}
} else {
// This is the child
// cout << "new process started to handle new connection for "
// << next_tile << endl;
+ // Read client's command
+ char buf[MAXBUF];
+ if ( (length = read(msgsock, buf, MAXBUF)) < 0) {
+ perror("Cannot read command");
+ exit(-1);
+ }
+
+ buf[length] = '\0';
+ long int returned_tile = atoi(buf);
+ cout << "client replied with " << returned_tile << endl;
+
// reply to the client
char message[MAXBUF];
sprintf(message, "%ld", next_tile);
close(msgsock);
// cout << "process for " << next_tile << " ended" << endl;
- exit(0);
+ exit(returned_tile);
}
}
}