[omniORB] Nailing the port number of an orb...

novitk novitk@pobox.com
Tue, 25 Jan 2000 13:52:06 -0500


Michel Vayssade wrote:
> 
> On Mon, 24 Jan 2000, novitk wrote:
> > You can do this by pregenerating object key.
> >    E-mail me for code if you'll have a problem doing this. It's kinda
> >    long(2 functions at 20 lines ) to post here.
> 
> not so big and probably interesting other people on the list
> could you post it to the list ?
> thank you,
> Best regards,
> 

OK,

The function is 

Server_ptr attachServer(CORBA::ORB_ptr orb, char *hostname, int port,
const char *serverKey);

As a server key you pass the string constant defined in an idl file.
which uses another one(profileToEncapStream taking from genior.cc as
is).
Here it goes:

<CODE>
static void profileToEncapStream(IIOP::ProfileBody &p, 
  _CORBA_Unbounded_Sequence_Octet &s)
{
  CORBA::ULong hlen = strlen((const char *)p.host) + 1;
  CORBA::ULong klen = p.object_key.length();
  {
    // calculate the total size of the encapsulated stream
    CORBA::ULong total = 8 + hlen;        // first 4 bytes + aligned
host
    total = ((total + 1) & ~(1)) + 2;     // aligned port value
    total = ((total + 3) & ~(3)) + 4 +	// aligned object key
      klen;
    
    s.length(total);
  }

  s[0] = omni::myByteOrder;
  s[1] = IIOP::current_major;
  s[2] = IIOP::current_minor;
  s[3] = 0;
  {
    CORBA::ULong &l = (CORBA::ULong &) s[4];
    l = hlen;
  }
  memcpy((void *)&(s[8]),(void *)p.host,hlen);
  CORBA::ULong idx = ((8 + hlen) + 1) & ~(1);
  {
    CORBA::UShort &l = (CORBA::UShort &) s[idx];
    l = p.port;
  }
  idx = ((idx + 2) + 3) & ~(3);
  {
    CORBA::ULong &l = (CORBA::ULong &) s [idx];
    l = klen;
  }
  idx += 4;
  memcpy((void *)&s[idx],(void *)&p.object_key[0],klen);
  return;
}

// that's the meat
Server_ptr attachMartServer(CORBA::ORB_ptr orb, char *hostname, int
port, const char *serverKey)
{
  IIOP::ProfileBody profb;
	const char *IRTypeId = "IOR";

  profb.iiop_version.major = IIOP::current_major;
  profb.iiop_version.minor = IIOP::current_minor;
  profb.port = (CORBA::UShort) port;
  profb.host = (CORBA::Char*) strdup(hostname);

  // make an object key
  int l = strlen(serverKey) / 2;
  omniORB::seqOctets keySeed(l);
  keySeed.length(l);
  {
    const char* pKey = serverKey;
    for (int i = 0; i < l; i++)
    {
      int n;
      sscanf(pKey,"%02x",&n);
      keySeed[i] = n;
      pKey += 2;
    }
  }

  profb.object_key = keySeed;
  IOP::TaggedProfile p;
  p.tag = IOP::TAG_INTERNET_IOP;  
  profileToEncapStream(profb,p.profile_data);

  int intfname_len = strlen(IRTypeId)+1;
  CORBA::Char* intfname = new CORBA::Char[intfname_len];
  sprintf((char*) intfname,"%s",IRTypeId);
  intfname[intfname_len-1]='\0';

  IOP::TaggedProfileList* pList = new IOP::TaggedProfileList;
  pList->length(1);
  (*pList)[0] = p;
  CORBA::Char* string_ior = IOP::iorToEncapStr(intfname,pList);
  CORBA::Object_ptr obj = orb->string_to_object((const char
*)string_ior);
  Server_ptr server = Server::_narrow(obj);
  CORBA::release(obj);

  delete pList;  
  delete[] intfname;
  return server;
}
</CODE>