Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Création du dépôt AIL-PA
[ail-pa.git] / src / and / AIL / AILObject.java
1 package and.AIL ;
2
3 /*************************************************
4  *                                               *
5  * Classe permettant d'échanger des objets entre *
6  * les différents programmes actifs.             *
7  *                                               *
8  *                                               *
9  * Auteur : Sébastien Miquée -- miquee@gmail.com *
10  *                                               *
11  * Date : 21-05-2008                             *
12  * Date : 27-01-2009                             *
13  *                                               *
14  *************************************************/
15
16
17 /** Imports **/
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.Serializable;
24 import java.net.ServerSocket;
25 import java.net.Socket;
26
27 import org.objectweb.proactive.Body;
28 import org.objectweb.proactive.api.PAActiveObject;
29 import org.objectweb.proactive.api.PAGroup;
30 import org.objectweb.proactive.api.PASPMD;
31 import org.objectweb.proactive.core.group.Group;
32 import org.objectweb.proactive.core.node.Node;
33 import org.objectweb.proactive.core.node.NodeException;
34
35
36 /**
37  * This class permits to exchange Objects 
38  * between ActiveObjects of a program. This class allows
39  * to exchange multiple data with the same receiver with
40  * the use of tagged messages.
41  * 
42  * @author Sébastien Miquée
43  * @version 3.0
44  */
45 public class AILObject implements Serializable 
46 {       
47         private static final long serialVersionUID = 1L ;
48         /** Local variables **/
49         protected int myId = -1 ;
50         protected MessageList ml ;
51         protected int groupsize = 0 ;   
52         protected Object lockint ;
53         protected Object locktint ;
54         protected Object lockdouble ;
55         protected Object locktdouble ;
56         protected Object lockbool ;
57         protected Object locktbool ;
58         private int mode = 0 ;  // 0 : RMI ; 1 : Socket
59         protected Group<AILObject> group ;
60         private ServerSock servsock = null ;
61         private short cdirectory ;
62         protected String tab_url[] ;
63         protected short tab_port[] ;
64         protected String url = "" ;
65         protected short port = -1 ;
66         private short init = 0 ;
67         
68
69         /**
70          * Empty constructor for the Stub generation.
71          */
72         public AILObject() {}
73         
74
75         /**
76          * Set the communication mode of the library\n
77          *   - 0 : RMI protocole\n
78          *   - 1 : Socket protocole\n
79          * @param _mode : communication mode
80          */
81         public void setMode( int _mode )
82         {               
83                 if( init != 1 )
84                 {
85                         AILinit( ) ;
86                 }
87                 
88                 if( _mode == 1 && mode != 1 )
89                 {
90                         Node n = null ;
91                         try {
92                                 n = PAActiveObject.getNode() ;
93                         } catch( NodeException e1 ) {
94                                 e1.printStackTrace() ;
95                         }
96                         
97                         url = n.getNodeInformation().getURL().split("/")[2].split(":")[0] ;
98         
99                         servsock = new ServerSock( ) ;  
100
101                         createDirectory() ;
102
103                 }
104                 
105                 if( _mode == 0 && mode == 1)
106                 {
107                         servsock.halt() ;
108                 }
109                         
110                 mode = (short) _mode ;  
111         }
112         
113         protected void setUrl( String _url )
114         {
115                 url = _url ;
116         }
117         
118         public String getUrl( )
119         {
120                 return url ;
121         }
122         
123         protected void setPort( short _port )
124         {
125                 port = _port ;
126         }
127         
128         public int getPort()
129         {
130                 return port ;
131         }
132         
133         
134         /**
135          * Return the communication mode of the library
136          * @return the communication mode
137          */
138         public int getMode()
139         {
140                 return mode ;
141         }
142         
143         /**
144          * Print the url on which this active object is.
145          */
146         public void echoUrl()
147         {
148                 Body body ;
149                 body = PAActiveObject.getBodyOnThis() ;
150                 int rank = PASPMD.getMyRank() ;
151                 
152                 System.out.println("I am AILObject " + rank + " on node " + body.getNodeURL() ) ;
153         }
154         
155         private void createDirectory()
156         {
157                 if( cdirectory == 0 )
158                 {
159                         PASPMD.totalBarrier( "Directory Creation" ) ;
160                         
161                         int i = 0 ;
162                         for( i = 0 ; i < groupsize ; i++ )
163                         {
164                                 if( i != myId )
165                                 {                                       
166                                         tab_url[i] = group.get(i).getUrl() ; 
167                                         tab_port[i] = (short) group.get(i).getPort() ;
168                                 } else {
169                                         tab_url[i] = this.url ;
170                                         tab_port[i] = this.port ;
171                                 }
172                         }
173                         
174                         cdirectory = 1 ;
175                 }
176         } 
177         
178         public void AILend()
179         {
180                 if( mode == 1 )
181                 {
182                         servsock.halt() ;
183                 }
184         }
185         
186         
187         /**
188          * Initialization of the object AILObject.
189          */
190         public void AILinit( )
191         {
192                 if( init != 1 )
193                 {
194                         PASPMD.totalBarrier( "Init" ) ;
195                         
196                         lockint = new Object() ;
197                         locktint = new Object() ;
198                         lockdouble = new Object() ;
199                         locktdouble = new Object() ;
200                         lockbool = new Object() ;
201                         locktbool = new Object() ;
202                 
203                         group = PAGroup.getGroup( (AILObject) PASPMD.getSPMDGroup() ) ;
204                         myId = PASPMD.getMyRank() ;
205                         groupsize = PASPMD.getMySPMDGroupSize() ;
206                         
207                         ml = new MessageList( groupsize + 1 ) ;
208                         
209                         tab_url = new String[groupsize] ;
210                         tab_port = new short[groupsize] ;
211                         
212                 
213                         /** Parallelization of methods, to non block the Object **/
214                         PAActiveObject.setImmediateService( "getUrl", new Class[]{}) ;
215                         PAActiveObject.setImmediateService( "getPort", new Class[]{}) ;
216                         
217                         PAActiveObject.setImmediateService( "getIntBl", new Class[]{int.class, int.class}) ;
218                         PAActiveObject.setImmediateService( "getTabIntBl", new Class[]{int.class, int.class}) ;
219                         PAActiveObject.setImmediateService( "getDoubleBl", new Class[]{int.class, int.class}) ;
220                         PAActiveObject.setImmediateService( "getTabDoubleBl", new Class[]{int.class, int.class}) ;
221                         PAActiveObject.setImmediateService( "getBoolBl", new Class[]{int.class, int.class}) ;
222                         PAActiveObject.setImmediateService( "getTabBoolBl", new Class[]{int.class, int.class}) ;
223                         
224                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, int.class, int.class}) ;
225                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, int[].class, int.class}) ;
226                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, double.class, int.class}) ;
227                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, double[].class, int.class}) ;
228                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, boolean.class, int.class}) ;
229                         PAActiveObject.setImmediateService( "recvObject", new Class[]{int.class, boolean[].class, int.class}) ;
230                         
231                         init = 1 ;
232                 }
233         }
234         
235         /*********************/
236         /** Sending Objects **/
237         /*********************/
238         
239         /**
240          * Send an Integer _o from the id id_src with the tag _tag.
241          * Puts a new Message, composed of the Integer _o and the tag _tag
242          * in the MessageList of this object.
243          * @param _int : the Integer to send.
244          * @param idDst : the id of the sender.
245          * @param _tag : the tag of the Message.
246          */
247         protected void sendObject( int _idDst, int _int, int _tag )
248         {
249                 if( _idDst >= 0 && _idDst < groupsize + 1 )
250                 {       
251                         if( mode == 0 )  // RMI
252                         {
253                                 ((AILObject)group.get( _idDst )).recvObject( myId, _int, _tag ) ;
254                         } 
255                         
256                         if( mode == 1 ) // Socket
257                         {
258                                 try {
259                                         Socket soc = new Socket() ;
260                                         
261                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
262                                         
263                                         soc.setReuseAddress( true ) ;
264                                         soc.setSendBufferSize( 2048 * 2048 ) ;
265                                         soc.setSoLinger( false, 1 ) ;
266                                         soc.setTcpNoDelay( false ) ;
267                            
268                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
269                                         
270                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
271                                         
272                                         output.writeObject( new MessageInt( myId, _tag, _int ) ) ;
273                                         
274                                         output.close() ;
275                                         out.close() ;
276                                         soc.close() ;
277
278                                 } catch( Exception e ) {
279                                         e.printStackTrace() ;
280                                 }
281                                 
282                         }
283                 }
284         }
285         
286         /**
287          * Reception of an Integer with the RMI method
288          * @param _idSrc : id of the sender
289          * @param _int : the Integer to receive
290          * @param _tag : the tag of the message
291          */
292         public void recvObject( int _idSrc, int _int, int _tag )
293         {
294                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
295                 {       
296                         ml.addMessage( _idSrc, new MessageInt( _tag, _int ) ) ;
297                         
298                         synchronized( lockint )
299                         {
300                                 lockint.notifyAll();
301                         }
302                 }
303         }
304         
305         
306         /**
307          * Send a tabular of Integers _tint to the id idDst with the tag _tag.
308          * Puts a new Message, composed of the tabular of Integers _tint and the tag _tag
309          * in the MessageList of this object.
310          * @param _tint : the tabular of Integers to send.
311          * @param idDst : the id of the receiver.
312          * @param _tag : the tag of the Message.
313          */
314         protected void sendObject( int _idDst, int[] _tint, int _tag )
315         {
316                 if( _idDst >= 0 && _idDst < groupsize + 1 )
317                 {       
318                         if( mode == 0 )  // RMI
319                         {
320                                 ((AILObject)group.get( _idDst )).recvObject( myId, _tint, _tag ) ;
321                         } 
322                         
323                         if( mode == 1 ) // Socket
324                         {
325                                 try {
326                                         Socket soc = new Socket() ;
327                                         
328                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
329                                         
330                                         soc.setReuseAddress( true ) ;
331                                         soc.setSendBufferSize( 2048 * 2048 ) ;
332                                         soc.setSoLinger( false, 1 ) ;
333                                         soc.setTcpNoDelay( false ) ;
334                            
335                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
336                                         
337                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
338                                         
339                                         output.writeObject( new MessageTabInt( myId, _tag, _tint ) ) ;
340                                         
341                                         output.close() ;
342                                         out.close() ;
343                                         soc.close() ;
344
345                                 } catch( Exception e ) {
346                                         e.printStackTrace() ;
347                                 }
348                                 
349                         }
350                 }
351         }
352         
353         /**
354          * Reception of a tabular of Integers with the RMI method
355          * @param _idSrc : id of the sender
356          * @param _tint : the tabular of Integers to receive
357          * @param _tag : the tag of the message
358          */
359         public void recvObject( int _idSrc, int[] _tint, int _tag )
360         {
361                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
362                 {       
363                         ml.addMessage( _idSrc, new MessageTabInt( _tag, _tint ) ) ;
364                         
365                         synchronized( locktint )
366                         {
367                                 locktint.notifyAll();
368                         }
369                 }
370         }
371
372         
373         /**
374          * Send a Double _double to the id idDst with the tag _tag.
375          * Puts a new Message, composed of the Double _double and the tag _tag
376          * in the MessageList of this object.
377          * @param _double : the Double to send.
378          * @param idDst : the id of the sender.
379          * @param _tag : the tag of the Message.
380          */
381         protected void sendObject( int _idDst, double _double, int _tag )
382         {
383                 if( _idDst >= 0 && _idDst < groupsize + 1 )
384                 {       
385                         if( mode == 0 )  // RMI
386                         {
387                                 ((AILObject)group.get( _idDst )).recvObject( myId, _double, _tag ) ;
388                         } 
389                         
390                         if( mode == 1 ) // Socket
391                         {
392                                 try {
393                                         Socket soc = new Socket() ;
394                                         
395                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
396                                         
397                                         soc.setReuseAddress( true ) ;
398                                         soc.setSendBufferSize( 2048 * 2048 ) ;
399                                         soc.setSoLinger( false, 1 ) ;
400                                         soc.setTcpNoDelay( false ) ;
401                            
402                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
403                                         
404                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
405                                         
406                                         output.writeObject( new MessageDouble( myId, _tag, _double ) ) ;
407                                         
408                                         output.close() ;
409                                         out.close() ;
410                                         soc.close() ;
411
412                                 } catch( Exception e ) {
413                                         e.printStackTrace() ;
414                                 }
415                                 
416                         }
417                 }
418         }
419         
420         /**
421          * Reception of a Double with the RMI method
422          * @param _idSrc : id of the sender
423          * @param _double : the double to receive
424          * @param _tag : the tag of the message
425          */
426         public void recvObject( int _idSrc, double _double, int _tag )
427         {
428                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
429                 {       
430                         ml.addMessage( _idSrc, new MessageDouble( _tag, _double ) ) ;
431                         
432                         synchronized( lockdouble )
433                         {
434                                 lockdouble.notifyAll();
435                         }
436                 }
437         }
438         
439         /**
440          * Send a tabular of Doubles _tint to the id idDst with the tag _tag.
441          * Puts a new Message, composed of the tabular of Doubles _tint and the tag _tag
442          * in the MessageList of this object.
443          * @param _tdouble : the tabular of Doubles to send.
444          * @param idDst : the id of the receiver.
445          * @param _tag : the tag of the Message.
446          */
447         protected void sendObject( int _idDst, double[] _tdouble, int _tag )
448         {
449                 if( _idDst >= 0 && _idDst < groupsize + 1 )
450                 {       
451                         if( mode == 0 )  // RMI
452                         {
453                                 ((AILObject)group.get( _idDst )).recvObject( myId, _tdouble, _tag ) ;
454                         } 
455                         
456                         if( mode == 1 ) // Socket
457                         {
458                                 try {
459                                         Socket soc = new Socket() ;
460                                         
461                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
462                                         
463                                         soc.setReuseAddress( true ) ;
464                                         soc.setSendBufferSize( 2048 * 2048 ) ;
465                                         soc.setSoLinger( false, 1 ) ;
466                                         soc.setTcpNoDelay( false ) ;
467                            
468                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
469                                         
470                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
471                                         
472                                         output.writeObject( new MessageTabDouble( myId, _tag, _tdouble ) ) ;
473                                         
474                                         output.close() ;
475                                         out.close() ;
476                                         soc.close() ;
477                                         
478                                 } catch( Exception e ) {
479                                         e.printStackTrace() ;
480                                 }
481                                 
482                         }
483                 }
484         }
485         
486         /**
487          * Reception of a tabular of Doubles with the RMI method
488          * @param _idSrc : id of the sender
489          * @param _tdouble : the tabular of Doubles to receive
490          * @param _tag : the tag of the message
491          */
492         public void recvObject( int _idSrc, double[] _tdouble, int _tag )
493         {
494                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
495                 {       
496                         ml.addMessage( _idSrc, new MessageTabDouble( _tag, _tdouble ) ) ;
497                         
498                         synchronized( locktdouble )
499                         {
500                                 locktdouble.notifyAll();
501                         }
502                 }
503         }
504         
505         
506         /**
507          * Send a Boolean _bool to the id id_src with the tag _tag.
508          * Puts a new Message, composed of the Boolean _bool and the tag _tag
509          * in the MessageList of this object.
510          * @param _bool : the Boolean to send.
511          * @param idDst : the id of the receiver.
512          * @param _tag : the tag of the Message.
513          */
514         protected void sendObject( int _idDst, boolean _bool, int _tag )
515         {
516                 if( _idDst >= 0 && _idDst < groupsize + 1 )
517                 {       
518                         if( mode == 0 )  // RMI
519                         {
520                                 ((AILObject)group.get( _idDst )).recvObject( myId, _bool, _tag ) ;
521                         } 
522                         
523                         if( mode == 1 ) // Socket
524                         {
525                                 try {
526                                         Socket soc = new Socket() ;
527                                         
528                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
529                                         
530                                         soc.setReuseAddress( true ) ;
531                                         soc.setSendBufferSize( 2048 * 2048 ) ;
532                                         soc.setSoLinger( false, 1 ) ;
533                                         soc.setTcpNoDelay( false ) ;
534                            
535                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
536                                         
537                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
538                                         
539                                         output.writeObject( new MessageBool( myId, _tag, _bool ) ) ;
540                                         
541                                         output.close() ;
542                                         out.close() ;
543                                         soc.close() ;
544                                         
545                                 } catch( Exception e ) {
546                                         e.printStackTrace() ;
547                                 }
548                                 
549                         }
550                 }
551         }
552         
553         /**
554          * Reception of a Boolean with the RMI method
555          * @param _idSrc : id of the sender
556          * @param _bool : the boolean to receive
557          * @param _tag : the tag of the message
558          */
559         public void recvObject( int _idSrc, boolean _bool, int _tag )
560         {
561                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
562                 {       
563                         ml.addMessage( _idSrc, new MessageBool( _tag, _bool ) ) ;
564                         
565                         synchronized( lockbool )
566                         {
567                                 lockbool.notifyAll();
568                         }
569                 }
570         }
571         
572         /**
573          * Send a tabular of Booleans _tint to the id idDst with the tag _tag.
574          * Puts a new Message, composed of the tabular of Booleans _tint and the tag _tag
575          * in the MessageList of this object.
576          * @param _tbool : the tabular of Booleans to send.
577          * @param idDst : the id of the receiver.
578          * @param _tag : the tag of the Message.
579          */
580         protected void sendObject( int _idDst, boolean[] _tbool, int _tag )
581         {
582                 if( _idDst >= 0 && _idDst < groupsize + 1 )
583                 {       
584                         if( mode == 0 )  // RMI
585                         {
586                                 ((AILObject)group.get( _idDst )).recvObject( myId, _tbool, _tag ) ;
587                         } 
588                         
589                         if( mode == 1 ) // Socket
590                         {
591                                 try {
592                                         Socket soc = new Socket() ;
593                                         
594                                         soc = new Socket( tab_url[_idDst], tab_port[_idDst] ) ;  
595                                         
596                                         soc.setReuseAddress( true ) ;
597                                         soc.setSendBufferSize( 2048 * 2048 ) ;
598                                         soc.setSoLinger( false, 1 ) ;
599                                         soc.setTcpNoDelay( false ) ;
600                            
601                                         BufferedOutputStream out = new BufferedOutputStream( soc.getOutputStream() );
602                                         
603                                         ObjectOutputStream output = new ObjectOutputStream( out ) ;
604                                         
605                                         output.writeObject( new MessageTabBool( myId, _tag, _tbool ) ) ;
606                                         
607                                         output.close() ;
608                                         out.close() ;
609                                         soc.close() ;
610                                         
611                                 } catch( Exception e ) {
612                                         e.printStackTrace() ;
613                                 }
614                                 
615                         }
616                 }
617         }
618         
619         /**
620          * Reception of a tabular of Booleans with the RMI method
621          * @param _idSrc : id of the sender
622          * @param _tdouble : the tabular of Booleans to receive
623          * @param _tag : the tag of the message
624          */
625         public void recvObject( int _idSrc, boolean[] _tbool, int _tag )
626         {
627                 if( _idSrc >= 0 && _idSrc < groupsize + 1 )
628                 {       
629                         ml.addMessage( _idSrc, new MessageTabBool( _tag, _tbool ) ) ;
630                         
631                         synchronized( locktbool )
632                         {
633                                 locktbool.notifyAll();
634                         }
635                 }
636         }
637
638
639         /*******************************************/
640         /** Getting Objects - Non waiting methods **/
641         /*******************************************/
642         
643         /**
644          * Returns the Integer from the id id_src, with the tag _tag.
645          * Search in the MessageList if there is corresponding Message. 
646          * @param id_src : index of the sender.
647          * @param _tag : tag of the Message.
648          * @return : the Integer.
649          */
650         public int getInt( int id_src, int _tag )
651         {
652                 if( id_src >= 0 && id_src < groupsize + 1 )
653                 {
654                         MessageInt mes = (MessageInt) ml.searchMsg( id_src, _tag ) ;
655                         
656                         if( mes != null )
657                         {
658                                 return mes.getInt() ;
659                         }
660                 } 
661                 
662                 return 0 ;
663         }
664         
665         /**
666          * Returns the tabular of Integers from the id id_src, with the tag _tag.
667          * Search in the MessageList if there is corresponding Message. 
668          * @param id_src : index of the sender.
669          * @param _tag : tag of the Message.
670          * @return : the tabular of Integers.
671          */
672         public int[] getTabInt( int id_src, int _tag )
673         {
674                 if( id_src >= 0 && id_src < groupsize + 1 )
675                 {
676                         MessageTabInt mes = (MessageTabInt) ml.searchMsg( id_src, _tag ) ;
677                         
678                         if( mes != null )
679                         {
680                                 return mes.getTabInt() ;
681                         }
682                 } 
683                 
684                 return null ;
685         }
686         
687         /**
688          * Returns the Double from the id id_src, with the tag _tag.
689          * Search in the MessageList if there is corresponding Message. 
690          * @param id_src : index of the sender.
691          * @param _tag : tag of the Message.
692          * @return : the Double.
693          */
694         public double getDouble( int id_src, int _tag )
695         {
696                 if( id_src >= 0 && id_src < groupsize + 1 )
697                 {
698                         MessageDouble mes = (MessageDouble) ml.searchMsg( id_src, _tag ) ;
699                         
700                         if( mes != null )
701                         {
702                                 return mes.getDouble() ;
703                         }
704                 } 
705                 
706                 return 0 ;
707         }
708         
709         /**
710          * Returns the tabular of Doubles from the id id_src, with the tag _tag.
711          * Search in the MessageList if there is corresponding Message. 
712          * @param id_src : index of the sender.
713          * @param _tag : tag of the Message.
714          * @return : the tabular of Doubles.
715          */
716         public double[] getTabDouble( int id_src, int _tag )
717         {
718                 if( id_src >= 0 && id_src < groupsize + 1 )
719                 {
720                         MessageTabDouble mes = (MessageTabDouble) ml.searchMsg( id_src, _tag ) ;
721                         
722                         if( mes != null )
723                         {
724                                 return mes.getTabDouble() ;
725                         }
726                 } 
727                 
728                 return null ;
729         }
730         
731         /**
732          * Returns the Boolean from the id id_src, with the tag _tag.
733          * Search in the MessageList if there is corresponding Message. 
734          * @param id_src : index of the sender.
735          * @param _tag : tag of the Message.
736          * @return : the Boolean.
737          */
738         public boolean getBool( int id_src, int _tag )
739         {
740                 if( id_src >= 0 && id_src < groupsize + 1 )
741                 {
742                         MessageBool mes = (MessageBool) ml.searchMsg( id_src, _tag ) ;
743                         
744                         if( mes != null )
745                         {
746                                 return mes.getBool() ;
747                         }
748                 } 
749                 
750                 return false ;
751         }
752         
753         /**
754          * Returns the tabular of Booleans from the id id_src, with the tag _tag.
755          * Search in the MessageList if there is corresponding Message. 
756          * @param id_src : index of the sender.
757          * @param _tag : tag of the Message.
758          * @return : the tabular of Booleans.
759          */
760         public boolean[] getTabBool( int id_src, int _tag )
761         {
762                 if( id_src >= 0 && id_src < groupsize + 1 )
763                 {
764                         MessageTabBool mes = (MessageTabBool) ml.searchMsg( id_src, _tag ) ;
765                         
766                         if( mes != null )
767                         {
768                                 return mes.getTabBool() ;
769                         }
770                 } 
771                 
772                 return null ;
773         }
774         
775         
776         /***************************************/
777         /** Getting Objects - Waiting methods **/
778         /***************************************/
779         
780         /**
781          * Returns the Integer from the id id_src, with the tag _tag.
782          * Search in the MessageList if there is corresponding Message. 
783          * Wait until a corresponding Message comes.
784          * @param id_src : index of the sender.
785          * @param _tag : tag of the Message.
786          * @return : the Integer.
787          */
788         public int getIntBl( int id_src, int _tag )
789         {
790                 if( id_src >= 0 && id_src < groupsize + 1 )
791                 {
792                         MessageInt mes = null ;
793
794                         while( mes == null )
795                         {
796                                 mes = (MessageInt) ml.searchMsg( id_src, _tag ) ;
797                                 
798                                 if( mes == null )
799                                 synchronized( lockint )
800                                 {
801                                         try {
802                                                 lockint.wait() ;
803                                         } catch (InterruptedException e) {
804                                                 e.printStackTrace();
805                                         }
806                                 }
807                         }
808
809                         return mes.getInt() ;
810                 } 
811
812                 return 0 ;
813         }
814         
815         /**
816          * Returns the tabular of Integers from the id id_src, with the tag _tag.
817          * Search in the MessageList if there is corresponding Message. 
818          * Wait until a corresponding Message comes.
819          * @param id_src : index of the sender.
820          * @param _tag : tag of the Message.
821          * @return : the tabular of Integers.
822          */
823         public int[] getTabIntBl( int id_src, int _tag )
824         {
825                 if( id_src >= 0 && id_src < groupsize + 1 )
826                 {
827                         MessageTabInt mes = null ;
828
829                         while( mes == null )
830                         {
831                                 mes = (MessageTabInt) ml.searchMsg( id_src, _tag ) ;
832                                 
833                                 if( mes == null )
834                                 synchronized( locktint )
835                                 {
836                                         try {
837                                                 locktint.wait() ;
838                                         } catch (InterruptedException e) {
839                                                 e.printStackTrace();
840                                         }
841                                 }
842                         }
843
844                         return mes.getTabInt() ;
845                 } 
846
847                 return null ;
848         }
849         
850         /**
851          * Returns the Double from the id id_src, with the tag _tag.
852          * Search in the MessageList if there is corresponding Message. 
853          * Wait until a corresponding Message comes.
854          * @param id_src : index of the sender.
855          * @param _tag : tag of the Message.
856          * @return : the Double.
857          */
858         public double getDoubleBl( int id_src, int _tag )
859         {
860                 if( id_src >= 0 && id_src < groupsize + 1 )
861                 {
862                         MessageDouble mes = null ;
863
864                         while( mes == null )
865                         {
866                                 mes = (MessageDouble) ml.searchMsg( id_src, _tag ) ;
867                                 
868                                 if( mes == null )
869                                 synchronized( lockdouble )
870                                 {
871                                         try {
872                                                 lockdouble.wait() ;
873                                         } catch (InterruptedException e) {
874                                                 e.printStackTrace();
875                                         }
876                                 }
877                         }
878                         
879                         return mes.getDouble() ;
880                 } 
881
882                 return 0 ;
883         }
884         
885         /**
886          * Returns the tabular of Doubles from the id id_src, with the tag _tag.
887          * Search in the MessageList if there is corresponding Message. 
888          * Wait until a corresponding Message comes.
889          * @param id_src : index of the sender.
890          * @param _tag : tag of the Message.
891          * @return : the tabular of Doubles.
892          */
893         public double[] getTabDoubleBl( int id_src, int _tag )
894         {
895                 if( id_src >= 0 && id_src < groupsize + 1 )
896                 {
897                         MessageTabDouble mes = null ;
898
899                         while( mes == null )
900                         {
901                                 mes = (MessageTabDouble) ml.searchMsg( id_src, _tag ) ;
902                                 
903                                 if( mes == null )
904                                 {
905                                         synchronized( locktdouble )
906                                         {
907                                                 try {
908                                                         locktdouble.wait() ;
909                                                 } catch (InterruptedException e) {
910                                                         e.printStackTrace();
911                                                 }
912                                         }
913                                 }
914                         }
915
916                         return mes.getTabDouble() ;
917                 } 
918
919                 return null ;
920         }
921         
922         /**
923          * Returns the Boolean from the id id_src, with the tag _tag.
924          * Search in the MessageList if there is corresponding Message.
925          * Wait until a corresponding Message comes. 
926          * @param id_src : index of the sender.
927          * @param _tag : tag of the Message.
928          * @return : the Boolean.
929          */
930         public boolean getBoolBl( int id_src, int _tag )
931         {
932                 if( id_src >= 0 && id_src < groupsize + 1 )
933                 {
934                         MessageBool mes = null ;
935
936                         while( mes == null )
937                         {
938                                 mes = (MessageBool) ml.searchMsg( id_src, _tag ) ;
939                                 
940                                 if( mes == null )
941                                 synchronized( lockbool )
942                                 {
943                                         try {
944                                                 lockbool.wait() ;
945                                         } catch (InterruptedException e) {
946                                                 e.printStackTrace();
947                                         }
948                                 }
949                         }
950
951                         return mes.getBool() ;
952                 } 
953
954                 return false ;
955         }
956         
957         /**
958          * Returns the tabular of Booleans from the id id_src, with the tag _tag.
959          * Search in the MessageList if there is corresponding Message. 
960          * Wait until a corresponding Message comes.
961          * @param id_src : index of the sender.
962          * @param _tag : tag of the Message.
963          * @return : the tabular of Booleans.
964          */
965         public boolean[] getTabBoolBl( int id_src, int _tag )
966         {
967                 if( id_src >= 0 && id_src < groupsize + 1 )
968                 {
969                         MessageTabBool mes = null ;
970
971                         while( mes == null )
972                         {
973                                 mes = (MessageTabBool) ml.searchMsg( id_src, _tag ) ;
974                                 
975                                 if( mes == null )
976                                 {
977                                         synchronized( locktbool )
978                                         {
979                                                 try {
980                                                         locktbool.wait() ;
981                                                 } catch (InterruptedException e) {
982                                                         e.printStackTrace();
983                                                 }
984                                         }
985                                 }
986                         }
987
988                         return mes.getTabBool() ;
989                 } 
990
991                 return null ;
992         }
993         
994         
995         /**
996          * 
997          * @author miquee
998          *
999          */
1000         class ServerSock extends Thread
1001         {
1002                 protected boolean ok ;
1003                 protected boolean exec ;
1004
1005                 public ServerSock( )
1006                 {               
1007                         start() ;
1008                 }
1009                 
1010                 
1011                 public void run()
1012                 {
1013                         ServerSocket s = null ;
1014                         ok = false ;
1015                         exec = true ;
1016                         
1017                         /** Determination of the address and port to listen **/                         
1018                         port = 2000 ;
1019                         
1020                         while( ! ok )
1021                         {
1022                                 ok = true ;
1023                                 try {
1024                                         s = new ServerSocket( port );
1025                                 } catch (IOException e1) {
1026                                         ok = false ;
1027                                         port++ ;
1028                                         
1029                                         if( port == 3001 )
1030                                         {
1031                                                 System.out.println( "Unable to bind port !!" );
1032                                                 System.exit(1) ;
1033                                         }
1034
1035                                         System.out.println( "Binding address already in use !" ) ;
1036                                 }
1037                         }   
1038                         
1039                         System.out.println( "I am AILObject " + myId + " with address " + url +" on port " + port ) ;
1040                         
1041                         try {
1042                                 while( exec )
1043                                 {       
1044                                         Socket soc = new Socket() ;
1045                                         
1046                                         soc = s.accept() ;
1047                                         
1048                                         soc.setReceiveBufferSize( 2048 * 2048 ) ;
1049                                         soc.setTcpNoDelay( false ) ;
1050                                         soc.setSoLinger( false, 1 ) ;
1051                                         soc.setReuseAddress( true ) ;
1052
1053                                         new ServClientSock( soc ) ;
1054                                 }
1055                         
1056                         } catch (Exception e) {
1057                                 e.printStackTrace() ;
1058                         }       
1059                 }       
1060                 
1061                 public void halt()
1062                 {
1063                         exec = false ;
1064                 }
1065                 
1066                 
1067                 class ServClientSock extends Thread 
1068                 {
1069                         private BufferedInputStream in = null ;
1070                         private Socket client = null ;
1071                         
1072                         public ServClientSock( Socket s ) throws IOException
1073                         {
1074                                 client = s ;
1075                                 
1076                                 start() ;
1077                         }
1078                         
1079                         
1080                         public void run()
1081                         {
1082                                 /** Receiving protocol :
1083                                  * --------------------
1084                                  * receiving integer which represents the id_src
1085                                  * receiving tag
1086                                  * receiving integer to indicate the type of data
1087                                  * receiving data
1088                                  */
1089                                 
1090                                 try {
1091                                         in = new BufferedInputStream( client.getInputStream() ) ;
1092                                         
1093                                         ObjectInputStream input = new ObjectInputStream( in ) ;
1094                                         
1095                                         Message m = (Message) input.readObject() ;
1096                                                                 
1097                                         ml.addMessage( m.getSender(), m ) ;
1098                                         
1099                                         try {
1100                                                 input.close() ;
1101                                                 in.close() ;
1102                                                 client.close() ;
1103                                         } catch (IOException e) {
1104                                                 e.printStackTrace();
1105                                         }
1106                                         
1107                                         switch( m.getType() )
1108                                         {
1109                                         case 0 :                                
1110                                                 synchronized( lockint )
1111                                                 {
1112                                                         lockint.notifyAll();
1113                                                 } ; break ;
1114                                                 
1115                                         case 1 :                                
1116                                                 synchronized( locktint )
1117                                                 {
1118                                                         locktint.notifyAll();
1119                                                 } ; break ;
1120                                                 
1121                                         case 2 :
1122                                                 synchronized( lockdouble )
1123                                                 {
1124                                                         lockdouble.notifyAll();
1125                                                 } ; break ;
1126                                                 
1127                                         case 3 :
1128                                                 synchronized( locktdouble )
1129                                                 {
1130                                                         locktdouble.notifyAll();
1131                                                 } ; break ;
1132                                                 
1133                                         case 4 :
1134                                                 synchronized( lockbool )
1135                                                 {
1136                                                         lockbool.notifyAll();
1137                                                 } ; break ;
1138                                                 
1139                                         case 5 :
1140                                                 synchronized( locktbool )
1141                                                 {
1142                                                         locktbool.notifyAll();
1143                                                 } ; break ;                             
1144                                                 
1145                                         default: ;      
1146                                         }
1147                                         
1148                                 } catch (IOException e) {
1149                                         e.printStackTrace();
1150                                 } catch (ClassNotFoundException e) {
1151                                         e.printStackTrace();
1152                                 } 
1153                                 
1154                                 
1155                         }
1156                         
1157                 }
1158         }
1159 }
1160
1161
1162 /** La programmation est un art, respectons ceux qui la pratiquent !! **/