Revision: 7481
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7481&view=rev
Author:   rtv
Date:     2009-03-14 01:12:23 +0000 (Sat, 14 Mar 2009)

Log Message:
-----------
much more functionality - currently fighting a race condition - do not use

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

Modified: code/websim/examples/world.fed
===================================================================
--- code/websim/examples/world.fed      2009-03-13 21:50:24 UTC (rev 7480)
+++ code/websim/examples/world.fed      2009-03-14 01:12:23 UTC (rev 7481)
@@ -6,9 +6,9 @@
 deckard:8003=slave3
 
 [master]
-monkey=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
-chunky=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
-punky=slave1:pioneer2dx;slave2:pioneer2dx;slave3:pioneer2dx
+monkey=slave1:pioneer2dx;slave2:pioneer2dx
+chunky=slave1:pioneer2dx;slave2:pioneer2dx
+punky=slave1:pioneer2dx;slave2:pioneer2dx
 
 
 

Modified: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc      2009-03-13 21:50:24 UTC (rev 7480)
+++ code/websim/src/confederate.cc      2009-03-14 01:12:23 UTC (rev 7481)
@@ -33,6 +33,39 @@
 #include "websim.hh"
 using namespace websim;
 
+void greetingCb( evhttp_request* req, void* arg )
+{
+  if( req == NULL )
+        {
+               puts( "[websim] warning: broken connection waiting for greeting 
reply." );
+               return;
+        }
+  
+  bool* created = (bool*)arg;
+  
+  printf( "cb created %p %d\n", created, *created );
+
+  switch( req->response_code )
+        {
+        case 0:        // host not available - we just retry
+               putchar('.'); fflush(stdout);
+               break;
+        case 200: // OK
+               (*created) = true;
+               break;
+        case 400:
+               printf( "[websim] server responds to greeting with error (400): 
%s.\n",
+                               req->input_buffer->buffer );
+               break;
+        default:
+               printf( "[websim] unknown greeting response code (%u): %s.\n", 
+                                 req->response_code,
+                                 req->input_buffer->buffer );
+               break;
+        }
+}
+
+
 WebSim::Confederate::Confederate( WebSim* ws, 
                                                                                
         std::string uri ) :
   ws(ws),
@@ -62,8 +95,49 @@
   
   printf( "\t\tConfederate %s constructed \n", name.c_str() );
 
-  if( uri != ws->hostportname )
-        ws->tick_count_expected++;
+  // greet the server - loop until reply is received
+
+  bool created = false;
+  
+  struct timeval last;
+  last.tv_sec = 0;
+  last.tv_usec = 0;
+
+  while( ! created )
+        {       
+               struct evhttp_request* er = 
+                 evhttp_request_new( greetingCb, &created );
+               assert(er);
+               
+               struct timeval now;
+               gettimeofday( &now, NULL );
+               
+               if( now.tv_sec > last.tv_sec )
+                 {
+                        std::string buf = "/sim/greet/dummy";
+
+                        printf( "emit created %p %u\n", &created, created );
+                        printf( "Emitting: http://%s/%s\n";, name.c_str(), 
buf.c_str() );               
+                        
+                        int ret = evhttp_make_request( http_con, er, 
EVHTTP_REQ_GET, buf.c_str());
+                        if( ret != 0 )
+                               {
+                                 printf( "make request returned error %d\n", 
ret );
+                                 exit(0);
+                               }
+                        
+                        last.tv_sec = now.tv_sec;
+                 }
+                        
+               // loop until something happens or a short time passes
+               struct timeval tv;
+               tv.tv_sec=1;
+               tv.tv_usec=0;
+               event_loopexit( &tv );          
+               event_loop( EVLOOP_ONCE ); // loops until the request has 
completed             
+        }
+
+  ws->tick_count_expected++;
 }
 
 WebSim::Confederate::~Confederate()
@@ -127,7 +201,8 @@
   if( req->response_code == 200 )
         {              
                ch->conf->ws->unacknowledged_pushes--;
-               
+               ch->conf->ws->EscapeLoopIfDone();
+                               
                //printf( "puppet push for \"%s\" acked OK. Outstanding pushes 
%u\n",
                //        ch->name.c_str(), ch->conf->ws->unacknowledged_pushes 
);
         }
