Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Examples

TestClient2.cc

#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;

  // initialisation
  if (argc == 1) { // default
    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;
  }


  //! Pointer on the ORB
  CORBA::ORB_ptr _orbp;
  PortableServer::POA_var _poa;
  PortableServer::POAManager_var  _poa_man;

  // start corba server
  _orbp = CORBA::ORB_init(argc, argv);
    
  // Obtain a reference of the root POA
  CORBA::Object_var objref = _orbp->resolve_initial_references("RootPOA");
  _poa = PortableServer::POA::_narrow(objref);

  // Get and  Activate the POA Manager
  _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;
  // create local nodes
  ColCOWS::Node ** localNodes = (ColCOWS::Node **) malloc(psize * sizeof(ColCOWS::Node *));
  //  ColCOWS::Node * localNodes[psize];

  for(int i = 0; i < psize ; i++) { // for each node
    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;

  // add ref on local nodes
  for(int i = 0; i < psize ; i++) { // for each node
    Echo_impl * echo = new Echo_impl(i);
    localNodes[i]->addReference("echo", "echo-echo", echo->_this());
  }

  cout << endl << "**************** ACTIVATION ****************" << endl;

  // activation
  for(int i = 0; i < psize ; i++) { // for each node
    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;
  // deactivation
  for(int i = 0; i < psize ; i++) { // for each node
    cerr << "deactivate " << i << endl;
    localNodes[i]->deactivate();
  }

  localNodes[psize-1]->waitDeactivation();


  // deactivate POA
  _poa_man->deactivate(true,true);

  // shutdown ORB
  if(!CORBA::is_nil(_orbp))
    _orbp->shutdown(1); // do not wait for completion

  // goodbye !  
  cerr << "return OK" << endl;
  return 0;
}

// EOF


Generated on Tue Jun 21 12:55:55 2005 for ColCOWS by  doxygen 1.4.1