Download CORBA/IDL - University of Memphis

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CORBA/IDL
• Common Object Resource Broker
Architecture (CORBA)
• Interface Definition Language (IDL)
• Object Management Group (OMG)
(http://www.omg.org) Specification
• Java IDL (jdk1.2) use CORBA objects with
Java Programming Language
Introduction
•
•
•
•
•
Technology for Distributed Objects
Similar in function to Java RMI
Not Java-centric
Any language with IDL specification
IDL is a language-neutral interface
definition language
• Object Requst Broker (ORB) enables lowlevel communication between CORBA
Objects
CORBA Architecture
CORBA Architecture
• Client and Server Relationships common to
CORBA and RMI
• Server provides remote interface
• Client calls remote interface
• Object level interaction rather than
application level interaction (sockets)
• Objects can fulfill both roles
Client Side
•
•
•
•
•
Client has reference to remote object
Object reference has a stub method
Stand-in for remote method
Stub wired into ORB
Call on stub invokes ORB’s low-level
communication routines
• ORB forwards invocation to server
Server Side
• ORB on Server Side uses Skeleton Code to
translate remote invocation into call on the
local object
• Skeleton transforms results or errors and
returns to ORB for delivery to client
• ORB-ORB communication with IIOP
(Internet Inter-ORB Protocol)
ORB
• Different Vendors
• IIOP based on TCP/IP by OMG
• ORB Services
–
–
–
–
Look up (JDK1.2)
Object Persistence
Transactions
Messaging
Java IDL Development
• Define interface to Remote Object with IDL
• idltojava compiler generates stub and
skeleton source, and code to interface with
ORB
• IDL interface can be implemented in any
CORBA compliant language (C, C++,
Smalltalk, COBOL, Ada)
• Compile remote interface (idltojava)
• Implement the server. It should start ORB
and wait on invocations from clients, as
well as implement remote methods.
• Implement client. Start ORB, look up
server, obtain remote reference, and call
remote method
• Start applications.
Example
• Hello World has single remote method that
returns a string to be displayed.
• Client invokes sayHello on Hello server
• ORB transfers invocation to servant object
registered for interface
• Servant’s sayHello runs, returns String
• ORB transfers String back to client
• Client Displays String
Hello.idl
module HelloApp
{
interface Hello
{
string sayHello();
};
};
• CORBA module is name space that is a
container for related interfaces and
definitions.
• Like a Java Package
• module statement mapped to java package
statement
• interface specifies contract object has with
other objects
• interface to interface in java
• CORBA operations are behaviors that
server promises to do on client’s behalf
• operation to method in java
• idltojava Hello.idl creates a HelloApp
directory, and five files.
• _HelloImplBase.java: Abstract class is the
server skeloton. It implements Hello.java.
Server class will extend this class.
• _HelloStub.java: Client stub. Implements
Hello.java
• Hello.java: Java version of IDL interface.
Subclasses org.omg.CORBA.Object to
provide base CORBA functionality.
• HelloHelper.java: Provides other
functionality. narrow method cast CORBA
object reference to proper type.
• HelloHolder.java: final class provides out
and inout arguments (more later).
/*
* File:
* From:
* Date:
*
By:
*/
./HelloApp/Hello.java
Hello.idl
Thu Sep 3 09:46:22 1998
idltojava Java IDL 1.2 Nov 12 1997 12:23:47
package HelloApp;
public interface Hello
extends org.omg.CORBA.Object {
String sayHello()
;
}
/*
* File:
* From:
* Date:
*
By:
*/
./HelloApp/_HelloImplBase.java
Hello.idl
Thu Sep 3 09:46:22 1998
idltojava Java IDL 1.2 Nov 12 1997 12:23:47
package HelloApp;
public abstract class _HelloImplBase extends org.omg.CORBA.DynamicImplementation
implements HelloApp.Hello {
// Constructor
public _HelloImplBase() {
super();
}
// Type strings for this class and its superclases
private static final String _type_ids[] = {
"IDL:HelloApp/Hello:1.0"
};
public String[] _ids() { return (String[]) _type_ids.clone(); }
private static java.util.Dictionary _methods = new java.util.Hashtable();
static {
_methods.put("sayHello", new java.lang.Integer(0));
}
// DSI Dispatch call
public void invoke(org.omg.CORBA.ServerRequest r) {
switch (((java.lang.Integer) _methods.get(r.op_name())).intValue()) {
case 0: // HelloApp.Hello.sayHello
{
org.omg.CORBA.NVList _list = _orb().create_list(0);
r.params(_list);
String ___result;
___result = this.sayHello();
org.omg.CORBA.Any __result = _orb().create_any();
__result.insert_string(___result);
r.result(__result);
}
break;
default:
throw new org.omg.CORBA.BAD_OPERATION(0,
org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
}
}
}
/*
* File:
* From:
* Date:
*
By:
*/
./HelloApp/_HelloStub.java
Hello.idl
Thu Sep 3 09:46:22 1998
idltojava Java IDL 1.2 Nov 12 1997 12:23:47
package HelloApp;
public class _HelloStub
extends org.omg.CORBA.portable.ObjectImpl
implements HelloApp.Hello {
public _HelloStub(org.omg.CORBA.portable.Delegate d) {
super();
_set_delegate(d);
}
private static final String _type_ids[] = {
"IDL:HelloApp/Hello:1.0"
};
public String[] _ids() { return (String[]) _type_ids.clone(); }
//
IDL operations
//
Implementation of ::HelloApp::Hello::sayHello
public String sayHello()
{
org.omg.CORBA.Request r = _request("sayHello");
r.set_return_type(org.omg.CORBA.ORB.init().get_primitive_tc(org.omg.CORBA.TCKind
.tk_string));
r.invoke();
String __result;
__result = r.return_value().extract_string();
return __result;
}
};
/*
* File:
* From:
* Date:
*
By:
*/
./HelloApp/HelloHelper.java
Hello.idl
Thu Sep 3 09:46:22 1998
idltojava Java IDL 1.2 Nov 12 1997 12:23:47
package HelloApp;
public class HelloHelper {
// It is useless to have instances of this class
private HelloHelper() { }
public static void write(org.omg.CORBA.portable.OutputStream out,
HelloApp.Hello that) {
out.write_Object(that);
}
public static HelloApp.Hello read(org.omg.CORBA.portable.InputStream in) {
return HelloApp.HelloHelper.narrow(in.read_Object());
}
public static HelloApp.Hello extract(org.omg.CORBA.Any a) {
org.omg.CORBA.portable.InputStream in = a.create_input_stream();
return read(in);
}
public static void insert(org.omg.CORBA.Any a, HelloApp.Hello that) {
org.omg.CORBA.portable.OutputStream out = a.create_output_stream();
write(out, that);
a.read_value(out.create_input_stream(), type());
}
private static org.omg.CORBA.TypeCode _tc;
synchronized public static org.omg.CORBA.TypeCode type() {
if (_tc == null)
_tc = org.omg.CORBA.ORB.init().create_interface_tc(id(), "Hello");
return _tc;
}
public static String id() {
return "IDL:HelloApp/Hello:1.0";
}
public static HelloApp.Hello narrow(org.omg.CORBA.Object that)
throws org.omg.CORBA.BAD_PARAM {
if (that == null)
return null;
if (that instanceof HelloApp.Hello)
return (HelloApp.Hello) that;
if (!that._is_a(id())) {
throw new org.omg.CORBA.BAD_PARAM();
}
org.omg.CORBA.portable.Delegate dup =
((org.omg.CORBA.portable.ObjectImpl)that)._get_delegate();
HelloApp.Hello result = new HelloApp._HelloStub(dup);
return result;
}
}
/*
* File:
* From:
* Date:
*
By:
*/
./HelloApp/HelloHolder.java
Hello.idl
Thu Sep 3 09:46:22 1998
idltojava Java IDL 1.2 Nov 12 1997 12:23:47
package HelloApp;
public final class HelloHolder
implements org.omg.CORBA.portable.Streamable{
//
instance variable
public HelloApp.Hello value;
//
constructors
public HelloHolder() {
this(null);
}
public HelloHolder(HelloApp.Hello __arg) {
value = __arg;
}
public void _write(org.omg.CORBA.portable.OutputStream out) {
HelloApp.HelloHelper.write(out, value);
}
public void _read(org.omg.CORBA.portable.InputStream in) {
value = HelloApp.HelloHelper.read(in);
}
public org.omg.CORBA.TypeCode _type() {
return HelloApp.HelloHelper.type();
}
}
Client
• Needs ORB object to do marshaling and
IIOP
• Instantiate ORB object and pass it
information
• Asks ORB for service it needs
• Gets object reference from COS naming
service provided with Java IDL
• Get reference to naming service
• All CORBA object references returned as
generic CORBA object
• Must narrow it to its proper type
• NameComponent for complex names,
including fully qualified paths
• resolve naming context
• narrow to proper type
• invoke method
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
// The package containing our stubs.
// HelloClient will use the naming service.
// All CORBA applications need these classes.
public class HelloClient
{
public static void main(String args[])
{
try{
// Create and initialize the ORB
ORB orb = ORB.init(args, null);
// Get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Resolve the object reference in naming
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
// Call the Hello server object and print results
String Hello = helloRef.sayHello();
System.out.println(Hello);
} catch(Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
}
}
Server
•
•
•
•
•
Servant object implements the interface
Create ORB
COS Naming: get instance and narrow
Register servant with name server
WAIT
// The package containing our stubs.
import HelloApp.*;
// HelloServer will use the naming service.
import org.omg.CosNaming.*;
// The package containing special exceptions thrown by the name service.
import org.omg.CosNaming.NamingContextPackage.*;
// All CORBA applications need these classes.
import org.omg.CORBA.*;
public class HelloServer
{
public static void main(String args[])
{
try{
// Create and initialize the ORB
ORB orb = ORB.init(args, null);
// Create the servant and register it with the ORB
HelloServant helloRef = new HelloServant();
orb.connect(helloRef);
// Get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Bind the object reference in naming
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
ncRef.rebind(path, helloRef);
// Wait for invocations from clients
java.lang.Object sync = new java.lang.Object();
synchronized(sync){
sync.wait();
}
} catch(Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}
}
class HelloServant extends _HelloImplBase
{
public String sayHello()
{
return "\nHello world!!\n";
}
}
Running the Example
• Compile the files generated by idltojava
• Compile client and server
• start name service:
tnameserv -ORBInitialPort 1050
• start client and server
java HelloServer -ORBInitialPort 1050
java HelloClient -ORBInitialPort 1050