#include <unistd.h>
#include <iostream>
#include <cassert>
#include "ColCOWS.hh"
#include "echo.hh"
#ifdef ORBACUS_ORB
#include "echo_skel.hh"
#endif
using namespace std;
void * orb_run( void * arg )
{
((CORBA::ORB_ptr)arg)->run();
return NULL;
}
int main(int argc, char** argv) {
int psize;
char c;
if (argc == 1) {
psize = 1;
}
else if(argc >= 2) {
psize = atoi(argv[1]);
}
else {
cerr << "usage: [np] [ORB options]" << endl;
return 0;
}
CORBA::ORB_ptr _orbp;
PortableServer::POA_var _poa;
PortableServer::POAManager_var _poa_man;
_orbp = CORBA::ORB_init(argc, argv);
CORBA::Object_var objref = _orbp->resolve_initial_references("RootPOA");
_poa = PortableServer::POA::_narrow(objref);
_poa_man = _poa->the_POAManager( );
_poa_man->activate( );
pthread_t orb_thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create( &orb_thread, &attr, orb_run, (void *)_orbp);
pthread_attr_destroy(&attr);
cout << endl << "**************** CREATE COLLECTIVE NODE ****************" << endl;
ColCOWS::Node * collectiveNode = ColCOWS::Node::NewProxy("A",
"TestServer",
psize,
_orbp);
cout << endl << "**************** CREATE LOCAL NODES ****************" << endl;
ColCOWS::Node ** localNodes = (ColCOWS::Node **) malloc(psize * sizeof(ColCOWS::Node *));
for(int i = 0; i < psize ; i++) {
localNodes[i] = ColCOWS::Node::NewPort("A",
"TestServer",
i,
psize,
_orbp);
}
cout << endl << "**************** ACTIVATION ****************" << endl;
for(int i = 0; i < psize ; i++) {
localNodes[i]->activate();
}
collectiveNode->activate();
collectiveNode->waitActivation();
cout << "Workspace activated : " << endl << *(collectiveNode->getLocalWorkspace()) << endl;
cout << endl << endl << "**************** CONNECTION ****************" << endl << endl;
cout << endl << "To call 'echo' press any key (x - exit) > " << flush; c = getchar();
while (c != 'x') {
if (c == 'n') {
cout << endl << endl << "**************** EXPERIMENT 1 ****************" << endl << endl;
ColCOWS::RemoteWorkspaceManager * ws_manager = collectiveNode->getRemoteWorkspaceManager();
cout << "Got " << ws_manager->size() << " num connection ..." << endl;
ColCOWS::RemoteWorkspaceManager::iterator wsm_it, wsm_it_begin, wsm_it_end;
wsm_it_begin = ws_manager->begin();
wsm_it_end = ws_manager->end();
for (wsm_it = wsm_it_begin; wsm_it != wsm_it_end; wsm_it++ )
{
cout << "for connection " << wsm_it->first << endl;
ColCOWS::WorkspaceDesc * ws_desc = wsm_it->second;
ColCOWS::WorkspaceDesc::iterator ws_it, ws_it_begin, ws_it_end;
ws_it_begin = ws_desc->begin();
ws_it_end = ws_desc->end();
for (ws_it = ws_it_begin; ws_it != ws_it_end; ws_it++ )
{
cout << "\t for node " << ws_it->getNumNode() << endl;
if ( ws_it->find("echo") != ws_it->end())
{
ColCOWS::ObjectDesc & obj_desc = ws_it->find("echo")->second;
Echo_var echo = Echo::_narrow( obj_desc.obj_ref );
cout << "( " << obj_desc.obj_id << ":" << obj_desc.obj_kind
<< " ) -> echo() = " << echo->echoString("new coucou !!") << endl;
}
}
}
}
else {
cout << endl << endl << "**************** EXPERIMENT 2 ****************" << endl << endl;
ColCOWS::RemoteWorkspaceManager * ws_manager = collectiveNode->getRemoteWorkspaceManager();
cout << "Got " << ws_manager->size() << " num connection ..." << endl;
ColCOWS::RemoteWorkspaceManager::iterator wsm_it, wsm_it_begin, wsm_it_end;
wsm_it_begin = ws_manager->begin();
wsm_it_end = ws_manager->end();
for (wsm_it = wsm_it_begin; wsm_it != wsm_it_end; wsm_it++ )
{
cout << "for connection " << wsm_it->first << endl;
ColCOWS::WorkspaceDesc * ws_desc = wsm_it->second;
vector< ColCOWS::ExtObjectDesc > ref_vec;
vector< ColCOWS::ExtObjectDesc >::iterator ref_it;
ws_desc->getObjectDescVec(ref_vec, "" , "echo", "echo-echo");
cout << "Got " << ref_vec.size() << " references ..." << endl;
for (ref_it = ref_vec.begin(); ref_it != ref_vec.end(); ++ref_it ){
cout << "( " << ref_it->num_node << " " << ref_it->obj_id << ":" << ref_it->obj_kind << " ) " << flush;
if (! CORBA::is_nil(ref_it->obj_ref)) {
Echo_var echo = Echo::_narrow(ref_it->obj_ref);
cout << "-> echo() = " << flush ;
cout << echo->echoString("new coucou !!") << endl;
}
else {
cout << "PROBLEM !!!" << endl;
}
}
}
}
cout << endl << "(x - exit) > " << flush; cin >> c;
}
cout << endl << "**************** DISCONNECTION ****************" << endl;
collectiveNode->disconnectAll();
cout << endl << "**************** DEACTIVATION ****************" << endl;
for(int i = 0; i < psize ; i++) {
localNodes[i]->deactivate();
}
collectiveNode->deactivate();
collectiveNode->waitDeactivation();
for(int i = 0; i < psize ; i++) {
delete localNodes[i];
}
delete collectiveNode;
_poa_man->deactivate(true,true);
if(!CORBA::is_nil(_orbp))
_orbp->shutdown(1);
cerr << "return OK" << endl;
return 0;
}