@@ -143,9 +218,9 @@
 
 int WebSim::Confederate::Push( std::string name, Pose p, Velocity v, 
Acceleration a )
 {
-  printf( "\tconfederate %s pushing state of \"%s\"\n",
-                        this->name.c_str(),
-                        name.c_str() );
+  //printf( "\tconfederate %s pushing state of \"%s\"\n",
+  //    this->name.c_str(),
+  //    name.c_str() );
   
   // compose a struct to send into the callback
   cb_chunk_t* ch = new cb_chunk_t();
@@ -194,8 +269,10 @@
   if( req->response_code == 200 )
         {              
                --ws->unacknowledged_ticks;
-               //printf( "tick ACK. Outstanding ticks %u\n",
-               //        ws->unacknowledged_ticks );
+               ws->EscapeLoopIfDone();
+
+               printf( "tick ACK. Outstanding ticks %u\n",
+                                 ws->unacknowledged_ticks );
         }
   else
         {
@@ -224,9 +301,6 @@
                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-13 21:50:24 UTC (rev 7480)
+++ code/websim/src/parser.cc   2009-03-14 01:12:23 UTC (rev 7481)
@@ -78,11 +78,15 @@
                                  *fedkeyp, logicalname );
                
                string hosturi = *fedkeyp;
-               Confederate* conf = new Confederate( this, hosturi );           
                
-               assert(conf);
-               
-               // copy the hash table key so we can delete the original below
-               g_hash_table_insert( confederates, strdup(logicalname), conf );
+
+               if( hosturi != hostportname ) // don't conf with myself
+                 {
+                        Confederate* conf = new Confederate( this, hosturi );  
                        
+                        assert(conf);
+                        
+                        // copy the hash table key so we can delete the 
original below
+                        g_hash_table_insert( confederates, 
strdup(logicalname), conf );
+                 }
         }
   
   cout << "Looking up logical name for myself " << hostportname << endl;

Modified: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc   2009-03-13 21:50:24 UTC (rev 7480)
+++ code/websim/src/websim.cc   2009-03-14 01:12:23 UTC (rev 7481)
@@ -37,12 +37,10 @@
 const std::string WebSim::package = "WebSim";
 const std::string WebSim::version = "0.1";
 
-WebSim::WebSim(const std::string& _fedfile,
-               const std::string& _host, 
-               unsigned short _port) :
+WebSim::WebSim( const std::string& _host, 
+                const unsigned short _port) :
   tick_count_expected(0),
   ticks_remaining(0),
-  fedfile(_fedfile), 
   host(_host), 
   port(_port),
   puppets( g_hash_table_new( g_str_hash, g_str_equal ) ),
@@ -74,14 +72,6 @@
   evhttp_set_gencb(eh, &WebSim::EventCallback, (void*)this);
   puts("Done.");
   
-  // if a federation file was specified, we parse it to populate the
-  // WebSim with confederates and puppets.
-  if( fedfile != "" )
-        {
-               printf( "[websim] Loading federation file %s\n", 
_fedfile.c_str() );
-               LoadFederationFile( _fedfile.c_str() );  
-        }
-  
   puts("[websim] Ready");
 }
 
@@ -93,40 +83,26 @@
 
 void WebSim::Confederate::TickCb( std::string name, Confederate* conf, void* 
arg )
 {
-  WebSim* ws = (WebSim*)arg;
-
-  std::string confname = conf->name;
-
-  if( confname != ws->hostportname ) // don't tick myself
-        {
-               // printf( "ticking conf %s\n", conf->name.c_str() );
-               conf->Tick();
-        }
-  //else
-  // printf( "NOT ticking myself %s\n", conf->name.c_str() );
+  conf->Tick();
 }
 
-void
-WebSim::Update()
+void 
+WebSim::Go()
 {
   // tick all my confederates
-  ForEachConfederate( Confederate::TickCb, this );
-  
+  ForEachConfederate( Confederate::TickCb, NULL );
+}
+
+void
+WebSim::Wait()
+{ 
   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 );
 
-  printf( "STEP %lu\n", total_ticks++ );
+  // a callback will jump out of this loop when we've heard Ticks from
+  // all confederates
+  event_dispatch();
+
+  //printf( "STEP %lu\n", total_ticks++ );
 }
 
 void 
@@ -194,6 +170,18 @@
   }
 }
 
