Revision: 7469
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7469&view=rev
Author:   rtv
Date:     2009-03-13 02:49:05 +0000 (Fri, 13 Mar 2009)

Log Message:
-----------
parsing and clock sync now working

Modified Paths:
--------------
    code/websim/examples/simple.cc
    code/websim/examples/world.fed
    code/websim/src/confederate.cc
    code/websim/src/parser.cc
    code/websim/src/puppet.cc
    code/websim/src/websim.cc
    code/websim/src/websim.hh

Modified: code/websim/examples/simple.cc
===================================================================
--- code/websim/examples/simple.cc      2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/examples/simple.cc      2009-03-13 02:49:05 UTC (rev 7469)
@@ -34,7 +34,8 @@
                                                                        const 
websim::Acceleration& a,
                                                                        
std::string& error)
   {
-        printf( "set model PVA name:%s\n", name.c_str() ); 
+        printf( "set model PVA name:%s\n", name.c_str() );      
+
         return true;
   }
 
@@ -52,20 +53,24 @@
 
 int main( int argc, char** argv )
 {
-  MinWebSim mws( argv[1], argv[2], atoi(argv[3]) );
+  std::string filename = argv[1];
+  std::string host = argv[2];
+  unsigned short port = atoi( argv[3] );
+  
+  MinWebSim mws( filename, host, port );
 
   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" );
+               if( port == 8000 )
+                 {
+                        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 );
+                        
+                        //usleep(100);
+                        
+                        mws.SetPuppetPVA( "monkey", p, v, a );
+                 }                             
+               mws.Update();                   
         }
 }

Modified: code/websim/examples/world.fed
===================================================================
--- code/websim/examples/world.fed      2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/examples/world.fed      2009-03-13 02:49:05 UTC (rev 7469)
@@ -1,11 +1,14 @@
 
 [federation]
-deckard:8000=websimple
-gort:8000=webgazebo
+deckard:8000=master
+deckard:8001=slave1
+deckard:8002=slave2
+deckard:8003=slave3
 
-[websimple]
-chunky=webgazebo:pioneer2dx
-punky=webgazebo:pioneer2dx
+[master]
+monkey=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
+chunky=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
+punky=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
 
 
 

Modified: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc      2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/src/confederate.cc      2009-03-13 02:49:05 UTC (rev 7469)
@@ -59,12 +59,10 @@
                                                                                
&remote_host, 
                                                                                
&remote_port );
   
-  // build the canonical name for this confederate
-  //char buf[512];
-  //snprintf( buf, 512, "%s:%u", remote_host, remote_port );  
-  //name = strdup(buf); 
+  printf( "\t\tConfederate %s constructed \n", name.c_str() );
 
-  printf( "\t\tConfederate %s constructed \n", name.c_str() );
+  if( uri != ws->hostportname )
+        ws->tick_count_expected++;
 }
 
 WebSim::Confederate::~Confederate()
@@ -88,15 +86,6 @@
 void WebSim::Confederate::AddPuppet( Puppet* puppet,
                                                                          
std::string prototype )
 {
-  // send a create message
-  //printf( "Creating puppet \"%s\" on %s using prototype \"%s\"\n",
-  //    puppet->name,
-  //    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);
@@ -117,7 +106,7 @@
   
   // a successful response sets puppet->created to true
   while( ! puppet->created )
-        event_loop( EVLOOP_NONBLOCK ); // loops until the request has completed
+        event_loop( EVLOOP_ONCE ); // loops until the request has completed
   
   puppet_list = 
         g_list_append( puppet_list, puppet );                                  
                                                 
@@ -125,13 +114,21 @@
 
 void WebSim::Confederate::PuppetPushCallback( evhttp_request* req, void* arg )
 {
+  if( req == NULL )
+        {
+               puts( "[websim] warning: broken connection waiting for push 
reply." );
+               return;
+        }
+
   cb_chunk_t* ch = (cb_chunk_t*)arg;
-  
+  assert(ch);
+
   if( req->response_code == 200 )
         {              
-               --ch->conf->ws->unacknowledged_pushes;
-               printf( "puppet push for \"%s\" acked OK. Outstanding pushes 
%u\n",
-                                 ch->name.c_str(), 
ch->conf->ws->unacknowledged_pushes );
+               ch->conf->ws->unacknowledged_pushes--;
+               
+               //printf( "puppet push for \"%s\" acked OK. Outstanding pushes 
%u\n",
+               //        ch->name.c_str(), ch->conf->ws->unacknowledged_pushes 
);
         }
   else
         {
@@ -139,6 +136,8 @@
                printf( "code: %d\n", req->response_code );             
                printf( "response: %s\n", req->input_buffer->buffer );
         }
+
+  delete ch;
 }
 
 int WebSim::Confederate::Push( std::string name, Pose p, Velocity v, 
Acceleration a )
@@ -148,12 +147,12 @@
                         name.c_str() );
   
   // compose a struct to send into the callback
