Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NetClientTest.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011  ---------------------------------------------------------------------------*/
00012 #include <NetClientTest.h>
00013 
00014 /* ----------------------------------------------------------------------------
00015  * NetClientTest.cpp -- display client demo
00016  * ----------------------------------------------------------------------------
00017  * Coded by John R. Watson (jwatson@cs.uml.edu)
00018  * 91.549 Robotics II, Final Project
00019  * DUE 12/18/03
00020  * ----------------------------------------------------------------------------
00021  *
00022  * Modified by Philip D.S. Thoren (pthoren@cs.uml.edu) 1/18/2004
00023  *  
00024  */
00025 
00026 #include <phission.h>
00027 
00028 /* ------------------------------------------------------------------------ */
00029 int glbl_disable_displays = 0;
00030 int glbl_save_images = 0;
00031 
00032 #if 0
00033 #undef DEBUG_PRINT
00034 #define DEBUG_PRINT phPROGRESS
00035 #endif
00036 
00037 #define PRINT_IMAGE_SIZE() 0
00038 
00039 /* ----------------------------------------------------------------------------
00040  * ------------------------------------------------------------------------- */
00041 int recv_init( phSocket *comms, 
00042                char     **title, 
00043                uint32_t *w, 
00044                uint32_t *h, 
00045                uint32_t *f )
00046 {
00047     phFUNCTION("recv_init")
00048 
00049     uint32_t width      = 0;
00050     uint32_t height     = 0;
00051     uint32_t format     = phImageNOFORMAT;
00052     uint32_t titleSize  = 0;
00053     uint32_t next       = 0xaaaa;
00054     
00055     rc = comms->recv_uint32(&titleSize);
00056     phCHECK_RC(rc,NULL,"Failed to receive title size\n\n");
00057     DEBUG_PRINT("titleSize:%d\n",titleSize);
00058 
00059     phFree(*title);
00060     
00061     *title = (char *)phMalloc(titleSize+5);
00062     phCHECK_NULLPTR(*title,"phMalloc","phMalloc failed to allocate the title space.");
00063     memset(*title,0,titleSize+5);
00064     
00065     rc = comms->recv(*title, titleSize);
00066     DEBUG_PRINT("title:%s\n",*title);
00067     phCHECK_RC(rc,NULL,"Failed to receive title size\n\n");
00068 
00069     /* receive image dimensions from server */
00070     rc = comms->recv_uint32(&width);
00071     phCHECK_RC(rc,NULL,"Failed to receive image dimensions(width)\n\n");
00072     DEBUG_PRINT("width:%d\n",width);            
00073 
00074     rc = comms->recv_uint32(&height);
00075     phCHECK_RC(rc,NULL,"Failed to receive image dimensions(height)\n\n");
00076     DEBUG_PRINT("height:%d\n",height);
00077 
00078     rc = comms->recv_uint32(&format);
00079     phCHECK_RC(rc,NULL,"Failed to receive image format \n\n");
00080     DEBUG_PRINT("format:0x%08x\n",format);
00081 
00082     *w = width;
00083     *h = height;
00084     *f = format;
00085 
00086     return phSUCCESS;
00087 error:
00088     
00089     return phFAIL;
00090 }
00091 
00092 /* ----------------------------------------------------------------------------
00093  * ------------------------------------------------------------------------- */
00094 int recv_data( phSocket *comms,
00095                phImage *inputImage,
00096                uint32_t *w, 
00097                uint32_t *h, 
00098                uint32_t *f,
00099                uint8_t **data,
00100                uint32_t *data_size
00101                )
00102 {
00103     phFUNCTION("recv_data")
00104 
00105     uint32_t width  = 0;
00106     uint32_t height = 0;
00107     uint32_t format = (uint32_t)phImageNOFORMAT;
00108     uint32_t size   = 0;
00109     
00110     int32_t  packet_id      = 0xaaaa;
00111     uint32_t host_send_time = 0;
00112 
00113     phTimeStamp time_start;
00114     phTimeStamp time_stop;
00115     
00116     time_start.stamp();
00117 
00118     /* receive image dimensions from server */
00119     rc = comms->recv_uint32(&width);
00120     phCHECK_RC(rc,NULL,"Failed to receive image dimensions(width)\n\n");
00121     DEBUG_PRINT("width:%d\n",width);            
00122 
00123     rc = comms->recv_uint32(&height);
00124     phCHECK_RC(rc,NULL,"Failed to receive image dimensions(height)\n\n");
00125     DEBUG_PRINT("height:%d\n",height);
00126 
00127     rc = comms->recv_uint32(&format);
00128     phCHECK_RC(rc,NULL,"Failed to receive image format \n\n");
00129     DEBUG_PRINT("format:0x%08x %s\n",format,phImageFormatToString(format));
00130 
00131     rc = comms->recv_uint32(&host_send_time);
00132     phCHECK_RC(rc,NULL,"Failed to receive image host_send_time \n\n");
00133     DEBUG_PRINT("host_send_time:%d:0x%08x\n",host_send_time,host_send_time);
00134     /* 0.)  packet_id */
00135     rc = comms->send_int32(packet_id);
00136     phSocket_CHECK_RC(rc,NULL,"Failed to send packet_id image message \n\n");
00137     /* 1.) host_send_time: time the server started sending the data */
00138     rc = comms->send_int32(host_send_time);
00139     phSocket_CHECK_RC(rc,NULL,"Failed to send host_send_time image message \n\n");
00140 
00141     rc = comms->recv_uint32(&size);
00142     phCHECK_RC(rc,NULL,"Failed to receive image size \n\n");
00143     DEBUG_PRINT("size:%d\n",size);
00144     
00145     /* receive a image from the server */
00146     DEBUG_PRINT("receiving image data\n");
00147 
00148     if (size == 0) /* Try to calculate it */
00149     {
00150         size = phIMAGE_SIZE(width,height,format);
00151     }
00152     
00153     /* allocate data if it hasn't been initialized yet */
00154     if (((*data_size) == 0) || (*data == NULL))
00155     {
00156         (*data) = (uint8_t *)phCalloc(sizeof(uint8_t),size);
00157         phCHECK_NULLPTR(*data,"phCalloc","phCalloc failed to allocate data.");
00158     }
00159     /* reallocate the data if the size changes */
00160     else if (((*data_size) != size) && (*data != NULL))
00161     {
00162         (*data) = (uint8_t *)phRealloc((*data),size);
00163         phCHECK_NULLPTR(*data,"phRealloc","phRealloc failed to allocate data.");
00164     }
00165        
00166     /* Always set this */
00167     *data_size = size;
00168     
00169     /* Get the data from the server */
00170     rc = comms->recv(*data, *data_size);
00171     phCHECK_RC(rc,NULL,"recv for image data failed.");
00172 
00173     time_stop.stamp();
00174        
00175     /* Send stats back to the server */
00176     /* 2.) */
00177     rc = comms->send_uint32(*data_size);
00178     phSocket_CHECK_RC(rc,NULL,"Failed to send message \n\n");
00179     /* 3.) */
00180     rc = comms->send_int32(time_start.getMicroseconds());
00181     phSocket_CHECK_RC(rc,NULL,"Failed to send message\n\n");
00182     /* 4.) */
00183     rc = comms->send_int32(time_stop.getMicroseconds());
00184     phSocket_CHECK_RC(rc,NULL,"Failed to send message\n\n");
00185 
00186 
00187 #if PRINT_IMAGE_SIZE()
00188     phPROGRESS("format:%s\n",
00189              phImageFormatToString(format));
00190     phPROGRESS("recv:%lu\tactual:%lu\n",
00191              (long unsigned)*data_size,
00192              (long unsigned)phIMAGE_SIZE(width,height,format));
00193 #endif
00194 
00195     /* allocate space for the image */
00196     rc = inputImage->setImage(width,height,format,
00197                               *data_size,
00198                               *data);
00199     phPRINT_RC(rc,NULL,"inputImage->setImage");
00200 
00201     /* keep track */
00202     *w = width;
00203     *h = height;
00204     *f = format;
00205     
00206     return phSUCCESS;
00207 error:
00208     /* might as well... */
00209     phFree(*data);
00210     
00211     *data_size = 0;
00212     
00213     return phFAIL;
00214 }
00215 
00216 /* ----------------------------------------------------------------------------
00217  * ------------------------------------------------------------------------- */
00218 int connect_to_server(  phSocket    *comms,
00219                         char        *host,
00220                         int         port,
00221                         uint32_t    *w,
00222                         uint32_t    *h,
00223                         uint32_t    *f,
00224                         char        **title )
00225 {
00226     phFUNCTION("connect_to_server")
00227     
00228     char *serverTitle = NULL;
00229     
00230     phPROGRESS("Attempting to connect to server - %s:%d\n",
00231              host,port );
00232     
00233     /* connect to the server */
00234     rc = comms->connect(host, port, true);
00235     phCHECK_RC(rc,NULL,"Failed to connect to %s\n\n", host);
00236     
00237     phPROGRESS("Connected to server - %s:%d\n",
00238              host,port );
00239     
00240     rc = recv_init(comms,&serverTitle,w,h,f);
00241     phCHECK_RC(rc,NULL,"recv_init");
00242     
00243     phFree(*title);
00244     
00245     (*title) = (char *)phCalloc(  strlen(serverTitle) + 
00246                                 strlen(host) +
00247                                 255,
00248                                 sizeof(char));
00249     phCHECK_NULLPTR(title,"phCalloc","phCalloc failed to allocate title.");
00250 
00251     sprintf((*title),"%s:%d - %s",
00252             host,
00253             port,
00254             serverTitle );
00255     
00256     phFree(serverTitle);
00257 
00258     return phSUCCESS;
00259 error:
00260     phFree(serverTitle);
00261     return phFAIL;
00262 }
00263 
00264 /* ----------------------------------------------------------------------------
00265  * ------------------------------------------------------------------------- */
00266 int main(int argc, char **argv)
00267 {
00268     phFUNCTION("main")
00269     
00270     uint32_t            nDisplays   = 1;
00271     phDisplayInterface  **displays  = NULL;
00272     phImage             *outputImage= NULL;
00273     phImage             *inputImage = NULL;
00274     
00275     phSocket            *comms      = NULL;
00276     int                 portNum     = 0;
00277     uint32_t            w           = 0;
00278     uint32_t            h           = 0;
00279     uint32_t            f           = (uint32_t)phImageNOFORMAT;
00280     char                *title      = NULL;
00281     
00282     uint32_t            imgsize     = 0;
00283     uint8_t             *imgbuf     = NULL;
00284     char                filename[255];
00285     uint32_t            i           = 0;
00286 
00287     int                 displaysOpen= 1;
00288     
00289     phSystem            system;
00290    
00291     /* Remove the code below when using this code as an example.
00292      * 
00293      * This just checks whether "--test" has been specified with
00294      * a time value argument. It's for testing all the examples
00295      * without the need for human intervention. */
00296     int             test            = 0;
00297     int             test_loop_count = 0;
00298     int             count           = 0;
00299 
00300     /* transport verification */
00301     int verify = 0;
00302     
00303     if (argc >= 2)
00304     {
00305         for (int k = 1; k < argc; k++ )
00306         {
00307             if (strcmp(argv[k],"--test") == 0)
00308             {
00309                 k++;
00310                 if (k < argc)
00311                 {
00312                     count = test = atoi(argv[k]);
00313                 }
00314                 k++;
00315                 if (k < argc)
00316                 {
00317                     count = atoi(argv[k]);
00318                 }
00319             }
00320             else if (strcmp(argv[k],"--verify") == 0)
00321             {
00322                 verify = 1;
00323             }
00324             else if (strcmp(argv[k],"--nodisplay") == 0)
00325             {
00326                 glbl_disable_displays = 1;
00327             }
00328             else if (strcmp(argv[k],"--help") == 0)
00329             {
00330                 printf("\n\n%s\n\tUsage:\n",argv[0]);
00331                 printf("\t\t\t--help\t\t\tdisplay usage\n");
00332                 printf("\t\t\t--nodisplay\tdisable the allocation, opening or any use of a display.\n");
00333                 printf("\t\t\t--test <value> <count>\tsleep 'value' seconds and then close test\n");
00334                 printf("\t\t\t\t\tloops 'count' times during net capture before sleeping\n");
00335                 printf("\n\n");
00336                 goto error; /* close program, free memory */
00337             }
00338         }
00339     }
00340     
00341     
00342     if (argc < 3)
00343     {
00344         printf("\nUSAGE:  %s <host> <port>\n\n", argv[0]);
00345         exit(0);
00346     }
00347 
00348     displays    = new phDisplayInterface *[nDisplays];
00349     outputImage = new phImage();
00350     inputImage  = new phImage();
00351     comms       = new phSocket();
00352 
00353     if (glbl_disable_displays == 0)
00354     {
00355         for (i = 0; i < nDisplays; i++ )
00356         {
00357             displays[i]   = new X11Display();
00358         
00359             rc = system.addDisplay(displays[i]);
00360             phPRINT_RC(rc,NULL,"system.addDisplay(displays[%d])",i);
00361         
00362             displays[i]->set(w, h, title);
00363             displays[i]->setLiveSourceInput(outputImage);
00364         }
00365     }
00366     
00367     portNum = atoi(argv[2]);
00368     
00369     rc = system.startup();
00370     phPRINT_RC(rc,NULL,"system.startup()");
00371     
00372     while (displaysOpen)
00373     {
00374         if (comms->isConnected())
00375         {
00376             rc = recv_data(comms,inputImage,
00377                            &w,&h,&f,
00378                            &imgbuf,&imgsize);
00379             //DEBUG_PRINT("(%u,%u,%x,%s)\n",w,h,f,title);
00380             /* If we just got disconnected, rc > 0.
00381              * If an error occured rc < 0.
00382              * If things are fine and dandy, rc == 0 */
00383             /* Things can go on if there is a problem here.
00384              * Use this to do a one-shot NetClientCapture instead
00385              * of continually trying to connect to a server */
00386             /*
00387             if (rc < 0) 
00388             {
00389                 displaysOpen = 0;
00390                 continue;
00391             }
00392             */  
00393             
00394             if ((verify == 1) && (rc == 0))
00395             {
00396                 uint8_t *data = NULL;
00397                 uint32_t size = 0;
00398                 uint8_t x = 0;
00399                 uint8_t y = 0;
00400                 int direction = 0;
00401                 
00402                 inputImage->convert(phImageRGB24);
00403                 
00404                 data = (uint8_t *)inputImage->getImage();
00405                 size = inputImage->getSize();
00406 
00407                 for ( i = 0; i < size; i ++ )
00408                 {
00409                     y = data[i];
00410                     if (x != y)
00411                     {
00412                         phPROGRESS("err: data[%u]:%u != %u\n",i,y,x);
00413                     }
00414                     else
00415                     {
00416                         //phPROGRESS("     data[%u]:%u == %u\n",i,x,y);
00417                     }
00418                      
00419                     if ((x == 255) && (direction == 0))
00420                     {
00421                         direction = -1;
00422                     }
00423                     else if ((x == 0) && (direction == -1))
00424                     {
00425                         direction = 0;
00426                     }
00427                     
00428                     if (direction == -1)
00429                     {
00430                         x--;
00431                     }
00432                     else
00433                     {
00434                         x++;
00435                     }
00436                     
00437                     if (((i + 1) % w) == 0) x = 0;
00438                 }
00439             }
00440             if (rc == 0)
00441             {
00442                 /* display the new image */
00443                 DEBUG_PRINT("updating outputImage\n");
00444                 outputImage->setImage(*inputImage);
00445                 
00446                 if (glbl_save_images)
00447                 {
00448                     sprintf(filename,"test_%lu.ppm",(long unsigned int)i);
00449                     outputImage->save(filename);
00450                     i++;
00451                 }
00452             }
00453         }
00454         else
00455         {
00456             rc = connect_to_server( comms,
00457                                     argv[1],
00458                                     portNum,
00459                                     &w, &h, &f,
00460                                     &title );
00461             phCHECK_RC(rc,NULL,"re-connect_to_server failed.");
00462 
00463             for (i = 0; i < nDisplays; i++ )
00464             {
00465                 DEBUG_PRINT("(%u,%u,%x,%s)\n",w,h,f,title);
00466                 displays[i]->set(w, h, title);
00467             }
00468             //phMSleep(1);
00469         }
00470 
00471         if (glbl_disable_displays == 0)
00472         {
00473             displaysOpen = 0;
00474             for (i = 0; i < nDisplays; i++)
00475             {
00476                 if (displays[i]->isOpen())
00477                 {
00478                     displaysOpen = 1;
00479                 }
00480             }
00481         }
00482 
00483         /* Remove this if block when using this code as an example */
00484         /* Set the loop control value to end the loop when testing */
00485         if (test > 0)
00486         { 
00487             if (test_loop_count > count)
00488             {
00489                 displaysOpen = 0;
00490                 phSleep(test); /* test's value should be a time (in secs) value > 0*/
00491             }
00492             test_loop_count++;
00493         }
00494     }
00495 
00496 error:
00497     /* We need to shutdown the system here otherwise the destructor will try to 
00498      * shut things down after we've already deleted them */
00499     rc = system.shutdown();
00500     phPRINT_RC(rc,NULL,"system.shutdown()");
00501 
00502     for (i = 0; i < nDisplays; i++ )
00503     {
00504         phDelete(displays[i]);
00505     }
00506     phDeleteArray(displays);
00507     phDelete(comms);
00508     phDelete(outputImage);
00509     phDelete(inputImage);
00510 
00511     phFree(imgbuf);
00512     
00513     phFree(title);
00514 }




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:06 2007 for phission by  doxygen 1.4.4