]> git.mxchange.org Git - flightgear.git/commitdiff
Client/server relationship now seems to be working well.
authorcurt <curt>
Sat, 15 May 1999 14:06:41 +0000 (14:06 +0000)
committercurt <curt>
Sat, 15 May 1999 14:06:41 +0000 (14:06 +0000)
Tools/Construct/Parallel/Makefile.am
Tools/Construct/Parallel/client.cxx
Tools/Construct/Parallel/server.cxx

index 5f617bec58d86dacde3c7e80f2bbeea549d203ab..352376bd2857d8a41e221db902e06d51ea7379c7 100644 (file)
@@ -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
 
index 2f262fd7ea1e0abec66cad2483ac1c8c4c338500..fb4bc517e0fd4d058b43ecf7c7d6c86209e94b51 100644 (file)
@@ -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 */
-    if ( argc < 3 ) {
-       printf("Usage: %s remote_machine port\n", argv[0]);
-       exit(1);
-    }
+    sock = make_socket( host.c_str(), port );
 
-    port = atoi(argv[2]);
-    sock = make_socket(argv[1], port);
-
-    /* build a command string from the argv[]'s */
+    // 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");
+    // 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;
+       }
     }
 
-    for ( ;; ) {
-       /* loop until remote program finishes */
+    close(sock);
+    return -1;
+}
+
 
-        FD_ZERO(&ready);
-        FD_SET(sock, &ready);
+// build the specified tile
+void run_task( long int tile ) {
+}
 
-       /* 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);
-           }
-       }
+main(int argc, char *argv[]) {
+    long int tile;
+
+    // Check usage
+    if ( argc < 3 ) {
+       printf("Usage: %s remote_machine port\n", argv[0]);
+       exit(1);
+    }
+
+    string host = argv[1];
+    int port = atoi( argv[2] );
+
+    while ( true ) {
+       tile = get_next_task( host, port );
+       run_task( tile );
     }
-    close(sock);
 }
index 251dd5440487e88eef82d01aedcc5a3f1cb76d51..3cc05cdde27a5dc73ad876c6ab34aee15b66cc9b 100644 (file)
@@ -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];
 
-    sock = make_socket( &port );
+    // quick argument sanity check
+    if ( argc < 3 ) {
+       usage( argv[0] );
+    }
 
-    // Save the port number
-    // set_port( port );
+    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 );
     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;
+               // clean up all of our zombie children
+               int status;
+               while ( (pid = waitpid( WAIT_ANY, &status, WNOHANG )) > 0 ) {
+                   // cout << "waitpid() returned " << pid << endl;
+               }
+           } else {
+               // This is the child
 
-                   // reply to the client
-                   if ( write(sock, message, sizeof(message)) < 0 ) {
-                       perror("Cannot write to stream socket");
-                   }
+               // 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");
                }
-               cout << "process ended" << endl;
+               close(msgsock);
+               // cout << "process for " << next_tile << " ended" << endl;
+
                exit(0);
            }
        }