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

TestServer.cc

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

  // initialisation
  if (argc == 1) { // default
    psize = 1;
  }  
  else if(argc >= 2) {
    psize = atoi(argv[1]);
  }
  else {
    cerr << "usage: [np] [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 COLLECTIVE NODE ****************" << endl;
  // build a collective node
  ColCOWS::Node * collectiveNode = ColCOWS::Node::NewProxy("A", 
                                                           "TestServer", 
                                                           psize,
                                                           _orbp);

  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
    localNodes[i] = ColCOWS::Node::NewPort("A", 
                                           "TestServer", 
                                           i, 
                                           psize,
                                           _orbp);
  }

  cout << endl << "**************** ACTIVATION ****************" << endl;
  // activation
  for(int i = 0; i < psize ; i++) { // for each node
    localNodes[i]->activate();
  }
  collectiveNode->activate();
  collectiveNode->waitActivation();

  cout << "Workspace activated : " << endl << *(collectiveNode->getLocalWorkspace()) << endl;

  cout << endl << endl << "**************** CONNECTION ****************" << endl << endl;

  //  collectiveNode->waitConnection("B","TestClient");

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

  for(int i = 0; i < psize ; i++) { // for each node
    delete localNodes[i];
  }
  delete collectiveNode;


  // 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:56 2005 for ColCOWS by  doxygen 1.4.1