Revision: 7468
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7468&view=rev
Author:   rtv
Date:     2009-03-13 00:46:58 +0000 (Fri, 13 Mar 2009)

Log Message:
-----------
completely reworked parser

Modified Paths:
--------------
    code/websim/CMakeLists.txt
    code/websim/examples/Makefile
    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/CMakeLists.txt
===================================================================
--- code/websim/CMakeLists.txt  2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/CMakeLists.txt  2009-03-13 00:46:58 UTC (rev 7468)
@@ -61,7 +61,7 @@
 link_directories(gtest/lib)
 include_directories(${PROJECT_BINARY_DIR}/gtest/include)
 add_executable(uritest EXCLUDE_FROM_ALL test/uritest.cc)
-target_link_libraries(uritest websim gtest yaml event)
+target_link_libraries(uritest websim gtest event)
 add_dependencies(uritest fetch_and_build)
 
 add_custom_target(test

Modified: code/websim/examples/Makefile
===================================================================
--- code/websim/examples/Makefile       2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/examples/Makefile       2009-03-13 00:46:58 UTC (rev 7468)
@@ -1,8 +1,14 @@
 
-all: clean minwebsim
+# set this to the directory that you installed websim
+WSPREFIX=/Users/vaughan/Stage-3.0
 
+WSLDFLAGS=-L${WSPREFIX}/lib -lwebsim `pkg-config --libs glib-2.0`
+WSCFLAGS=-g -W -Wall -I${WSPREFIX}/include  `pkg-config --cflags glib-2.0`
+
+all: clean websimple
+
 clean:
-       rm -f minwebsim
+       rm -f websimple
 
-minwebsim: 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
+websimple: simple.cc
+       g++ ${WSCFLAGS}  simple.cc -o $@ ${WSLDFLAGS}
\ No newline at end of file

Modified: code/websim/examples/world.fed
===================================================================
--- code/websim/examples/world.fed      2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/examples/world.fed      2009-03-13 00:46:58 UTC (rev 7468)
@@ -1,11 +1,11 @@
 
+[federation]
+deckard:8000=websimple
+gort:8000=webgazebo
 
-federation: 
-   - "gort 8000": minwebsim
-   - "gort 8001": webgazebo
+[websimple]
+chunky=webgazebo:pioneer2dx
+punky=webgazebo:pioneer2dx
 
-minwebsim:
-     - monkey: 
-               - webgazbo: pioneer2dx
 
 

Modified: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc      2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/src/confederate.cc      2009-03-13 00:46:58 UTC (rev 7468)
@@ -27,17 +27,29 @@
 
 #include <stdio.h>
 #include <assert.h>
+#include <iostream>
 
 #include "websim.hh"
 using namespace websim;
 
-WebSim::Confederate::Confederate( WebSim* ws, std::string host, unsigned short 
port ) :
+WebSim::Confederate::Confederate( WebSim* ws, 
+                                                                               
         std::string uri ) :
+  name(uri),
   ws(ws),
   puppet_list( NULL )
 {
-  if(! (http_con = evhttp_connection_new( host.c_str(), port )) )
+
+  size_t pos = uri.find( ":" ); // find the start of the port number
+  std::string portstr = uri.substr( pos+1 ); // set to the port number
+  
+  std::string hoststr = uri.substr( 0, pos );
+  unsigned short portnum = atoi(portstr.c_str() ); 
+
+  std::cout << "Conf constructing connection: " << hoststr << " : " << portstr 
<< std::endl;
+
+  if(! (http_con = evhttp_connection_new( hoststr.c_str(), portnum )) )
         printf( "Error: Confederate object failed to connect to server at 
%s:%d\n",
-                               host.c_str(), port );
+                               hoststr.c_str(), portnum );
   
   char* remote_host = NULL;
   unsigned short remote_port = 0;
@@ -48,13 +60,10 @@
                                                                                
&remote_port );
   
   // build the canonical name for this confederate
-  char buf[512];
-  snprintf( buf, 512, "%s:%u", remote_host, remote_port );
-  
-  name = strdup(buf); 
-  
-  g_hash_table_insert( ws->confederates, (void*)name.c_str(), this );
-  
+  //char buf[512];
+  //snprintf( buf, 512, "%s:%u", remote_host, remote_port );  
+  //name = strdup(buf); 
+
   printf( "\t\tConfederate %s constructed \n", name.c_str() );
 }
 

Modified: code/websim/src/parser.cc
===================================================================
--- code/websim/src/parser.cc   2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/src/parser.cc   2009-03-13 00:46:58 UTC (rev 7468)
@@ -30,255 +30,129 @@
 #include <string>
 #include <iostream>
 
