[ Pobierz całość w formacie PDF ]
.Creating an object-to-string mapping is called binding anobject, and removing the mapping is called unbinding.Getting anobject reference passing a string is called resolving the name.[ Add Comment ]For example, on startup, a serverapplication could create a server object, bind the object into the name service,and then wait for clients to make requests.A client first obtains a serverobject reference, resolving the string name, and then can make calls into theserver using the reference.[ Add Comment ]Again, the Naming Service specificationis part of CORBA, but the application that implements it is provided by the ORBvendor.The way you get access to the Naming Service functionality can vary fromvendor to vendor.[ Add Comment ]An exampleThe code shown here will not be elaboratebecause different ORBs have different ways to access CORBA services, so examplesare vendor specific.(The example below uses JavaIDL, a free product from Sunthat comes with a light-weight ORB, a naming service, and an IDL-to-Javacompiler.) In addition, since Java is young and still evolving, not all CORBAfeatures are present in the various Java/CORBA products.[ Add Comment ]We want to implement a server, running onsome machine, that can be queried for the exact time.We also want to implementa client that asks for the exact time.In this case we’ll be implementingboth programs in Java, but we could also use two different languages (whichoften happens in real situations).[ Add Comment ]Writing the IDL sourceThe first step is to write an IDLdescription of the services provided.This is usually done by the serverprogrammer, who is then free to implement the server in any language in which aCORBA IDL compiler exists.The IDL file is distributed to the client sideprogrammer and becomes the bridge between languages.[ Add Comment ]The example below shows the IDLdescription of our ExactTime server://: c15:corba:ExactTime.idl//# You must install idltojava.exe from//# java.sun.com and adjust the settings to use//# your local C preprocessor in order to compile//# This file.See docs at java.sun.com.module remotetime {interface ExactTime {string getTime();};}; ///:~This is a declaration of theExactTime interface inside the remotetime namespace.The interfaceis made up of one single method that gives back the current time instring format.[ Add Comment ]Creating stubs and skeletonsThe second step is to compile the IDL tocreate the Java stub and skeleton code that we’ll use for implementing theclient and the server.The tool that comes with the JavaIDL product isidltojava:idltojava remotetime.idlThis will automatically generate code forboth the stub and the skeleton.Idltojava generates a Java packagenamed after the IDL module, remotetime, and the generated Java files areput in the remotetime subdirectory._ExactTimeImplBase.java is theskeleton that we’ll use to implement the server object, and_ExactTimeStub.java will be used for the client.There are Javarepresentations of the IDL interface in ExactTime.java and a couple ofother support files used, for example, to facilitate access to the namingservice operations.[ Add Comment ]Implementing the server and the clientBelow you can see the code for the serverside.The server object implementation is in the ExactTimeServer class.The RemoteTimeServer is the application that creates a server object,registers it with the ORB, gives a name to the object reference, and then sitsquietly waiting for client requests.//: c15:corba:RemoteTimeServer.javaimport remotetime.*;import org.omg.CosNaming.*;import org.omg.CosNaming.NamingContextPackage.*;import org.omg.CORBA.*;import java.util.*;import java.text.*;// Server object implementationclass ExactTimeServer extends _ExactTimeImplBase {public String getTime(){return DateFormat.getTimeInstance(DateFormat.FULL).format(new Date(System.currentTimeMillis()));}}// Remote application implementationpublic class RemoteTimeServer {// Throw exceptions to console:public static void main(String[] args)throws Exception {// ORB creation and initialization:ORB orb = ORB.init(args, null);// Create the server object and register it:ExactTimeServer timeServerObjRef =new ExactTimeServer();orb.connect(timeServerObjRef);// Get the root naming context:org.omg.CORBA.Object objRef =orb.resolve_initial_references("NameService");NamingContext ncRef =NamingContextHelper.narrow(objRef);// Assign a string name to the// object reference (binding):NameComponent nc =new NameComponent("ExactTime", "");NameComponent[] path = { nc };ncRef.rebind(path, timeServerObjRef);// Wait for client requests:java.lang.Object sync =new java.lang.Object();synchronized(sync){sync
[ Pobierz całość w formacie PDF ]