Client/server relationship now seems to be working well.
This commit is contained in:
parent
5def32901a
commit
5a7e54125b
3 changed files with 239 additions and 66 deletions
|
@ -2,7 +2,9 @@ bin_PROGRAMS = server client
|
|||
|
||||
server_SOURCES = server.cxx
|
||||
|
||||
server_LDADD =
|
||||
server_LDADD = \
|
||||
$(top_builddir)/Lib/Bucket/libBucket.a \
|
||||
$(top_builddir)/Lib/Misc/libMisc.a
|
||||
|
||||
client_SOURCES = client.cxx
|
||||
|
||||
|
|
|
@ -13,9 +13,8 @@
|
|||
#include <stdlib.h> // atoi()
|
||||
#include <string.h> // bcopy()
|
||||
|
||||
#include "remote_exec.h"
|
||||
|
||||
char *determine_port(char *host);
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
|
||||
int make_socket (char *host, unsigned short int port) {
|
||||
|
@ -53,46 +52,71 @@ int make_socket (char *host, unsigned short int port) {
|
|||
}
|
||||
|
||||
|
||||
main(int argc, char *argv[]) {
|
||||
// connect to the server and get the next task
|
||||
long int get_next_task( const string& host, int port ) {
|
||||
long int tile;
|
||||
int sock, len;
|
||||
fd_set ready;
|
||||
int port;
|
||||
char message[256];
|
||||
|
||||
/* Check usage */
|
||||
sock = make_socket( host.c_str(), port );
|
||||
|
||||
// build a command string from the argv[]'s
|
||||
strcpy(message, "hello world!\n");
|
||||
|
||||
// send command and arguments to remote server
|
||||
// if ( write(sock, message, sizeof(message)) < 0 ) {
|
||||
// perror("Cannot write to stream socket");
|
||||
// }
|
||||
|
||||
// loop until remote program finishes
|
||||
cout << "looping ..." << endl;
|
||||
|
||||
FD_ZERO(&ready);
|
||||
FD_SET(sock, &ready);
|
||||
|
||||
// block until input from sock
|
||||
select(32, &ready, 0, 0, NULL);
|
||||
cout << "unblocking ... " << 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;
|
||||
close(sock);
|
||||
return tile;
|
||||
} else {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// build the specified tile
|
||||
void run_task( long int tile ) {
|
||||
}
|
||||
|
||||
|
||||
main(int argc, char *argv[]) {
|
||||
long int tile;
|
||||
|
||||
// Check usage
|
||||
if ( argc < 3 ) {
|
||||
printf("Usage: %s remote_machine port\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
port = atoi(argv[2]);
|
||||
sock = make_socket(argv[1], port);
|
||||
string host = argv[1];
|
||||
int port = atoi( argv[2] );
|
||||
|
||||
/* build a command string from the argv[]'s */
|
||||
strcpy(message, "hello world!\n");
|
||||
|
||||
/* send command and arguments to remote server */
|
||||
if ( write(sock, message, sizeof(message)) < 0 ) {
|
||||
perror("Cannot write to stream socket");
|
||||
while ( true ) {
|
||||
tile = get_next_task( host, port );
|
||||
run_task( tile );
|
||||
}
|
||||
|
||||
for ( ;; ) {
|
||||
/* loop until remote program finishes */
|
||||
|
||||
FD_ZERO(&ready);
|
||||
FD_SET(sock, &ready);
|
||||
|
||||
/* block until input from sock or stdin */
|
||||
select(32, &ready, 0, 0, NULL);
|
||||
|
||||
if ( FD_ISSET(sock, &ready) ) {
|
||||
/* input coming from socket */
|
||||
if ( (len = read(sock, message, 1024)) > 0 ) {
|
||||
write(1, message, len);
|
||||
} else {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
close(sock);
|
||||
}
|
||||
|
|
|
@ -4,21 +4,28 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h> // for stat()
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/socket.h> // bind
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// #include <netdb.h>
|
||||
// #include <fcntl.h>
|
||||
// #include <stdio.h>
|
||||
#include <Bucket/newbucket.hxx>
|
||||
|
||||
|
||||
#define MAXBUF 1024
|
||||
|
||||
static double lat = 0.0;
|
||||
static double lon = 0.0;
|
||||
static double dy = 0.0;
|
||||
static int pass = 0;
|
||||
|
||||
int make_socket (unsigned short int* port) {
|
||||
int sock;
|
||||
struct sockaddr_in name;
|
||||
|
@ -52,61 +59,201 @@ int make_socket (unsigned short int* port) {
|
|||
}
|
||||
|
||||
|
||||
main() {
|
||||
#if 0 // better move this to client
|
||||
// return true if file exists
|
||||
static bool file_exists( const string& file ) {
|
||||
struct stat buf;
|
||||
|
||||
if ( stat( file.c_str(), &buf ) == 0 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// check if the specified tile has data defined for it
|
||||
static bool has_data( const string& path, const FGBucket& b ) {
|
||||
string dem_file = path + ".dem" + "/Scenery/" + b.gen_base_path()
|
||||
+ "/" + b.gen_index_str() + ".dem";
|
||||
if ( file_exists( dem_file ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
dem_file += ".gz";
|
||||
if ( file_exists( dem_file ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
// initialize the tile counting system
|
||||
void init_tile_count() {
|
||||
// pre-pass
|
||||
pass = 0;
|
||||
|
||||
// initial bogus value
|
||||
lat = 100;
|
||||
|
||||
// determine tile height
|
||||
FGBucket tmp1( 0.0, 0.0 );
|
||||
dy = tmp1.get_height();
|
||||
}
|
||||
|
||||
|
||||
// return the next tile
|
||||
long int get_next_tile( const string& work_base, const string& output_base )
|
||||
{
|
||||
FGBucket b;
|
||||
static double shift_over = 0.0;
|
||||
static double shift_up = 0.0;
|
||||
|
||||
// cout << "lon = " << lon << " lat = " << lat << endl;
|
||||
|
||||
if ( lat > 90.0 ) {
|
||||
++pass;
|
||||
if ( pass == 1 ) {
|
||||
shift_over = 0.0;
|
||||
shift_up = 0.0;
|
||||
} else if ( pass == 2 ) {
|
||||
shift_over = 1.0;
|
||||
shift_up = 0.0;
|
||||
} else if ( pass == 3 ) {
|
||||
shift_over = 0.0;
|
||||
shift_up = 1.0;
|
||||
} else if ( pass == 4 ) {
|
||||
shift_over = 1.0;
|
||||
shift_up = 1.0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// reset lat
|
||||
lat = -89.0 + (shift_up*dy) - (dy*0.5);
|
||||
|
||||
// reset lon
|
||||
FGBucket tmp( 0.0, lat );
|
||||
double dx = tmp.get_width();
|
||||
lon = -180 + (shift_over*dx) + (dx*0.5);
|
||||
|
||||
cout << "starting pass = " << pass
|
||||
<< " with lat = " << lat << " lon = " << lon << endl;
|
||||
}
|
||||
|
||||
// if ( ! start_lon ) {
|
||||
// lon = -180 + dx * 0.5;
|
||||
// } else {
|
||||
// start_lon = false;
|
||||
// }
|
||||
|
||||
if ( lon > 180.0 ) {
|
||||
// increment to next row
|
||||
// skip every other row (to avoid two clients working on
|
||||
// adjacent tiles)
|
||||
lat += 2.0 * dy;
|
||||
|
||||
FGBucket tmp( 0.0, lat );
|
||||
double dx = tmp.get_width();
|
||||
lon = -180 + (shift_over*dx) + (dx*0.5);
|
||||
}
|
||||
|
||||
b = FGBucket( lon, lat );
|
||||
cout << "Bucket = " << b << endl;
|
||||
|
||||
// increment to next tile
|
||||
FGBucket tmp( 0.0, lat );
|
||||
double dx = tmp.get_width();
|
||||
|
||||
// skip every other column (to avoid two clients working on
|
||||
// adjacent tiles)
|
||||
lon += 2.0 * dx;
|
||||
|
||||
return b.gen_index();
|
||||
}
|
||||
|
||||
|
||||
// display usage and exit
|
||||
void usage( const string name ) {
|
||||
cout << "Usage: " << name << " <work_base> <output_base>" << endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char **argv ) {
|
||||
long int next_tile;
|
||||
int sock, msgsock, length, pid;
|
||||
fd_set ready;
|
||||
short unsigned int port;
|
||||
char buf[MAXBUF];
|
||||
|
||||
// quick argument sanity check
|
||||
if ( argc < 3 ) {
|
||||
usage( argv[0] );
|
||||
}
|
||||
|
||||
string work_base = argv[1];
|
||||
string output_base = argv[2];
|
||||
|
||||
// initialize tile counter / incrementer
|
||||
init_tile_count();
|
||||
|
||||
// setup socket to listen on
|
||||
sock = make_socket( &port );
|
||||
|
||||
// Save the port number
|
||||
// set_port( port );
|
||||
cout << "socket is connected to port = " << port << endl;
|
||||
|
||||
/* Specify the maximum length of the connection queue */
|
||||
// Specify the maximum length of the connection queue
|
||||
listen(sock, 3);
|
||||
|
||||
for ( ;; ) {
|
||||
FD_ZERO(&ready);
|
||||
FD_SET(sock, &ready);
|
||||
|
||||
/* block until we get some input on sock */
|
||||
// block until we get some input on sock
|
||||
select(32, &ready, 0, 0, NULL);
|
||||
|
||||
if ( FD_ISSET(sock, &ready) ) {
|
||||
/* printf("%d %d Incomming message --> ", getpid(), pid); */
|
||||
// printf("%d %d Incomming message --> ", getpid(), pid);
|
||||
|
||||
// get the next tile to work on
|
||||
next_tile = get_next_tile(work_base, output_base);
|
||||
cout << "next tile = " << next_tile << endl;;
|
||||
|
||||
msgsock = accept(sock, 0, 0);
|
||||
// cout << "msgsock = " << msgsock << endl;
|
||||
|
||||
/* spawn a child */
|
||||
// spawn a child
|
||||
pid = fork();
|
||||
|
||||
if ( pid < 0 ) {
|
||||
/* error */
|
||||
// error
|
||||
perror("Cannot fork child process");
|
||||
exit(-1);
|
||||
} else if ( pid > 0 ) {
|
||||
/* This is the parent */
|
||||
// This is the parent
|
||||
close(msgsock);
|
||||
} else {
|
||||
/* This is the child */
|
||||
|
||||
cout << "new process started to handle new connection" << endl;
|
||||
// Read client's message
|
||||
while ( (length = read(msgsock, buf, MAXBUF)) > 0) {
|
||||
cout << "buffer length = " << length << endl;
|
||||
buf[length] = '\0';
|
||||
cout << "Incoming command -> " << buf;
|
||||
|
||||
// reply to the client
|
||||
if ( write(sock, message, sizeof(message)) < 0 ) {
|
||||
perror("Cannot write to stream socket");
|
||||
}
|
||||
|
||||
|
||||
// clean up all of our zombie children
|
||||
int status;
|
||||
while ( (pid = waitpid( WAIT_ANY, &status, WNOHANG )) > 0 ) {
|
||||
// cout << "waitpid() returned " << pid << endl;
|
||||
}
|
||||
cout << "process ended" << endl;
|
||||
} else {
|
||||
// This is the child
|
||||
|
||||
// cout << "new process started to handle new connection for "
|
||||
// << next_tile << endl;
|
||||
|
||||
// reply to the client
|
||||
char message[MAXBUF];
|
||||
sprintf(message, "%ld", next_tile);
|
||||
length = strlen(message);
|
||||
if ( write(msgsock, message, length) < 0 ) {
|
||||
perror("Cannot write to stream socket");
|
||||
}
|
||||
close(msgsock);
|
||||
// cout << "process for " << next_tile << " ended" << endl;
|
||||
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue