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

Log Message:
-----------
refactored to get instance of websim

Modified Paths:
--------------
    code/branches/federation/stage/examples/gzfed/confederate.cc
    code/branches/federation/stage/examples/gzfed/main.cc
    code/branches/federation/stage/examples/gzfed/parser.cc
    code/branches/federation/stage/examples/gzfed/puppet.cc
    code/branches/federation/stage/examples/gzfed/websim.cc
    code/branches/federation/stage/examples/gzfed/websim.hh

Modified: code/branches/federation/stage/examples/gzfed/confederate.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/confederate.cc        
2009-03-10 02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/confederate.cc        
2009-03-10 03:07:02 UTC (rev 7420)
@@ -3,7 +3,7 @@
 
 #include "websim.hh"
 
-Confederate::Confederate( const char* host, unsigned short port ) :
+WebSim::Confederate::Confederate( WebSim* ws, const char* host, unsigned short 
port ) :
   puppet_list( NULL )
 {
   if(! (http_con = evhttp_connection_new( host, port )) )
@@ -19,18 +19,21 @@
                                                                                
&remote_port );
   
   // build the canonical name for this confederate
-  snprintf( name, 512, "%s:%u", remote_host, remote_port );
-
-  g_hash_table_insert( WebSim::confederates, name, this );
+  char buf[512];
+  snprintf( buf, 512, "%s:%u", remote_host, remote_port );
+  
+  name = strdup(buf); 
+  
+  g_hash_table_insert( ws->confederates, (void*)name, this );
   //printf( "\t\tConfederate %s constructed \n", name );
 }
 
-Confederate::~Confederate()
+WebSim::Confederate::~Confederate()
 {
   evhttp_connection_free( http_con );
 }
 
-void Confederate::PuppetCreationCallback( evhttp_request* req, void* arg )
+void WebSim::Confederate::PuppetCreationCallback( evhttp_request* req, void* 
arg )
 {
   Puppet* pup = (Puppet*)arg;
   printf( "response regarding puppet \"%s\"\n", pup->name );
@@ -43,7 +46,7 @@
         pup->created = true;
 }
 