-#include <yaml.h>   // YAML parser
+// we#include <yaml.h>   // YAML parser
 using namespace std;
 
 #include "websim.hh"
 using namespace websim;
 
-void httprequest( string req )
-{
-  cout << "\t" << req << endl;
-}
 
-void WebSim::Parser::parse_failed( std::string message )
+WebSim::Parser::Parser( WebSim* ws, std::string filename ) :
+  ws( ws )
 {
-  printf( "federation file parse failed at line: %u column: %u type %u %s\n", 
-                        event.start_mark.line,
-                        event.start_mark.column,
-                        event.type,
-                        message.c_str() );
-  exit(0);
-}
-
-void WebSim::Parser::next_event( std::string msg )
-{
-  //yaml_event_delete( &event );
-
-  //cout << "next_event msg " << msg << endl;
-
-  if (!yaml_parser_parse( &parser, &event))
-        parse_failed( msg );
-}
-
-void WebSim::Parser::expect( yaml_event_type_t expected_type, std::string msg )
-{
-  //cout << "expecting " << msg << endl;
   
-  next_event( "IN EXPECTING" );
-  if( event.type != expected_type )
+  GError* err = NULL;
+  GKeyFile* keyfile = g_key_file_new();
+  g_key_file_load_from_file( keyfile, 
+                                                                         
filename.c_str(),
+                                                                         
G_KEY_FILE_NONE,
+                                                                         &err 
);
+  
+  if( ! g_key_file_has_group( keyfile, "federation" ) )
         {
-               printf( "Expecting type %d, got type %d\n",
-                                 expected_type, event.type );
-               parse_failed(  msg );
+               printf( "[websim] Can't find the required [federation] section 
in federation file %s.\n",
+                                 filename.c_str() );
+               exit( 0 );
         }
-}
 
-WebSim::Confederate* WebSim::Parser::GetConfederate( std::string host, 
unsigned short port )
-{
-  char lookup[256];
-  snprintf( lookup, 256, "%s:%u", host.c_str(), port );
-
-  assert( ws );
-  assert( ws->confederates );
-
-  Confederate* conf = (Confederate*)
-        g_hash_table_lookup( ws->confederates, lookup );
-  
-  if( ! conf )  
-        conf = new Confederate( ws, host, port );              
-  
-  return conf;
-}
-
-
-WebSim::Confederate* WebSim::Parser::GetConfederate( std::string hostandport )
-{
-  unsigned int port = ws->port;
-  char hostnoport[256];
-  
-  // parse out port number from hostname
-  sscanf( hostandport.c_str(), "%s %u", hostnoport, &port );
-  
-  return GetConfederate( hostnoport, port );           
-}
-
-void WebSim::Parser::parse_puppet_mapping( Puppet* pup )
-{
-  expect( YAML_SCALAR_EVENT, "puppet host" );
-  std::string host = (const char*)event.data.scalar.value;
-  
-  expect( YAML_SCALAR_EVENT, "puppet type" );
-  std::string prototype = (const char*)event.data.scalar.value;
-  
-  expect( YAML_MAPPING_END_EVENT, "puppet mapping" );
-  
-  if( pup )
-        pup->AddConfederate( GetConfederate( host ), 
-                                                                prototype );
-  else
-        GetConfederate( host ); // federate for time sync only
-}
-
-void WebSim:: Parser::parse_puppet_mapping_sequence( Puppet* pup )
-{
-  expect( YAML_SEQUENCE_START_EVENT, "start of sequence of puppet mappings" );
-  
-  while( 1 )
+  // get an array of confederate hosts
+  gchar** fedkeys = g_key_file_get_keys( keyfile,
+                                                                               
                          "federation",
+                                                                               
                          NULL,
+                                                                               
                          &err );  
+  // for each host
+  for( char** fedkeyp = fedkeys; 
+                *fedkeyp;
+                fedkeyp++ )
         {
-               next_event( "" );
-               switch( event.type )
-                 { 
-                 case YAML_MAPPING_START_EVENT:
-                        parse_puppet_mapping( pup );
-                        break;
-                 case  YAML_SEQUENCE_END_EVENT:
-                        return;
-                 default:
-                        parse_failed( "expecting a puppet mapping or end of 
sequence" );
-                 }
+               // get the logical name
+               gchar* logicalname = g_key_file_get_string( keyfile,
+                                                                               
                                                        "federation",
+                                                                               
                                                        *fedkeyp,
+                                                                               
                                                        &err );         
+               printf( "[websim] host %s is %s\n",
+                                 *fedkeyp, logicalname );
+               
+               string hosturi = *fedkeyp;
+               Confederate* conf = new Confederate( ws, hosturi );             
                
+               assert(conf);
+               
+               // copy the hash table key so we can delete the original below
+               g_hash_table_insert( ws->confederates, strdup(logicalname), 
conf );
         }
-}
-
-void WebSim::Parser::parse_model_mapping( string host )
-{
-  expect( YAML_SCALAR_EVENT, "model name" );  
-  string model = (const 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() );
+  cout << "Looking up logical name for myself " << ws->hostportname << endl;
 
-  parse_puppet_mapping_sequence( pup );
+  // 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(),
+                                                                               
                                                &err );           
   
-  expect( YAML_MAPPING_END_EVENT, "end of model mapping" );
-}
+  cout << "my logical name is " << mylogicalname << endl;
 
