CORBA development based on the name Service implementation

Source: Internet
Author: User
Tags port number

In the previous article, the implementation of CORBA based on IOR was described, and a common one is based on nameservice implementation; CORBA's object naming service is to give the object instance a name so that the user can get an instance of the object by these names. Object Naming Service is the basic mechanism by which objects on the Orb find other objects. A name is a human-readable value used to identify an object, the naming service maps the names to object tags, and the name-Object association is called a name-link. The naming locale is a namespace, and the object name is unique here. Each object has a unique reference identifier. You can selectively associate one or more names with an object tag.


Directly on the key code:


1. Service server:

public class Server_aom {


Server-initiated code, the main function of this code is to register the interface implementation into the Orb, and start listening, waiting for the client to call
public static void Main (string[] args) throws Servantnotactive,
Wrongpolicy, Invalidname, Adapterinactive,
Org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound,
Cannotproceed {


Specify the port number of the Orb-orbinitialport 1050
args = new String[2];
Args[0] = "-orbinitialport";
ARGS[1] = "1050";


Create an Orb instance
Orb orb = orb.init (args, null);


Get the ROOTPOA reference and activate Poamanager, which is equivalent to starting the server
Org.omg.CORBA.Object obj = orb.resolve_initial_references ("Rootpoa");
POA Rootpoa = Poahelper.narrow (obj);
Rootpoa.the_poamanager (). Activate ();


Create a Helloworldimpl instance
Helloserverimpl Helloimpl = new Helloserverimpl ();


Get a reference to the object from the service and register it in the service
Org.omg.CORBA.Object ref = Rootpoa.servant_to_reference (Helloimpl);
Hello href = hellohelper.narrow (ref);


Gets the context of a root name
Org.omg.CORBA.Object ObjRef = Orb
. Resolve_initial_references ("Nameservice");
Namingcontextext ncref = Namingcontextexthelper.narrow (OBJREF);


Bind this object in a naming context
String name = "Hello";
Namecomponent path[] = ncref.to_name (name);

ncref.rebind (path, href);


SYSTEM.OUT.PRINTLN ("Server startup ...");


Start the thread service, waiting for the client to call
Orb.run ();


}
}



2, Client clients:

public class Helloclient {


Static Hello Helloworldimpl;


static {
SYSTEM.OUT.PRINTLN ("Client starts connecting to server ...");


Initialize IP and port number,-orbinitialhost 127.0.0.1-orbinitialport 1050
String args[] = new STRING[4];
Args[0] = "-orbinitialhost";
The IP address of the server side, as defined in the HelloServer
ARGS[1] = "172.168.xxx.xxx";
ARGS[2] = "-orbinitialport";
Server-side port, defined in the HelloServer
ARGS[3] = "1050";


Create an Orb instance
Orb orb = orb.init (args, null);


Get ROOT Name context
Org.omg.CORBA.Object objRef = null;
try {
ObjRef = orb.resolve_initial_references ("Nameservice");
} catch (Invalidname e) {
E.printstacktrace ();
}
Namingcontextext neref = Namingcontextexthelper.narrow (OBJREF);


Corresponding service-side naming Service
String name = "Hello";
try {
Get the server instantiation of a good implementation class with the Orb
Helloworldimpl = Hellohelper.narrow (neref.resolve_str (name));
} catch (NotFound e) {
E.printstacktrace ();
} catch (Cannotproceed e) {
E.printstacktrace ();
} catch (Org.omg.CosNaming.NamingContextPackage.InvalidName e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Client Connected server ...");
}


public static void Main (String args[]) throws Exception {
SayHello ();
}


To invoke a method of implementing a class
public static void SayHello () {
String str = Helloworldimpl.sayhello ();
System.out.println (str);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.