Você está na página 1de 13

In this CORBA Programming we will learn how to create :

Simple Remote Object.

Server to instantiate (create ) and bind a remote object.

Client to invoke remotely an object

You need to install the Java Development Kit ( JDK ) as well as a programming editor ( IDE )
such as Eclipse. For more details of how to install JDK and Eclipse, Use the following tutorial:
Java Programming : Creating a simple java project using Eclipse
The structure of the files for the projects created using Eclipse throughout this tutorials is shown
below:

1. Server Side
1 Lets create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and
call it : CorbaAdditionServer-> Click Finish once done
2 Under the project CorbaAdditionServer, Create a new file called Addition.idl. Copy the code
below into the new file.
1

/**

* @author imed

*/

module AdditionApp

interface Addition

long add(in long a,in long b);

10

oneway void shutdown();

11

};

12 };

3 Open your CMD console, Change directory ( cd ) to src folder within the project location. The
location of your project can be known through clicking: Select the project CorbaAdditionServer,
click : File -> Properties
cd C:\Users\imed\workspace\CorbaAdditionServer\src

4 Compile the idl file using the idlj command as below:


idlj -fall Addition.idl
You will see that inside the src folder, new directory called AdditionApp is created with a
number of Java files inside.

5 Right click on the src folder and select Refresh

6 Under the project CorbaAdditionServer, Create a new Class called AdditionObj. Copy the
code below into the new class.

/**

* @author imed

*/

import AdditionApp.*;

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

import org.omg.PortableServer.*;

10 import org.omg.PortableServer.POA;
11 import java.util.Properties;
12
13 class AdditionObj extends AdditionPOA {
14

private ORB orb;

15
16

public void setORB(ORB orb_val) {

17
18

orb = orb_val;
}

19
20

// implement add() method

21

public int add(int a, int b) {

22

int r=a+b;

23

return r;

24

25
26

// implement shutdown() method

27

public void shutdown() {

28

orb.shutdown(false);

29

30 }

7 Under the project CorbaAdditionServer, Create a new Class called StartServer. Copy the
code below into the new class.
1

/**

* @author imed

*/

import AdditionApp.*;

6
7

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

10 import org.omg.PortableServer.*;
11 import org.omg.PortableServer.POA;
12 import java.util.Properties;
13
14 public class StartServer {
15
16
17
18

19

public static void main(String args[]) {


try{
// create and initialize the ORB //// get reference to rootpoa
& activate the POAManager
ORB orb = ORB.init(args, null);

20

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

21

rootpoa.the_POAManager().activate();

22
23

// create servant and register it with the ORB

24

AdditionObj addobj = new AdditionObj();

25

addobj.setORB(orb);

26
27

// get object reference from the servant

28

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

29

Addition href = AdditionHelper.narrow(ref);

30
31

org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");

32

NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

33
34

NameComponent path[] = ncRef.to_name( "ABC" );

35

ncRef.rebind(path, href);

36
37

System.out.println("Addition Server ready and waiting ...");

38
39

// wait for invocations from clients

40

for (;;){

41

orb.run();

42
43
44

}
}

45

catch (Exception e) {

46

System.err.println("ERROR: " + e);

47

e.printStackTrace(System.out);

48

49
50

System.out.println("HelloServer Exiting ...");

51
52

53 }

8 Open a new windows of CMD console, type in the following command to start the ORB
start orbd -ORBInitialPort 1050
9 Under Eclipse now, click on the project CorbaAdditionServer, then Click Run -> Run
Configuration as shown below:

10 Make sure you select the StartServer for the CorbaAdditionServer.


If it does NOT show up, Double click on Java Application

11 Click on Arguments and type in the following arguments inside the Program arguments.
Click Apply once done:
-ORBInitialPort 1050 -ORBInitialHost localhost

12 Thats all for the server, it should be running.

Client Side.
1 Lets create a new Java Project using Eclipse and call it : CorbaAdditionClient-> Click
Finish once done
2 Right click on AdditionApp Package under CorbaAdditionServer and click Copy

3 Right click on src Package under CorbaAdditionClient and click Paste

4 Under the project CorbaAdditionClient, Create a new Class called StartClient. Copy the
code below into the new class.
1

/**

* @author imed

*/

import AdditionApp.*;

6
7

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

10 import java.io.*;
11 import java.util.*;
12
13 public class StartClient {
14
15

/**

16

* @param args the command line arguments

17

*/

18

public static void main(String[] args) {

19

try {

20

ORB orb = ORB.init(args, null);

21

22
23

org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
Addition addobj = (Addition)
AdditionHelper.narrow(ncRef.resolve_str("ABC"));

24
25

Scanner c=new Scanner(System.in);

26

System.out.println("Welcome to the addition system:");

27

for(;;){

28

System.out.println("Enter a:");

29

String aa = c.nextLine();

30

System.out.println("Enter b:");

31

String bb = c.nextLine();

32

int a=Integer.parseInt(aa);

33

int b=Integer.parseInt(bb);

34

int r=addobj.add(a,b);

35

System.out.println("The result for addition is : "+r);

36

System.out.println("-----------------------------------");

37

38

39

catch (Exception e) {

40
41

System.out.println("Hello Client exception: " + e);


e.printStackTrace();

42

43
44

45
46 }

5 Click on Run -> Run Configuration, Make sure you select the StartClient for the
CorbaAdditionClient.

6 Click on Arguments, then under the Program arguments, type in the following :
-ORBInitialPort 1050 -ORBInitialHost localhost

7 Click Apply, then Run. Thats all for the client, it should be running.