corba programming: presentation of a simple “hello world” corba client and server application

13
Slide 1 CORBA Programming: Presentation of a simple “Hello World” CORBA client and server application ITONK1

Upload: tatyana-blanchard

Post on 30-Dec-2015

30 views

Category:

Documents


0 download

DESCRIPTION

ITONK1. CORBA Programming: Presentation of a simple “Hello World” CORBA client and server application. Outline. CORBA programming Code examples We will make a very small CORBA application with a Java server, Java Client & C++ client application In Java (SUN ORB) What gets generated? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 1

CORBA Programming:Presentation of a simple

“Hello World”CORBA client and server application

ITONK1

Page 2: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 2

Outline• CORBA programming

• Code examples• We will make a very small CORBA application with a

Java server, Java Client & C++ client application• In Java (SUN ORB)

• What gets generated?• What files do we need to write (client + server)?

• In Orbacus C++ ORB• We make a client

Only small steps in CORBA programming – much complexity

Studets are not required to be able to use CORBA for development

Page 3: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 3

“Hello World” CORBA Example with file IOR

Clientapp.

Serverapp.

Development PC

CORBAJava / C++

Hello WorldClient

CORBAJava

Hello WorldServer

TCP/IPNetwor

k

TCP/IPNetwor

k

User activa-

tes client

Server returns“Hello World !“

Page 4: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 4

Who’s doing what?

• Some code will get generated by the IDL compiler• Some code we will need to implement ourselves• Staring with the IDL file

Page 5: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 5

IDL Interface of Hello Servant

module HelloApp

interface Hello{ string sayHello();};

Page 6: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 6

IDL Compiler Example Java

Hello.idlfile

Java IDLCompiler -

IDLJ

Hello.java (Both Client & Server)

contains the Java version of the IDL interface.

HelloOperations.java

contains the methods – here only sayHello(). All the operations in the IDL interface are placed in the operations file.

_HelloStub.java

is the client stub.

HelloPOA.java

is the skeleton class you should extend from. It implements dynamic invocation functions.

HelloHelper.java (Both Client & Server)

provides auxiliary functionality, notably the narrow() method required to cast CORBA object references to their proper types.

HelloHolder.java

Whenever the IDL type is an out or an inout parameter, the Holder class is used.

Generates

Input

What gets generated

by the IDL Compiler

Page 7: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 7

// HelloServer.java, stringified object reference version

// Stefan Wagner, 2003

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

import org.omg.PortableServer.*;

import org.omg.PortableServer.POA;

import HelloApp.*;

//This is the servant - implementing the methods from the IDL

class HelloServant extends HelloPOA

{

private ORB orb;

public HelloServant(ORB orb) {

this.orb = orb;

}

public String sayHello()

{

return "\nHello world !!\n";

}

}

Constructor taking ORB as a parameter (from HelloPOA)Constructor taking ORB as a parameter (from HelloPOA)

HelloServant The server object(Part 1)

The CORBA operation implementedThe CORBA operation implemented

By extending from HelloPOA we may communicate with ORB By extending from HelloPOA we may communicate with ORB

Implemented manually

Page 8: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 8

//This is the HelloServer - the server running the HelloServant - Servant

public class HelloServer {

public static void main(String args[])

{

try{ // create and initialize the ORB

org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

// create servant and register it with the ORB

HelloServant helloRef = new HelloServant(orb);

// get reference to rootpoa and activate the POAManager

POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

rootpoa.the_POAManager().activate();

// get object reference from the servant

org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloRef);

Hello href = HelloHelper.narrow(ref);

// stringify the helloRef and dump it in a file

String oir = orb.object_to_string(href);

java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileOutputStream("object.ref"));

out.println(oir);

out.close();

// wait for invocations from clients

orb.run();

} catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } }

}

HelloServant The server object(Part 2)

Init ORB and register servant with ORBInit ORB and register servant with ORB

Start the orb server processStart the orb server process

The POA produces the referenceThe POA produces the reference

Narrow the call (CORBA type cast + IDL type check)Narrow the call (CORBA type cast + IDL type check)

Object reference ”stringified” and

Sent to file object.ref

Object reference ”stringified” and

Sent to file object.ref

Activate rootPOAActivate rootPOA

Implemented manually

Page 9: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 9

// HelloClientSOR.java, stringified object reference version

import java.io.*;

import org.omg.CORBA.*;

import HelloApp.HelloHelper;

import HelloApp.*;

public class HelloClientSOR

{

public static void main(String args[])

{

try {

// create and initialize the ORB

org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

// Get the stringified object reference and destringify it.

java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader("object.ref"));

String ref = in.readLine();

org.omg.CORBA.Object obj = orb.string_to_object(ref) ;

Hello helloRef = HelloHelper.narrow(obj);

// 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); }

}

}

