Revision: 7447
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7447&view=rev
Author:   rtv
Date:     2009-03-10 09:40:49 +0000 (Tue, 10 Mar 2009)

Log Message:
-----------
working on examples and client

Modified Paths:
--------------
    code/websim/src/confederate.cc
    code/websim/src/parser.cc
    code/websim/src/websim.cc
    code/websim/src/websim.hh

Added Paths:
-----------
    code/websim/examples/
    code/websim/examples/Makefile
    code/websim/examples/simple.cc
    code/websim/examples/world.fed

Added: code/websim/examples/Makefile
===================================================================
--- code/websim/examples/Makefile                               (rev 0)
+++ code/websim/examples/Makefile       2009-03-10 09:40:49 UTC (rev 7447)
@@ -0,0 +1,3 @@
+
+simple: simple.cc
+       g++ -g -W -Wall -I/home/vaughan/gzfed/include `pkg-config --cflags 
glib-2.0` simple.cc -o $@ `pkg-config --libs glib-2.0` 
-L/home/vaughan/gzfed/lib -lwebsim -lyaml -lstdc++
\ No newline at end of file

Added: code/websim/examples/simple.cc
===================================================================
--- code/websim/examples/simple.cc                              (rev 0)
+++ code/websim/examples/simple.cc      2009-03-10 09:40:49 UTC (rev 7447)
@@ -0,0 +1,71 @@
+#include "websim/websim.hh"
+
+class MinWebSim : public websim::WebSim
+{
+public:
+  MinWebSim(const std::string& fedfile,
+                               const std::string& host, int port ) :
+        websim::WebSim( fedfile, host, port )
+  {
+  }
+  
+  virtual ~MinWebSim()
+  {}
+ 
+  // Interface to be implemented by simulators
+  virtual bool CreateModel(const std::string& name, 
+                                                                       const 
std::string& type,
+                                                                       
std::string& error)
+  { 
+        printf( "create model name:%s type:%s\n", name.c_str(), type.c_str() 
); 
+        return true;
+  }
+
+  virtual bool DeleteModel(const std::string& name,
+                                                                       
std::string& error)
+  {
+        printf( "deletee model name:%s \n", name.c_str() ); 
+        return true;
+  }
+
+  virtual bool SetModelPVA(const std::string& name, 
+                                                                       const 
websim::Pose& p,
+                                                                       const 
websim::Velocity& v,
+                                                                       const 
websim::Acceleration& a,
+                                                                       
std::string& error)
+  {
+        printf( "set model PVA name:%s\n", name.c_str() ); 
+        return true;
+  }
+
+  virtual bool GetModelPVA(const std::string& name, 
+                                                                       
websim::Pose& p,
+                                                                       
websim::Velocity& v,
+                                                                       
websim::Acceleration& a,
+                                                                       
std::string& error)
+  {
+        printf( "get model name:%s\n", name.c_str() ); 
+        return true;
+  }
+};
+
+
+int main( int argc, char** argv )
+{
+  MinWebSim mws( argv[1], argv[2], atoi(argv[3]) );
+
+  while( 1 )
+        {
+               websim::Pose p( 0,0,0,0,0,0 );
+               websim::Velocity v( 0,0,0,0,0,0 );
+               websim::Acceleration a( 0,0,0,0,0,0 );
+               
+               //sleep(1);
+
+               mws.SetPuppetPVA( "monkey", p, v, a );
+
+               mws.Update();
+
+               puts( "websim update returned" );
+        }
+}

Added: code/websim/examples/world.fed
===================================================================
--- code/websim/examples/world.fed                              (rev 0)
+++ code/websim/examples/world.fed      2009-03-10 09:40:49 UTC (rev 7447)
@@ -0,0 +1,30 @@
+# sourcehost:
+#    - modelname1:
+#        - desthost: prototype
+
+gort:
+  - monkey: 
+       - "localhost 8001": pioneer2dx
+
+#  - punky: 
+#       - localhost: pioneer2dx
+#  - chunky: 
+#       - localhost: pioneer2dx
+#  - funky: 
+#       - localhost: pioneer2dx
+
+
+#       - roy: pioneer
+#       - "priss 8888": pioneer
+#  - pioneer2dx_model2: 
+#       - deckard: pioneer
+#  - pioneer2dx_model3: 
+#       - deckard: pioneer
+
+#"tenner 9999":
+#  - pr2_1:
+#       - deckard: pr2
+
+#priss:
+#  - pr2_1:
+#       - "deckard 1234": pr2

