1 / 46

Programming with CORBA

Programming with CORBA. Hongtao Shi 04/23/01. Outline. CORBA Overview Advantages of CORBA Interface Definition Language Application: Address Book. CORBA Overview. What is CORBA?. • Acronym for Common Object Request Broker Architecture • Not a language, definition of an

louvain
Download Presentation

Programming with CORBA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with CORBA Hongtao Shi 04/23/01

  2. Outline • CORBA Overview • Advantages of CORBA • Interface Definition Language • Application: Address Book

  3. CORBA Overview What is CORBA? • Acronym for Common Object Request Broker Architecture • Not a language, definition of an application framework

  4. • Platform-independent, language-independent architecture for writing distributed, object- oriented applications • Object Management Group: www.omg.org • More than 800 leading member companies Note: (1)Not a competitor of RMI (2)More powerful framework if combined with RMI

  5. CORBA Framework (Major Parts) • Interface Definition Language (IDL): Programming language independent • Object Request Broker (ORB): Creation and transmission of message, reference to remote object • Internet Inter-ORB Protocol (IIOP): TCP/IP version of generic GIOP for ORB to send message back and forth

  6. Why Distributed Applications? • The data used by the application are distributed. • The computation is distributed. • The users of the application are distributed.

  7. Fundamental Realities of Distributed SystemsCo-located Distributed Communication Fast Slow Failures Objects Objects fail fail together separately Network can partition Concurrent access Only with multiple threads Yes Security Yes No

  8. 2-tier Applications Client Object Most Distributed Object Framework: Request Client Stub Skeleton Object Reply N-tier Applications

  9. Client IDL ORB Stub Interface ORB Object (w/in an application) ORB IDL Interface Skeleton ORB Request and Reply IIOP N-tier Using CORBA as Framework IIOP: Internet Inter-ORB Protocol

  10. Location of Remote CORBA Object • Using an object location service, such as OMG Naming Service • Recreating an object reference from its ‘stringified’ form (ORB’s interface: object_to_string() and string_to_object()) • Receiving an object reference from another CORBA object, referred to as a factory object

  11. Naming Service: Root Context Mary Smith Bill Jones Checking Saving Checking Saving

  12. CORBA ProductsThe CORBA products that support the Java programming language include:The Java 2 ORB: The Java 2 ORB comes with Sun's Java 2 SDK. VisiBroker for Java: A popular Java ORB from Inprise Corporation. VisiBroker is also embedded in other products, e.g., the Netscape Communicator browser. Netscape Communicator: Netscape browsers have a version of VisiBroker embedded in them. Applets can issue request on CORBA objects without downloading ORB classes into the browser. They are already there. OrbixWeb: A popular Java ORB from Iona Technologies.WebSphere: A popular application server with an ORB from IBM.

  13. Advantages of CORBA • Language Independence: OMG has defined mappings for Java, C, C++, SmallTalk, Ada, Lisp, COBOL, Python and IDLscript • Location Transparency: Relocation no impact on the other • Support for Heterogeneous Network: Small, handheld devices to mainframes • Interoperability: Objects within different vendor’s ORBs can communicate.

  14. Interoperability results from two key parts of the specification: (1)OMG Interface Definition Language (OMG IDL), ISO International Standard for several years (2)Standardized protocols GIOP and IIOP

  15. Tools You Can Use • Java 2 SDK: CORBA/IIOP 2.0 compliant product, JavaIDL • SDK 1.3: Bundled with an IDL to Java compiler, idlj • Previous Version: Download idltojava compiler or RMI-IIOP 1.0.1 package which includes the idlj compiler

  16. Related Classes: (1)Test.idl //starting point (2)idlj -fall Test.idl //Two more options: //idlj -fclient Test.idl and //idlj -fserver Test.idl (3)Test.java //empty interface in java //extending TestOperations.java //and org.omg.CORBA.Object (4)TestOperations.java //This interface defines the IDL //to Java mapped operations of //the IDL interface

  17. Related Classes (cntd’)(5)_TestStub.java //the client stub which //implements interface Test(6)_TestImplBase.java //the server skeleton which //implements interface Test(7)TestHelper.java //downcasting object references //to type Test(8)TestHolder.java //a container for streaming //objects of type Test to and //from input and output streams

  18. Interface Definition Language (IDL) IDL Data Types Modules Interfaces Attributes Operations Exceptions Inheritance

  19.  IDL Data Types • integer(unsigned short, long, etc.) • float • char/string • boolean • struct • union • enum • array • sequence

  20. //Example of struct interface Log {struct date {unsigned short month; unsigned short day; unsigned short year; }; struct logMessage {string msg; date msgDate; }; };

  21. //Example of enum enum Color(red, yellow, green, blue, purple); //Example of array interface Log {//…interface same as before typedef logMessage logMessageArray[10]; void submitLogMsg(in logMessage msg); void submitLogMsgs(in out logMessageArray msgArray); };

  22. Note: • An array could be multi-dimensional. • Index starts with zero. • A typedef must be created to identify array type and size. • Sequence can be bounded or unbounded, which is different from arrays.

  23.  Modules IDL interfaces may be defined within modules, providing naming space that contains and isolates any names within it. e.g. module OnlineBroker {interface Account { //... }; }; Note: need scope resolution operator to access the interface from outside the interface

  24.  Interfaces • Define the functionality of an object in a distributed system• Everything defined in an interface must be public.  Attributesmodule OnlineBroker {interface Account { readonly attribute string accountNumber; attribute string accountHolderName; }; //get and set methods connected to the attributes };

  25.  Operationsmodule OnlineBroker {interface Account { readonly attribute string accountNumber; attribute string accountHolderName; boolean buy(in string symbol, inout long quantity); boolean sell(in string symbol, out string cusip); }; }; Note: No overloading is permitted!

  26.  Exceptions module OnlineBroker {interface Account {readonly attribute string accountNumber; attribute string accountHolderName; exception InvalidSymbol() {string symbol; }; string sell(in string symbol) raises (InvalidSymbol); }; };

  27.  Inheritance • IDL interface can inherit from other interfaces. • Implicitly inherit from the Object interface • Multiple inheritance possible, commas delimited module OnlineBroker {// interface Account same as before interface MarginAccount : Account {readonly attribute double margin; void RequestAdditionalMargin(in double additionalAmt); }; };

  28. Application: Address Book • Define the Interface (name=>email, email=>name, register a new user) • Create the CORBA Object and Server • Create the CORBA Client • Run the Server and the Client name email David Reilly dodo@fan.net.au

  29. 1. Interface Definition Language// Address book system module module address_book_system { // Specify interface to our address bookinterface address_book { // Unknown user exceptionexception unknown_user {}; // User already exists exceptionexception user_exists {}; // Lookup name from email addressstring name_from_email(in string email) raises (unknown_user); // Lookup email from full namestring email_from_name(in string name) raises (unknown_user); // Record a new name and emailvoid record_user(in string name, in string email) raises (user_exists); }; };

  30. 2. From IDL to Java: idlj -fall address_book.idlA package contains skeleton source code for your CORBA client and server. A second package covers two exceptions. \address_book_system\ _address_bookStub.java address_book.java address_bookHolder.java address_bookHelper.java address_bookPackage _address_bookImplBase.java address_bookPackage\ unknown_user.java unknown_userHelper.java unknown_userHolder.java user_exists.java user_existsHelper.java user_existsHolder.java

  31. 3. Implementing a CORBA Servantpackage address_book_system; import address_book_system.address_bookPackage.*; import java.util.Hashtable; import java.util.Enumeration; //// AddressBookServant // This servant class is responsible for implementing // three methods //// * String name_from_email ( String email ); // * String email_from_name ( String name ); // * void record_user ( String name, String email );

  32. Implementing a CORBA Servant(cntd’)class AddressBookServant extends_address_bookImplBase{ private Hashtable name2email; public AddressBookServant() // Create a new hashtable to store name & email {name2email = new Hashtable(); } // Get the name of this email userpublic String name_from_email ( String email ) throws unknown_user { if (name2email.contains(email)) { // Begin search for that namefor (Enumeration e = name2email.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); String e_mail = (String) name2email.get(name); // Match on email ?if (email.compareTo(e_mail) == 0) { return name; } } } } // User not found - throw unknown user exceptionthrownew unknown_user(); }

  33. Implementing a CORBA Servant(cntd’)// Get the email of this personpublic String email_from_name ( String name ) throws unknown_user { // If user existsif (name2email.containsKey(name)) { // Return email addressreturn (String) name2email.get(name); } // User doesn't existthrownew unknown_user(); } // Add a new user to the systempublic void record_user ( String name, String email ) throws user_exists { // Is the user already listedif (name2email.containsKey( name ) || name2email.contains( email ) ) { // If so, throw exceptionthrownew user_exists(); } // Add to our hash table name2email.put (name, email); } }

  34. 4. Writing a CORBA Serverpackage address_book_system; import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*;// AddressBookServer// This server is responsible for creating an object // request broker (ORB), and registering an AddressBookServant// with a naming service public class AddressBookServer { public static void main(String args[]) { try { // Create a new object request broker ORB orb = ORB.init(args, null); // Create a new address book ... AddressBookServant servant = new AddressBookServant();

  35. Writing a CORBA Server(cntd’) // ... and connect it to our orb orb.connect(servant); // Object Request Broker Initialised System.out.println ("Address book ORB initialised"); // Obtain reference for our nameservice org.omg.CORBA.Object object = orb.resolve_initial_references("NameService"); // Since we have only an object reference, we must // cast it to a NamingContext. We use a helper // class for this purpose NamingContext namingContext = NamingContextHelper.narrow(object);

  36. Writing a CORBA Server(cntd’)// Add a new naming component for our interface NameComponent list[] = { new NameComponent("address_book", "") }; // Now notify naming service of our new interface namingContext.rebind(list, servant); // Wait for clients for (;;){} } catch (Exception e) { System.err.println ("ORB Error - " + e); e.printStackTrace(System.out); System.exit(0); } } }

  37. 5. Writing a CORBA clientpackage address_book_system; import address_book_system.address_bookPackage.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import java.io.*; // // AddressBookClient // // This client demonstrates the AddressBook server and servant. // A menu is presented to the user, allow he or she to add users,// and look up their names & email addresses.

  38. Writing a CORBA client(cntd’)public class AddressBookClient { public static void main(String args[]) throws IOException { try { // Create an object request broker ORB orb = ORB.init(args, null); // Obtain object reference for name service ... org.omg.CORBA.Object object = orb.resolve_initial_references("NameService"); // ... and narrow it to a NameContext NamingContext namingContext = NamingContextHelper.narrow(object); // Create a name component array NameComponent nc_array[] = { new NameComponent("address_book","") };

  39. Writing a CORBA client(cntd’)// Get an address book object reference ... org.omg.CORBA.Object objectReference = namingContext.resolve(nc_array); // ... and narrow it to get an address book address_book AddressBook = address_bookHelper.narrow(objectReference); // DataInputStream for system.in DataInputStream din = new DataInputStream (System.in);

  40. Writing a CORBA client(cntd’) for (;;) { try { // Print menu System.out.println ("1- Add user"); System.out.println ("2- Look up email"); System.out.println ("3- Look up name"); System.out.println ("4- Exit"); System.out.print ("Command :"); // Read a line from user String command = din.readLine(); // Convert to number Integer num = new Integer (command); int choice = num.intValue(); // Variables we'll need for service callsString name; String email;

  41. Writing a CORBA client(cntd’) switch (choice) {case 1: System.out.print ("Name:"); name = din.readLine(); System.out.print ("Email:"); email = din.readLine();// Call AddressBook service AddressBook.record_user(name,email); break; case 2: System.out.println ("Name:"); name = din.readLine(); // Call AddressBook service email = ddressBook.email_from_name(name); System.out.println ("Email of " + name + " is ” + email); break;

  42. Writing a CORBA client(cntd’) case 3: System.out.println ("Email:"); email = din.readLine(); // Call AddressBook service name = AddressBook.name_from_email(email); System.out.println ("Name of " + email + " is ” + name); break; case 4: System.exit(0); } } catch (user_exists already_there) { System.out.println ("User already exists - cannot be added to address book"); }

  43. Writing a CORBA client(cntd’) catch (unknown_user bad_user) { System.out.println ("User doesn't exist"); } } } catch (Exception e) { System.err.println ("CORBA error - " + e); } } }

  44. Start the server:java address_book_system.AddressBookServer -ORBInitialPort 2000 -ORBInitialHost myhostStart the client:java address_book_system.AddressBookClient -ORBInitialPort 2000 -ORBInitialHost myhost

  45. Running the client:1- Add user2- Look up email3- Look up name4- ExitCommand :1Name: David ReillyEmail: dodo@fan.net.au1- Add user2- Look up email3- Look up name4- ExitCommand :2Name:David ReillyEmail of David Reilly is dodo@fan.net.au

  46. References: • Professional Java Server Programming by Wrox • http://www.omg.org/gettingstarted • http://www.corba.org • http://java.sun.com

More Related