Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To write a program to print “Hello World” in CORBA.
#1

Aim:
To write a program to print Hello World in CORBA.

Algorithm:
Step 1: Start the process
Step 2: Create an idl class in a module Hello1and declare a
method sayHello().
Step 3: Create an implement class HelloImpl which extends
_HelloImplBase and import the module.
Step 4: Define the sayHello method and return the result.
Step 5: Define the client program named HelloClient which
imports the module
(i) Declare a static reference for interface Hello
(ii) Create and initialize the ORB and get the root naming
context.
(ii) Resolve the Object Reference in Naming.
(iv) Call the sayHello method
Step 6: Create the HelloServer which imports the module.
(i) Create and initialize the ORB.
(ii) Create servant and register it with the ORB.
(ii) Get object reference from the servant.
(iv) Bind the Object Reference in Naming.
(v) Use NamingContext.
(vi) Wait for invocations from clients
Step 7: Stop the process.
Program Code:
Code:
IDL Class:-
module Hello1
{
  interface Hello
  {
  string sayHello();
  };
  };

Client Class:-
import Hello1.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class HelloClient
{
static Hello helloImpl;
public static void main(String args[])
  {
  try
  {
  // create and initialize the ORB
  ORB orb = ORB.init(args, null);
  // get the root naming context
  org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
  NamingContext ncRef = NamingContextHelper.narrow(objRef);
  
  // resolve the Object Reference in Naming
  NameComponent nc=new NameComponent ("Hello","");
  NameComponent path[] = {nc};
  helloImpl = HelloHelper.narrow(ncRef.resolve(path));
  System.out.println(helloImpl.sayHello());
  }
  catch (Exception e)
  {
  System.out.println("ERROR : " + e) ;
  }
  }
}

Implement Class:
import Hello1.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.Properties;

class HelloImpl extends _HelloImplBase
  {
  // implement sayHello() method
  public String sayHello()
  {
  return "\nHello world !!\n";
  }
  }

Server Class:-
Code:
import Hello1.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class HelloServer {
  public static void main(String args[]) {
  try
  {
  // create and initialize the ORB
  ORB orb = ORB.init(args, null);
  // create servant and register it with the ORB
  HelloImpl helloImpl = new HelloImpl();
  orb.connect(helloImpl);

  // get object reference from the servant
  org.omg.CORBA.Object objRef =orb.resolve_initial_references("NameService");
  // Use NamingContext
  NamingContext ncRef = NamingContextHelper.narrow(objRef);

  // bind the Object Reference in Naming
  NameComponent nc=new NameComponent ("Hello","");
  NameComponent path[] = {nc};
  ncRef.rebind(path, helloImpl);

  System.out.println("HelloServer ready and waiting ..");
  // wait for invocations from clients
  orb.run();
  }
  catch (Exception e)
  {
  System.err.println("ERROR: " + e);
  }
  System.out.println("HelloServer Exiting ..");
  }
}
Output:
Impl Side:

Z:\Middle ware\corba1>idlj -fclient -fserver -implBase hello.idl
Z:\Middle ware\corba1>cd hello1
Z:\Middle ware\corba1\Hello1>javac *.java
Z:\Middle ware\corba1>tnameserv
Initial Naming Context:
IOR:000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e
67436f6e746578743a312e3000000000010000000000000054000101000000000d313732
2e31362e312e3130380000041f00000018afabcafe00000002584b271b000000080000000
000000000000000010000000100000014000000000001002000000000000101000000000
TransientNameServer: setting port for initial object references to: 900
Ready.
Server Side:
Z:\Middle ware\corba1>javac *.java
Z:\Middle ware\corba1>java HelloServer
HelloServer ready and waiting..
Client Side:
Z:\Middle ware\corba1>java HelloClient
Hello world!!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.