-void WebSim::Parser::parse_model_mapping_sequence( string host )
-{
-  expect( YAML_SEQUENCE_START_EVENT, "start of sequence of model mappings" );
-  
-  while( 1 )
+  // now we look up all the models listed under my logical name
+  // get an array of confederate hosts
+  gchar** models = g_key_file_get_keys( keyfile,
+                                                                               
                         mylogicalname,
+                                                                               
                         NULL,
+                                                                               
                         &err );    
+
+  if( models == NULL )
+        printf( "[websim] No models to export.\n" );
+  else
         {
-               next_event( "" );
-               switch( event.type )
-                 { 
-                 case YAML_MAPPING_START_EVENT:
-                        parse_model_mapping( host );
-                        break;
-                 case  YAML_SEQUENCE_END_EVENT:
-                        return;
-                 default:
-                        parse_failed( "expecting a model mapping or end of 
sequence" );
-                 }
-        }
-}
+               // for each model
+               for( char** modelp = models; 
+                         *modelp;
+                         modelp++ )
+                 {
+                        printf( "[websim] model %s\n", *modelp );              
         
+                        
+                        // get the puppet instructions
+                        gchar** puppets = g_key_file_get_string_list( keyfile,
+                                                                               
                                                                  mylogicalname,
+                                                                               
                                                                  *modelp,
+                                                                               
                                                                  NULL,
+                                                                               
                                                                  &err );       
        
+                        
+                        if( puppets == NULL )
+                               printf( "[websim] No puppets specified for 
model \"%s\".\n",
+                                                 *modelp );
+                        else
+                               {       
+                                 Puppet*  pup = new Puppet( ws, *modelp );
+                                 
+                                 // for each puppet
+                                 for( char** puppetp = puppets; 
+                                                *puppetp;
+                                                puppetp++ )
+                                        {
+                                               cout << " creating remote 
puppet " << *puppetp << endl;
+                                               
+                                               // split the name
 
+                                               string pupstr = *puppetp;
+                                               size_t pos = pupstr.find( ":" 
); 
+                                               std::string confstr = 
pupstr.substr( 0, pos );
+                                               std::string typestr = 
pupstr.substr( pos+1 );                                           
 
-void WebSim::Parser::parse_logical_host()
-{
-  expect( YAML_SCALAR_EVENT, "host and port" );  
-
-  string hostandport = (const char*)event.data.scalar.value;
-  //cout << "[websim] host mapping \"" << hostandport << "\"" << endl;
-
-  expect( YAML_SCALAR_EVENT, "logical name" );  
-
-  string logicalname = (const char*)event.data.scalar.value;
-  cout << "[websim] " << hostandport << " -> " << logicalname << endl;
-
-  expect( YAML_MAPPING_END_EVENT, "end of model mapping" );
-}
-
-void WebSim::Parser::parse_logical_host_sequence()
-{
-  expect( YAML_SEQUENCE_START_EVENT, "start of logical host mappings" );
-  
-  while( 1 )
-        {
-               next_event( "barf" );
-               switch( event.type )
-                 { 
-                 case YAML_MAPPING_START_EVENT:
-                        // cout << "parsing logical host" << endl;
-                        parse_logical_host();
-                        break;
-                 case  YAML_SEQUENCE_END_EVENT:
-                        cout << "[websim] host mapping done." << endl;
-                        return;
-                 default:
-                        parse_failed( "expecting a logical host mapping or end 
of sequence" );
+                                               Confederate* conf = 
ws->GetConfederate( confstr );
+                                               assert( conf );
+                                               
+                                               pup->AddConfederate( conf, 
typestr );
+                                        }
+                               }
                  }
         }
-}
-
-void WebSim::Parser::parse_host_mapping()
-{
-  expect( YAML_MAPPING_START_EVENT, "host sequence start" );
   
-  while( 1 )
-        {
-               next_event( "start of host sequence" );
-               switch( event.type )
-                 { 
-                 case YAML_SCALAR_EVENT:
-                        {
-                               string host = (const 
char*)event.data.scalar.value;
-                               
-                               if( host == "federation" )
-                                 {
-                                        cout << "[websim] mapping hosts..." << 
endl;
-                                        parse_logical_host_sequence();
-                                        cout << "[websim] mapping hosts done." 
<< endl;
-                                 }
-                               else
-                                 {
-                                        cout << "[websim] host: " << host << " 
hostportname: " << ws->hostportname << endl;
-                                        
-                                        Confederate* conf = GetConfederate( 
host.c_str() );
-                                        assert(conf);
-                                        
-                                        cout << "[websim] conf->host: " << 
conf->name << " hostportname: " << ws->hostportname << endl;
-                                        
-                                        parse_model_mapping_sequence( 
conf->name );
-                                 }
-                        }
-                        break;
-                 case  YAML_SEQUENCE_END_EVENT:
-                        return;
-                 default:
-                        parse_failed( "expecting a host scalar or a end of 
mapping" );
-                 }
-        }
-}
-
-WebSim::Parser::Parser( WebSim* ws, std::string filename ) :
-  ws( ws )
-{
-    /* Create the Parser object. */
-  yaml_parser_initialize(&parser);
+  // XX
+  exit( 0 );
   
-  /* Set a file input. */
-  FILE *input = fopen( filename.c_str(), "rb");
-  assert( input );
-  
-  yaml_parser_set_input_file(&parser, input);
-  
-  expect( YAML_STREAM_START_EVENT, "stream start" );
-  expect( YAML_DOCUMENT_START_EVENT, "document start" );
-  
-  parse_host_mapping();
-  
-  expect( YAML_DOCUMENT_END_EVENT, "document end" );
-  expect( YAML_STREAM_END_EVENT, "stream end" );
-  
-  /* The application is responsible for destroying the event object. */
-  yaml_event_delete(&event);
-  
-  /* Destroy the Parser object. */
-  yaml_parser_delete(&parser);
+  // free the key array
+  g_strfreev( fedkeys );                               
 }
 
 

Modified: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc   2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/src/websim.cc   2009-03-13 00:46:58 UTC (rev 7468)
@@ -40,18 +40,18 @@
 WebSim::WebSim(const std::string& _fedfile,
                const std::string& _host, 
                unsigned short _port) :
-                 tick_count_expected(0),
-        fedfile(_fedfile), 
-                 host(_host), 
-                 port(_port),
-                 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)
+  tick_count_expected(0),
+  fedfile(_fedfile), 
+  host(_host), 
+  port(_port),
+  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)
 {
   
   char buf[512];
-  snprintf( buf, 512, "%s %u", host.c_str(), port );
+  snprintf( buf, 512, "%s:%u", host.c_str(), port );
   hostportname = buf;
 
   printf( "[websim] %s %s ID \"%s\"\n", 

Modified: code/websim/src/websim.hh
===================================================================
--- code/websim/src/websim.hh   2009-03-12 23:37:08 UTC (rev 7467)
+++ code/websim/src/websim.hh   2009-03-13 00:46:58 UTC (rev 7468)
@@ -41,7 +41,7 @@
 #include <glib.h>
 
 // YAML
-#include <yaml.h>
+// #include <yaml.h>
 
 namespace websim
 {
@@ -141,7 +141,7 @@
   public:  
         Puppet( WebSim* ws, std::string name );
         
-        // unique ID used as a system-widel handle for this puppet, used as
+        // unique ID used as a system-wide handle for this puppet, used as
         // hash table key
         std::string name;
         
@@ -180,7 +180,7 @@
         WebSim* ws;
 
   public:
-        Confederate( WebSim* ws, std::string host, unsigned short port );  
+        Confederate( WebSim* ws, std::string uri );  
         ~Confederate();
         
         void AddPuppet( Puppet* puppet,
@@ -207,23 +207,23 @@
   class Parser
   {
   private:
-        yaml_event_t event;
-        yaml_parser_t parser;
+        //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 );
-        Confederate* GetConfederate( std::string host, unsigned short port );
-        Confederate* GetConfederate( std::string 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_logical_host();
-        void parse_logical_host_sequence();
+       //  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 );
@@ -244,7 +244,7 @@
   /** Get a puppet by name */
   Puppet* GetPuppet( std::string name );
   
-  /** Get a confederate by name in format "host:port" */
+  /** Get a confederate by its logical name */
   Confederate* GetConfederate( std::string name );
 
   /** For each confederate, call the callback function */


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