Modified: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc      2009-03-10 09:40:23 UTC (rev 7446)
+++ code/websim/src/confederate.cc      2009-03-10 09:40:49 UTC (rev 7447)
@@ -32,7 +32,7 @@
 using namespace websim;
 
 WebSim::Confederate::Confederate( WebSim* ws, const char* host, unsigned short 
port ) :
-  unacknowledged_pushes( 0 ),
+  ws(ws),
   puppet_list( NULL )
 {
   if(! (http_con = evhttp_connection_new( host, port )) )
@@ -84,6 +84,9 @@
   //    name,
   //    prototype );
 
+  // the server needs to wait for ticks from this connected server
+  ++ws->tick_count_expected;
+
   struct evhttp_request* er = 
         evhttp_request_new( PuppetCreationCallback, puppet );
   assert(er);
@@ -116,9 +119,9 @@
   
   if( req->response_code == 200 )
         {              
-               --ch->conf->unacknowledged_pushes;
+               --ch->conf->ws->unacknowledged_pushes;
                printf( "puppet push for \"%s\" acked OK. Outstanding pushes 
%u\n",
-                                 ch->name, ch->conf->unacknowledged_pushes );
+                                 ch->name, ch->conf->ws->unacknowledged_pushes 
);
         }
   else
         {
@@ -155,7 +158,7 @@
   
   printf( "Emitting: http://%s%s\n";, this->name, buf );
   
-  ++unacknowledged_pushes; // decremented when reply is received
+  ++ws->unacknowledged_pushes; // decremented when reply is received
 
   int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, buf );
   if( ret != 0 )
@@ -164,17 +167,50 @@
                exit(0);
         } 
   
-  while( unacknowledged_pushes )
-        event_loop( EVLOOP_NONBLOCK ); // loops until the request has completed
-
   return 0;
 }
 
-int WebSim::Confederate::RunStep()
+
+void WebSim::Confederate::TickReplyCb( evhttp_request* req, void* arg )
 {
+  WebSim* ws = (WebSim*)arg;
+  
+  if( req->response_code == 200 )
+        {              
+               --ws->unacknowledged_pushes;
+               printf( "tick acked OK. Outstanding pushes %u\n",
+                                 ws->unacknowledged_pushes );
+        }
+  else
+        {
+               printf( "bad response regarding tick\n" );
+               printf( "code: %d\n", req->response_code );             
+               printf( "response: %s\n", req->input_buffer->buffer );
+        }
+}
+
+
+int WebSim::Confederate::Tick()
+{
   // construct and send a tick message
   printf( "Confederate %s clock tick\n",
                         name );
+  
+  ++ws->unacknowledged_pushes;
+  
+  struct evhttp_request* er = 
+        evhttp_request_new( PuppetPushCallback, ws );
+  assert(er);
+  
+  int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, 
"/sim/clock/tick" );
+  if( ret != 0 )
+        {
+               printf( "send tick returned error %d\n", ret );
+               exit(0);
+        } 
+
+
+
   return 0;
 }
 

Modified: code/websim/src/parser.cc
===================================================================
--- code/websim/src/parser.cc   2009-03-10 09:40:23 UTC (rev 7446)
+++ code/websim/src/parser.cc   2009-03-10 09:40:49 UTC (rev 7447)
@@ -74,6 +74,9 @@
   char lookup[256];
   snprintf( lookup, 256, "%s:%u", host, port );
 
+  assert( ws );
+  assert( ws->confederates );
+
   Confederate* conf = (Confederate*)
         g_hash_table_lookup( ws->confederates, lookup );
   
@@ -138,7 +141,8 @@
   string model = (char*)event.data.scalar.value;
   
   Puppet* pup = NULL;
-
+  
+  cout << "host " << host << " hostportname " << ws->hostportname << endl;
   if( host == ws->hostportname )
         pup = new Puppet( ws, model.c_str() );
 
@@ -180,7 +184,7 @@
                         {
                                string host = (const 
char*)event.data.scalar.value;
                                
-                               // cout << "HOST " << host << endl;
+                               cout << "HOST " << host << endl;
 
                                Confederate* conf = GetConfederate( 
host.c_str() );
                                parse_model_mapping_sequence( conf->name );

Modified: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc   2009-03-10 09:40:23 UTC (rev 7446)
+++ code/websim/src/websim.cc   2009-03-10 09:40:49 UTC (rev 7447)
@@ -43,7 +43,10 @@
                  tick_count_expected(0),
         fedfile(_fedfile), 
                  host(_host), 
-                 port(_port)
+                 port(_port),
+                 puppets( g_hash_table_new( g_str_hash, g_str_equal ) ),
+                 confederates( g_hash_table_new( g_str_hash, g_str_equal ) ),
+                 unacknowledged_pushes(0)
 {
   // Set up the HTTP server
   // Not sure whether it's safe to do this more that once in one process
@@ -66,7 +69,10 @@
   // if a federation file was specified, we parse it to populate the
   // WebSim with confederates and puppets.
   if( fedfile != "" )
-        Parser p( this, _fedfile.c_str() );  
+        {
+               printf( "[websim] Loading federation file %s\n", 
_fedfile.c_str() );
+               Parser p( this, _fedfile.c_str() );  
+        }
   
   puts("[websim] Ready");
 }
@@ -77,13 +83,21 @@
   // No event_fini() to call...
 }
 
