Revision: 7435
http://playerstage.svn.sourceforge.net/playerstage/?rev=7435&view=rev
Author: rtv
Date: 2009-03-10 07:01:16 +0000 (Tue, 10 Mar 2009)
Log Message:
-----------
added rtv federation client side stuff
Added Paths:
-----------
code/websim/src/confederate.cc
code/websim/src/parser.cc
code/websim/src/puppet.cc
Added: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc (rev 0)
+++ code/websim/src/confederate.cc 2009-03-10 07:01:16 UTC (rev 7435)
@@ -0,0 +1,152 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "websim.hh"
+using namespace websim;
+
+WebSim::Confederate::Confederate( WebSim* ws, const char* host, unsigned short
port ) :
+ puppet_list( NULL ),
+ unacknowledged_pushes( 0 )
+{
+ if(! (http_con = evhttp_connection_new( host, port )) )
+ printf( "Error: Confederate object failed to connect to server at
%s:%d\n",
+ host, port );
+
+ char* remote_host = NULL;
+ unsigned short remote_port = 0;
+
+ // store the name and port post-connection for sanoty check
+ evhttp_connection_get_peer( http_con,
+
&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);
+
+ g_hash_table_insert( ws->confederates, (void*)name, this );
+ //printf( "\t\tConfederate %s constructed \n", name );
+}
+
+WebSim::Confederate::~Confederate()
+{
+ evhttp_connection_free( http_con );
+}
+
+void WebSim::Confederate::PuppetCreationCallback( evhttp_request* req, void*
arg )
+{
+ Puppet* pup = (Puppet*)arg;
+ if( req->response_code == 200 )
+ pup->created = true;
+ else
+ {
+ printf( "bad response regarding puppet creation \"%s\"\n",
pup->name );
+ printf( "code: %d\n", req->response_code );
+ printf( "response: %s\n", req->input_buffer->buffer );
+ }
+}
+
+void WebSim::Confederate::AddPuppet( Puppet* puppet,
+ const
char* prototype )
+{
+ // send a create message
+ //printf( "Creating puppet \"%s\" on %s using prototype \"%s\"\n",
+ // puppet->name,
+ // name,
+ // prototype );
+
+ struct evhttp_request* er =
+ evhttp_request_new( PuppetCreationCallback, puppet );
+ assert(er);
+
+ char buf[512];
+ snprintf( buf, 512, "/sim/factory/create?name=%s&type=%s",
+ puppet->name,
+ prototype );
+
+ printf( "Emitting: http://%s%s\n", name, buf );
+
+ int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, buf );
+ if( ret != 0 )
+ {
+ printf( "make request returned error %d\n", ret );
+ exit(0);
+ }
+
+ // a successful response sets puppet->created to true
+ while( ! puppet->created )
+ event_loop( EVLOOP_NONBLOCK ); // loops until the request has completed
+
+ puppet_list =
+ g_list_append( puppet_list, puppet );
+}
+
+void WebSim::Confederate::PuppetPushCallback( evhttp_request* req, void* arg )
+{
+ cb_chunk_t* ch = (cb_chunk_t*)arg;
+
+ if( req->response_code == 200 )
+ {
+ --ch->conf->unacknowledged_pushes;
+ printf( "puppet push for \"%s\" acked OK. Outstanding pushes
%u\n",
+ ch->name, ch->conf->unacknowledged_pushes );
+ }
+ else
+ {
+ printf( "bad response regarding puppet push \"%s\"\n", ch->name
);
+ printf( "code: %d\n", req->response_code );
+ printf( "response: %s\n", req->input_buffer->buffer );
+ }
+}
+
+int WebSim::Confederate::Push( const char* name, Pose p, Velocity v,
Acceleration a )
+{
+ printf( "\tconfederate %s pushing state of \"%s\"\n",
+ this->name,
+ name );
+
+ // compose a struct to send into the callback
+ cb_chunk_t ch;
+ ch.name = name;
+ ch.conf = this;
+
+ struct evhttp_request* er =
+ evhttp_request_new( PuppetPushCallback, &ch );
+ assert(er);
+
+ char buf[512];
+ snprintf( buf, 512, "/%s/pva/set?"
+
"px=%.6f&py=%.6f&pz=%.6f&pr=%.6f&pp=%.6f&pa=%.6f&"
+
"vx=%.6f&vy=%.6f&vz=%.6f&vr=%.6f&vp=%.6f&va=%.6f&"
+
"ax=%.6f&ay=%.6f&az=%.6f&ar=%.6f&ap=%.6f&aa=%.6f",
+ name,
+ p.x, p.y, p.z, p.r, p.p, p.a,
+ 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, buf );
+
+ ++unacknowledged_pushes; // decremented when reply is received
+
+ int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, buf );
+ if( ret != 0 )
+ {
+ printf( "make request returned error %d\n", ret );
+ exit(0);
+ }
+
+ while( unacknowledged_pushes )
+ event_loop( EVLOOP_NONBLOCK ); // loops until the request has completed
+}
+
+int WebSim::Confederate::RunStep()
+{
+ // construct and send a tick message
+ printf( "Confederate %s clock tick\n",
+ name );
+ return 0;
+}
+
+
Added: code/websim/src/parser.cc
===================================================================
--- code/websim/src/parser.cc (rev 0)
+++ code/websim/src/parser.cc 2009-03-10 07:01:16 UTC (rev 7435)
@@ -0,0 +1,219 @@
+
+#include <stdio.h>
+#include <assert.h>
+#include <string>
+#include <iostream>
+
+#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( 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 WebSim::Parser::next_event( const char* msg )
+{
+ yaml_event_delete( &event );
+
+ if (!yaml_parser_parse( &parser, &event))
+ parse_failed( msg );
+
+ return event;
+}
+
+void WebSim::Parser::expect( yaml_event_type_t event_type, const char* msg )
+{
+ //printf( "expecting %s\n", msg );
+
+ if( next_event( "" ).type != event_type )
+ parse_failed( msg );
+}
+
+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( ws->confederates, lookup );
+
+ if( ! conf )
+ conf = new Confederate( ws, host, port );
+
+ return conf;
+}
+
+
+WebSim::Confederate* WebSim::Parser::GetConfederate( const char* hostandport )
+{
+ unsigned int port = ws->port;
+ char hostnoport[256];
+
+ // parse out port number from hostname
+ sscanf( hostandport, "%s %u", &hostnoport, &port );
+
+ return GetConfederate( hostnoport, port );
+}
+
+void WebSim::Parser::parse_puppet_mapping( Puppet* pup )
+{
+ expect( YAML_SCALAR_EVENT, "puppet host" );
+ const char* host = strdup((const char*)event.data.scalar.value);
+
+ expect( YAML_SCALAR_EVENT, "puppet type" );
+ const char* prototype = strdup((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 )
+ {
+ 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" );
+ }
+ }
+}
+
+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 == ws->hostportname )
+ pup = new Puppet( ws, model.c_str() );
+
+ parse_puppet_mapping_sequence( pup );
+
+ expect( YAML_MAPPING_END_EVENT, "end of model mapping" );
+}
+
+void WebSim::Parser::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 WebSim::Parser::parse_host_mapping()
+{
+ expect( YAML_MAPPING_START_EVENT, "host mapping start" );
+
+ while( 1 )
+ {
+ next_event( "" );
+ switch( event.type )
+ {
+ case YAML_SCALAR_EVENT:
+ {
+ string host = (const
char*)event.data.scalar.value;
+
+ // cout << "HOST " << host << endl;
+
+ Confederate* conf = GetConfederate(
host.c_str() );
+ parse_model_mapping_sequence( conf->name );
+ }
+ break;
+ case YAML_MAPPING_END_EVENT:
+ return;
+ default:
+ parse_failed( "expecting a host scalar or a end of
mapping" );
+ }
+ }
+}
+
+void WebSim::Parser::parse_document()
+{
+ expect( YAML_DOCUMENT_START_EVENT, "document start" );
+ parse_host_mapping();
+ expect( YAML_DOCUMENT_END_EVENT, "document end" );
+}
+
+void WebSim::Parser::parse_stream()
+{
+ expect( YAML_STREAM_START_EVENT, "stream start" );
+ parse_document();
+ expect( YAML_STREAM_END_EVENT, "stream end" );
+}
+
+WebSim::Parser::Parser( WebSim* ws, const char* filename ) :
+ ws( ws )
+{
+// char hst[256];
+// if( gethostname( hst, 256 ) )
+// {
+// 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
+// ws->hostportname = 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);
+}
+
+
Added: code/websim/src/puppet.cc
===================================================================
--- code/websim/src/puppet.cc (rev 0)
+++ code/websim/src/puppet.cc 2009-03-10 07:01:16 UTC (rev 7435)
@@ -0,0 +1,37 @@
+
+#include <string.h> //for strdup()
+#include <stdio.h>
+
+#include "websim.hh"
+using namespace websim;
+
+
+WebSim::Puppet::Puppet( WebSim* ws, const char* name ) :
+ name( strdup(name) ),
+ created( false ),
+ ws( ws )
+{
+ g_hash_table_insert( ws->puppets, (void*)this->name, this );
+}
+
+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( name, p, v, a );
+ }
+}
+
+void WebSim::Puppet::AddConfederate( Confederate* conf,
+
const char* prototype )
+{
+ conf->AddPuppet( this, prototype );
+ confederates = g_list_append( confederates, conf );
+}
+
+
+
+
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