urbiping.cc

00001 #include "urbi/uclient.hh"
00002 #include <sys/types.h>
00003 #include "libport/sys/stat.h"
00004 #include <signal.h>
00005 #include "libport/windows.hh"
00006 
00007 urbi::UClient* c;
00008 unsigned sendtime;
00009 unsigned mintime;
00010 unsigned maxtime;
00011 unsigned avgtime=0;
00012 unsigned pingCount=0;
00013 bool over=false;
00014 char * rname;
00015 bool received;
00016 unsigned count;
00017 
00018 urbi::UCallbackAction
00019 pong(const urbi::UMessage& msg)
00020 {
00021   unsigned int ptime = msg.client.getCurrentTime() - sendtime;
00022   if ((!pingCount) || mintime>ptime)
00023     mintime=ptime;
00024   if ((!pingCount) || maxtime<ptime)
00025     maxtime=ptime;
00026 
00027   avgtime+=ptime;
00028   printf("ping reply from %s: seq=%d time=%d ms\n", rname, pingCount+1, ptime);
00029   ++pingCount;
00030   received=true;
00031   if (pingCount == count)
00032     over = true;
00033   return urbi::URBI_CONTINUE;
00034 }
00035 
00036 void showstats(int)
00037 {
00038   if (!pingCount)
00039     urbi::exit(0);
00040   printf("rtt min/avg/max %d/%d/%d ms\n",
00041          (mintime),
00042          (int)(avgtime/(float)pingCount),
00043          (maxtime));
00044   exit(0);
00045 }
00046 
00047 
00048 int main(int argc, char * argv[])
00049 {
00050   signal(SIGINT, showstats);
00051   if (argc<2)
00052   {
00053     printf("usage: %s robot [msinterval] [count]\n",argv[0]); exit(1);
00054   }
00055 
00056   rname = argv[1];
00057 
00058   // Client initialization
00059   c = new urbi::UClient(argv[1]);
00060   c->start();
00061   if (c->error()) exit(1);
00062 
00063   int interval=1000;
00064 
00065   if (argc>2)
00066     interval=strtol(argv[2],NULL,0);
00067 
00068   // count initialization
00069   if (argc>3)
00070     count = strtol(argv[3],NULL,0);
00071   else
00072     count = 0;
00073 
00074   c->setCallback(&pong,"uping");
00075 
00076   received=true;
00077 
00078   for (unsigned i=0; i<count || !count; ++i)
00079     {
00080       while (!received)
00081         usleep(200);
00082       received=false;
00083       sendtime = c->getCurrentTime();
00084       c->send("uping:ping;");
00085       usleep(interval*1000);
00086     }
00087 
00088   while (!over)
00089     usleep(1000000);
00090   showstats(0);
00091 }

Generated on Tue Apr 10 17:45:45 2007 for URBISDK by  doxygen 1.5.1