--- /dev/null
+package and.AIL ;
+
+/*************************************************
+ * *
+ * Classe permettant d'échanger des objets entre *
+ * les différents programmes actifs. *
+ * *
+ * *
+ * Auteur : Sébastien Miquée -- miquee@gmail.com *
+ * *
+ * Date : 21-05-2008 *
+ * Date : 27-01-2009 *
+ * *
+ *************************************************/
+
+
+/** Imports **/
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import org.objectweb.proactive.Body;
+import org.objectweb.proactive.api.PAActiveObject;
+import org.objectweb.proactive.api.PAGroup;
+import org.objectweb.proactive.api.PASPMD;
+import org.objectweb.proactive.core.group.Group;
+import org.objectweb.proactive.core.node.Node;
+import org.objectweb.proactive.core.node.NodeException;
+
+
+/**
+ * This class permits to exchange Objects
+ * between ActiveObjects of a program. This class allows
+ * to exchange multiple data with the same receiver with
+ * the use of tagged messages.
+ *
+ * @author Sébastien Miquée
+ * @version 3.0
+ */
+public class AILObject implements Serializable
+{
+ private static final long serialVersionUID = 1L ;
+ /** Local variables **/
+ protected int myId = -1 ;
+ protected MessageList ml ;
+ protected int groupsize = 0 ;
+ protected Object lockint ;
+ protected Object locktint ;
+ protected Object lockdouble ;
+ protected Object locktdouble ;
+ protected Object lockbool ;
+ protected Object locktbool ;
+ private int mode = 0 ; // 0 : RMI ; 1 : Socket
+ protected Group<AILObject> group ;
+ private ServerSock servsock = null ;
+ private short cdirectory ;
+ protected String tab_url[] ;
+ protected short tab_port[] ;
+ protected String url = "" ;
+ protected short port = -1 ;
+ private short init = 0 ;
+
+
+ /**
+ * Empty constructor for the Stub generation.
+ */
+ public AILObject() {}
+
+
+ /**
+ * Set the communication mode of the library\n
+ * - 0 : RMI protocole\n
+ * - 1 : Socket protocole\n
+ * @param _mode : communication mode
+ */
+ public void setMode( int _mode )
+ {
+ if( init != 1 )
+ {
+ AILinit( ) ;
+ }
+
+ if( _mode == 1 && mode != 1 )
+ {
+ Node n = null ;
+ try {
+ n = PAActiveObject.getNode() ;
+ } catch( NodeException e1 ) {
+ e1.printStackTrace() ;
+ }
+
+ url = n.getNodeInformation().getURL().split("/")[2].split(":")[0] ;
+
+ servsock = new ServerSock( ) ;
+
+ createDirectory() ;
+
+ }
+
+ if( _mode == 0 && mode == 1)
+ {
+ servsock.halt() ;
+ }
+
+ mode = (short) _mode ;
+ }
+
+ protected void setUrl( String _url )
+ {
+ url = _url ;
+ }
+
+ public String getUrl( )
+ {
+ return url ;
+ }
+
+ protected void setPort( short _port )
+ {
+ port = _port ;
+ }
+
+ public int getPort()
+ {
+ return port ;
+ }
+
+
+ /**
+ * Return the communication mode of the library
+ * @return the communication mode
+ */
+ public int getMode()
+ {
+ return mode ;
+ }
+
+ /**
+ * Print the url on which this active object is.
+ */
+ public void echoUrl()
+ {
+ Body body ;
+ body = PAActiveObject.getBodyOnThis() ;
+ int rank = PASPMD.getMyRank() ;
+
+ System.out.println("I am AILObject " + rank + " on node " + body.getNodeURL() ) ;
+ }
+
+ private void createDirectory()
+ {
+ if( cdirectory == 0 )
+ {
+ PASPMD.totalBarrier( "Directory Creation" ) ;
+
+ int i = 0 ;
+ for( i = 0 ; i < groupsize ; i++ )
+ {
+ if( i != myId )
+ {
+ tab_url[i] = group.get(i).getUrl() ;
+ tab_port[i] = (short) group.get(i).getPort() ;
+ } else {
+ tab_url[i] = this.url ;
+ tab_port[i] = this.port ;
+ }
+ }
+
+ cdirectory = 1 ;
+ }
+ }
+
+ public void AILend()
+ {
+ if( mode == 1 )
+ {
+ servsock.halt() ;
+ }
+ }
+
+
+ /**
+ * Initialization of the object AILObject.
+ */
+ public void AILinit( )
+ {
+ if( init != 1 )
+ {
+ PASPMD.totalBarrier( "Init" ) ;
+
+ lockint = new Object() ;
+ locktint = new Object() ;
+ lockdouble = new Object() ;
+ locktdouble = new Object() ;
+ lockbool = new Object() ;
+ locktbool = new Object() ;
+
+ group = PAGroup.getGroup( (AILObject) PASPMD.getSPMDGroup() ) ;
+ myId = PASPMD.getMyRank() ;
+ groupsize = PASPMD.getMySPMDGroupSize() ;
+
+ ml = new MessageList( groupsize + 1 ) ;
+
+ tab_url = new String[groupsize] ;
+ tab_port = new short[groupsize] ;
+
+
+ /** Parallelization of methods, to non block the Object **/
+ PAActiveObject.setImmediateService( "getUrl", new Class[]{}) ;
+ PAActiveObject.setImmediateService( "getPort", new Class[]{}) ;
+
+ PAActiveObject.setImmediateService( "getIntBl", new Class[]{int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "getTabIntBl", new Class[]{int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "getDoubleBl", new Class[]{int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "getTabDoubleBl", new Class[]{int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "getBoolBl", new Class[]{int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "getTabBoolBl", new Class[]{int.class, int.class}) ;
+
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, int.class, int.class}) ;
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, int[].class, int.class}) ;
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, double.class, int.class}) ;
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, double[].class, int.class}) ;
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, boolean.class, int.class}) ;
+ PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, boolean[].class, int.class}) ;
+
+ init = 1 ;
+ }
+ }
+
+ /*********************/
+ /** Sending Objects **/
+ /*********************/
+
+ /**
+ * Send an Integer _o from the id id_src with the tag _tag.
+ * Puts a new Message, composed of the Integer _o and the tag _tag
+ * in the MessageList of this object.
+ * @param _int : the Integer to send.
+ * @param idDst : the id of the sender.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, int _int, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _int, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageInt( myId, _tag, _int ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of an Integer with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _int : the Integer to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, int _int, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageInt( _tag, _int ) ) ;
+
+ synchronized( lockint )
+ {
+ lockint.notifyAll();
+ }
+ }
+ }
+
+
+ /**
+ * Send a tabular of Integers _tint to the id idDst with the tag _tag.
+ * Puts a new Message, composed of the tabular of Integers _tint and the tag _tag
+ * in the MessageList of this object.
+ * @param _tint : the tabular of Integers to send.
+ * @param idDst : the id of the receiver.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, int[] _tint, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _tint, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageTabInt( myId, _tag, _tint ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of a tabular of Integers with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _tint : the tabular of Integers to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, int[] _tint, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageTabInt( _tag, _tint ) ) ;
+
+ synchronized( locktint )
+ {
+ locktint.notifyAll();
+ }
+ }
+ }
+
+
+ /**
+ * Send a Double _double to the id idDst with the tag _tag.
+ * Puts a new Message, composed of the Double _double and the tag _tag
+ * in the MessageList of this object.
+ * @param _double : the Double to send.
+ * @param idDst : the id of the sender.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, double _double, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _double, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageDouble( myId, _tag, _double ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of a Double with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _double : the double to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, double _double, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageDouble( _tag, _double ) ) ;
+
+ synchronized( lockdouble )
+ {
+ lockdouble.notifyAll();
+ }
+ }
+ }
+
+ /**
+ * Send a tabular of Doubles _tint to the id idDst with the tag _tag.
+ * Puts a new Message, composed of the tabular of Doubles _tint and the tag _tag
+ * in the MessageList of this object.
+ * @param _tdouble : the tabular of Doubles to send.
+ * @param idDst : the id of the receiver.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, double[] _tdouble, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _tdouble, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageTabDouble( myId, _tag, _tdouble ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of a tabular of Doubles with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _tdouble : the tabular of Doubles to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, double[] _tdouble, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageTabDouble( _tag, _tdouble ) ) ;
+
+ synchronized( locktdouble )
+ {
+ locktdouble.notifyAll();
+ }
+ }
+ }
+
+
+ /**
+ * Send a Boolean _bool to the id id_src with the tag _tag.
+ * Puts a new Message, composed of the Boolean _bool and the tag _tag
+ * in the MessageList of this object.
+ * @param _bool : the Boolean to send.
+ * @param idDst : the id of the receiver.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, boolean _bool, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _bool, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageBool( myId, _tag, _bool ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of a Boolean with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _bool : the boolean to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, boolean _bool, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageBool( _tag, _bool ) ) ;
+
+ synchronized( lockbool )
+ {
+ lockbool.notifyAll();
+ }
+ }
+ }
+
+ /**
+ * Send a tabular of Booleans _tint to the id idDst with the tag _tag.
+ * Puts a new Message, composed of the tabular of Booleans _tint and the tag _tag
+ * in the MessageList of this object.
+ * @param _tbool : the tabular of Booleans to send.
+ * @param idDst : the id of the receiver.
+ * @param _tag : the tag of the Message.
+ */
+ protected void sendObject( int _idDst, boolean[] _tbool, int _tag )
+ {
+ if( _idDst >= 0 && _idDst < groupsize + 1 )
+ {
+ if( mode == 0 ) // RMI
+ {
+ ((AILObject)group.get( _idDst )).recvObject( myId, _tbool, _tag ) ;
+ }
+
+ if( mode == 1 ) // Socket
+ {
+ try {
+ Socket soc = new Socket() ;
+
+ soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;
+
+ soc.setReuseAddress( true ) ;
+ soc.setSendBufferSize( 2048 * 2048 ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setTcpNoDelay( false ) ;
+
+ BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
+
+ ObjectOutputStream output = new ObjectOutputStream( out ) ;
+
+ output.writeObject( new MessageTabBool( myId, _tag, _tbool ) ) ;
+
+ output.close() ;
+ out.close() ;
+ soc.close() ;
+
+ } catch( Exception e ) {
+ e.printStackTrace() ;
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Reception of a tabular of Booleans with the RMI method
+ * @param _idSrc : id of the sender
+ * @param _tdouble : the tabular of Booleans to receive
+ * @param _tag : the tag of the message
+ */
+ public void recvObject( int _idSrc, boolean[] _tbool, int _tag )
+ {
+ if( _idSrc >= 0 && _idSrc < groupsize + 1 )
+ {
+ ml.addMessage( _idSrc, new MessageTabBool( _tag, _tbool ) ) ;
+
+ synchronized( locktbool )
+ {
+ locktbool.notifyAll();
+ }
+ }
+ }
+
+
+ /*******************************************/
+ /** Getting Objects - Non waiting methods **/
+ /*******************************************/
+
+ /**
+ * Returns the Integer from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Integer.
+ */
+ public int getInt( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageInt mes = (MessageInt) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getInt() ;
+ }
+ }
+
+ return 0 ;
+ }
+
+ /**
+ * Returns the tabular of Integers from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Integers.
+ */
+ public int[] getTabInt( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabInt mes = (MessageTabInt) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getTabInt() ;
+ }
+ }
+
+ return null ;
+ }
+
+ /**
+ * Returns the Double from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Double.
+ */
+ public double getDouble( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageDouble mes = (MessageDouble) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getDouble() ;
+ }
+ }
+
+ return 0 ;
+ }
+
+ /**
+ * Returns the tabular of Doubles from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Doubles.
+ */
+ public double[] getTabDouble( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabDouble mes = (MessageTabDouble) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getTabDouble() ;
+ }
+ }
+
+ return null ;
+ }
+
+ /**
+ * Returns the Boolean from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Boolean.
+ */
+ public boolean getBool( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageBool mes = (MessageBool) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getBool() ;
+ }
+ }
+
+ return false ;
+ }
+
+ /**
+ * Returns the tabular of Booleans from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Booleans.
+ */
+ public boolean[] getTabBool( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabBool mes = (MessageTabBool) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes != null )
+ {
+ return mes.getTabBool() ;
+ }
+ }
+
+ return null ;
+ }
+
+
+ /***************************************/
+ /** Getting Objects - Waiting methods **/
+ /***************************************/
+
+ /**
+ * Returns the Integer from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Integer.
+ */
+ public int getIntBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageInt mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageInt) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ synchronized( lockint )
+ {
+ try {
+ lockint.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return mes.getInt() ;
+ }
+
+ return 0 ;
+ }
+
+ /**
+ * Returns the tabular of Integers from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Integers.
+ */
+ public int[] getTabIntBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabInt mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageTabInt) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ synchronized( locktint )
+ {
+ try {
+ locktint.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return mes.getTabInt() ;
+ }
+
+ return null ;
+ }
+
+ /**
+ * Returns the Double from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Double.
+ */
+ public double getDoubleBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageDouble mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageDouble) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ synchronized( lockdouble )
+ {
+ try {
+ lockdouble.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return mes.getDouble() ;
+ }
+
+ return 0 ;
+ }
+
+ /**
+ * Returns the tabular of Doubles from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Doubles.
+ */
+ public double[] getTabDoubleBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabDouble mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageTabDouble) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ {
+ synchronized( locktdouble )
+ {
+ try {
+ locktdouble.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ return mes.getTabDouble() ;
+ }
+
+ return null ;
+ }
+
+ /**
+ * Returns the Boolean from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the Boolean.
+ */
+ public boolean getBoolBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageBool mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageBool) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ synchronized( lockbool )
+ {
+ try {
+ lockbool.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return mes.getBool() ;
+ }
+
+ return false ;
+ }
+
+ /**
+ * Returns the tabular of Booleans from the id id_src, with the tag _tag.
+ * Search in the MessageList if there is corresponding Message.
+ * Wait until a corresponding Message comes.
+ * @param id_src : index of the sender.
+ * @param _tag : tag of the Message.
+ * @return : the tabular of Booleans.
+ */
+ public boolean[] getTabBoolBl( int id_src, int _tag )
+ {
+ if( id_src >= 0 && id_src < groupsize + 1 )
+ {
+ MessageTabBool mes = null ;
+
+ while( mes == null )
+ {
+ mes = (MessageTabBool) ml.searchMsg( id_src, _tag ) ;
+
+ if( mes == null )
+ {
+ synchronized( locktbool )
+ {
+ try {
+ locktbool.wait() ;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ return mes.getTabBool() ;
+ }
+
+ return null ;
+ }
+
+
+ /**
+ *
+ * @author miquee
+ *
+ */
+ class ServerSock extends Thread
+ {
+ protected boolean ok ;
+ protected boolean exec ;
+
+ public ServerSock( )
+ {
+ start() ;
+ }
+
+
+ public void run()
+ {
+ ServerSocket s = null ;
+ ok = false ;
+ exec = true ;
+
+ /** Determination of the address and port to listen **/
+ port = 2000 ;
+
+ while( ! ok )
+ {
+ ok = true ;
+ try {
+ s = new ServerSocket( port );
+ } catch (IOException e1) {
+ ok = false ;
+ port++ ;
+
+ if( port == 3001 )
+ {
+ System.out.println( "Unable to bind port !!" );
+ System.exit(1) ;
+ }
+
+ System.out.println( "Binding address already in use !" ) ;
+ }
+ }
+
+ System.out.println( "I am AILObject " + myId + " with address " + url +" on port " + port ) ;
+
+ try {
+ while( exec )
+ {
+ Socket soc = new Socket() ;
+
+ soc = s.accept() ;
+
+ soc.setReceiveBufferSize( 2048 * 2048 ) ;
+ soc.setTcpNoDelay( false ) ;
+ soc.setSoLinger( false, 1 ) ;
+ soc.setReuseAddress( true ) ;
+
+ new ServClientSock( soc ) ;
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace() ;
+ }
+ }
+
+ public void halt()
+ {
+ exec = false ;
+ }
+
+
+ class ServClientSock extends Thread
+ {
+ private BufferedInputStream in = null ;
+ private Socket client = null ;
+
+ public ServClientSock( Socket s ) throws IOException
+ {
+ client = s ;
+
+ start() ;
+ }
+
+
+ public void run()
+ {
+ /** Receiving protocol :
+ * --------------------
+ * receiving integer which represents the id_src
+ * receiving tag
+ * receiving integer to indicate the type of data
+ * receiving data
+ */
+
+ try {
+ in = new BufferedInputStream( client.getInputStream() ) ;
+
+ ObjectInputStream input = new ObjectInputStream( in ) ;
+
+ Message m = (Message) input.readObject() ;
+
+ ml.addMessage( m.getSender(), m ) ;
+
+ try {
+ input.close() ;
+ in.close() ;
+ client.close() ;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ switch( m.getType() )
+ {
+ case 0 :
+ synchronized( lockint )
+ {
+ lockint.notifyAll();
+ } ; break ;
+
+ case 1 :
+ synchronized( locktint )
+ {
+ locktint.notifyAll();
+ } ; break ;
+
+ case 2 :
+ synchronized( lockdouble )
+ {
+ lockdouble.notifyAll();
+ } ; break ;
+
+ case 3 :
+ synchronized( locktdouble )
+ {
+ locktdouble.notifyAll();
+ } ; break ;
+
+ case 4 :
+ synchronized( lockbool )
+ {
+ lockbool.notifyAll();
+ } ; break ;
+
+ case 5 :
+ synchronized( locktbool )
+ {
+ locktbool.notifyAll();
+ } ; break ;
+
+ default: ;
+ }
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+
+
+ }
+
+ }
+ }
+}
+
+
+/** La programmation est un art, respectons ceux qui la pratiquent !! **/