-void Confederate::AddPuppet( Puppet* puppet,
+void WebSim::Confederate::AddPuppet( Puppet* puppet,
                                                                          const 
char* prototype )
 {
   // send a create message
@@ -78,14 +81,14 @@
         g_list_append( puppet_list, puppet );                                  
                                                 
 }
 
-int Confederate::Push( Puppet* puppet )
+int WebSim::Confederate::Push( const char* name, Pose p, Velocity v, 
Acceleration a )
 {
-  printf( "\tpushing \"%s\" to %s.\n",
-                        puppet->name,
+  printf( "\tconfederate %s pushing state of \"%s\"\n",
+                        this->name,
                         name );
 }
 
-int Confederate::RunStep()
+int WebSim::Confederate::RunStep()
 {
   // construct and send a tick message
   printf( "Confederate %s clock tick\n",

Modified: code/branches/federation/stage/examples/gzfed/main.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/main.cc       2009-03-10 
02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/main.cc       2009-03-10 
03:07:02 UTC (rev 7420)
@@ -4,48 +4,25 @@
 
 #include "websim.hh"
 
-void print_confederate( const char* name, Confederate* conf, void* prefix )
-{
-  //printf( "%skey: %s name :%s\n", prefix, name, conf->name );
-  printf( "%s%s\n", prefix, conf->name );
-}
-
-void update_puppet( const char* name, Puppet* pup, void* dummy )
-{
-  // poke new state into puppet here 
-  pup->pose.x = 10;
-  pup->pose.y = 10;
-  pup->pose.z = 10;
-  pup->pose.a = 10;
-  
-  // and send the changes to the remote simulator
-  pup->Push();
-}
-
 int main( int argc, char** argv )
 {
-  WebSim ws( argv[1], createcallback, stateupdatecallback );
+  WebSim ws( argv[1] );//, createcallback, stateupdatecallback );
   
-  puts( "\nConfederates:" );
-  ws.ForEachConfederate( print_confederate, 
-                                                                (void*)"\t" );
-  
   while( 1 )
         {
                        
-
                // native loop
                
-               until( simtime >= (lasttime + ws.interval) )
-                 { 
-                        for each model
-                                                       update model
-                 }
+               //              until( simtime >= (lasttime + ws.interval) )
+               //{ 
+               // for each model
+               //                              update model
+               //}
 
-               for each model          
-                         ws.Push( "monkey", x,y,z )              
+               //for each model                
+               //  ws.Push( "monkey", x,y,z )            
                                                  
-               ws.Update();
+               //ws.Update();
         }
 
   return 0;

Modified: code/branches/federation/stage/examples/gzfed/parser.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/parser.cc     2009-03-10 
02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/parser.cc     2009-03-10 
03:07:02 UTC (rev 7420)
@@ -9,16 +9,13 @@
 
 #include "websim.hh"
 
-static yaml_event_t event;
-static yaml_parser_t parser;
-static string hostname;
 
 void httprequest( string req )
 {
   cout << "\t" << req << endl;
 }
 
-void parse_failed( const char* message )
+void WebSim::Parser::parse_failed( const char* message )
 {
   printf( "federation file parse failed at line: %u column: %u type %u %s\n", 
                         event.start_mark.line,
@@ -28,7 +25,7 @@
   exit(0);
 }
 
-yaml_event_t next_event( const char* msg )
+yaml_event_t WebSim::Parser::next_event( const char* msg )
 {
   yaml_event_delete( &event );
 
@@ -38,7 +35,7 @@
   return event;
 }
 
-void expect( yaml_event_type_t event_type, const char* msg )
+void WebSim::Parser::expect( yaml_event_type_t event_type, const char* msg )
 {
   //printf( "expecting %s\n", msg );
 
@@ -46,30 +43,30 @@
         parse_failed(  msg );
 }
 
-bool test( yaml_event_type_t event_type, const char* msg )
-{
-  printf( "expecting %s\n", msg );
+// bool test( yaml_event_type_t event_type, const char* msg )
+// {
+//   printf( "expecting %s\n", msg );
 
-  return( next_event( "" ).type != event_type );
-}
+//   return( next_event( "" ).type != event_type );
+// }
 
 
-Confederate* GetConfederate( const char* host, unsigned short port )
+WebSim::Confederate* WebSim::Parser::GetConfederate( const char* host, 
unsigned short port )
 {
   char lookup[256];
   snprintf( lookup, 256, "%s:%u", host, port );
 
   Confederate* conf = (Confederate*)
-        g_hash_table_lookup( WebSim::confederates, lookup );
+        g_hash_table_lookup( ws->confederates, lookup );
   
   if( ! conf )  
-        conf = new Confederate( host, port );          
+        conf = new Confederate( ws, host, port );              
   
   return conf;
 }
 
 
-Confederate* GetConfederate( const char* hostandport )
+WebSim::Confederate* WebSim::Parser::GetConfederate( const char* hostandport )
 {
   unsigned int port = WebSim::DEFAULT_PORT;
   char hostnoport[256];
@@ -83,7 +80,7 @@
   return GetConfederate( hostnoport, port );           
 }
 
-void parse_puppet_mapping( Puppet* pup )
+void WebSim::Parser::parse_puppet_mapping( Puppet* pup )
 {
   expect( YAML_SCALAR_EVENT, "puppet host" );
   const char* host = strdup((const char*)event.data.scalar.value);
@@ -100,7 +97,7 @@
         GetConfederate( host ); // federate for time sync only
 }
 
-void parse_puppet_mapping_sequence( Puppet* pup )
+void WebSim:: Parser::parse_puppet_mapping_sequence( Puppet* pup )
 {
   expect( YAML_SEQUENCE_START_EVENT, "start of sequence of puppet mappings" );
   
@@ -120,22 +117,22 @@
         }
 }
 
-void parse_model_mapping( string host )
+void WebSim::Parser::parse_model_mapping( string host )
 {
   expect( YAML_SCALAR_EVENT, "model name" );  
   string model = (char*)event.data.scalar.value;
   
   Puppet* pup = NULL;
 
-  if( host == hostname )
-        pup = new Puppet( model.c_str() );
+  if( host == ws->hostname )
+        pup = new Puppet( ws, model.c_str() );
 
   parse_puppet_mapping_sequence( pup );
   
   expect( YAML_MAPPING_END_EVENT, "end of model mapping" );
 }
 
-void parse_model_mapping_sequence( string host )
+void WebSim::Parser::parse_model_mapping_sequence( string host )
 {
   expect( YAML_SEQUENCE_START_EVENT, "start of sequence of model mappings" );
   
@@ -155,7 +152,7 @@
         }
 }
 
-void parse_host_mapping()
+void WebSim::Parser::parse_host_mapping()
 {
   expect( YAML_MAPPING_START_EVENT, "host mapping start" );
   
@@ -182,21 +179,22 @@
         }
 }
 
-void parse_document()
+void WebSim::Parser::parse_document()
 {
   expect( YAML_DOCUMENT_START_EVENT, "document start" );
   parse_host_mapping();
   expect( YAML_DOCUMENT_END_EVENT, "document end" );
 }
 
-void parse_stream()
+void WebSim::Parser::parse_stream()
 {
   expect( YAML_STREAM_START_EVENT, "stream start" );
   parse_document();
   expect( YAML_STREAM_END_EVENT, "stream end" );
 }
 
-int parse_federation_file( const char* filename )
+WebSim::Parser::Parser( WebSim* ws, const char* filename ) :
+  ws( ws )
 {
   char hst[256];
   if( gethostname( hst, 256 ) )
@@ -204,31 +202,29 @@
                perror( "failed to get hostname" );
                exit( 0 );
         }
-
+  
   char buf[256];
   snprintf( buf, 256, "%s:%u", hst, WebSim::DEFAULT_PORT );
-
+  
   // copy the c string into a std:string
-  hostname = buf;
+  ws->hostname = buf;
   
   /* Create the Parser object. */
   yaml_parser_initialize(&parser);
-    
+  
   /* Set a file input. */
   FILE *input = fopen( filename, "rb");
   assert( input );
-
+  
   yaml_parser_set_input_file(&parser, input);
   
   parse_stream();
   
   /* The application is responsible for destroying the event object. */
   yaml_event_delete(&event);
-
+  
   /* Destroy the Parser object. */
   yaml_parser_delete(&parser);
-  
-  return 0;
 }
 
 

Modified: code/branches/federation/stage/examples/gzfed/puppet.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/puppet.cc     2009-03-10 
02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/puppet.cc     2009-03-10 
03:07:02 UTC (rev 7420)
@@ -5,29 +5,30 @@
 #include "websim.hh"
 
 
-Puppet::Puppet( const char* name ) :
+WebSim::Puppet::Puppet( WebSim* ws, const char* name ) :
   name( strdup(name) ),
-  created( false )
+  created( false ),
+  ws( ws )
 {
-  g_hash_table_insert( WebSim::puppets, (void*)this->name, this );
+  g_hash_table_insert( ws->puppets, (void*)this->name, this );
 
   //printf( "\t Puppet \"%s\" constructed\n", this->name );
 }
 
 
-void Puppet::Push()
+void WebSim::Puppet::Push( Pose p, Velocity v, Acceleration a )
 {
   for( GList* it = confederates;
                 it;
                 it = it->next )
         {
                Confederate* conf = (Confederate*)it->data;
-               conf->Push( this );               
+               conf->Push( name, p, v, a );              
         }
 }
 
-void Puppet::AddConfederate( Confederate* conf, 
-                                                                         const 
char* prototype )
+void WebSim::Puppet::AddConfederate( Confederate* conf, 
+                                                                               
                 const char* prototype )
 {
   conf->AddPuppet( this, prototype );  
   confederates = g_list_append( confederates, conf );

Modified: code/branches/federation/stage/examples/gzfed/websim.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/websim.cc     2009-03-10 
02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/websim.cc     2009-03-10 
03:07:02 UTC (rev 7420)
@@ -2,36 +2,22 @@
 
 #include "websim.hh"
 
-// declaration - parser defined in parser.cc
-int parse_federation_file( const char* filename );
 
 // static members  
- GHashTable* WebSim::confederates = 
-  g_hash_table_new( g_str_hash, g_str_equal );
-
-GHashTable* WebSim::puppets = 
-  g_hash_table_new( g_str_hash, g_str_equal );
-
 const unsigned short WebSim::DEFAULT_PORT = 8000;
-
 const std::string WebSim::package = "WebSim";
 const std::string WebSim::version = "0.1";
 
 
-// WebSim::WebSim( const char* filename )
-// {
-//   std::cout << package << ' ' << version << std::endl;
-//   parse_federation_file( filename );  
-//   std::cout << package << " ready." << std::endl;
-// }
-
-void WebSim::Init( const char* filename )
+WebSim::WebSim( const char* filename ) :
+  confederates( g_hash_table_new( g_str_hash, g_str_equal ) ),
+  puppets( g_hash_table_new( g_str_hash, g_str_equal ) )
 {
   std::cout << package << ' ' << version << std::endl;
-
   event_init(); // libevent startup
-  parse_federation_file( filename );  
-
+  
+  Parser p( this, filename );  
+  
   std::cout << package << " ready." << std::endl;
 }
 
@@ -42,25 +28,36 @@
                                                                arg );
 }
 
-void WebSim::ForEachPuppet( void(*cb)(const char*, Puppet*, void*), void* arg )
+void WebSim::ForEachPuppet( void(*cb)(const char*, WebSim::Puppet*, void*), 
void* arg )
 {
   g_hash_table_foreach( WebSim::puppets,
                                                                (GHFunc)cb, 
                                                                arg );
 }
 
-
-Puppet* WebSim::GetPuppet( const char* name )
+WebSim::Puppet* WebSim::GetPuppet( const char* name )
 {
   return( (Puppet*)g_hash_table_lookup( WebSim::puppets, name ) );
 }
 
-Confederate* WebSim::GetConfederate( const char* name )
+WebSim::Confederate* WebSim::GetConfederate( const char* name )
 {
   return( (Confederate*)g_hash_table_lookup( WebSim::confederates, name ) );
 }
 
 void WebSim::Update()
 {
-  event_loop( EVLOOP_ONCE );
+  //event_loop( EVLOOP_ONCE );
 }
+
+
+void WebSim::Push(  const char* name, Pose p, Velocity v, Acceleration a )
+{
+  Puppet* pup = GetPuppet( name );
+  
+  if( pup )
+        pup->Push( p, v, a );
+  else
+        printf( "websim warning: failed to push state for non-existent model 
\"%s\"\n",
+                               name );
+}

Modified: code/branches/federation/stage/examples/gzfed/websim.hh
===================================================================
--- code/branches/federation/stage/examples/gzfed/websim.hh     2009-03-10 
02:28:14 UTC (rev 7419)
+++ code/branches/federation/stage/examples/gzfed/websim.hh     2009-03-10 
03:07:02 UTC (rev 7420)
@@ -1,5 +1,6 @@
 #include <sys/types.h> 
 #include <sys/queue.h> 
+#include <yaml.h>
 
 #include <event.h> // libevent
 #include <evhttp.h> // libevent's http tools
@@ -7,140 +8,171 @@
 
 #include <glib.h>
 
-class Pose
+#define MODELNAMEMAX 512
+
+// forward decl.
+
+class WebSim
 {
-public:
-  double x,y,z,r,p,a;
+public: 
+  class Pose
+  {
+  public:
+        double x,y,z,r,p,a;
+        
+        Pose() :
+               x(0), y(0), z(0), a(0)
+        {}
+        
+        Pose( double x, double y, double z,
+                        double r, double p, double a ) :
+               x(x), y(y), z(z), a(a)
+        {}      
+  };
   
-  Pose() :
-        x(0), y(0), z(0), a(0)
+  class Velocity : public Pose
   {
-        // nothing to do
-  }
+  public:
+        Velocity() : Pose()
+        {}
+        
+        Velocity( double x, double y, double z,
+                                 double r, double p, double a ) :
+               Pose( x, y, z, r, p, a )
+        {}  
+  };
   
-  Pose( double x, double y, double z,
-                 double r, double p, double a ) :
-        x(x), y(y), z(z), a(a)
+  class Acceleration : public Pose
   {
-        // nothing to do
-  }
+  public:
+        Acceleration() : Pose()
+        {}
+        
+        Acceleration( double x, double y, double z,
+                                               double r, double p, double a ) :
+               Pose( x, y, z, r, p, a )
+        {}  
+  };
   
-};
+private:
 
-class Velocity : public Pose
-{
-public:
-  Velocity() : Pose()
-  {}
+  // forward decare
+  class Confederate;
 
-  Velocity( double x, double y, double z,
-                               double r, double p, double a ) :
-        Pose( x, y, z, r, p, a )
-  {}  
-};
+  // manage an object we control on some federated simulators
+  class Puppet
+  {
+  private:
+        WebSim* ws;
 
-class Acceleration : public Pose
-{
-public:
-  Acceleration() : Pose()
-  {}
+  public:  
+        Puppet( WebSim* ws,  const char* name );
         
-  Acceleration( double x, double y, double z,
-                                        double r, double p, double a ) :
-        Pose( x, y, z, r, p, a )
-  {}  
-};
+        // unique ID used as a system-widel handle for this puppet, used as
+        // hash table key
+        const char* name;
+        
+        bool created;
+        
+        // the remote servers that are hosting an instance of this puppet
+        GList* confederates;
+        
+        // sends the current physical state of the puppet to all
+        // confederates
+        void Push( Pose p, Velocity v, Acceleration a );
+        
+        // create an instance of the puppet on this confederate, using the
+        // named puppet prototype (defined locally at the conferderate)
+        void AddConfederate( Confederate* conf, 
+                                                                const char* 
prototype );
+  };
+  
+  // manage a federated simulator
+  class Confederate
+  {
+  private:
+        // connection to a remote http simulation server
+        struct evhttp_connection* http_con;  
+        struct evhttp_request* http_req;
+        
+        static void PuppetCreationCallback(  evhttp_request* req, void* arg );
+        
+        WebSim* ws;
 
+  public:
+        Confederate( WebSim* ws, const char* host, unsigned short port );  
+        ~Confederate();
+        
+        void AddPuppet( Puppet* puppet,
+                                                 const char* prototype );
+        
+        int Push( const char* name, Pose p, Velocity v, Acceleration a );
 
-// forward decl.
-class Confederate;
-
-// manage an object we control on some federated simulators
-class Puppet
-{
-public:  
-  Puppet( const char* name );
+        int RunStep();
+        
+        // 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
+        const char* name;  
+  };
   
-  // unique ID used as a system-widel handle for this puppet, used as
-  // hash table key
-  const char* name;
-  
-  Pose pose;
-  Velocity vel;
-  Acceleration acc;
-  
-  bool created;
+  class Parser
+  {
+  private:
+        yaml_event_t event;
+        yaml_parser_t parser;
+        WebSim* ws;
+        
+        void parse_failed( const char* message );
+        yaml_event_t next_event( const char* msg );
+        void expect( yaml_event_type_t event_type, const char* msg );
+        bool test( yaml_event_type_t event_type, const char* msg );
+        Confederate* GetConfederate( const char* host, unsigned short port );
+        Confederate* GetConfederate( const char* hostandport );
+        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_document();
+        void parse_stream();
+        
+  public:
+        Parser( WebSim* ws, const char* filename );
+  };
 
-  // the remote servers that are hosting an instance of this puppet
-  GList* confederates;
   
-  // sends the current physical state of the puppet to all
-  // confederates
-  void Push();
-  
-  // create an instance of the puppet on this confederate, using the
-  // named puppet prototype (defined locally at the conferderate)
-  void AddConfederate( Confederate* conf, 
-                                                         const char* prototype 
);
-};
 
