[omniORB] connecting to 2 servers from 1 client

Urs Rohrer Urs.Rohrer@psi.ch
Wed, 28 Jul 1999 17:38:26 +0200


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.  Contact your
mail administrator for information about upgrading your reader to a version
that supports MIME.
--mw0000000138555F18379F23F211FFFC1F00000084Dmw
Content-Type: text/plain;
	charset = ISO-8859-1;
	Type = Text;
	x-dmw-oid = 2A864886F70F0301;
	x-dmw-btname = "PlainText"
Content-Transfer-Encoding: 7bit

Hello there,

I am using omniORB2 for connecting to a remote Server running on a PC connected to some CAMAC hardware
which I want to control from a client PC in my office. Both PC's OS is Windows 98. Connection is done via the
COS Naming Service. Everything works fine. Now I want to expand my client software so I can connect at the
same time to 2 remote CAMAC servers. I tried it with the same code I am using for connecting to only 1 server
(see below). But I am only getting connected to the first server. I am calling InitializeCORBA twice with the 2
different server names (but same ports) and I am getting back 2 different CamCoStr_var for performing the intended CORBAsend's to the 2 different servers. The response comes only from the server initialized first.

Anybody around telling me what I am doing wrong ?

CODE-SAMPLE:

// IDL. In file CamCoStr.idl

// This is a very simple idl file.  It defines a single interface, called
// CamCoStr, and CamCoStr contains a single function, which takes a
// single in paramter of type string, which is mapped in C++ to a const
// char * (see file CamCoStr.h, for the cacoStr declaration and file 
// CamCoStr.c for the implementation).

interface CamCoStr
{
   string cacoStr(in string inStr);
};



#include <windows.h>
#include <string.h>
#include "CamCoStr.hh"

extern "C"
{
   char MBtitle[] = "CAMAC Set Point via CORBA";
}

////////////////////////////////////////////////////////////////////////////////

BOOL SetRegKeyValueEx(HKEY hKey, LPTSTR szKey,
		      LPTSTR szValueName, DWORD dwType,
		      LPTSTR lpValueData, DWORD dwDataLen)
{
    HKEY hKey2;

    if (RegOpenKeyEx(hKey, szKey, 0, KEY_ALL_ACCESS, &hKey2) == ERROR_SUCCESS)
    {
        if (RegSetValueEx(hKey2, szValueName, 0, dwType, (LPBYTE)lpValueData, dwDataLen) == ERROR_SUCCESS)
           return TRUE;
    }
    return FALSE;
}

////////////////////////////////////////////////////////////////////////////////

static CORBA::Object_ptr getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;
  
  try
  {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:
    rootContext = CosNaming::NamingContext::_narrow(initServ);
    if (CORBA::is_nil(rootContext)) 
    {
        MessageBox(NULL,"Failed to narrow naming context.",MBtitle,MB_OK);
        return CORBA::Object::_nil();
    }
  }
  catch(CORBA::ORB::InvalidName& ex)
  {
     MessageBox(NULL,"Service required is invalid [does not exist].",MBtitle,MB_OK);
     return CORBA::Object::_nil();
  }
  catch(...)
  {
    MessageBox(NULL,">> Connection to server not possible. <<", MBtitle, MB_ICONSTOP|MB_OK);
    return CORBA::Object::_nil();
  }

  // Create a name object, containing the name test/context:
  CosNaming::Name name;
  name.length(2);

  name[0].id   = (const char*) "test";       // string copied
  name[0].kind = (const char*) "my_context"; // string copied
  name[1].id   = (const char*) "Echo";
  name[1].kind = (const char*) "Object";
  // 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.)
  
  CORBA::Object_ptr obj;
  try
  {
    // Resolve the name to an object reference, and assign the reference 
    // returned to a CORBA::Object:
    obj = rootContext->resolve(name);
  }
  catch(CosNaming::NamingContext::NotFound& ex)
  {
      // This exception is thrown if any of the components of the
      // path [contexts or the object] aren't found:
      MessageBox(NULL,"Context not found.",MBtitle,MB_OK);
      return CORBA::Object::_nil();
  }
  catch (CORBA::COMM_FAILURE& ex)
  {
    MessageBox(NULL,
    "Caught system exception COMM_FAILURE, unable to contact the naming service.",
    MBtitle,MB_OK);
    return CORBA::Object::_nil();
  }
  catch(omniORB::fatalException& ex)
  {
    throw;
  }
  catch (...)
  {
    MessageBox(NULL,"Caught a system exception while using the naming service.",MBtitle,MB_OK);
    return CORBA::Object::_nil();
  }
  return obj;
}

