The IDL data type of CORBA defines a series of basic data types, but there may be some problems if an object needs to be transferred. Although the interface in the IDL file is actually a method for passing objects, my understanding is to divide the attributes of the objects to be passed into basic data types and pass them over, however, if the structure of the passed object changes, you also need to modify the IDL file. Therefore, we want to find a method for directly passing objects and study the data types defined by CORBA. The best way is to pass byte arrays like many methods for connecting transmission objects.
Create a directory named Hello (the name can be set at will), and then create two subdirectories in the directory: server and client.
Create a people. Java file in each of the two directories. The content is as follows:
Import java. Io .*;
Class People implements serializable
{
Private string name;
Private int age;
Public people ()
{
Name = "Cy ";
Age = 27;
}
Public people (string STR, int I)
{
Name = STR;
Age = I;
}
Public String getname ()
{
Return name;
}
Public int getage ()
{
Return age;
}
}
Create a hello. IDL file in the server directory with the following content:
Module helloapp
{
Interface hello
{
String sayhello ();
Oneway void Shutdown ();
};
};
Create the helloserver. Java file in the server directory as follows:
// A server for the hello object
Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;
Import org. Omg. portableserver .*;
Import org. Omg. portableserver. PoA;
Import java. util. properties;
Public class helloserver {
Public static void main (string ARGs []) {
Try {
// Create and initialize the orb
Orb orb = orb. INIT (ARGs, null );
// Get reference to rootpoa & activate the poamanager
POA rootpoa =
(PoA) orb. resolve_initial_references ("rootpoa ");
Rootpoa. the_poamanager (). Activate ();
// Create servant and register it with the orb
Helloimpl = new helloimpl ();
Helloimpl. setorb (ORB );
// Get object reference from the servant
Org. Omg. CORBA. Object ref =
Rootpoa. servant_to_reference (helloimpl );
// And cast the reference to a CORBA reference
Hello href = hellohelper. Narrow (REF );
// Get the root naming context
// Nameservice invokes the transient Name Service
Org. Omg. CORBA. Object objref =
Orb. resolve_initial_references ("nameservice ");
// Use namingcontextext, which is part of
// Interoperable Naming Service (INS) specification.
Namingcontextext ncref =
Namingcontextexthelper. Narrow (objref );
// Bind the object reference in naming
String name = "hello1 ";
Namecomponent path [] = ncref. to_name (name );
Ncref. rebind (path, href );
System. Out. println
("Helloserver ready and waiting ...");
// Wait for invocations from clients
Orb. Run ();
}
Catch (exception e ){
System. Err. println ("error:" + E );
E. printstacktrace (system. Out );
}
System. Out. println ("helloserver exiting ...");
} // End main
} // End class
Create the helloimpl. Java file in the server directory. The content is as follows:
// The servant -- Object implementation -- For the hello
// Example. Note that this is a subclass of hellopoa, whose
// Source file is generated from the compilation
// Hello. IDL using j2idl.
Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;
Import org. Omg. portableserver .*;
Import org. Omg. portableserver. PoA;
Import java. util. properties;
Import java. Io .*;
Class helloimpl extends hellopoa // This class must be inherited and is automatically generated in the helloapp directory.
{
Private Orb orb;
Private byte [] bytes;
Public void setorb (ORB orb_val)
{
Orb = orb_val;
}
// Implement sayhello () method
Public byte [] sayhello ()
{
Return (objecttobyte (New People ("Chen Yi", 27 )));
}
// Implement Shutdown () method
Public void Shutdown ()
{
Orb. Shutdown (false );
}
Public byte [] objecttobyte (Java. Lang. Object OBJ)
{
Try {
// Object to bytearray
Bytearrayoutputstream BO = new bytearrayoutputstream ();
Objectoutputstream oo = new objectoutputstream (BO );
OO. writeobject (OBJ );
Bytes = bo. tobytearray ();
Bo. Close ();
OO. Close ();
}
Catch (exception e ){
System. Out. println ("Translation" + E. getmessage ());
E. printstacktrace ();
}
Return (bytes );
}
} // End class
Create a helloclient. Java file in the Client Directory. The content is as follows:
// A sample Java IDL object client application.
Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;
Import java. Io .*;
Public class helloclient
{
Static Hello helloimpl;
String [] x = new string [6];
Static people;
Static java. Lang. Object OBJ;
Public static void main (string ARGs []) {
Try {
// Create and initialize the orb
Orb orb = orb. INIT (ARGs, null );
System. Out. println ("orb initialised/N ");
// Get the root naming context
Org. Omg. CORBA. Object objref =
Orb. resolve_initial_references ("nameservice ");
// Use namingcontextext instead of namingcontext,
// Part of the interoperable Naming Service.
Namingcontextext ncref =
Namingcontextexthelper. Narrow (objref );
// Resolve the object reference in naming
String name = "hello1 ";
Helloimpl =
Hellohelper. Narrow (ncref. resolve_str (name ));
People = (people) (bytetoobject (helloimpl. sayhello ()));
System. Out. println
("Obtained a handle on Server Object :"
+ Helloimpl );
System. Out. println (people. getname ());
Helloimpl. Shutdown ();
}
Catch (exception e ){
System. Out. println ("error:" + E );
E. printstacktrace (system. Out );
}
} // End main
Private Static java. Lang. Object bytetoobject (byte [] bytes ){
Try {
// Bytearray to object
Bytearrayinputstream Bi = new bytearrayinputstream (bytes );
Objectinputstream OI = new objectinputstream (BI );
OBJ = Oi. readobject ();
Bi. Close ();
Oi. Close ();
}
Catch (exception e ){
System. Out. println ("Translation" + E. getmessage ());
E. printstacktrace ();
}
Return OBJ;
}
} // End class
The following describes how to compile the standalone version.
1. Compile the IDL file on the server and run idlj-fall helloapp. IDL on the command line.
2. Compile the. Java file javac *. Java on the server.
3. Copy the directory helloapp generated after compiling the IDL file on the server to the client directory.
4. Compile the. Java file javac *. Java in the client.
5. Run orbd start orbd-orbinitialport 1050
6. Run Server start Java helloserver-orbinitialport 1050-orbinitialhost localhost
7. Run client Java helloclient-orbinitialport 1050-orbinitialhost localhost
The name of the printed person is displayed in the client command line. if you need further testing, you can modify the new people (STR, I) parameter in helloimpl or call other methods of people in the client.
Multi-host running Method
The compilation method is the same as that of the standalone version.
1. Run the orbd start orbd-orbinitialport 1050-orbinitialhost servermachinename command.
2. Run Server Java helloserver-orbinitialport 1050
3. Run client Java helloclient-orbinitialhost nameserverhost-orbinitialport 1050
Note that nameserverhost is the IP address of the server.
In addition, you can modify the parameters of the idlj command.-Fall is the server ing between the server and the client,-fserver is the server ing, and-fclient is the client ing. if the following two parameters are used. the IDL files are placed in two directories respectively.