Revision: 7455
http://playerstage.svn.sourceforge.net/playerstage/?rev=7455&view=rev
Author: rtv
Date: 2009-03-10 19:02:49 +0000 (Tue, 10 Mar 2009)
Log Message:
-----------
adding logical host mapping
Modified Paths:
--------------
code/websim/examples/Makefile
code/websim/examples/world.fed
code/websim/src/confederate.cc
code/websim/src/parser.cc
code/websim/src/puppet.cc
code/websim/src/websim.cc
code/websim/src/websim.hh
Modified: code/websim/examples/Makefile
===================================================================
--- code/websim/examples/Makefile 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/examples/Makefile 2009-03-10 19:02:49 UTC (rev 7455)
@@ -1,3 +1,8 @@
-simple: simple.cc
+all: clean minwebsim
+
+clean:
+ rm -f minwebsim
+
+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
Modified: code/websim/examples/world.fed
===================================================================
--- code/websim/examples/world.fed 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/examples/world.fed 2009-03-10 19:02:49 UTC (rev 7455)
@@ -1,30 +1,11 @@
-# sourcehost:
-# - modelname1:
-# - desthost: prototype
-gort:
- - monkey:
- - "localhost 8001": pioneer2dx
-# - punky:
-# - localhost: pioneer2dx
-# - chunky:
-# - localhost: pioneer2dx
-# - funky:
-# - localhost: pioneer2dx
+federation:
+ - "gort 8000": minwebsim
+ - "gort 8001": webgazebo
+minwebsim:
+ - monkey:
+ - webgazbo: pioneer2dx
-# - roy: pioneer
-# - "priss 8888": pioneer
-# - pioneer2dx_model2:
-# - deckard: pioneer
-# - pioneer2dx_model3:
-# - deckard: pioneer
-#"tenner 9999":
-# - pr2_1:
-# - deckard: pr2
-
-#priss:
-# - pr2_1:
-# - "deckard 1234": pr2
Modified: code/websim/src/confederate.cc
===================================================================
--- code/websim/src/confederate.cc 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/src/confederate.cc 2009-03-10 19:02:49 UTC (rev 7455)
@@ -31,13 +31,13 @@
#include "websim.hh"
using namespace websim;
-WebSim::Confederate::Confederate( WebSim* ws, const char* host, unsigned short
port ) :
+WebSim::Confederate::Confederate( WebSim* ws, std::string host, unsigned short
port ) :
ws(ws),
puppet_list( NULL )
{
- if(! (http_con = evhttp_connection_new( host, port )) )
+ if(! (http_con = evhttp_connection_new( host.c_str(), port )) )
printf( "Error: Confederate object failed to connect to server at
%s:%d\n",
- host, port );
+ host.c_str(), port );
char* remote_host = NULL;
unsigned short remote_port = 0;
@@ -53,8 +53,9 @@
name = strdup(buf);
- g_hash_table_insert( ws->confederates, (void*)name, this );
- //printf( "\t\tConfederate %s constructed \n", name );
+ g_hash_table_insert( ws->confederates, (void*)name.c_str(), this );
+
+ printf( "\t\tConfederate %s constructed \n", name.c_str() );
}
WebSim::Confederate::~Confederate()
@@ -69,14 +70,14 @@
pup->created = true;
else
{
- printf( "bad response regarding puppet creation \"%s\"\n",
pup->name );
+ printf( "bad response regarding puppet creation \"%s\"\n",
pup->name.c_str() );
printf( "code: %d\n", req->response_code );
printf( "response: %s\n", req->input_buffer->buffer );
}
}
void WebSim::Confederate::AddPuppet( Puppet* puppet,
- const
char* prototype )
+
std::string prototype )
{
// send a create message
//printf( "Creating puppet \"%s\" on %s using prototype \"%s\"\n",
@@ -93,10 +94,10 @@
char buf[512];
snprintf( buf, 512, "/sim/factory/create?name=%s&type=%s",
- puppet->name,
- prototype );
+ puppet->name.c_str(),
+ prototype.c_str() );
- printf( "Emitting: http://%s%s\n", name, buf );
+ printf( "Emitting: http://%s%s\n", name.c_str(), buf );
int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET, buf );
if( ret != 0 )
@@ -121,21 +122,21 @@
{
--ch->conf->ws->unacknowledged_pushes;
printf( "puppet push for \"%s\" acked OK. Outstanding pushes
%u\n",
- ch->name, ch->conf->ws->unacknowledged_pushes
);
+ ch->name.c_str(),
ch->conf->ws->unacknowledged_pushes );
}
else
{
- printf( "bad response regarding puppet push \"%s\"\n", ch->name
);
+ printf( "bad response regarding puppet push \"%s\"\n",
ch->name.c_str() );
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 )
+int WebSim::Confederate::Push( std::string name, Pose p, Velocity v,
Acceleration a )
{
printf( "\tconfederate %s pushing state of \"%s\"\n",
- this->name,
- name );
+ this->name.c_str(),
+ name.c_str() );
// compose a struct to send into the callback
cb_chunk_t ch;
@@ -151,12 +152,12 @@
"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,
+ name.c_str(),
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 );
+ printf( "Emitting: http://%s%s\n", this->name.c_str(), buf );
++ws->unacknowledged_pushes; // decremented when reply is received
@@ -194,12 +195,12 @@
{
// construct and send a tick message
printf( "Confederate %s clock tick\n",
- name );
+ name.c_str() );
++ws->unacknowledged_pushes;
struct evhttp_request* er =
- evhttp_request_new( PuppetPushCallback, ws );
+ evhttp_request_new( TickReplyCb, ws );
assert(er);
int ret = evhttp_make_request( http_con, er, EVHTTP_REQ_GET,
"/sim/clock/tick" );
Modified: code/websim/src/parser.cc
===================================================================
--- code/websim/src/parser.cc 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/src/parser.cc 2009-03-10 19:02:49 UTC (rev 7455)
@@ -41,38 +41,43 @@
cout << "\t" << req << endl;
}
-void WebSim::Parser::parse_failed( const char* message )
+void WebSim::Parser::parse_failed( std::string 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 );
+ message.c_str() );
exit(0);
}
-yaml_event_t WebSim::Parser::next_event( const char* msg )
+void WebSim::Parser::next_event( std::string msg )
{
- yaml_event_delete( &event );
+ //yaml_event_delete( &event );
+ //cout << "next_event msg " << msg << endl;
+
if (!yaml_parser_parse( &parser, &event))
parse_failed( msg );
-
- return event;
}
-void WebSim::Parser::expect( yaml_event_type_t event_type, const char* msg )
+void WebSim::Parser::expect( yaml_event_type_t expected_type, std::string msg )
{
- //printf( "expecting %s\n", msg );
-
- if( next_event( "" ).type != event_type )
- parse_failed( msg );
+ //cout << "expecting " << msg << endl;
+
+ next_event( "IN EXPECTING" );
+ if( event.type != expected_type )
+ {
+ printf( "Expecting type %d, got type %d\n",
+ expected_type, event.type );
+ parse_failed( msg );
+ }
}
-WebSim::Confederate* WebSim::Parser::GetConfederate( const char* host,
unsigned short port )
+WebSim::Confederate* WebSim::Parser::GetConfederate( std::string host,
unsigned short port )
{
char lookup[256];
- snprintf( lookup, 256, "%s:%u", host, port );
+ snprintf( lookup, 256, "%s:%u", host.c_str(), port );
assert( ws );
assert( ws->confederates );
@@ -87,13 +92,13 @@
}
-WebSim::Confederate* WebSim::Parser::GetConfederate( const char* hostandport )
+WebSim::Confederate* WebSim::Parser::GetConfederate( std::string hostandport )
{
unsigned int port = ws->port;
char hostnoport[256];
// parse out port number from hostname
- sscanf( hostandport, "%s %u", hostnoport, &port );
+ sscanf( hostandport.c_str(), "%s %u", hostnoport, &port );
return GetConfederate( hostnoport, port );
}
@@ -101,10 +106,10 @@
void WebSim::Parser::parse_puppet_mapping( Puppet* pup )
{
expect( YAML_SCALAR_EVENT, "puppet host" );
- const char* host = strdup((const char*)event.data.scalar.value);
+ std::string host = (const char*)event.data.scalar.value;
expect( YAML_SCALAR_EVENT, "puppet type" );
- const char* prototype = strdup((const char*)event.data.scalar.value);
+ std::string prototype = (const char*)event.data.scalar.value;
expect( YAML_MAPPING_END_EVENT, "puppet mapping" );
@@ -138,11 +143,11 @@
void WebSim::Parser::parse_model_mapping( string host )
{
expect( YAML_SCALAR_EVENT, "model name" );
- string model = (char*)event.data.scalar.value;
+ string model = (const char*)event.data.scalar.value;
Puppet* pup = NULL;
- cout << "host " << host << " hostportname " << ws->hostportname << endl;
+ // cout << "host " << host << " hostportname " << ws->hostportname << endl;
if( host == ws->hostportname )
pup = new Puppet( ws, model.c_str() );
@@ -171,26 +176,77 @@
}
}
+
+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" );
+ }
+ }
+}
+
void WebSim::Parser::parse_host_mapping()
{
- expect( YAML_MAPPING_START_EVENT, "host mapping start" );
+ expect( YAML_MAPPING_START_EVENT, "host sequence start" );
while( 1 )
{
- next_event( "" );
+ next_event( "start of host sequence" );
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 );
+ 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_MAPPING_END_EVENT:
+ case YAML_SEQUENCE_END_EVENT:
return;
default:
parse_failed( "expecting a host scalar or a end of
mapping" );
@@ -198,47 +254,26 @@
}
}
-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 ) :
+WebSim::Parser::Parser( WebSim* ws, std::string 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. */
+ /* Create the Parser object. */
yaml_parser_initialize(&parser);
/* Set a file input. */
- FILE *input = fopen( filename, "rb");
+ FILE *input = fopen( filename.c_str(), "rb");
assert( input );
yaml_parser_set_input_file(&parser, input);
- parse_stream();
+ 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);
Modified: code/websim/src/puppet.cc
===================================================================
--- code/websim/src/puppet.cc 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/src/puppet.cc 2009-03-10 19:02:49 UTC (rev 7455)
@@ -25,19 +25,20 @@
* SVN: $Id: gazebo.h 7398 2009-03-09 07:21:49Z natepak $
*/
-#include <string.h> //for strdup()
+//#include <string.h> //for strdup()
#include <stdio.h>
#include "websim.hh"
using namespace websim;
-WebSim::Puppet::Puppet( WebSim* ws, const char* name ) :
+WebSim::Puppet::Puppet( WebSim* ws, std::string name ) :
ws( ws ),
- name( strdup(name) ),
+ name( name ),
created( false )
{
- g_hash_table_insert( ws->puppets, (void*)this->name, this );
+ g_hash_table_insert( ws->puppets, (void*)this->name.c_str(), this );
+ printf( "Puppet \"%s\" constructed\n", this->name.c_str() );
}
void WebSim::Puppet::Push( Pose p, Velocity v, Acceleration a )
@@ -52,7 +53,7 @@
}
void WebSim::Puppet::AddConfederate( Confederate* conf,
-
const char* prototype )
+
std::string prototype )
{
conf->AddPuppet( this, prototype );
confederates = g_list_append( confederates, conf );
Modified: code/websim/src/websim.cc
===================================================================
--- code/websim/src/websim.cc 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/src/websim.cc 2009-03-10 19:02:49 UTC (rev 7455)
@@ -46,18 +46,23 @@
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 );
+ hostportname = buf;
+
+ printf( "[websim] %s %s ID \"%s\"\n",
+ package.c_str(),
+ version.c_str(),
+ hostportname.c_str() );
+
// Set up the HTTP server
// Not sure whether it's safe to do this more that once in one process
event_init();
- printf( "%s %s\n", package.c_str(), version.c_str() );
-
- char buf[512];
- snprintf( buf, 512, "%s:%u", host.c_str(), port );
- hostportname = buf;
-
printf("[websim] Starting HTTP server listening on %s:%d...",
this->host.c_str(), this->port);
fflush(stdout);
@@ -83,16 +88,26 @@
// No event_fini() to call...
}
-void WebSim::Confederate::TickCb( const char* name, Confederate* conf, void*
arg )
+void WebSim::Confederate::TickCb( std::string name, Confederate* conf, void*
arg )
{
- conf->Tick();
+ 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() );
}
void
WebSim::Update()
{
// tick all my confederates
- ForEachConfederate( Confederate::TickCb, NULL );
+ ForEachConfederate( Confederate::TickCb, this );
tick_count = 0;
do
@@ -414,14 +429,14 @@
return true;
}
-WebSim::Puppet* WebSim::GetPuppet( const char* name )
+WebSim::Puppet* WebSim::GetPuppet( std::string name )
{
- return( (Puppet*)g_hash_table_lookup( WebSim::puppets, name ) );
+ return( (Puppet*)g_hash_table_lookup( WebSim::puppets, name.c_str() ) );
}
-WebSim::Confederate* WebSim::GetConfederate( const char* name )
+WebSim::Confederate* WebSim::GetConfederate( std::string name )
{
- return( (Confederate*)g_hash_table_lookup( WebSim::confederates, name ) );
+ return( (Confederate*)g_hash_table_lookup( WebSim::confederates,
name.c_str() ) );
}
bool
@@ -443,14 +458,14 @@
return false;
}
- void WebSim::ForEachConfederate( void(*cb)(const char*,
WebSim::Confederate*, void*), void* arg )
+ void WebSim::ForEachConfederate( void(*cb)(std::string,
WebSim::Confederate*, void*), void* arg )
{
g_hash_table_foreach( WebSim::confederates,
(GHFunc)cb,
arg );
}
-void WebSim::ForEachPuppet( void(*cb)(const char*, WebSim::Puppet*, void*),
void* arg )
+void WebSim::ForEachPuppet( void(*cb)(std::string, WebSim::Puppet*, void*),
void* arg )
{
g_hash_table_foreach( WebSim::puppets,
(GHFunc)cb,
Modified: code/websim/src/websim.hh
===================================================================
--- code/websim/src/websim.hh 2009-03-10 18:50:16 UTC (rev 7454)
+++ code/websim/src/websim.hh 2009-03-10 19:02:49 UTC (rev 7455)
@@ -139,11 +139,11 @@
WebSim* ws;
public:
- Puppet( WebSim* ws, const char* name );
+ Puppet( WebSim* ws, std::string name );
// unique ID used as a system-widel handle for this puppet, used as
// hash table key
- const char* name;
+ std::string name;
bool created;
@@ -157,7 +157,7 @@
// 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 );
+ std::string
prototype );
};
// manage a federated simulator
@@ -165,7 +165,7 @@
{
typedef struct
{
- const char* name;
+ std::string name;
Confederate* conf;
} cb_chunk_t;
@@ -180,15 +180,15 @@
WebSim* ws;
public:
- Confederate( WebSim* ws, const char* host, unsigned short port );
+ Confederate( WebSim* ws, std::string host, unsigned short port );
~Confederate();
void AddPuppet( Puppet* puppet,
- const char* prototype );
+ std::string prototype );
- int Push( const char* name, Pose p, Velocity v, Acceleration a );
+ int Push( std::string name, Pose p, Velocity v, Acceleration a );
- static void TickCb( const char* name, Confederate* conf, void* arg );
+ static void TickCb( std::string name, Confederate* conf, void* arg );
static void TickReplyCb( evhttp_request* req, void* arg );
@@ -201,7 +201,7 @@
// unique ID for this confederate in format "hostname:port", used as
// hash table key
- const char* name;
+ std::string name;
};
class Parser
@@ -211,22 +211,22 @@
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_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_document();
- void parse_stream();
+ void parse_host_mapping();
+ void parse_logical_host();
+ void parse_logical_host_sequence();
+
public:
- Parser( WebSim* ws, const char* filename );
+ Parser( WebSim* ws, std::string filename );
};
private:
@@ -236,21 +236,22 @@
GHashTable* puppets;
GHashTable* confederates;
+ GHashTable* logical_hosts;
// counts the number of status messages sent and replies not yet received
unsigned int unacknowledged_pushes;
/** Get a puppet by name */
- Puppet* GetPuppet( const char* name );
+ Puppet* GetPuppet( std::string name );
/** Get a confederate by name in format "host:port" */
- Confederate* GetConfederate( const char* name );
+ Confederate* GetConfederate( std::string name );
/** For each confederate, call the callback function */
- void ForEachConfederate( void(*cb)(const char*, Confederate*, void*), void*
arg );
+ void ForEachConfederate( void(*cb)(std::string, Confederate*, void*), void*
arg );
/** For each puppet, call the callback function */
- void ForEachPuppet( void(*cb)(const char*, Puppet*, void*), void* arg );
+ void ForEachPuppet( void(*cb)(std::string, Puppet*, void*), void* arg );
};
class Pose
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