Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Java + Corba Help
Options
-
17-04-2009 3:27pmhey, i have to do an assignment to create a chat server with clients using corba objects and i'm completely lost. i have some sample code but it may as well be in dutch to me.
i've left this really late, and the fact that i missed a view vital classes doesn't help me either and was hoping for someone to give me a quick overview of what's happening here.
here's some sample code i have.
Client.javaimport java.util.Properties; import org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContext; import org.omg.CosNaming.NamingContextHelper; public class Client { public static void main(String[] args) { try { //initialize orb Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort", "1050"); //Replace MyHost with the name of the host on which you are running the server props.put("org.omg.CORBA.ORBInitialHost", "localhost"); ORB orb = ORB.init(args, props); System.out.println("Initialized ORB"); //Instantiate Servant and create reference POA rootPOA = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); ListenerImpl listener = new ListenerImpl(); rootPOA.activate_object(listener); Listener ref = ListenerHelper.narrow(rootPOA.servant_to_reference(listener)); //Resolve MessageServer MessageServer msgServer = MessageServerHelper.narrow(orb.string_to_object("corbaname:iiop:1.2@localhost:1050#MessageServer")); //Register listener reference (callback object) with MessageServer msgServer.register(ref); System.out.println("Listener registered with MessageServer"); //Activate rootpoa rootPOA.the_POAManager().activate(); //Wait for messages System.out.println("Wait for incoming messages"); orb.run(); } catch (Exception e) { e.printStackTrace(); } } }
ListenerImpl.javapublic class ListenerImpl extends ListenerPOA { public void message(String msg) { System.out.println("Message from server : " + msg); } }
MessageServerImpl.javaimport java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Vector; import java.util.Iterator; public class MessageServerImpl extends MessageServerPOA { private Vector clients = new Vector(); private ReadThread rt = null; public MessageServerImpl() { rt = new ReadThread(this); } public void register(Listener lt) { clients.add(lt); } public void startReadThread() { rt.start(); } public void message(String msg) { Iterator it = clients.iterator(); while (it.hasNext()) { Listener lt = (Listener) it.next(); lt.message(msg); //FOR THE SIMPLER EXAMPLE, ADD A SIMPLE //MESSAGE TO BE CALLED BACK, FOR EXAMPLE, //SLEEP FOR 30 SECONDS, THEN SEND THE TIME } } } //EXCLUDE THIS CLASS FOR THE SIMPLER EXAMPLE class ReadThread extends Thread { MessageServerImpl msImpl = null; public ReadThread(MessageServerImpl msImpl) { this.msImpl = msImpl; } public void run() { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); try { for (;;) { System.out.print("message > "); String msg = br.readLine(); msImpl.message(msg); } } catch (Exception e) { e.printStackTrace(); } } }
Server.javaimport org.omg.CORBA.ORB; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContext; import org.omg.CosNaming.NamingContextHelper; import java.util.Properties; public class Server { public static void main(String[] args) { try { //create and initialize the ORB Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort", "1050"); //Replace MyHost with the name of the host on which you are running the server props.put("org.omg.CORBA.ORBInitialHost", "localhost"); ORB orb = ORB.init(args, props); System.out.println("Initialized ORB"); //Instantiate Servant and create reference POA rootPOA = POAHelper.narrow( orb.resolve_initial_references("RootPOA")); MessageServerImpl msImpl = new MessageServerImpl(); rootPOA.activate_object(msImpl); MessageServer msRef = MessageServerHelper.narrow( rootPOA.servant_to_reference(msImpl)); //Bind reference with NameService NamingContext namingContext = NamingContextHelper.narrow( orb.resolve_initial_references("NameService")); System.out.println("Resolved NameService"); NameComponent[] nc = { new NameComponent("MessageServer", "") }; namingContext.rebind(nc, msRef); //Activate rootpoa rootPOA.the_POAManager().activate(); //Start readthread and wait for incoming requests System.out.println("Server ready and running ...."); //REMOVE THE NEXT LINE FOR THE SIMPLER EXAMPLE msImpl.startReadThread(); orb.run(); } catch (Exception e) { e.printStackTrace(); } } }
callback.idlinterface Listener { void message(in string msg); }; interface MessageServer { void register(in Listener lt); };
i compile it using the following.
mkdir classes
idlj -fall -td ./classes callback.idl
javac -classpath ./classes -d ./classes *.java
javac -classpath ./classes *.java
in terminal 1
orbd -ORBInitialPort 1050 -ORBInitialHost localhost &
in terminal 2
java -classpath ./classes Server -ORBInitialPort 1050
in terminal 3
java -classpath ./classes Client -ORBInitialPort 1050
how hard would it be to re-jig this example into making the client send the message to the server and the server then sending it out to the other clients.0
Advertisement