+void WebSim::EscapeLoopIfDone()
+{
+  // if we're done waiting for things we can bail out of the event loop
+  
+  printf( "testing %d %d %d\n", ticks_remaining, unacknowledged_pushes, 
unacknowledged_ticks );
+  if( ! (ticks_remaining || unacknowledged_pushes || unacknowledged_ticks) )
+        {
+               puts( "OUTTA HERE" );
+               event_loopbreak();
+        }
+}
+
 bool
 WebSim::HandleSimRequest(const std::string& prop,
                             const std::string& action,
@@ -202,55 +190,64 @@
 {
   // The special factory property
   if(prop == "factory")
-  {
-    if(action == "create")
-    {
-      std::string name, type;
-      if(!GetValue(name, kv, "name") ||
-         !GetValue(type, kv, "type"))
-      {
-        response = "ERROR: Missing name and/or type argument for 
sim/factor/create";
-        return false;
-      }
-
-      return(CreateModel(name, type, response));
-    }
-    else if(action == "create")
-    {
-      std::string name, type;
-      if(!GetValue(name, kv, "name"))
-      {
-        response = "ERROR: Missing name argument for sim/factor/create";
-        return false;
-      }
-
-      return(DeleteModel(name, response));
-    }
-    else
-    {
-      response = "ERROR: Unknown action " + action + " for sim/factory";
-      return false;
-    }
-  }
+        {
+               if(action == "create")
+                 {
+                        std::string name, type;
+                        if(!GetValue(name, kv, "name") ||
+                                !GetValue(type, kv, "type"))
+                               {
+                                 response = "ERROR: Missing name and/or type 
argument for sim/factor/create";
+                                 return false;
+                               }
+                        
+                        return(CreateModel(name, type, response));
+                 }
+               else if(action == "create")
+                 {
+                        std::string name, type;
+                        if(!GetValue(name, kv, "name"))
+                               {
+                                 response = "ERROR: Missing name argument for 
sim/factor/create";
+                                 return false;
+                               }
+                        
+                        return(DeleteModel(name, response));
+                 }
+               else
+                 {
+                        response = "ERROR: Unknown action " + action + " for 
sim/factory";
+                        return false;
+                 }
+        }
   else if(prop == "clock")
-  {
-    if(action == "tick")
-    {
-      ticks_remaining--;
-      response = "Ticked the clock";
-      return true;
-    }
-    else
-    {
-      response = "ERROR: Unknown action " + action + " for sim/clock";
-      return false;
-    }
-  }
+        {
+               if(action == "tick")
+                 {
+                        ticks_remaining--;
+                        response = "Ticked the clock";
+                        
+                        EscapeLoopIfDone();
+                        return true;
+                 }
+               else
+                 {
+                        response = "ERROR: Unknown action " + action + " for 
sim/clock";
+                        return false;
+                 }
+        }
+  else if(prop == "greet") // action is the name of the greeting server
+        {        
+               // TODO - check to see if this server was anticipated
+               response = "Greetings " + action;
+               printf( "[websim] WebSim peer %s connected\n", action.c_str() );
+               return true;
+        }
   else
-  {
-    response = "ERROR: Unknown property " + prop + " for sim";
-    return false;
-  }
+        {
+               response = "ERROR: Unknown property " + prop + " for sim";
+               return false;
+        }
 }
 
 bool
@@ -424,7 +421,9 @@
 
   // We require 3 path components: model/property/action
   StringSplit(bare_uri, uri_parts, "/");
-  // There should be 4 parts, with the first one empty
+
+  
+  // otherwise there should be 4 parts, with the first one empty
   if(uri_parts.size() != 4)
   {
     response = "Must be 3 slash-separated parts in the URI";

Modified: code/websim/src/websim.hh
===================================================================
--- code/websim/src/websim.hh   2009-03-13 21:50:24 UTC (rev 7480)
+++ code/websim/src/websim.hh   2009-03-14 01:12:23 UTC (rev 7481)
@@ -53,15 +53,18 @@
 class WebSim
 {
 public:
-  WebSim(const std::string& _fedfile, 
-                       const std::string& _host,
-                       unsigned short _port);
+  WebSim( const std::string& _host,
+                        const unsigned short _port);
 
   virtual ~WebSim();
   
   void LoadFederationFile( std::string filename );
   
-  void Update();
+  /** Wait for go signals from all confederates */
+  void Wait();
+
+  /** Send go signals to all confederates */
+  void Go();
   
   // Interface to be implemented by simulators
   virtual bool CreateModel(const std::string& name, 
@@ -90,14 +93,15 @@
                                                 std::vector<std::string> &t, 
                                                 const std::string &d);
   
-  
+  void EscapeLoopIfDone();
+
+
   // Number of ticks we require before exiting Update()
   int tick_count_expected;
   // Number of ticks remaining before we can quit update
   int ticks_remaining;
   
-private:
-  std::string fedfile;
+protected:
   std::string host;
   std::string hostportname; // format "host:port" to uniquely identify this 
instance
   unsigned short port;
@@ -224,6 +228,7 @@
 
   unsigned long total_ticks;
 
+public:
   /** 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