Guest User

Untitled

a guest
Oct 20th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. module HelloApp
  2. {
  3.     interface Hello
  4.     {
  5.         string sayHello();
  6.         oneway void shutdown();
  7.     };
  8. };
  9.  
  10.  
  11. //server
  12.  
  13. import HelloApp.*;
  14. import org.omg.CosNaming.*;
  15. import org.omg.CosNaming.NamingContextPackage.*;
  16. import org.omg.CORBA.*;
  17. import org.omg.PortableServer.*;
  18. import org.omg.PortableServer.POA;
  19. import java.util.Properties;
  20. class HelloImpl extends HelloPOA {
  21.     private ORB orb;
  22.     public void setORB(ORB orb_val) {
  23.         orb = orb_val;
  24.     }
  25.  
  26.     // implement sayHello() method
  27.     public String sayHello() {
  28.         return "\nHello world !!\n";
  29.     }
  30.  
  31.     // implement shutdown() method
  32.     public void shutdown() {
  33.         orb.shutdown(false);
  34.     }
  35. }
  36.  
  37. public class HelloServer {
  38.     public static void main(String args[]) {
  39.         try{
  40.             // create and initialize the ORB
  41.             ORB orb = ORB.init(args, null);
  42.             // get reference to rootpoa & activate the POAManager
  43.             POA rootpoa =
  44.             POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
  45.             rootpoa.the_POAManager().activate();
  46.             // create servant and register it with the ORB
  47.             HelloImpl helloImpl = new HelloImpl();
  48.             helloImpl.setORB(orb);
  49.             // get object reference from the servant
  50.             org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
  51.             Hello href = HelloHelper.narrow(ref);
  52.  
  53.             // get the root naming context
  54.             // NameService invokes the name service
  55.             org.omg.CORBA.Object objRef =
  56.             orb.resolve_initial_references("NameService");
  57.             // Use NamingContextExt which is part of the Interoperable
  58.             // Naming Service (INS) specification.
  59.             NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
  60.             // bind the Object Reference in Naming
  61.             String name = "Hello";
  62.             NameComponent path[] = ncRef.to_name( name );
  63.             ncRef.rebind(path, href);
  64.             System.out.println("HelloServer ready and waiting ...");
  65.             // wait for invocations from clients
  66.             orb.run();
  67.         }
  68.  
  69.         catch (Exception e) {
  70.             System.err.println("ERROR: " + e);
  71.             e.printStackTrace(System.out);
  72.         }
  73.  
  74.         System.out.println("HelloServer Exiting ...");
  75.     }
  76. }
  77.  
  78. //klient
  79.  
  80. import HelloApp.*;
  81. import org.omg.CosNaming.*;
  82. import org.omg.CosNaming.NamingContextPackage.*;
  83. import org.omg.CORBA.*;
  84. public class HelloClient {
  85.     static Hello helloImpl;
  86.    
  87.     public static void main(String args[])
  88.     {
  89.         try{
  90.             // create and initialize the ORB
  91.             ORB orb = ORB.init(args, null);
  92.             // get the root naming context
  93.             org.omg.CORBA.Object objRef =
  94.             orb.resolve_initial_references("NameService");
  95.             // Use NamingContextExt instead of NamingContext. This is
  96.             // part of the Interoperable naming Service.
  97.             NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
  98.  
  99.             // resolve the Object Reference in Naming
  100.             String name = "Hello";
  101.             helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));
  102.             System.out.println("Obtained a handle on server object: " + helloImpl);
  103.             System.out.println(helloImpl.sayHello());
  104.             helloImpl.shutdown();
  105.         } catch (Exception e) {
  106.             System.out.println("ERROR : " + e) ;
  107.             e.printStackTrace(System.out);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment