[omniORB] problems transferring a structure containing a string

Jakob Happach jakob_happach@yahoo.de
Mon Aug 19 09:12:03 2002


 --- Bruce Fountain <fountainb@switch.aust.com>
schrieb: > On Monday 19 August 2002 15:32, Jakob
Happach wrote:
............> 
> How about some code that demonstrates the problem.
> Are
> you initialising the string in the client before
> invoking
> recvdata() ?

Sorry that I forgot to post the client and servant
code.

I will append both src for the client and the server.
I use CORBA::strng_dup() to initialize the
string-member of the struct. 

Servus,
Jakob


-----------------Servant-----------------------
>>
// MiniDebugDatasink.cpp : Defines the entry point for
the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "datasink.h"

//
// Example class implementing IDL interface
idldrei::IDatensenke
//
class idldrei_IDatensenke_i: public
POA_idldrei::IDatensenke,
                public
PortableServer::RefCountServantBase {
private:
  // Make sure all instances are built on the heap by
making the
  // destructor non-public
  //virtual ~idldrei_IDatensenke_i();
public:
  // standard constructor
  idldrei_IDatensenke_i();
  virtual ~idldrei_IDatensenke_i();

  // methods corresponding to defined IDL attributes
and operations
  CORBA::Long recvdata(const idldrei::datentyp& data);
  
};

//
// Example implementational code for IDL interface
idldrei::IDatensenke
//
idldrei_IDatensenke_i::idldrei_IDatensenke_i(){
  // add extra constructor code here
}
idldrei_IDatensenke_i::~idldrei_IDatensenke_i(){
  // add extra destructor code here
}
//   Methods corresponding to IDL attributes and
operations
CORBA::Long idldrei_IDatensenke_i::recvdata(const
idldrei::datentyp& data)
{
	CORBA::Long res = 0;

	printf("recvdata>> %s\n", data.info);

	return res;
}


int main(int argc, char* argv[])
{
	FILE* iorfile;

	try
	{    
		// Initialise the ORB.
		CORBA::ORB_var orb = CORBA::ORB_init(argc, argv,
"omniORB3");
		
		// Obtain a reference to the root POA.
		CORBA::Object_var obj =
orb->resolve_initial_references("RootPOA");
		PortableServer::POA_var poa =
PortableServer::POA::_narrow(obj);
		
		idldrei_IDatensenke_i* myidldrei_IDatensenke_i = new
idldrei_IDatensenke_i();
		
		// Activate the objects.  This tells the POA that
the objects are
		// ready to accept requests.
		PortableServer::ObjectId_var
myidldrei_IDatensenke_iid =
poa->activate_object(myidldrei_IDatensenke_i);
		
		// Obtain a reference to each object and output the
stringified
		// IOR to stdout
		
		// IDL interface: idldrei::IDatensenke
		CORBA::Object_var ref =
myidldrei_IDatensenke_i->_this();
		CORBA::String_var sior(orb->object_to_string(ref));
		
		printf("idldreidatensenke>> %s\n", (char*)sior);
		
		// write the IOR to a file
		iorfile =  fopen("dasi.ior","wb");
		if (iorfile != NULL)
		{
			fprintf(iorfile, "%s", (char*)sior);
			fclose(iorfile);
		}
		

		// Obtain a POAManager, and tell the POA to start
accepting
		// requests on its objects.
		PortableServer::POAManager_var pman =
poa->the_POAManager();
		pman->activate();
		
		orb->run();
		orb->destroy();
	}
	catch(CORBA::COMM_FAILURE& )
	{
		printf("Caught system exception COMM_FAILURE --
unable to contact the object\n");
	}
	catch(CORBA::SystemException&)
	{
		printf("Caught a CORBA::SystemException.\n");
	}
	catch(CORBA::Exception&)
	{
		printf("Caught CORBA::Exception.\n");
	}
	catch(omniORB::fatalException&)
	{
		printf("Caught omniORB::fatalException. \n");
	}
	catch(...)
	{
		printf("Caught unknown exception.\n");
	}
	
	return 0;
}
<<
----------------Client--------------------------
>>
// MiniDdfClient.cpp : Defines the entry point for the
console application.
//

#include "stdafx.h"
#include "windows.h"
#include "memory.h"
#include "winsock2.h"
#include <stdio.h>
#include <stdlib.h>
#include "datasink.h"

int main(int argc, char* argv[])
{
	
	try {
		CORBA::ORB_var orb = CORBA::ORB_init(argc, argv,
"omniORB3");
		
		// here comes the stringified IOR
		FILE* iorfile;
		
		iorfile = fopen("dasi.ior", "r");
		
		if (iorfile != NULL)
		{
			// obtain file size.
			fseek (iorfile , 0 , SEEK_END);
			long iSize = ftell (iorfile);
			rewind (iorfile);
			
			// allocate memory to contain the whole file.
			char* dasisior = (char*) malloc (iSize+1);
			
			memset(dasisior, 0, iSize+1);
			
			int size = fread(dasisior, sizeof(char), iSize,
iorfile);
			
			fclose (iorfile); 
			
			printf("idldreiclient>> %s", dasisior);
			
			long iTest = strlen(dasisior);
			
			CORBA::Object_var dasiobj =
orb->string_to_object(dasisior);
			
			idldrei::IDatensenke_var DaSiRef =
idldrei::IDatensenke::_narrow(dasiobj);
			if( CORBA::is_nil(DaSiRef) )
			{
				printf("Can't narrow reference to type IDatensenke
(or it was nil).\n");
				return 1;
			}
			
			idldrei::datentyp tData;
			tData.info = CORBA::string_dup("983298");

			CORBA::Long res = DaSiRef->recvdata(tData);
			
		}
		
		
		Sleep(1500);
		
		orb->destroy();
		
		printf("Test completed ");
		SYSTEMTIME LocalTime;
		
		GetLocalTime(&LocalTime);
		
		printf("at: %02i:%02i:%02i -- %02i.%02i.%04i\n",
(int)LocalTime.wHour, (int)LocalTime.wMinute,
(int)LocalTime.wSecond,
			(int)LocalTime.wDay, (int)LocalTime.wMonth,
(int)LocalTime.wYear);
		
	}

	catch(CORBA::COMM_FAILURE&) {
		printf("Caught system exception COMM_FAILURE --
unable to contact the object\n");
	}
	catch(CORBA::SystemException&) {
		printf("Caught a CORBA::SystemException.\n");
	}
	catch(CORBA::Exception&) {
		printf("Caught CORBA::Exception.\n");
	}
	catch(omniORB::fatalException&) {
		printf("Caught omniORB::fatalException. \n");
	}
	catch(...) {
		printf("Caught unknown exception.\n");
	}
	
  return 0;
}
<<

__________________________________________________________________

Gesendet von Yahoo! Mail - http://mail.yahoo.de
Möchten Sie mit einem Gruß antworten? http://grusskarten.yahoo.de