Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. SERVER SIDE
  2.  
  3. 1. Create a new Java Project and call it : CorbaAdditionServer
  4.  
  5. 2. Create an empty file and save it with an *.idl extension and compile using idlj - fall Addition.idl
  6.  
  7. module AdditionApp
  8. {
  9. //do not use an interface name as the name of a module
  10. interface Addition
  11. {
  12. long add(in long a,in long b);
  13. oneway void shutdown();
  14. };
  15. };
  16.  
  17. 3. Generates 6 files automatically
  18.  
  19. POA - the server skeleton
  20. Holder - the instance "holder"
  21. Helper - provides functions for casting references to their types
  22. Stub - the client stub
  23. Operations = the operations specific to the app
  24. Addition.java = the "signature" file implementing the addition contract
  25.  
  26. 4. Create a new class called AdditionObj
  27.  
  28. import AdditionApp.*;
  29. import org.omg.CosNaming.*;
  30. import org.omg.CosNaming.NamingContextPackage.*;
  31. import org.omg.CORBA.*;
  32. import org.omg.PortableServer.*;
  33. import org.omg.PortableServer.POA;
  34. import java.util.Properties;
  35.  
  36. class AdditionObj extends AdditionPOA {
  37. private ORB orb;
  38.  
  39. public void setORB(ORB orb_val) {
  40. orb = orb_val;
  41. }
  42.  
  43. // implement add() method
  44. public int add(int a, int b) {
  45. int r=a+b;
  46. return r;
  47. }
  48.  
  49. // implement shutdown() method
  50. public void shutdown() {
  51. orb.shutdown(false);
  52. }
  53. }
  54.  
  55. 5. Create a Class called StartServer
  56.  
  57. import AdditionApp.*;
  58. import org.omg.CosNaming.*;
  59. import org.omg.CosNaming.NamingContextPackage.*;
  60. import org.omg.CORBA.*;
  61. import org.omg.PortableServer.*;
  62. import org.omg.PortableServer.POA;
  63. import java.util.Properties;
  64.  
  65. public class StartServer {
  66.  
  67. public static void main(String args[]) {
  68. try{
  69. // create and initialize the ORB //// get reference to rootpoa & activate the POAManager
  70. ORB orb = ORB.init(args, null);
  71. POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
  72. rootpoa.the_POAManager().activate();
  73.  
  74. // create servant and register it with the ORB
  75. AdditionObj addobj = new AdditionObj();
  76. addobj.setORB(orb);
  77.  
  78. // get object reference from the servant
  79. org.omg.CORBA.Object ref = rootpoa.servant_to_reference(addobj);
  80. Addition href = AdditionHelper.narrow(ref);
  81.  
  82. // get the root naming context
  83. // NameService invokes the name service
  84. org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
  85.  
  86. // Use NamingContextExt which is part of the Interoperable
  87. // Naming Service (INS) specification.
  88. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
  89.  
  90. // bind the Object Reference in Naming
  91. NameComponent path[] = ncRef.to_name( "ABC" );
  92. ncRef.rebind(path, href);
  93.  
  94. System.out.println("Addition Server ready and waiting ...");
  95.  
  96. // wait for invocations from clients
  97. for (;;){
  98. orb.run();
  99. }
  100. }
  101.  
  102. catch (Exception e) {
  103. System.err.println("ERROR: " + e);
  104. e.printStackTrace(System.out);
  105. }
  106.  
  107. System.out.println("HelloServer Exiting ...");
  108.  
  109. }
  110. }
  111.  
  112. 6. Open a new CMD console and type in the command below
  113.  
  114. start orbd -ORBInitialPort 1050
  115.  
  116. i.) Click Run on the IDE -> Run Configuration
  117. ii.) Select the StartServer for the CorbaAdditionServer
  118. iii.) Click on Arguments and type in : -ORBInitialPort 1050 -ORBInitialHost localhost
  119.  
  120. //The server should be running.
  121.  
  122. CLIENT SIDE
  123.  
  124. 1. Create a new Java Project and call it : CorbaAdditionClient
  125.  
  126. 2. Copy the AdditionApp Package under CorbaAdditionServer on the src Package under CorbaAdditionClient
  127.  
  128. 3. Create a new Class called StartClient
  129.  
  130. import AdditionApp.*;
  131. import org.omg.CosNaming.*;
  132. import org.omg.CosNaming.NamingContextPackage.*;
  133. import org.omg.CORBA.*;
  134. import java.io.*;
  135. import java.util.*;
  136.  
  137. public class StartClient {
  138.  
  139. /**
  140. * @param args the command line arguments
  141. */
  142. public static void main(String[] args) {
  143. try {
  144.  
  145. // create and initialize the ORB
  146. ORB orb = ORB.init(args, null);
  147.  
  148. // get the root naming context
  149. org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
  150.  
  151. // Use NamingContextExt instead of NamingContext. This is
  152. // part of the Interoperable naming Service.
  153. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
  154.  
  155. // resolve the Object Reference in Naming
  156. Addition addobj = (Addition) AdditionHelper.narrow(ncRef.resolve_str("ABC"));
  157.  
  158. Scanner c=new Scanner(System.in);
  159. System.out.println("Welcome to the addition system:");
  160. for(;;){
  161. System.out.println("Enter a:");
  162. String aa = c.nextLine();
  163. System.out.println("Enter b:");
  164. String bb = c.nextLine();
  165. int a=Integer.parseInt(aa);
  166. int b=Integer.parseInt(bb);
  167. int r=addobj.add(a,b);
  168. System.out.println("The result for addition is : "+r);
  169. System.out.println("-----------------------------------");
  170. }
  171. }
  172. catch (Exception e) {
  173. System.out.println("Hello Client exception: " + e);
  174. e.printStackTrace();
  175. }
  176.  
  177. }
  178.  
  179. }
  180.  
  181. 4. i.) Click on Run -> Run Configuration
  182. ii.) Select the StartClient for the CorbaAdditionClient.
  183. iii.) Click on Arguments, then under the Program arguments, type in : -ORBInitialPort 1050 -ORBInitialHost localhost
  184. iv.) Click Apply
  185.  
  186. 5. Run the program =)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement