From: Sébastien Miquée Date: Wed, 13 Jan 2010 10:20:04 +0000 (+0100) Subject: Création du dépôt AIL-PA X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/ail-pa.git/commitdiff_plain/67fe09c7f18ef33db89495ede920f324f341a8a5 Création du dépôt AIL-PA --- 67fe09c7f18ef33db89495ede920f324f341a8a5 diff --git a/AIL.jar b/AIL.jar new file mode 100644 index 0000000..6d85d53 Binary files /dev/null and b/AIL.jar differ diff --git a/bin/and/AIL/AILObject$ServerSock$ServClientSock.class b/bin/and/AIL/AILObject$ServerSock$ServClientSock.class new file mode 100644 index 0000000..3a02cea Binary files /dev/null and b/bin/and/AIL/AILObject$ServerSock$ServClientSock.class differ diff --git a/bin/and/AIL/AILObject$ServerSock.class b/bin/and/AIL/AILObject$ServerSock.class new file mode 100644 index 0000000..cc43cd6 Binary files /dev/null and b/bin/and/AIL/AILObject$ServerSock.class differ diff --git a/bin/and/AIL/AILObject.class b/bin/and/AIL/AILObject.class new file mode 100644 index 0000000..9d07c78 Binary files /dev/null and b/bin/and/AIL/AILObject.class differ diff --git a/bin/and/AIL/Convergence.class b/bin/and/AIL/Convergence.class new file mode 100644 index 0000000..fbc17ab Binary files /dev/null and b/bin/and/AIL/Convergence.class differ diff --git a/bin/and/AIL/Message.class b/bin/and/AIL/Message.class new file mode 100644 index 0000000..716dfd3 Binary files /dev/null and b/bin/and/AIL/Message.class differ diff --git a/bin/and/AIL/MessageBool.class b/bin/and/AIL/MessageBool.class new file mode 100644 index 0000000..1f805b6 Binary files /dev/null and b/bin/and/AIL/MessageBool.class differ diff --git a/bin/and/AIL/MessageDouble.class b/bin/and/AIL/MessageDouble.class new file mode 100644 index 0000000..de370ec Binary files /dev/null and b/bin/and/AIL/MessageDouble.class differ diff --git a/bin/and/AIL/MessageInt.class b/bin/and/AIL/MessageInt.class new file mode 100644 index 0000000..7ca5b83 Binary files /dev/null and b/bin/and/AIL/MessageInt.class differ diff --git a/bin/and/AIL/MessageList.class b/bin/and/AIL/MessageList.class new file mode 100644 index 0000000..67ad942 Binary files /dev/null and b/bin/and/AIL/MessageList.class differ diff --git a/bin/and/AIL/MessageTabBool.class b/bin/and/AIL/MessageTabBool.class new file mode 100644 index 0000000..f5db345 Binary files /dev/null and b/bin/and/AIL/MessageTabBool.class differ diff --git a/bin/and/AIL/MessageTabDouble.class b/bin/and/AIL/MessageTabDouble.class new file mode 100644 index 0000000..1517b96 Binary files /dev/null and b/bin/and/AIL/MessageTabDouble.class differ diff --git a/bin/and/AIL/MessageTabInt.class b/bin/and/AIL/MessageTabInt.class new file mode 100644 index 0000000..9f32ed0 Binary files /dev/null and b/bin/and/AIL/MessageTabInt.class differ diff --git a/src/and/AIL/AILObject.java b/src/and/AIL/AILObject.java new file mode 100644 index 0000000..2cdbb6e --- /dev/null +++ b/src/and/AIL/AILObject.java @@ -0,0 +1,1162 @@ +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 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 !! **/ diff --git a/src/and/AIL/Convergence.java b/src/and/AIL/Convergence.java new file mode 100644 index 0000000..b9b311f --- /dev/null +++ b/src/and/AIL/Convergence.java @@ -0,0 +1,190 @@ +package and.AIL ; + +import java.io.Serializable; +import org.objectweb.proactive.api.PAGroup; +import org.objectweb.proactive.core.group.Group; + +/** + * Class which manages the convergence of a problem. + * @author Sébastien Miquée + * @version 3.0 + */ +public class Convergence implements Serializable +{ + /** Local variables **/ + private static final long serialVersionUID = 1L ; + private boolean total[] ; + private short nbconv[] ; + private short taille ; + private boolean conv ; + private Group as ; + private boolean fin ; + private short attente ; + private short maxIterBeforeConv ; + private short __tag = 999 ; + + /** + * Empty constructor for the stub generation. + */ + public Convergence() + { + } + + /** + * Constructor. + * @param nb : number of worker. + */ + public Convergence( short nb ) + { + taille = nb ; + total = new boolean[ taille ] ; + nbconv = new short[ taille ] ; + conv = false ; + fin = false ; + maxIterBeforeConv = 0 ; + attente = taille ; + as = null ; + + for( short i = 0 ; i < taille ; i++ ) + { + nbconv[ i ] = 0 ; + } + } + + /** + * Set the ALPA group, in order to communicate with workers. + * @param group : AALPA group. + */ + public void majGroup( AILObject group ) + { + as = PAGroup.getGroup( group ) ; + } + + /** + * Method to detect convergence in asynchronous mode. + * @param id : rank of the worker. + * @param b : convergence detected by the worker. + */ + public synchronized void convergeAsync( short id, boolean b ) + { + if( ! conv ) + { + if( b ) + { + if( maxIterBeforeConv != 0 ) + nbconv[ id ]++ ; + } else { + nbconv[ id ] = 0 ; + } + + if( nbconv[ id ] == maxIterBeforeConv ) + { + total[ id ] = b ; + nbconv[ id ] = 0 ; + } + + boolean tmp = true ; + + for( short i = 0 ; i < taille ; i++ ) + { + tmp = tmp && total[i] ; + } + + conv = tmp ; + } + + if( as != null ) + { + as.get( id ).recvObject( taille, conv, __tag ) ; + } + + } + + /** + Method to detect convergence in synchronous mode. + * @param id : rank of the worker. + * @param b : convergence detected by the worker. + */ + public synchronized void convergeSync( short id, boolean b ) + { + if( ! conv ) + { + total[ id ] = b ; + + attente-- ; + + if( attente == 0 ) + { + boolean tmp = true ; + + for( short i = 0 ; i < taille ; i++ ) + { + tmp = tmp && total[i] ; + } + + conv = tmp ; + + for( short i = 0 ; i < taille ; i++ ) + { + as.get( i ).recvObject( taille, conv, __tag ) ; + } + + attente = taille ; + } + + } else { + as.get( id ).recvObject( taille, conv, __tag ) ; + } + } + + /** + * Initialization of the convergence. + */ + public void init() + { + total = new boolean[ taille ] ; + nbconv = new short[ taille ] ; + conv = false ; + attente = taille ; + + for( short i = 0 ; i < taille ; i++ ) + { + nbconv[ i ] = 0 ; + } + } + + /** + * Sets the number of time the convergence should be detected by a worker. + * (it is in the way to avoid the yo-yo effect) + * It is used only in asynchronous mode. + * @param _nb + */ + public void setIterBeforeConv( short _nb ) + { + if( _nb >= 0 ) + { + maxIterBeforeConv = _nb ; + } else { + maxIterBeforeConv = 0 ; + } + } + + /** + * Indicates that all workers have stopped. + */ + public void termine() + { + fin = true ; + } + + /** + * Return the status of all workers, to the Main program. + * @return : the status of all workers. + */ + public boolean fin() + { + return fin ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/Message.java b/src/and/AIL/Message.java new file mode 100644 index 0000000..64e682b --- /dev/null +++ b/src/and/AIL/Message.java @@ -0,0 +1,39 @@ +package and.AIL ; + +import java.io.Serializable; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public abstract class Message implements Serializable +{ + /** + * + */ + private static final long serialVersionUID = 1L; + + protected short tag ; + + protected short sender ; + + protected short type ; + + /*************/ + /** Getters **/ + /*************/ + + /** + * Return the tag of the message. + * @return the tag of the message + */ + public int getTag() { return tag ; } + + public int getSender() { return sender ; } + + public int getType() { return type ; } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageBool.java b/src/and/AIL/MessageBool.java new file mode 100644 index 0000000..4abd165 --- /dev/null +++ b/src/and/AIL/MessageBool.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageBool extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private boolean bool ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageBool() {} + + /** + * Constructor. + * For Message which contains a Boolean. + * @param _tag : tag of the Message. + * @param _o : the Boolean object. + */ + public MessageBool( int _id, int _tag, boolean _o ) + { + super() ; + sender = (short) _id ; + tag = (short) _tag ; + bool = _o ; + type = 4 ; + } + + public MessageBool( int _tag, boolean _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + bool = _o ; + type = 4 ; + } + + /** + * Returns the boolean value. + * @return : the boolean value of the Message. + */ + public boolean getBool() + { + return bool ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageDouble.java b/src/and/AIL/MessageDouble.java new file mode 100644 index 0000000..334b1b4 --- /dev/null +++ b/src/and/AIL/MessageDouble.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageDouble extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private double d ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageDouble() {} + + /** + * Constructor. + * For Message which contains a Double. + * @param _tag : tag of the Message. + * @param _o : the Double object. + */ + public MessageDouble( int _id, int _tag, double _o ) + { + super() ; + sender = (short)_id ; + tag = (short)_tag ; + d = _o ; + type = 2 ; + } + + public MessageDouble( int _tag, double _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + d = _o ; + type = 2 ; + } + + /** + * Returns the double value. + * @return : the double value of the Message. + */ + public double getDouble() + { + return d ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageInt.java b/src/and/AIL/MessageInt.java new file mode 100644 index 0000000..8426437 --- /dev/null +++ b/src/and/AIL/MessageInt.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageInt extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private int i ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageInt() {} + + /** + * Constructor. + * For Message which contains an Integer. + * @param _tag : tag of the Message. + * @param _o : the Integer object. + */ + public MessageInt( int _id, int _tag, int _o ) + { + super() ; + sender = (short)_id ; + tag = (short)_tag ; + i = _o ; + type = 0 ; + } + + public MessageInt( int _tag, int _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + i = _o ; + type = 0 ; + } + + /** + * Returns the integer value. + * @return : the tabular of doubles of the Message. + */ + public int getInt() + { + return i ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageList.java b/src/and/AIL/MessageList.java new file mode 100644 index 0000000..57be578 --- /dev/null +++ b/src/and/AIL/MessageList.java @@ -0,0 +1,112 @@ +package and.AIL ; + +import java.util.ArrayList; + +/** + * Class which contains and manages a messages list. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageList +{ + /** Local variables **/ + private ArrayList tab[] ; + private int taille ; + + /** + * Empty constructor for stub generation. + */ + public MessageList() + { + } + + /** + * Constructor. + * @param nb : size of the list. + */ + @SuppressWarnings( "unchecked" ) + public MessageList( Integer nb ) + { + taille = nb.intValue() ; + + tab = new ArrayList[ taille ] ; + for( int i = 0 ; i < taille ; i++ ) + { + tab[i] = new ArrayList() ; + } + } + + /** + * Search and returns, if it exists, a Message in the list _noList + * with the tag _tag. Returns null if there is no Message. + * @param _noList : index of the list. + * @param _tag : the tag of the Message. + * @return : the Message. + */ + public synchronized Message searchMsg( int _noList, int _tag ) + { + if( _noList >= 0 && _noList < taille ) + { + int pos = searchPos( _noList, _tag ) ; + + if( pos != -1 ) + { + return tab[ _noList ].remove( pos ) ; + } + } + + return null ; + } + + /** + * Add a Message _mes in the list of index _noList. + * @param _noList : index of the list. + * @param _mes : Message to add. + */ + public synchronized void addMessage( int _noList, Message _mes ) + { + if( _noList >= 0 && _noList < taille ) + { + int _t = _mes.getTag() ; + /** Search if message exists **/ + int pos = searchPos( _noList, _t ) ; + + if( pos != -1 ) + { + tab[ _noList ].set( pos, _mes ) ; + } else { + /** Add the message **/ + tab[ _noList ].add( _mes ) ; + } + } + } + + + /** + * Search the position of a Message with a specific tag _tag. + * If it doesn't exist, -1 will be returned. + * @param id : index of the sender. + * @param tag : tag of the Message. + * @return : the position in the list. + */ + private int searchPos( int _id, int _tag ) + { + int t = tab[ _id ].size() ; + + for( int i = 0 ; i < t ; i++ ) + { + try { + if( tab[ _id ].get( i ).getTag() == _tag ) + { + return i ; + } + } catch( Exception e ) { + return -1 ; + } + } + + return -1 ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageTabBool.java b/src/and/AIL/MessageTabBool.java new file mode 100644 index 0000000..a034fde --- /dev/null +++ b/src/and/AIL/MessageTabBool.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageTabBool extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private boolean[] tabBool ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageTabBool() {} + + /** + * Constructor. + * For Message which contains a tabular of Booleans. + * @param _tag : tag of the Message. + * @param _o : the Boolean object. + */ + public MessageTabBool( int _id, int _tag, boolean[] _o ) + { + super() ; + sender = (short)_id ; + tag = (short)_tag ; + tabBool = _o ; + type = 5 ; + } + + public MessageTabBool( int _tag, boolean[] _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + tabBool = _o ; + type = 5 ; + } + + /** + * Returns the boolean value. + * @return : the boolean value of the Message. + */ + public boolean[] getTabBool() + { + return tabBool ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageTabDouble.java b/src/and/AIL/MessageTabDouble.java new file mode 100644 index 0000000..09fc789 --- /dev/null +++ b/src/and/AIL/MessageTabDouble.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageTabDouble extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private double[] tabd ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageTabDouble() {} + + /** + * Constructor. + * For Message which contains a Double. + * @param _tag : tag of the Message. + * @param _o : the tabular of Doubles. + */ + public MessageTabDouble( int _id, int _tag, double[] _o ) + { + super() ; + sender = (short)_id ; + tag = (short)_tag ; + tabd = _o ; + type = 3 ; + } + + public MessageTabDouble( int _tag, double[] _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + tabd = _o ; + type = 3 ; + } + + /** + * Returns the tabular of doubles. + * @return : the integer value of the Message. + */ + public double[] getTabDouble() + { + return tabd ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/ diff --git a/src/and/AIL/MessageTabInt.java b/src/and/AIL/MessageTabInt.java new file mode 100644 index 0000000..4b9da50 --- /dev/null +++ b/src/and/AIL/MessageTabInt.java @@ -0,0 +1,57 @@ +package and.AIL ; + +/** + * Class which contains the Object of the message (integer, double, tabular...) + * and the tag associated to this message. + * @author Sébastien Miquée + * @version 3.0 + */ +public class MessageTabInt extends Message +{ + /** + * + */ + private static final long serialVersionUID = 1L; + /** Local variables **/ + private int[] tabi ; + + /** + * Empty constructor for the Stub generation. + */ + public MessageTabInt() {} + + /** + * Constructor. + * For Message which contains a tabular of Integers. + * @param _tag : tag of the Message. + * @param _o : the tabular of Integers. + */ + public MessageTabInt( int _id, int _tag, int[] _o ) + { + super() ; + sender = (short)_id ; + tag = (short)_tag ; + tabi = _o ; + type = 1 ; + } + + public MessageTabInt( int _tag, int[] _o ) + { + super() ; + sender = -1 ; + tag = (short)_tag ; + tabi = _o ; + type = 1 ; + } + + /** + * Returns the tabular of integers. + * @return : the tabular of integers of the Message. + */ + public int[] getTabInt() + { + return tabi ; + } +} + +/** La programmation est un art, respectons ceux qui la pratiquent !! **/