Revision: 7401
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7401&view=rev
Author:   rtv
Date:     2009-03-09 07:54:57 +0000 (Mon, 09 Mar 2009)

Log Message:
-----------
federation file parsing code

Added Paths:
-----------
    code/branches/federation/stage/examples/gzfed/Makefile
    code/branches/federation/stage/examples/gzfed/main.cc
    code/branches/federation/stage/examples/gzfed/parser.cc

Added: code/branches/federation/stage/examples/gzfed/Makefile
===================================================================
--- code/branches/federation/stage/examples/gzfed/Makefile                      
        (rev 0)
+++ code/branches/federation/stage/examples/gzfed/Makefile      2009-03-09 
07:54:57 UTC (rev 7401)
@@ -0,0 +1,4 @@
+
+
+parser: parser.cc main.cc
+       g++ parser.cc main.cc -o parser -lyaml
\ No newline at end of file

Added: code/branches/federation/stage/examples/gzfed/main.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/main.cc                       
        (rev 0)
+++ code/branches/federation/stage/examples/gzfed/main.cc       2009-03-09 
07:54:57 UTC (rev 7401)
@@ -0,0 +1,17 @@
+
+#include "stdio.h"
+
+// declaration
+int parse_federation_file( const char* filename );
+
+int main( int argc, char** argv )
+{
+  printf( "parsing file %s\n", argv[1] );
+  
+  if( parse_federation_file( argv[1] ) )
+        printf( "failed to parse file." );
+  else
+        printf( "file parsed OK" );
+
+  return 0;
+}

Added: code/branches/federation/stage/examples/gzfed/parser.cc
===================================================================
--- code/branches/federation/stage/examples/gzfed/parser.cc                     
        (rev 0)
+++ code/branches/federation/stage/examples/gzfed/parser.cc     2009-03-09 
07:54:57 UTC (rev 7401)
@@ -0,0 +1,191 @@
+#include <yaml.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+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 )
+{
+  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 );
+  exit(0);
+}
+
+yaml_event_t next_event( const char* msg )
+{
+  yaml_event_delete( &event );
+
+  if (!yaml_parser_parse( &parser, &event))
+        parse_failed( msg );
+
+  return event;
+}
+
+void expect( yaml_event_type_t event_type, const char* msg )
+{
+  //printf( "expecting %s\n", msg );
+
+  if( next_event( "" ).type != event_type )
+        parse_failed(  msg );
+}
+
+bool test( yaml_event_type_t event_type, const char* msg )
+{
+  printf( "expecting %s\n", msg );
+
+  return( next_event( "" ).type != event_type );
+}
+
+
+void parse_ghost_mapping( string host,
+                                                                 string model )
+{
+  expect( YAML_SCALAR_EVENT, "ghost host" );
+  string ghosthost = (const char*)event.data.scalar.value;
+  
+  expect( YAML_SCALAR_EVENT, "ghost type" );
+  string ghosttype = (const char*)event.data.scalar.value;
+  
+  expect( YAML_MAPPING_END_EVENT, "ghost mapping" );
+                               
+  if( host == hostname )        
+        httprequest( "http://"; + ghosthost + "/sim/create?type=" + ghosttype );
+}
+
+
+void parse_ghost_mapping_sequence( string host, 
+                                                                               
          string model )
+{
+  expect( YAML_SEQUENCE_START_EVENT, "start of sequence of ghost mappings" );
+  
+  while( 1 )
+        {
+               next_event( "" );
+               switch( event.type )
+                 { 
+                 case YAML_MAPPING_START_EVENT:
+                        parse_ghost_mapping( host, model );
+                        break;
+                 case  YAML_SEQUENCE_END_EVENT:
+                        return;
+                 default:
+                        parse_failed( "expecting a ghost mapping or end of 
sequence" );
+                 }
+        }
+}
+
+void parse_model_mapping( string host )
+{
+  expect( YAML_SCALAR_EVENT, "model name" );  
+  string model = (char*)event.data.scalar.value;
+
+  parse_ghost_mapping_sequence( host, model );
+  
+  expect( YAML_MAPPING_END_EVENT, "end of model mapping" );
+}
+
+void parse_model_mapping_sequence( string host )
+{
+  expect( YAML_SEQUENCE_START_EVENT, "start of sequence of model mappings" );
+  
+  while( 1 )
+        {
+               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" );
+                 }
+        }
+}
+
+void parse_host_mapping()
+{
+  expect( YAML_MAPPING_START_EVENT, "host mapping start" );
+  
+  while( 1 )
+        {
+               next_event( "" );
+               switch( event.type )
+                 { 
+                 case YAML_SCALAR_EVENT:
+                        {
+                               string host = (char*)event.data.scalar.value;  
+                               cout << "HOST " << host << endl;
+                               parse_model_mapping_sequence( host );
+                        }
+                        break;
+                 case  YAML_MAPPING_END_EVENT:
+                        return;
+                 default:
+                        parse_failed( "expecting a host scalar or a end of 
mapping" );
+                 }
+        }
+}
+
+void parse_document()
+{
+  expect( YAML_DOCUMENT_START_EVENT, "document start" );
+  parse_host_mapping();
+  expect( YAML_DOCUMENT_END_EVENT, "document end" );
+}
+
+void 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 )
+{
+  char hst[256];
+  if( gethostname( hst, 256 ) )
+        {
+               perror( "failed to get hostname" );
+               exit( 0 );
+        }
+
+  // copy the c string into a std:string
+  hostname = hst;
+  
+  /* 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;
+}
+
+


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

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to