////////////////////////////////////////////////////////////////////////////////

int CORBAsend(CamCoStr_var cacostrVar, char *sendbuf, char *recbuf)
{
  CORBA::String_var src  = (char *)"";
  CORBA::String_var dest = (char *)"";
  int retval;

  try
  {
     strcpy(src,(char *)sendbuf);
     dest = cacostrVar->cacoStr(src);        // Call the remote object's pushStr function
     strcpy(recbuf,(char *)dest);
     retval = 1;
  }
  catch( CORBA::COMM_FAILURE & ex)
  {
      if (MessageBox(NULL,"Disconnected from server, shall I try to reconnect ?",
                    MBtitle,MB_ICONQUESTION|MB_YESNO) == IDYES)
      {
	 retval = -1;
      }
      else
	 retval = 0;
  }
  catch( omniORB::fatalException & ex)
  {
     MessageBox(NULL,"Caught omniORB2 fatalException. This is a bug in omniORB",MBtitle,MB_OK);
     retval = 0;
  }
  return retval;
}

////////////////////////////////////////////////////////////////////////////////

int InitializeCORBA(char *server, char *myport, CamCoStr_var *var)
{
   char key[100], name[20], port[20];
   int  argc = 3;			// dummy
   char *argv[] = {"","",""};		// dummy

// getObjectReferece() needs 2 registry entries:

   strcpy(key,"SOFTWARE\\ORL\\omniORB\\2.0");
   strcpy(name,"ORBInitialHost");
   strcpy(port,"ORBInitialPort");
   SetRegKeyValueEx(HKEY_LOCAL_MACHINE,key,name,REG_SZ,server,strlen(server));
   SetRegKeyValueEx(HKEY_LOCAL_MACHINE,key,port,REG_SZ,myport,strlen(myport));

// initialize CORBA object:

   CORBA::ORB_ptr orb           = CORBA::ORB_init(argc,argv,"omniORB2");
   CORBA::BOA_ptr boa           = orb->BOA_init(argc,argv,"omniORB2_BOA");
   CORBA::Object_var obj        = getObjectReference(orb);
   if (obj == CORBA::Object::_nil())
      return 0;
   CamCoStr_var cacostrVar      = CamCoStr::_narrow(obj);
   *var = cacostrVar;
   return 1;
}

--mw0000000138555F18379F23F211FFFC1F00000084Dmw
Content-Type: application/vnd.ms-tnef;
	Type = TNF;
	name = "dmw.tnf";
	x-dmw-oid = 2B0C02877305018230;
	x-dmw-btname = "TNEF"
Content-Transfer-Encoding: base64

eJ8+IiUAAQaQCAAEAAAAAAABAAEAAQeQBgAIAAAA5AQAAAAAAADoAAEIgAcAGAAAAElQTS5N
aWNyb3NvZnQgTWFpbC5Ob3RlADEIASCAAwAOAAAAzwcHABwAEAA6ACsAAwBxAQEFgAMADgAA
AM8HBwAcABEAJwAyAAMAZgEBA5AGALQBAAARAAAAQAAHMCBESa8J2b4BQAAIMCBESa8J2b4B
AwD3DwEAAAADAPQPAwAAAAMA/g8FAAAAAgEJDgEAAABMAAAAAAAAAHJvaHJlcgAAAAAAAAAA
AAAQAAEAAgAAAG8BAAAAAAAAIGj1AQYAAAByb2hyZXIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAsAIwAAAI0ACwApAAAAenwDADYAAAAAAAIBCg4BAAAATAAAAAAAAAByb2hyZXIAAAAA
AAAAAAAAEAABAAIAAAACAAAAAAAAACBo9QEDAAAAcm9ocmVyAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAeAHAAAQAAACYAAABjb25uZWN0aW5nIHRvIDIgc2VydmVycyBmcm9tIDEgY2xp
ZW50AAAAAgFxAAEAAAAWAAAAAb7ZD0pkguG9zkUNEdOdHAAAwOa+TgAAQAA5AIAILm4P2b4B
AwAUDgAAAAALABsOAQAAAB4AHQ4BAAAAJgAAAGNvbm5lY3RpbmcgdG8gMiBzZXJ2ZXJzIGZy
b20gMSBjbGllbnQAAAAeAD0AAQAAAAEAAAAAAAAAWUU=
--mw0000000138555F18379F23F211FFFC1F00000084Dmw--