#include <unistd.h>
#include <iostream>
#include <sstream>
#include <cassert>
#include "ColCOWS.hh"
#include <math.h>
#include "echo.hh"
#ifdef ORBACUS_ORB
#include "echo_skel.hh"
#endif
using namespace std;
class Echo_impl : public POA_Echo,
public PortableServer::RefCountServantBase
{
public:
inline Echo_impl(int num_proc) {_counter = 0; _num_proc = num_proc;}
virtual ~Echo_impl() {}
virtual char* echoString(const char* mesg) throw (CORBA::SystemException);
long int _counter;
long int _num_proc;
};
char* Echo_impl::echoString(const char* mesg) throw (CORBA::SystemException)
{
_counter++;
stringstream ss(stringstream::in|stringstream::out);
ss << "(" << _num_proc << ":" << _counter << ") " << mesg ;
cout << "Upcall : " << ss.str() << endl;
return CORBA::string_dup(ss.str().c_str());
}
void * orb_run( void * arg )
{
((CORBA::ORB_ptr)arg)->run();
return NULL;
}
int main(int argc, char** argv) {
int psize;
int base;
char c;
if (argc == 1) {
psize = 2;
base = 2;
}
else if(argc == 2) {
psize = atoi(argv[1]);
base = 2;
}
else if(argc >= 3) {
psize = atoi(argv[1]);
base = atoi(argv[2]);
}
else {
cerr << "usage: [b][n] [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 LOCAL NODES ****************" << endl;
ColCOWS::Node ** localNodes = (ColCOWS::Node **) malloc(psize * sizeof(ColCOWS::Node *));
for(int i = 0; i < psize ; i++) {
cout << "Test calcul father and max descendant :"
<< "psize = " << psize << ", "
<< "i = " << i << ", "
<< "base = " << base << endl;
localNodes[i] = ColCOWS::Node::NewHierachical("B",
"TestClient2",
i,
psize,
base,
_orbp);
}
cout << endl << "**************** ADD REFERENCES ****************" << endl;
for(int i = 0; i < psize ; i++) {
Echo_impl * echo = new Echo_impl(i);
localNodes[i]->addReference("echo", "echo-echo", echo->_this());
}
cout << endl << "**************** ACTIVATION ****************" << endl;
for(int i = 0; i < psize ; i++) {
cerr << "activate " << i << endl;
localNodes[i]->activate();
}
localNodes[psize-1]->waitActivation();
cout << endl << "**************** CONNECTION ****************" << endl;
unsigned long num_connection = localNodes[0]->connect("A", "TestServer");
if ( num_connection != ColCOWS::NOT_CONNECTED ) {
cout << endl << "**************** GET_SIZE ****************" << endl;
cout << " getRemoteNbNodes( num_connection ) = " << localNodes[0]->getRemoteNbNodes( num_connection ) << endl;
}
else {
cout << "Connection failed !!!" << endl;
}
cout << endl << "To exit press any key > " << flush; cin >> c;
cout << endl << "**************** DISCONNECTION ****************" << endl;
localNodes[0]->disconnectAll();
cout << endl << "**************** DEACTIVATION ****************" << endl;
for(int i = 0; i < psize ; i++) {
cerr << "deactivate " << i << endl;
localNodes[i]->deactivate();
}
localNodes[psize-1]->waitDeactivation();
_poa_man->deactivate(true,true);
if(!CORBA::is_nil(_orbp))
_orbp->shutdown(1);
cerr << "return OK" << endl;
return 0;
}