001    package toolbus.communication;
002    
003    import aterm.ATerm;
004    
005    /**
006     * This class enables direct communication (through java method calls) between a client and a
007     * server, under the condition that they are running the the same VM. This class does nothing fancy,
008     * it just passes messages between the data handler and its I/O handler counter part.
009     * 
010     * @author Arnold Lankamp
011     */
012    public class DirectIOHandler implements IIOHandler{
013            private final IDataHandler dataHandler;
014            private volatile DirectIOHandler directIOHandler = null;
015    
016            /**
017             * Constructor.
018             * 
019             * @param dataHandler
020             *            The data handler this I/O handler is associated with.
021             */
022            public DirectIOHandler(IDataHandler dataHandler){
023                    super();
024    
025                    this.dataHandler = dataHandler;
026            }
027    
028            /**
029             * Links this I/O handler with its counter part.
030             * 
031             * @param directIOHandler
032             *            The counter part of this I/O handler.
033             */
034            public void setDirectIOHandler(DirectIOHandler directIOHandler){
035                    this.directIOHandler = directIOHandler;
036            }
037    
038            /**
039             * @see IIOHandler#receive(byte, ATerm)
040             */
041            public void receive(byte operation, ATerm aTerm){
042                    dataHandler.receive(operation, aTerm);
043            }
044    
045            /**
046             * @see IIOHandler#send(byte, ATerm)
047             */
048            public void send(byte operation, ATerm aTerm){
049                    directIOHandler.receive(operation, aTerm);
050            }
051    
052            /**
053             * @see IIOHandler#terminate()
054             */
055            public void terminate(){
056                    directIOHandler.shutDown();
057            }
058    
059            /**
060             * @see IIOHandler#shutDown()
061             */
062            public void shutDown(){
063                    dataHandler.shutDown();
064            }
065            
066            /**
067             * @see IIOHandler#exceptionOccured()
068             */
069            public void exceptionOccured(){
070                    dataHandler.exceptionOccured();
071            }
072    }