+void WebSim::Confederate::TickCb( const char* name, Confederate* conf, void* 
arg )
+{
+  conf->Tick();
+}
+
 void
 WebSim::Update()
 {
+  // tick all my confederates
+  ForEachConfederate( Confederate::TickCb, NULL );
+
   tick_count = 0;
   do
   {
-    if(tick_count_expected)
+    if(tick_count_expected || unacknowledged_pushes )
       event_loop(EVLOOP_ONCE);
     else
       event_loop(EVLOOP_NONBLOCK);
@@ -399,3 +413,46 @@
 
   return true;
 }
+
+WebSim::Puppet* WebSim::GetPuppet( const char* name )
+{
+  return( (Puppet*)g_hash_table_lookup( WebSim::puppets, name ) );
+}
+
+WebSim::Confederate* WebSim::GetConfederate( const char* name )
+{
+  return( (Confederate*)g_hash_table_lookup( WebSim::confederates, name ) );
+}
+
+bool
+WebSim::SetPuppetPVA( const std::string& name, 
+                                                        Pose& p,
+                                                        Velocity& v,
+                                                        Acceleration& a )
+{
+  Puppet* pup = GetPuppet( name.c_str() );
+  
+  if( pup )
+        {
+               pup->Push( p,v,a );
+               return true;
+        }
+  
+  printf( "[websim] warning: attempt to set puppet PVA on non-existent puppet 
\"%s\"\n",
+                        name.c_str() );
+  return false;
+}
+
+ void WebSim::ForEachConfederate(  void(*cb)(const char*, 
WebSim::Confederate*, void*), void* arg )
+{
+  g_hash_table_foreach( WebSim::confederates,
+                                                               (GHFunc)cb, 
+                                                               arg );
+}
+
+void WebSim::ForEachPuppet( void(*cb)(const char*, WebSim::Puppet*, void*), 
void* arg )
+{
+  g_hash_table_foreach( WebSim::puppets,
+                                                               (GHFunc)cb, 
+                                                               arg );
+}

Modified: code/websim/src/websim.hh
===================================================================
--- code/websim/src/websim.hh   2009-03-10 09:40:23 UTC (rev 7446)
+++ code/websim/src/websim.hh   2009-03-10 09:40:49 UTC (rev 7447)
@@ -77,6 +77,11 @@
                              Acceleration& a,
                              std::string& response) = 0;
 
+  bool SetPuppetPVA( const std::string& name, 
+                                                       Pose& p,
+                                                       Velocity& v,
+                                                       Acceleration& a );
+
   protected:
     void StringSplit(const std::string &s, 
                      std::vector<std::string> &t, 
@@ -154,7 +159,7 @@
         void AddConfederate( Confederate* conf, 
                                                                 const char* 
prototype );
   };
-  
+ 
   // manage a federated simulator
   class Confederate
   {
@@ -172,9 +177,6 @@
         static void PuppetCreationCallback(  evhttp_request* req, void* arg );
         static void PuppetPushCallback( evhttp_request* req, void* arg );
 
-        // counts the number of status messages sent and replies not yet 
received
-        unsigned int unacknowledged_pushes;
-
         WebSim* ws;
 
   public:
@@ -186,7 +188,12 @@
         
         int Push( const char* name, Pose p, Velocity v, Acceleration a );
 
-        int RunStep();
+        static void TickCb( const char* name, Confederate* conf, void* arg );
+        static void TickReplyCb( evhttp_request* req, void* arg );
+
+
+        // send a tick request to this host
+        int Tick();
         
         // list of pointers to Puppet objects that are hosted on this
         // connection - can iterate over these to update all puppets
@@ -230,11 +237,20 @@
   GHashTable* puppets;
   GHashTable* confederates;
 
+  // counts the number of status messages sent and replies not yet received
+  unsigned int unacknowledged_pushes;
+
   /** Get a puppet by name */
   Puppet* GetPuppet( const char* name );
   
   /** Get a confederate by name in format "host:port" */
   Confederate* GetConfederate( const char* name );
+
+  /** For each confederate, call the callback function */
+  void ForEachConfederate( void(*cb)(const char*, Confederate*, void*), void* 
arg );
+  
+  /** For each puppet, call the callback function */
+  void ForEachPuppet( void(*cb)(const char*, Puppet*, void*), void* arg );
 };
 
 class Pose


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to