KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > tcp > TcpProxyService


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  * Contributor(s): Alex Porras (MediaOcean)
24  */

25 package org.objectweb.joram.mom.proxies.tcp;
26
27 import java.net.*;
28 import java.util.*;
29
30 import fr.dyade.aaa.agent.*;
31 import fr.dyade.aaa.util.*;
32
33 import org.objectweb.joram.shared.JoramTracing;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 /**
37  * Starts a TCP entry point for MOM clients.
38  */

39 public class TcpProxyService {
40   /**
41    * Name the property that allow to fix the TCP SO_TIMEOUT property for the
42    * client's connections.
43    */

44   public static final String JavaDoc SO_TIMEOUT_PROP =
45       "org.objectweb.joram.mom.proxies.tcp.soTimeout";
46
47   /**
48    * Default value for the TCP SO_TIMEOUT property.
49    */

50   public static final int DEFAULT_SO_TIMEOUT = 10000;
51
52   /**
53    * Name the property that allow to fix the pool size for the
54    * connection's listener.
55    */

56   public static final String JavaDoc POOL_SIZE_PROP =
57       "org.objectweb.joram.mom.proxies.tcp.poolSize";
58
59   /**
60    * Default value for the pool size.
61    */

62   public static final int DEFAULT_POOL_SIZE = 1;
63
64   /**
65    * Name the property that allow to fix the TCP BACKLOG property for the
66    * client's connections.
67    */

68   public static final String JavaDoc BACKLOG_PROP =
69       "org.objectweb.joram.mom.proxies.tcp.backlog";
70
71   /**
72    * Default value for the TCP BACKLOG property.
73    */

74   public static final int DEFAULT_BACKLOG = 10;
75
76   /**
77    * Default value for the TCP port of the listen socket.
78    */

79   public static final int DEFAULT_PORT = 16010;
80
81   public static final String JavaDoc DEFAULT_BINDADDRESS = "0.0.0.0"; // all
82

83
84   /**
85    * The proxy service reference (used to stop it).
86    */

87   protected static TcpProxyService proxyService;
88
89   private static int port;
90
91   public static final int getListenPort() {
92     return port;
93   }
94
95   private static String JavaDoc address;
96
97   public static final String JavaDoc getListenAddress() {
98     return address;
99   }
100
101   /**
102    * Initializes the TCP entry point by creating a server socket listening
103    * to the specified port.
104    *
105    * @param args stringified listening port
106    * @param firstTime <code>true</code> when the agent server starts.
107    */

108   public static void init(String JavaDoc args, boolean firstTime) throws Exception JavaDoc {
109     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
110       JoramTracing.dbgProxy.log(BasicLevel.DEBUG,
111                                 "TcpProxyService.init(" + args + ',' + firstTime + ')');
112
113     port = DEFAULT_PORT;;
114     address = DEFAULT_BINDADDRESS;
115     if (args != null) {
116       StringTokenizer st = new StringTokenizer(args);
117       port = Integer.parseInt(st.nextToken());
118       if (st.hasMoreTokens()) {
119         address = st.nextToken();
120       }
121     }
122     
123     int backlog = Integer.getInteger(BACKLOG_PROP, DEFAULT_BACKLOG).intValue();
124
125     // Create the socket here in order to throw an exception
126
// if the socket can't be created (even if firstTime is false).
127
ServerSocket serverSocket;
128
129     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
130       JoramTracing.dbgProxy.log(BasicLevel.DEBUG,
131                                 "SSLTcpProxyService.init() - binding to " +
132                                 address + ", port " + port);
133
134     if (address.equals("0.0.0.0")) {
135       serverSocket = new ServerSocket(port, backlog);
136     } else {
137       serverSocket = new ServerSocket(port, backlog, InetAddress.getByName(address));
138     }
139
140     int poolSize = Integer.getInteger(POOL_SIZE_PROP, DEFAULT_POOL_SIZE).intValue();
141
142     int timeout = Integer.getInteger(SO_TIMEOUT_PROP, DEFAULT_SO_TIMEOUT).intValue();
143
144     proxyService = new TcpProxyService(serverSocket, poolSize, timeout);
145     proxyService.start();
146   }
147
148   /**
149    * Stops the service.
150    */

151   public static void stopService() {
152     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
153       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpProxyService.stop()");
154     proxyService.stop();
155   }
156
157   /**
158    * The listening server socket
159    */

160   private ServerSocket serverSocket;
161
162   /**
163    * The list of opened connections
164    */

165   private Vector connections;
166
167   /**
168    * The thread listening to incoming
169    * TCP connections.
170    */

171   private TcpConnectionListener[] connectionListeners;
172
173   public TcpProxyService(ServerSocket serverSocket,
174                          int poolSize,
175                          int timeout) {
176     this.serverSocket = serverSocket;
177     this.connections = new Vector();
178     connectionListeners = new TcpConnectionListener[poolSize];
179     for (int i = 0; i < poolSize; i++) {
180       connectionListeners[i] = new TcpConnectionListener(serverSocket,
181                                                          this,
182                                                          timeout);
183     }
184   }
185
186   protected void start() {
187     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
188       JoramTracing.dbgProxy.log(
189         BasicLevel.DEBUG, "TcpProxyService.start()");
190     for (int i = 0; i < connectionListeners.length; i++) {
191       connectionListeners[i].start();
192     }
193   }
194
195   void registerConnection(TcpConnection tcpConnection) {
196     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
197       JoramTracing.dbgProxy.log(
198         BasicLevel.DEBUG, "TcpProxyService.registerConnection(" +
199         tcpConnection + ')');
200     connections.addElement(tcpConnection);
201   }
202
203   void unregisterConnection(TcpConnection tcpConnection) {
204     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
205       JoramTracing.dbgProxy.log(
206         BasicLevel.DEBUG, "TcpProxyService.unregisterConnection(" +
207         tcpConnection + ')');
208     connections.removeElement(tcpConnection);
209   }
210
211   TcpConnection getConnection(AgentId proxyId, int key) {
212     for (int i = 0; i < connections.size(); i++) {
213       TcpConnection tc = (TcpConnection)connections.elementAt(i);
214       if (tc.getProxyId() == proxyId &&
215           tc.getKey() == key) {
216         return tc;
217       }
218     }
219     return null;
220   }
221
222   private void stop() {
223     Vector stopList = (Vector)connections.clone();
224     for (int i = 0; i < stopList.size(); i++) {
225       TcpConnection tc =
226         (TcpConnection)stopList.elementAt(i);
227       tc.close();
228     }
229     for (int i = 0; i < connectionListeners.length; i++) {
230       connectionListeners[i].stop();
231     }
232   }
233 }
234
Popular Tags