HelloClientSORThe Client program

Init ORBInit ORB

Narrow the call (CORBA type cast + IDL type check)Narrow the call (CORBA type cast + IDL type check)

Object reference Read from file

Object reference Read from file

Call via ProxyCall via Proxy

Implemented manually

Discuss with your neighbor 2 min. what happens after sayHello()

Page 10: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 10

What is this object.ref file?• IOR: Interoperable Object Reference

• Includes info on: Repository ID (standard), Endpoint Info (standard) - including IP and port number, Object Key (proprietary)

• Can be written into a file• Not really nice with a file-based reference – or

what?• May employ a naming service instead

• This we shall look at later• File-based may be necessary due to firewall

problems• Possible to use a HTTP or FTP server for

distributing the references

IOR:000000000000001749444c3a48656c6c6f4170702f48656c6c6f3a312e30000000000001000000000000006c000102000000000e3139322e3136382e312e3130300011b600000021afabcb0000000020a80a250300000001000000000000000000000004000000000a000000000000010000000100000020000000000001000100000002050100010001002000010

1090000000100010100

Page 11: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 11

Parsed IOR

_IIOP_ParseCDR: byte order BigEndian, repository id <IDL:HelloApp/Hello:1.0>, 1 profile _IIOP_ParseCDR: profile 1 is 138 bytes, tag 0 (INTERNET), BigEndian byte order (iiop.c:parse_IIOP_Profile): bo=BigEndian, version=1.2, hostname=172.20.186.138, port=1658, object_key=<....... ..s.................RootPOA..............> (iiop.c:parse_IIOP_Profile): encoded object key is <%AF%AB%CB%00%00%00%00%20%E8%DCs%BE%00%00%00%01%00%00%00%00%00%00%00%01%00%00%00%08RootPOA%00%00%00%00%08%00%00%00%01%00%00%00%00%14> (iiop.c:parse_IIOP_Profile): non-native cinfo is <iiop_1_2_1_%25AF%25AB%25CB%2500%2500%2500%2500%2520%25E8%25DCs%25BE%2500%2500%2500%2501%2500%2500%2500%2500%2500%2500%2500%2501%2500%2500%2500%2508RootPOA%2500%2500%2500%2500%2508%2500%2500%2500%2501%2500%2500%2500%2500%2514@tcp_172.20.186.138_1658> object key is <#AF#AB#CB#00#00#00#00 #E8#DCs#BE#00#00#00#01#00#00#00#00#00#00#00#01#00#00#00#08RootPOA#00#00#00#00#08#00#00#00#01#00#00#00#00#14>; no trustworthy most-specific-type info; unrecognized ORB type; reachable with IIOP 1.2 at host "172.20.186.138", port 1658

http://www2.parc.com/istl/projects/ILU/parseIOR/ For the translation of IORs

Page 12: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 12

#include <OB/CORBA.h>

#include <Hello.h>

#include <fstream.h>

int run(CORBA::ORB_ptr);

int main(int argc, char* argv[])

{ int status = EXIT_SUCCESS;

CORBA::ORB_var orb;

try {

orb = CORBA::ORB_init(argc, argv);

status = run(orb);

} catch (const CORBA::Exception&) {

status = EXIT_FAILURE;

}

if(!CORBA::is_nil(orb))

{ try

{

orb -> destroy();

} catch(const CORBA::Exception&) { status = EXIT_FAILURE; }

}

return status;

}

HelloCorbaC++ ClientPart 1

Init ORBInit ORB

Destroy ORBDestroy ORB

Call run method (see next slide)Call run method (see next slide)

Implemented manually

Page 13: CORBA Programming: Presentation of a simple “Hello World” CORBA client and  server application

Slide 13

int run(CORBA::ORB_ptr orb)

{

const char* refFile = "object.ref";

ifstream in(refFile);

char s[2048];

in >> s;

CORBA::Object_var obj = orb -> string_to_object(s);

HelloApp::Hello_var hello = HelloApp::Hello::_narrow(obj);

cout << hello->sayHello() << endl;

return 0;

}

HelloCorbaC++ ClientPart 2

Narrow the call (CORBA type cast)to the Hello_var smartpointer

(helper + memory management)

Narrow the call (CORBA type cast)to the Hello_var smartpointer

(helper + memory management)

Object reference Read from file

Object reference Read from file

Call method via Proxy and print resultCall method via Proxy and print result

HelloApp::Hello_var smartpointer type

Generated by IDL compiler + Hello

Read more on Smartpointer types in OOMI-1 chapter 4