ExempleCh07_01_cas1_helloRetourTélécharger Exemple simple de la mise en oeuvre d'un objet distribué en CORBA sans utiliser de service de nommage. Client.java package hello; import java.io.*; import org.omg.CORBA.*; public class Client { public static void main( String args[] ) { if( args.length != 1 ) { System.out.println( "Usage: jaco hello.Client <ior_file>" ); System.exit( 1 ); } try { File f = new File( args[ 0 ] ); //check if file exists if( ! f.exists() ) { System.out.println("File " + args[0] + " does not exist."); System.exit( -1 ); } //check if args[0] points to a directory if( f.isDirectory() ){ System.out.println("File " + args[0] + " is a directory."); System.exit( -1 ); } // Initialisation de l'ORB ORB orb = ORB.init( args, null ); // Récuparation de la référence CORBA à partir de l'IOR BufferedReader br = new BufferedReader( new FileReader( f )); org.omg.CORBA.Object obj = orb.string_to_object( br.readLine() ); br.close(); // Récupération du servant GoodDay goodDay = GoodDayHelper.narrow( obj ); // Utilisation du servant goodDay.compteur(100); while(true) { // invoke the operation again and print the wide string result String str = goodDay.hello_world( "J. Laforgue "); System.out.println(str); try{Thread.sleep(1000);}catch(Exception ex){}; } } catch( Exception ex ) { ex.printStackTrace(); } } } GoodDayImpl.java // GoodDayImpl // package hello; import org.omg.CORBA.*; public class GoodDayImpl extends GoodDayPOA { private String location; private int compteur ; public GoodDayImpl( String location ) { this.location = location; this.compteur = 0; } public String hello_world(String wide_msg) { compteur++; System.out.println("The message is : " + wide_msg ); return "Hello : Mr " + wide_msg+" "+location+" "+compteur; } public int compteur() { return compteur; } public void compteur(int c) { compteur = c; } } Server.java package hello; import java.io.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; public class Server { public static void main(String[] args) { if( args.length != 1 ) { System.out.println("Usage: jaco hello.Server <ior_file>"); System.exit( 1 ); } try { //initialisation de l'ORB ORB orb = ORB.init( args, null ); //Création du POA POA poa = POAHelper.narrow( orb.resolve_initial_references( "RootPOA" )); // Activation du POA manager poa.the_POAManager().activate(); // Création du servant GoodDayImpl goodDayImpl = new GoodDayImpl( "CNAM NFA032" ); // Création de la référence CORBA (IOR) org.omg.CORBA.Object obj = poa.servant_to_reference( goodDayImpl ); // Ecrire l'IOR dans un fichier PrintWriter pw = new PrintWriter( new FileWriter( args[ 0 ] )); pw.println( orb.object_to_string( obj )); pw.flush(); pw.close(); // Attentes des requetes orb.run(); } catch( Exception e ) { System.out.println( e ); } } } build.xml <?xml version="1.0"?> <!-- ==================================================== --> <!-- Compilation d'un prog Java Corba --> <!-- ==================================================== --> <project name="PROG" default="all" basedir="."> <!-- ==================================================== --> <!-- Definition de la tache de compilation des idl --> <!-- ==================================================== --> <target name="load-taskdef"> <taskdef name="jacidl" classname="org.jacorb.idl.JacIDL"> <classpath> <pathelement path="${env.CLASSPATH}" /> <fileset dir="${env.JACORB_HOME}/lib"> <include name="*.jar" /> </fileset> </classpath> </taskdef> </target> <!-- ==================================================== --> <!-- Initialisation : definition des proprietes, creation des repertoires, ... --> <!-- ==================================================== --> <target name="init"> <property environment="env" /> <property name="dirs.base" value="${basedir}"/> <property name="jacorb.dir" value="C:\JacORB-2.2.3" /> <property name="classdir" value="${dirs.base}/classes"/> <property name="lib" value="${jacorb.dir}/lib"/> <property name="dest" value="${dirs.base}/generated"/> <property name="include" value="${jacorb.dir}/idl/omg"/> <mkdir dir="${classdir}"/> </target> <!-- ==================================================== --> <!-- Compilation Java : - compilation des fichiers generes par la compilation des idl - compilation des fichiers java de haut nouveau du projet --> <!-- ==================================================== --> <target name="all" depends="init,idl"> <javac srcdir="${dest}" destdir="${classdir}" includes="**/*.java" /> <javac srcdir="${dirs.base}" destdir="${classdir}" includes="*.java" /> </target> <!-- ==================================================== --> <!-- Compilation des idl --> <!-- ==================================================== --> <target name="idl" depends="init,load-taskdef"> <mkdir dir="${dest}" /> <jacidl srcdir="${dirs.base}" destdir="${dest}" includepath="${include}" includes="*.idl"/> </target> <!-- ==================================================== --> <!-- Nettoyage avant compilation --> <!-- ==================================================== --> <target name="clean" depends="init"> <delete dir="${classdir}"/> <delete dir="${dest}"/> </target> </project> <!-- ==================================================== --> <!-- QUELQUES RECOMMANDATIONS : - ne pas mettre de caracteres accentuees: "Invalid byte 2 of 3 bytes UTF-8 sequence" (ceci a ete observe sous Windows Xp) - attention au item "delete" pour ne pas supprimer des fichiers lors de vos premiers pas avec ant. Faites toujours une sauvegarde de vos repertoires avant. - ne pas mettre de commentaires avant la 1ere ligne. - ce programme ant n'utilise aucun autre fichier ant (pas d'include). --> <!-- ==================================================== --> classes fior.txt IOR:000000000000001649444C3A68656C6C6F2F476F6F644461793A312E3000000000000001000000000000004C000102000000000D3139322E3136382E35362E310000C9C10000001C7374616E64617264496D706C4E616D652F001A34110B300622020E3D000000010000000000000008000000004A414300 compil.bat call ant pause generated hello #_GoodDayStub.java# package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public class _GoodDayStub extends org.omg.CORBA.portable.ObjectImpl implements hello.GoodDay { private String[] ids = {"IDL:hello/GoodDay:1.0"}; public String[] _ids() { return ids; } public final static java.lang.Class _opsClass = hello.GoodDayOperations.class; public void compteur(int a) { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request("_set_compteur",true); _os.write_long(a); _is = _invoke(_os); return; } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "_set_compteur", _opsClass); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; try { _localServant.compteur(a); } finally { _servant_postinvoke(_so); } return; } } } public int compteur() { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request("_get_compteur",true); _is = _invoke(_os); return _is.read_long(); } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "_get_compteur", _opsClass); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; int _result; try { _result = _localServant.compteur(); } finally { _servant_postinvoke(_so); } return _result; } } } public java.lang.String hello_world(java.lang.String msg) { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request( "hello_world", true); _os.write_string(msg); _is = _invoke(_os); java.lang.String _result = _is.read_string(); return _result; } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "hello_world", _opsClass ); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; java.lang.String _result; try { _result = _localServant.hello_world(msg); } finally { _servant_postinvoke(_so); } return _result; } } } } GoodDay.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public interface GoodDay extends GoodDayOperations, org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity { } GoodDayHelper.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public final class GoodDayHelper { public static void insert (final org.omg.CORBA.Any any, final hello.GoodDay s) { any.insert_Object(s); } public static hello.GoodDay extract(final org.omg.CORBA.Any any) { return narrow(any.extract_Object()) ; } public static org.omg.CORBA.TypeCode type() { return org.omg.CORBA.ORB.init().create_interface_tc("IDL:hello/GoodDay:1.0", "GoodDay"); } public static String id() { return "IDL:hello/GoodDay:1.0"; } public static GoodDay read(final org.omg.CORBA.portable.InputStream in) { return narrow(in.read_Object()); } public static void write(final org.omg.CORBA.portable.OutputStream _out, final hello.GoodDay s) { _out.write_Object(s); } public static hello.GoodDay narrow(final java.lang.Object obj) { if (obj instanceof hello.GoodDay) { return (hello.GoodDay)obj; } else if (obj instanceof org.omg.CORBA.Object) { return narrow((org.omg.CORBA.Object)obj); } throw new org.omg.CORBA.BAD_PARAM("Failed to narrow in helper"); } public static hello.GoodDay narrow(final org.omg.CORBA.Object obj) { if (obj == null) return null; try { return (hello.GoodDay)obj; } catch (ClassCastException c) { if (obj._is_a("IDL:hello/GoodDay:1.0")) { hello._GoodDayStub stub; stub = new hello._GoodDayStub(); stub._set_delegate(((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate()); return stub; } } throw new org.omg.CORBA.BAD_PARAM("Narrow failed"); } public static hello.GoodDay unchecked_narrow(final org.omg.CORBA.Object obj) { if (obj == null) return null; try { return (hello.GoodDay)obj; } catch (ClassCastException c) { hello._GoodDayStub stub; stub = new hello._GoodDayStub(); stub._set_delegate(((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate()); return stub; } } } GoodDayHolder.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public final class GoodDayHolder implements org.omg.CORBA.portable.Streamable{ public GoodDay value; public GoodDayHolder() { } public GoodDayHolder (final GoodDay initial) { value = initial; } public org.omg.CORBA.TypeCode _type() { return GoodDayHelper.type(); } public void _read (final org.omg.CORBA.portable.InputStream in) { value = GoodDayHelper.read (in); } public void _write (final org.omg.CORBA.portable.OutputStream _out) { GoodDayHelper.write (_out,value); } } GoodDayOperations.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public interface GoodDayOperations { /* constants */ /* operations */ java.lang.String hello_world(java.lang.String msg); int compteur(); void compteur(int arg); } GoodDayPOA.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public abstract class GoodDayPOA extends org.omg.PortableServer.Servant implements org.omg.CORBA.portable.InvokeHandler, hello.GoodDayOperations { static private final java.util.Hashtable m_opsHash = new java.util.Hashtable(); static { m_opsHash.put ( "_set_compteur", new java.lang.Integer(0)); m_opsHash.put ( "_get_compteur", new java.lang.Integer(1)); m_opsHash.put ( "hello_world", new java.lang.Integer(2)); } private String[] ids = {"IDL:hello/GoodDay:1.0"}; public hello.GoodDay _this() { return hello.GoodDayHelper.narrow(_this_object()); } public hello.GoodDay _this(org.omg.CORBA.ORB orb) { return hello.GoodDayHelper.narrow(_this_object(orb)); } public org.omg.CORBA.portable.OutputStream _invoke(String method, org.omg.CORBA.portable.InputStream _input, org.omg.CORBA.portable.ResponseHandler handler) throws org.omg.CORBA.SystemException { org.omg.CORBA.portable.OutputStream _out = null; // do something // quick lookup of operation java.lang.Integer opsIndex = (java.lang.Integer)m_opsHash.get ( method ); if ( null == opsIndex ) throw new org.omg.CORBA.BAD_OPERATION(method + " not found"); switch ( opsIndex.intValue() ) { case 0: // _set_compteur { _out = handler.createReply(); compteur(_input.read_long()); break; } case 1: // _get_compteur { _out = handler.createReply(); _out.write_long(compteur()); break; } case 2: // hello_world { java.lang.String _arg0=_input.read_string(); _out = handler.createReply(); _out.write_string(hello_world(_arg0)); break; } } return _out; } public String[] _all_interfaces(org.omg.PortableServer.POA poa, byte[] obj_id) { return ids; } } GoodDayPOATie.java package hello; import org.omg.PortableServer.POA; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public class GoodDayPOATie extends GoodDayPOA { private GoodDayOperations _delegate; private POA _poa; public GoodDayPOATie(GoodDayOperations delegate) { _delegate = delegate; } public GoodDayPOATie(GoodDayOperations delegate, POA poa) { _delegate = delegate; _poa = poa; } public hello.GoodDay _this() { return hello.GoodDayHelper.narrow(_this_object()); } public hello.GoodDay _this(org.omg.CORBA.ORB orb) { return hello.GoodDayHelper.narrow(_this_object(orb)); } public GoodDayOperations _delegate() { return _delegate; } public void _delegate(GoodDayOperations delegate) { _delegate = delegate; } public POA _default_POA() { if (_poa != null) { return _poa; } else { return super._default_POA(); } } public void compteur(int a) { _delegate.compteur(a); } public int compteur() { return _delegate.compteur(); } public java.lang.String hello_world(java.lang.String msg) { return _delegate.hello_world(msg); } } _GoodDayStub.java package hello; /** * Generated from IDL interface "GoodDay" * @author JacORB IDL compiler V 2.2.3, 10-Dec-2005 */ public class _GoodDayStub extends org.omg.CORBA.portable.ObjectImpl implements hello.GoodDay { private String[] ids = {"IDL:hello/GoodDay:1.0"}; public String[] _ids() { return ids; } public final static java.lang.Class _opsClass = hello.GoodDayOperations.class; public void compteur(int a) { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request("_set_compteur",true); _os.write_long(a); _is = _invoke(_os); return; } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "_set_compteur", _opsClass); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; try { _localServant.compteur(a); } finally { _servant_postinvoke(_so); } return; } } } public int compteur() { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request("_get_compteur",true); _is = _invoke(_os); return _is.read_long(); } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "_get_compteur", _opsClass); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; int _result; try { _result = _localServant.compteur(); } finally { _servant_postinvoke(_so); } return _result; } } } public java.lang.String hello_world(java.lang.String msg) { while(true) { if(! this._is_local()) { org.omg.CORBA.portable.InputStream _is = null; try { org.omg.CORBA.portable.OutputStream _os = _request( "hello_world", true); _os.write_string(msg); _is = _invoke(_os); java.lang.String _result = _is.read_string(); return _result; } catch( org.omg.CORBA.portable.RemarshalException _rx ){} catch( org.omg.CORBA.portable.ApplicationException _ax ) { String _id = _ax.getId(); throw new RuntimeException("Unexpected exception " + _id ); } finally { this._releaseReply(_is); } } else { org.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( "hello_world", _opsClass ); if( _so == null ) throw new org.omg.CORBA.UNKNOWN("local invocations not supported!"); GoodDayOperations _localServant = (GoodDayOperations)_so.servant; java.lang.String _result; try { _result = _localServant.hello_world(msg); } finally { _servant_postinvoke(_so); } return _result; } } } } runClient.bat cd classes call jaco hello.Client fior.txt pause runServer.bat cd classes jaco hello.Server fior.txt pause server.idl // server.idl // module hello { interface GoodDay { string hello_world( in string msg ); attribute long compteur; }; };