-
-// manage a federated simulator
-class Confederate
-{
 private:
-  // connection to a remote http simulation server
-  struct evhttp_connection* http_con;  
-  struct evhttp_request* http_req;
-
-  static void PuppetCreationCallback(  evhttp_request* req, void* arg );
-
-
-public:
-  Confederate( const char* host, unsigned short port );  
-  ~Confederate();
+  static const unsigned short DEFAULT_PORT;
+  static const std::string package;
+  static const std::string version;
   
-  void AddPuppet( Puppet* puppet,
-                                               const char* prototype );
+  std::string hostname;
 
-  int Push( Puppet* puppet );
-  int RunStep();
+  GHashTable* puppets;
+  GHashTable* confederates;
   
-  // list of pointers to Puppet objects that are hosted on this
-  // connection - can iterate over these to update all puppets
-  GList* puppet_list;
+  /** Get a puppet by name */
+  Puppet* GetPuppet( const char* name );
+  
+  /** Get a confederate by name in format "host:port" */
+  Confederate* GetConfederate( const char* name );
 
+public:  
+  WebSim( const char* federation_filename );
   
-  // unique ID for this confederate in format "hostname:port", used as
-  // hash table key
-  char name[512];  
-};
-
-
-class WebSim
-{
-public:
-  static const unsigned short DEFAULT_PORT;
-  static const std::string package;
-  static const std::string version;
-  static GHashTable* puppets;
-  static GHashTable* confederates;
-
-  static void Init(  const char* federation_filename );
-        
-  //WebSim( const char* federation_filename );
-
   /** For each confederate, call the callback function */
-  static void ForEachConfederate( void(*cb)(const char*, Confederate*, void*), 
void* arg );
-
+  void ForEachConfederate( void(*cb)(const char*, Confederate*, void*), void* 
arg );
+  
   /** For each puppet, call the callback function */
-  static void ForEachPuppet( void(*cb)(const char*, Puppet*, void*), void* arg 
);
+  void ForEachPuppet( void(*cb)(const char*, Puppet*, void*), void* arg );
   
-  /** Get a puppet by name */
-  static Puppet* GetPuppet( const char* name );
+  
+  void Push( const char* name, Pose p, Velocity v, Acceleration a );
 
-  /** Get a confederate by name in format "host:port" */
-  static Confederate* GetConfederate( const char* name );
-
-  static void Update();
+  void Update();
 };


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