Salome HOME
[EDF27816] : Get rid of SALOME session for 3 last tests (YacsLoader, YacsLoader_Swig...
[modules/yacs.git] / src / yacsloader / Test / echoSrv.cxx
index 6212fc3b0a22aba2f88b382ec0b55e4289a6735f..21930e01d5c120b20f41843cd50141cdf23ac3e8 100644 (file)
 #include <echo.hh>
 
 #include <iostream>
+#include <memory>
 
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
 
+#include <SALOMEconfig.h>
+#include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService)
+
 using namespace std;
 
 CORBA::ORB_var orb;
-static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
+static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*);
 
 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
 {
@@ -377,6 +381,15 @@ int main(int argc, char** argv)
   try {
     orb = CORBA::ORB_init(argc, argv);
 
+    std::string theIOR( argv[1] );
+    
+    CORBA::Object_var obj = orb->string_to_object(theIOR.c_str());
+    Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj );
+    if( CORBA::is_nil( ns ) )
+      return 1;
+    //std::cout << getpid() << std::endl;
+    //usleep(20000000);
+
     {
       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
@@ -408,29 +421,28 @@ int main(int argc, char** argv)
       // print the reference as a stringified IOR.
       CORBA::String_var sior(orb->object_to_string(obj2));
       DEBTRACE("'" << (char*)sior << "'");
-
-      if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
+      if( !bindObjectToName(ns, myechoref,"Echo") ) return 1;
 
       //create object C and register it in naming service
       C_i* myC = new C_i();
       CORBA::Object_var obj3 =myC->_this();
       eo::C_var myCref=eo::C::_narrow(obj3);
       myC->_remove_ref();
-      if( !bindObjectToName(orb, myCref,"C") ) return 1;
+      if( !bindObjectToName(ns, myCref,"C") ) return 1;
 
       //create object D and register it in naming service
       D_i* myD = new D_i();
       CORBA::Object_var obj4=myD->_this();
       eo::D_var myDref=eo::D::_narrow(obj4);
       myD->_remove_ref();
-      if( !bindObjectToName(orb, myDref,"D") ) return 1;
+      if( !bindObjectToName(ns, myDref,"D") ) return 1;
 
       //create object Obj and register it in naming service
       Obj_i* myObj = new Obj_i();
       CORBA::Object_var obj5=myObj->_this();
       eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
       myObj->_remove_ref();
-      if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
+      if( !bindObjectToName(ns, myObjref,"Obj") ) return 1;
     }
     orb->run();
     std::cout << "Returned from orb->run()." << std::endl;
@@ -459,90 +471,16 @@ int main(int argc, char** argv)
 //////////////////////////////////////////////////////////////////////
 
 static CORBA::Boolean
-bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
+bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name)
 {
-  CosNaming::NamingContext_var rootContext;
-
-  try {
-    // Obtain a reference to the root context of the Name service:
-    CORBA::Object_var obj;
-    obj = orb->resolve_initial_references("NameService");
-
-    // Narrow the reference returned.
-    rootContext = CosNaming::NamingContext::_narrow(obj);
-    if( CORBA::is_nil(rootContext) ) {
-      DEBTRACE("Failed to narrow the root naming context.");
-      return 0;
-    }
-  }
-  catch(CORBA::ORB::InvalidName& ex) {
-    // This should not happen!
-    DEBTRACE("Service required is invalid [does not exist]." );
-    return 0;
-  }
-
-  try {
-    // Bind a context called "test" to the root context:
-
-    CosNaming::Name contextName;
-    contextName.length(1);
-    contextName[0].id   = (const char*) "test";       // string copied
-    contextName[0].kind = (const char*) "my_context"; // string copied
-    // Note on kind: The kind field is used to indicate the type
-    // of the object. This is to avoid conventions such as that used
-    // by files (name.type -- e.g. test.ps = postscript etc.)
-
-    CosNaming::NamingContext_var testContext;
-    try {
-      // Bind the context to root.
-      testContext = rootContext->bind_new_context(contextName);
-    }
-    catch(CosNaming::NamingContext::AlreadyBound& ex) {
-      // If the context already exists, this exception will be raised.
-      // In this case, just resolve the name and assign testContext
-      // to the object returned:
-      CORBA::Object_var obj;
-      obj = rootContext->resolve(contextName);
-      testContext = CosNaming::NamingContext::_narrow(obj);
-      if( CORBA::is_nil(testContext) ) {
-        DEBTRACE("Failed to narrow naming context.");
-        return 0;
-      }
-    }
-
-    // Bind objref with name Echo to the testContext:
-    CosNaming::Name objectName;
-    objectName.length(1);
-    objectName[0].id   = name;   // string copied
-    objectName[0].kind = (const char*) "Object"; // string copied
-
-    try {
-      testContext->bind(objectName, objref);
-    }
-    catch(CosNaming::NamingContext::AlreadyBound& ex) {
-      testContext->rebind(objectName, objref);
-    }
-    // Note: Using rebind() will overwrite any Object previously bound
-    //       to /test/Echo with obj.
-    //       Alternatively, bind() can be used, which will raise a
-    //       CosNaming::NamingContext::AlreadyBound exception if the name
-    //       supplied is already bound to an object.
-
-    // Amendment: When using OrbixNames, it is necessary to first try bind
-    // and then rebind, as rebind on it's own will throw a NotFoundexception if
-    // the Name has not already been bound. [This is incorrect behaviour -
-    // it should just bind].
-  }
-  catch(CORBA::COMM_FAILURE& ex) {
-    DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
-             << "naming service.");
-    return 0;
-  }
-  catch(CORBA::SystemException&) {
-    DEBTRACE("Caught a CORBA::SystemException while using the naming service.");
-    return 0;
-  }
-
+  CORBA::String_var iorNSUg = orb->object_to_string(objref);
+  std::string iorNS(iorNSUg);
+  Engines::IORType iorInput;
+  auto len = iorNS.length();
+  iorInput.length( len );
+  for(auto i = 0 ; i < len ; ++i)
+    iorInput[i] = iorNS[i];
+  ns->Register(iorInput,name);
   return 1;
 }