90 likes | 242 Views
MSc Course in Advanced Distributed Systems Session 2.2: Practical CORBA Programming. http://info.comp.lancs.ac.uk/msc/ads/index.htm. Resources. the course website ( http://info.comp.lancs.ac.uk/msc/AdvDistSys/frames_index.htm ) has the problem specifications... there are 3 problems: Echo
E N D
MSc Course in Advanced Distributed SystemsSession 2.2: Practical CORBA Programming http://info.comp.lancs.ac.uk/msc/ads/index.htm
Resources • the course website (http://info.comp.lancs.ac.uk/msc/AdvDistSys/frames_index.htm) has the problem specifications... • there are 3 problems: • Echo • File • Notify • we will now look at the Echo example in some detail to help get you started...
The files for ‘Echo’ • compiling Echo.idl produces (mainly)... • _EchoInterfaceImplBase.java – the class your skeleton will inherit from • Echo.java – the type-specific object ref you will use locally at the client • EchoInterface.java –the IDL interface represented as a Java interface • you need to provide these: • Client.java – the client ‘main’ program • Server.java – the servant, together with a containing ‘main’ (server) program
Echo.idl // Echo.idl // Put the interface in a module to avoid global namespace pollution module Exercise { interface EchoServer { // Simply return the given string string echo(in string s); }; };
server.java 1 (of 2) // server.java, version with stringified object reference import java.io.*; import org.omg.CORBA.*; class EchoServant extends _EchoImplBase { public String echo(String s) { return s; } } public class server { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // create the servant and ‘register’ it with the ORB EchoServant echoRef = new EchoServant(); orb.connect(echoRef);
server.java 2 (of 2) // stringify the EchoRef and save it in a file String str = orb.object_to_string(echoRef); FileOutputStream fos = new FileOutputStream(“server.ior”); PrintStream ps = new PrintStream(fos); ps.print(str); ps.close(); // finally, wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } }
client.java 1 (of 2) // client.java, version with stringified object reference import java.io.*; import org.omg.CORBA.*; public class client { public static void main(String args[]) { try { // create and initialize the ORB ORB orb = ORB.init(args, null); // Get the stringified object reference and de-stringify it BufferedReader br = new BufferedReader( new FileReader(“server.ior”));
client.java 2 (of 2) String ior = br.readLine(); org.omg.CORBA.Object obj = orb.string_to_object(ior); Echo echoRef = EchoHelper.narrow(obj); // call the Echo server object and print results String echo = echoRef.echo("Hello, World"); System.out.println(echo); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } }
Running the Echo application • follow instructions on the website to download the pre-compiled classes • when you need to run the application… • run the server first: ‘java server’ • then run the client: ‘java client’ • then go on to exercises 2 and 3... • good luck!