View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.interloper;
30  
31  import java.net.InetSocketAddress;
32  import java.io.IOException;
33  
34  import org.apache.mina.io.IoFilter;
35  import org.apache.mina.io.filter.IoThreadPoolFilter;
36  import org.apache.mina.io.socket.SocketConnector;
37  import org.apache.mina.protocol.ProtocolHandler;
38  import org.apache.mina.protocol.ProtocolFilter;
39  import org.apache.mina.protocol.ProtocolProvider;
40  import org.apache.mina.protocol.ProtocolSession;
41  import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
42  import org.apache.mina.protocol.io.IoProtocolConnector;
43  
44  import org.apache.log4j.Logger;
45  
46  import uk.ac.rdg.resc.jstyx.StyxUtils;
47  
48  /***
49   * The client-side part of an Interloper
50   *
51   * @author Jon Blower
52   * $Revision: 313 $
53   * $Date: 2005-07-08 16:23:04 +0100 (Fri, 08 Jul 2005) $
54   * $Log$
55   * Revision 1.5  2005/07/08 15:23:03  jonblower
56   * Upgraded MINA library to 0.7.3-SNAPSHOT
57   *
58   * Revision 1.4  2005/05/05 16:57:37  jonblower
59   * Updated MINA library to revision 168337 and changed code accordingly
60   *
61   * Revision 1.3  2005/03/11 14:01:59  jonblower
62   * Merged MINA-Test_20059309 into main line of development
63   *
64   * Revision 1.2.2.2  2005/03/11 08:30:30  jonblower
65   * Moved to log4j logging system (from apache commons logging)
66   *
67   * Revision 1.2.2.1  2005/03/10 14:31:48  jonblower
68   * Modified for MINA framework
69   *
70   * Revision 1.1.1.1  2005/02/16 18:58:26  jonblower
71   * Initial import
72   *
73   */
74  public class InterloperClient
75  {
76      private static final Logger log = Logger.getLogger(InterloperClient.class);
77      
78      private static final int CONNECT_TIMEOUT = 30; // seconds
79      
80      private ProtocolSession session;
81      private ProtocolSession serverSession;
82      private IoThreadPoolFilter ioThreadPoolFilter;
83      private ProtocolThreadPoolFilter protocolThreadPoolFilter;
84      
85      private InterloperListener listener;
86      
87      private InetSocketAddress sockAddress;
88      
89      /*** Creates a new instance of InterloperClient */
90      public InterloperClient(InetSocketAddress sockAddress,
91          ProtocolSession serverSession, InterloperListener listener)
92      {
93          this.sockAddress = sockAddress;
94          this.serverSession = serverSession;
95          this.listener = listener;
96      }    
97      
98      /***
99       * Starts the InterloperClient
100      * @return true if the client was started successfully, false otherwise
101      */
102     public boolean start()
103     {
104         this.ioThreadPoolFilter = new IoThreadPoolFilter();
105         this.protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
106 
107         this.ioThreadPoolFilter.start();
108         this.protocolThreadPoolFilter.start();
109 
110         IoProtocolConnector connector = new IoProtocolConnector( new SocketConnector() );
111         
112         connector.getIoConnector().getFilterChain().addLast( "IO Thread pool filter",
113             ioThreadPoolFilter );
114         connector.getFilterChain().addLast( "Protocol Thread pool filter",
115             protocolThreadPoolFilter );
116         
117         ProtocolProvider protocolProvider =
118             new StyxInterloperProtocolProvider(this.serverSession, this.listener);
119         
120         try
121         {
122             this.session = connector.connect( this.sockAddress, CONNECT_TIMEOUT,
123                 protocolProvider );
124         }
125         catch( IOException e )
126         {
127             log.error("Failed to connect: " + e.getMessage());
128             return false;
129         }
130         return true;
131     }
132     
133     public void stop()
134     {        
135         // stop threads
136         this.ioThreadPoolFilter.stop();
137         this.protocolThreadPoolFilter.start();
138     }
139     
140     public void send(Object message)
141     {
142         session.write(message);
143     }
144 }