-  cb_chunk_t ch;
-  ch.name = name;                                                              
                  
-  ch.conf = this;
+  cb_chunk_t* ch = new cb_chunk_t();
+  ch->name = name;                                                             
                  
+  ch->conf = this;
 
   struct evhttp_request* er = 
-        evhttp_request_new( PuppetPushCallback, &ch );
+        evhttp_request_new( PuppetPushCallback, ch );
   assert(er);
   
   char buf[512];
@@ -166,7 +165,7 @@
                                v.x, v.y, v.z, v.r, v.p, v.a,
                                a.x, a.y, a.z, a.r, a.p, a.a );
   
-  printf( "Emitting: http://%s%s\n";, this->name.c_str(), buf );
+  // printf( "Emitting: http://%s%s\n";, this->name.c_str(), buf );
   
   ++ws->unacknowledged_pushes; // decremented when reply is received
 
@@ -183,13 +182,19 @@
 
 void WebSim::Confederate::TickReplyCb( evhttp_request* req, void* arg )
 {
+  if( req == NULL )
+        {
+               puts( "[websim] warning: broken connection waiting for tick 
reply." );
+               return;
+        }
+
   WebSim* ws = (WebSim*)arg;
   
   if( req->response_code == 200 )
         {              
-               --ws->unacknowledged_pushes;
-               printf( "tick acked OK. Outstanding pushes %u\n",
-                                 ws->unacknowledged_pushes );
+               --ws->unacknowledged_ticks;
+               //printf( "tick ACK. Outstanding ticks %u\n",
+               //        ws->unacknowledged_ticks );
         }
   else
         {
@@ -203,15 +208,15 @@
 int WebSim::Confederate::Tick()
 {
   // construct and send a tick message
-  printf( "Confederate %s clock tick\n",
-                        name.c_str() );
+  //printf( "Confederate %s clock tick\n",
+  //    name.c_str() );
   
-  ++ws->unacknowledged_pushes;
-  
   struct evhttp_request* er = 
         evhttp_request_new( TickReplyCb, ws );
   assert(er);
   
+  ++ws->unacknowledged_ticks; // decremented when reply is received
+  
   int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, 
"/sim/clock/tick" );
   if( ret != 0 )
         {

Modified: code/websim/src/parser.cc
===================================================================
--- code/websim/src/parser.cc   2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/src/parser.cc   2009-03-13 02:49:05 UTC (rev 7469)
@@ -30,17 +30,14 @@
 #include <string>
 #include <iostream>
 
-// we#include <yaml.h>   // YAML parser
+#include "websim.hh"
+
 using namespace std;
-
-#include "websim.hh"
 using namespace websim;
 
 
-WebSim::Parser::Parser( WebSim* ws, std::string filename ) :
-  ws( ws )
-{
-  
+void WebSim::LoadFederationFile( std::string filename ) 
+{  
   GError* err = NULL;
   GKeyFile* keyfile = g_key_file_new();
   g_key_file_load_from_file( keyfile, 
@@ -74,21 +71,21 @@
                                  *fedkeyp, logicalname );
                
                string hosturi = *fedkeyp;
-               Confederate* conf = new Confederate( ws, hosturi );             
                
+               Confederate* conf = new Confederate( this, hosturi );           
                
                assert(conf);
                
                // copy the hash table key so we can delete the original below
-               g_hash_table_insert( ws->confederates, strdup(logicalname), 
conf );
+               g_hash_table_insert( confederates, strdup(logicalname), conf );
         }
   
-  cout << "Looking up logical name for myself " << ws->hostportname << endl;
+  cout << "Looking up logical name for myself " << hostportname << endl;
 
   // now we have made all the confederates. 
   // Let's look up the confederate for this instance
   // read the array of entries for this host
   gchar* mylogicalname = g_key_file_get_string( keyfile,
                                                                                
                                                "federation",
-                                                                               
                                                ws->hostportname.c_str(),
+                                                                               
                                                hostportname.c_str(),
                                                                                
                                                &err );           
   
   cout << "my logical name is " << mylogicalname << endl;
@@ -123,7 +120,7 @@
                                                  *modelp );
                         else
                                {       
-                                 Puppet*  pup = new Puppet( ws, *modelp );
+                                 Puppet*  pup = new Puppet( this, *modelp );
                                  
                                  // for each puppet
                                  for( char** puppetp = puppets; 
@@ -139,18 +136,21 @@
                                                std::string confstr = 
pupstr.substr( 0, pos );
                                                std::string typestr = 
pupstr.substr( pos+1 );                                           
 
-                                               Confederate* conf = 
ws->GetConfederate( confstr );
-                                               assert( conf );
-                                               
-                                               pup->AddConfederate( conf, 
typestr );
+                                               Confederate* conf = 
GetConfederate( confstr );
+                                               if( conf == NULL )
+                                                 {
+                                                        printf( "[websim] 
error: request to export %s/%s to unspecified server %s\n",
+                                                                               
mylogicalname, *modelp, confstr.c_str() );
+                                                 }
+                                               else
+                                                 {                             
                         
+                                                        pup->AddConfederate( 
conf, typestr );
+                                                 }
                                         }
                                }
                  }
         }
   
-  // XX
-  exit( 0 );
-  
   // free the key array
   g_strfreev( fedkeys );                               
 }

Modified: code/websim/src/puppet.cc
===================================================================
--- code/websim/src/puppet.cc   2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/src/puppet.cc   2009-03-13 02:49:05 UTC (rev 7469)
@@ -35,7 +35,8 @@
 WebSim::Puppet::Puppet( WebSim* ws, std::string name ) :
   ws( ws ),
   name( name ),
-  created( false )
+  created( false ),
+  confederates( NULL )
 {
   g_hash_table_insert( ws->puppets, (void*)this->name.c_str(), this );
   printf( "Puppet \"%s\" constructed\n", this->name.c_str() );

Modified: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc   2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/src/websim.cc   2009-03-13 02:49:05 UTC (rev 7469)
@@ -40,6 +40,7 @@
 WebSim::WebSim(const std::string& _fedfile,
                const std::string& _host, 
                unsigned short _port) :
+  ticks_remaining(0),
   tick_count_expected(0),
   fedfile(_fedfile), 
   host(_host), 
@@ -47,7 +48,9 @@
   puppets( g_hash_table_new( g_str_hash, g_str_equal ) ),
   confederates( g_hash_table_new( g_str_hash, g_str_equal ) ),
   logical_hosts( g_hash_table_new( g_str_hash, g_str_equal ) ),
-  unacknowledged_pushes(0)
+  unacknowledged_pushes(0),
+  unacknowledged_ticks(0),
+  total_ticks(0)
 {
   
   char buf[512];
@@ -76,7 +79,7 @@
   if( fedfile != "" )
         {
                printf( "[websim] Loading federation file %s\n", 
_fedfile.c_str() );
-               Parser p( this, _fedfile.c_str() );  
+               LoadFederationFile( _fedfile.c_str() );  
         }
   
   puts("[websim] Ready");
@@ -96,11 +99,11 @@
 
   if( confname != ws->hostportname ) // don't tick myself
         {
-               printf( "ticking conf %s\n", conf->name.c_str() );
+               // printf( "ticking conf %s\n", conf->name.c_str() );
                conf->Tick();
         }
-  else
-        printf( "NOT ticking myself %s\n", conf->name.c_str() );
+  //else
+  // printf( "NOT ticking myself %s\n", conf->name.c_str() );
 }
 
 void
@@ -108,16 +111,22 @@
 {
   // tick all my confederates
   ForEachConfederate( Confederate::TickCb, this );
+  
+  ticks_remaining = tick_count_expected;
+  do
+        {
+//             printf( "Update: ticks pending %d/%d  ACKS( tick %u push %u)\n",
+//                               ticks_remaining, tick_count_expected,
+//                               unacknowledged_ticks, 
+//                               unacknowledged_pushes );
+               
+               if(tick_count_expected || unacknowledged_pushes )
+                 event_loop(EVLOOP_ONCE);
+               else
+                 event_loop(EVLOOP_NONBLOCK);
+        } while( ticks_remaining );
 
-  tick_count = 0;
-  do
-  {
-    if(tick_count_expected || unacknowledged_pushes )
-      event_loop(EVLOOP_ONCE);
-    else
-      event_loop(EVLOOP_NONBLOCK);
-  } while(tick_count < tick_count_expected);
-  tick_count = 0;
+  printf( "STEP %lu\n", total_ticks++ );
 }
 
 void 
@@ -227,7 +236,7 @@
   {
     if(action == "tick")
     {
-      tick_count++;
+      ticks_remaining--;
       response = "Ticked the clock";
       return true;
     }

Modified: code/websim/src/websim.hh
===================================================================
--- code/websim/src/websim.hh   2009-03-13 00:46:58 UTC (rev 7468)
+++ code/websim/src/websim.hh   2009-03-13 02:49:05 UTC (rev 7469)
@@ -52,92 +52,95 @@
 
 class WebSim
 {
-  public:
-    WebSim(const std::string& _fedfile, 
-           const std::string& _host,
-           unsigned short _port);
-    virtual ~WebSim();
+public:
+  WebSim(const std::string& _fedfile, 
+                       const std::string& _host,
+                       unsigned short _port);
 
-    void Update();
-
-    // Interface to be implemented by simulators
-    virtual bool CreateModel(const std::string& name, 
-                             const std::string& type,
-                             std::string& response) = 0;
-    virtual bool DeleteModel(const std::string& name,
-                             std::string& response) = 0;
-    virtual bool SetModelPVA(const std::string& name, 
-                             const Pose& p,
-                             const Velocity& v,
-                             const Acceleration& a,
-                             std::string& response) = 0;
-    virtual bool GetModelPVA(const std::string& name, 
-                             Pose& p,
-                             Velocity& v,
-                             Acceleration& a,
-                             std::string& response) = 0;
-
+  virtual ~WebSim();
+  
+  void LoadFederationFile( std::string filename );
+  
+  void Update();
+  
+  // Interface to be implemented by simulators
+  virtual bool CreateModel(const std::string& name, 
+                                                                       const 
std::string& type,
+                                                                       
std::string& response) = 0;
+  virtual bool DeleteModel(const std::string& name,
+                                                                       
std::string& response) = 0;
+  virtual bool SetModelPVA(const std::string& name, 
+                                                                       const 
Pose& p,
+                                                                       const 
Velocity& v,
+                                                                       const 
Acceleration& a,
+                                                                       
std::string& response) = 0;
+  virtual bool GetModelPVA(const std::string& name, 
+                                                                       Pose& p,
+                                                                       
Velocity& v,
+                                                                       
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, 
-                     const std::string &d);
-
-
+  
+protected:
+  void StringSplit(const std::string &s, 
+                                                std::vector<std::string> &t, 
+                                                const std::string &d);
+  
+  
   // Number of ticks we require before exiting Update()
   int tick_count_expected;
-  // Number of ticks we've received since the last Update()
-  int tick_count;
+  // Number of ticks remaining before we can quit update
+  int ticks_remaining;
   
-  private:
-    std::string fedfile;
-    std::string host;
-    std::string hostportname; // format "host:port" to uniquely identify this 
instance
-    unsigned short port;
-
-    struct evhttp* eh;
-
-    // Static, so that it can be passed as a callback to libevent
-    static void EventCallback(evhttp_request* req, void* arg);
-
-    bool GetValue(std::string& value,
-                  struct evkeyvalq* query_args, 
-                  const std::string& key);
-    bool HandleURI(const std::string& model,
-                   const std::string& prop,
-                   const std::string& action,
-                   struct evkeyvalq* kv,
-                   std::string& response);
-    bool HandleSimRequest(const std::string& prop,
-                          const std::string& action,
-                          struct evkeyvalq* kv,
-                          std::string& response);
-    bool HandleModelRequest(const std::string& model,
-                            const std::string& prop,
-                            const std::string& action,
-                            struct evkeyvalq* kv,
-                            std::string& response);
-    bool ParseURI(std::string& model,
-                  std::string& prop,
-                  std::string& action,
-                  std::string uri,
-                  std::string& response);
-    void DeleteKeyVal(struct evkeyvalq* query_args);
-
-
-          // forward decare
+private:
+  std::string fedfile;
+  std::string host;
+  std::string hostportname; // format "host:port" to uniquely identify this 
instance
+  unsigned short port;
+  
+  struct evhttp* eh;
+  
+  // Static, so that it can be passed as a callback to libevent
+  static void EventCallback(evhttp_request* req, void* arg);
+  
+  bool GetValue(std::string& value,
+                                        struct evkeyvalq* query_args, 
+                                        const std::string& key);
+  bool HandleURI(const std::string& model,
+                                         const std::string& prop,
+                                         const std::string& action,
+                                         struct evkeyvalq* kv,
+                                         std::string& response);
+  bool HandleSimRequest(const std::string& prop,
+                                                               const 
std::string& action,
+                                                               struct 
evkeyvalq* kv,
+                                                               std::string& 
response);
+  bool HandleModelRequest(const std::string& model,
+                                                                 const 
std::string& prop,
+                                                                 const 
std::string& action,
+                                                                 struct 
evkeyvalq* kv,
+                                                                 std::string& 
response);
+  bool ParseURI(std::string& model,
+                                        std::string& prop,
+                                        std::string& action,
+                                        std::string uri,
+                                        std::string& response);
+  void DeleteKeyVal(struct evkeyvalq* query_args);
+  
+  
+  // forward decare
   class Confederate;
-
+  
   // manage an object we control on some federated simulators
   class Puppet
   {
   private:
         WebSim* ws;
-
+        
   public:  
         Puppet( WebSim* ws, std::string name );
         
@@ -159,7 +162,7 @@
         void AddConfederate( Confederate* conf, 
                                                                 std::string 
prototype );
   };
- 
+  
   // manage a federated simulator
   class Confederate
   {
@@ -198,37 +201,12 @@
         // list of pointers to Puppet objects that are hosted on this
         // connection - can iterate over these to update all puppets
         GList* puppet_list;
-                
+        
         // unique ID for this confederate in format "hostname:port", used as
         // hash table key
         std::string name;  
   };
   
-  class Parser
-  {
-  private:
-        //yaml_event_t event;
-        //yaml_parser_t parser;
-        WebSim* ws;
-        
-       //  void parse_failed( std::string message );
-//      void next_event( std::string msg );
-//      void expect( yaml_event_type_t event_type, std::string msg );
-//      void parse_puppet_mapping( Puppet* pup );
-//      void parse_puppet_mapping_sequence( Puppet* pup );
-//      void parse_model_mapping( std::string host );
-//      void parse_model_mapping_sequence( std::string host );
-//      void parse_host_mapping();      
-//      void parse_logical_host();
-//      void parse_logical_host_sequence();
-
-
-        //      Confederate* GetConfederate( std::string name, std::string 
uri);
-        
-  public:
-        Parser( WebSim* ws, std::string filename );
-  };
-
  private:
   // static const unsigned short DEFAULT_PORT;
   static const std::string package;
@@ -241,6 +219,11 @@
   // counts the number of status messages sent and replies not yet received
   unsigned int unacknowledged_pushes;
 
+  // counts the number of clock ticks sent and replies not yet received
+  unsigned int unacknowledged_ticks;
+
+  unsigned long total_ticks;
+
   /** Get a puppet by name */
   Puppet* GetPuppet( std::string name );
   


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

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to