~ubuntu-branches/ubuntu/intrepid/tomcat5.5/intrepid

« back to all changes in this revision

Viewing changes to container/modules/groupcom/src/share/org/apache/catalina/tribes/transport/ReceiverBase.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-27 11:19:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060927111917-wov6fmkz3x6rsl68
Tags: 5.5.17-1ubuntu1
(Build-) depend on libmx4j-java (>= 3.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999,2006 The Apache Software Foundation.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 
5
 * use this file except in compliance with the License. You may obtain a copy of
 
6
 * the License at
 
7
 * 
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
 * License for the specific language governing permissions and limitations under
 
14
 * the License.
 
15
 */
 
16
package org.apache.catalina.tribes.transport;
 
17
 
 
18
import java.io.IOException;
 
19
import java.net.InetAddress;
 
20
import java.net.InetSocketAddress;
 
21
import java.net.ServerSocket;
 
22
 
 
23
import org.apache.catalina.tribes.ChannelMessage;
 
24
import org.apache.catalina.tribes.ChannelReceiver;
 
25
import org.apache.catalina.tribes.MessageListener;
 
26
import org.apache.catalina.tribes.io.ListenCallback;
 
27
import org.apache.commons.logging.Log;
 
28
 
 
29
/**
 
30
 * <p>Title: </p>
 
31
 *
 
32
 * <p>Description: </p>
 
33
 *
 
34
 * <p>Copyright: Copyright (c) 2005</p>
 
35
 *
 
36
 * <p>Company: </p>
 
37
 *
 
38
 * @author not attributable
 
39
 * @version 1.0
 
40
 */
 
41
public abstract class ReceiverBase implements ChannelReceiver, ListenCallback, ThreadPool.ThreadCreator {
 
42
 
 
43
    public static final int OPTION_DIRECT_BUFFER = 0x0004;
 
44
 
 
45
 
 
46
    protected static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ReceiverBase.class);
 
47
    
 
48
    private MessageListener listener;
 
49
    private String host = "auto";
 
50
    private InetAddress bind;
 
51
    private int port  = 4000;
 
52
    private int rxBufSize = 43800;
 
53
    private int txBufSize = 25188;
 
54
    private boolean listen = false;
 
55
    private ThreadPool pool;
 
56
    private boolean direct = true;
 
57
    private long tcpSelectorTimeout = 100;
 
58
    //how many times to search for an available socket
 
59
    private int autoBind = 10;
 
60
    private int maxThreads = 25;
 
61
    private int minThreads = 6;
 
62
    private boolean tcpNoDelay = true;
 
63
    private boolean soKeepAlive = false;
 
64
    private boolean ooBInline = true;
 
65
    private boolean soReuseAddress = true;
 
66
    private boolean soLingerOn = true;
 
67
    private int soLingerTime = 3;
 
68
    private int soTrafficClass = 0x04 | 0x08 | 0x010;
 
69
    private int timeout = 15000; //15 seconds
 
70
 
 
71
 
 
72
    public ReceiverBase() {
 
73
    }
 
74
    
 
75
    /**
 
76
     * getMessageListener
 
77
     *
 
78
     * @return MessageListener
 
79
     * @todo Implement this org.apache.catalina.tribes.ChannelReceiver method
 
80
     */
 
81
    public MessageListener getMessageListener() {
 
82
        return listener;
 
83
    }
 
84
 
 
85
    /**
 
86
     *
 
87
     * @return The port
 
88
     * @todo Implement this org.apache.catalina.tribes.ChannelReceiver method
 
89
     */
 
90
    public int getPort() {
 
91
        return port;
 
92
    }
 
93
 
 
94
    public int getRxBufSize() {
 
95
        return rxBufSize;
 
96
    }
 
97
 
 
98
    public int getTxBufSize() {
 
99
        return txBufSize;
 
100
    }
 
101
    
 
102
    /**
 
103
     * @deprecated use getMinThreads()/getMaxThreads()
 
104
     * @return int
 
105
     */
 
106
    public int getTcpThreadCount() {
 
107
        return getMinThreads();
 
108
    }
 
109
 
 
110
    /**
 
111
     * setMessageListener
 
112
     *
 
113
     * @param listener MessageListener
 
114
     * @todo Implement this org.apache.catalina.tribes.ChannelReceiver method
 
115
     */
 
116
    public void setMessageListener(MessageListener listener) {
 
117
        this.listener = listener;
 
118
    }
 
119
 
 
120
    public void setTcpListenPort(int tcpListenPort) {
 
121
        this.port = tcpListenPort;
 
122
    }
 
123
 
 
124
    public void setTcpListenAddress(String tcpListenHost) {
 
125
        this.host = tcpListenHost;
 
126
    }
 
127
 
 
128
    public void setRxBufSize(int rxBufSize) {
 
129
        this.rxBufSize = rxBufSize;
 
130
    }
 
131
 
 
132
    public void setTxBufSize(int txBufSize) {
 
133
        this.txBufSize = txBufSize;
 
134
    }
 
135
 
 
136
    public void setTcpThreadCount(int tcpThreadCount) {
 
137
        setMinThreads(tcpThreadCount);
 
138
    }
 
139
 
 
140
    /**
 
141
     * @return Returns the bind.
 
142
     */
 
143
    public InetAddress getBind() {
 
144
        if (bind == null) {
 
145
            try {
 
146
                if ("auto".equals(host)) {
 
147
                    host = java.net.InetAddress.getLocalHost().getHostAddress();
 
148
                }
 
149
                if (log.isDebugEnabled())
 
150
                    log.debug("Starting replication listener on address:"+ host);
 
151
                bind = java.net.InetAddress.getByName(host);
 
152
            } catch (IOException ioe) {
 
153
                log.error("Failed bind replication listener on address:"+ host, ioe);
 
154
            }
 
155
        }
 
156
        return bind;
 
157
    }
 
158
    
 
159
    /**
 
160
     * recursive bind to find the next available port
 
161
     * @param socket ServerSocket
 
162
     * @param portstart int
 
163
     * @param retries int
 
164
     * @return int
 
165
     * @throws IOException
 
166
     */
 
167
    protected int bind(ServerSocket socket, int portstart, int retries) throws IOException {
 
168
        while ( retries > 0 ) {
 
169
            try {
 
170
                InetSocketAddress addr = new InetSocketAddress(getBind(), portstart);
 
171
                socket.bind(addr);
 
172
                setTcpListenPort(portstart);
 
173
                log.info("Receiver Server Socket bound to:"+addr);
 
174
                return 0;
 
175
            }catch ( IOException x) {
 
176
                retries--;
 
177
                if ( retries <= 0 ) throw x;
 
178
                portstart++;
 
179
                retries = bind(socket,portstart,retries);
 
180
            }
 
181
        }
 
182
        return retries;
 
183
    }
 
184
    
 
185
    public void messageDataReceived(ChannelMessage data) {
 
186
        if ( this.listener != null ) {
 
187
            listener.messageReceived(data);
 
188
        }
 
189
    }
 
190
    
 
191
    public int getWorkerThreadOptions() {
 
192
        int options = 0;
 
193
        if ( getDirect() ) options = options | OPTION_DIRECT_BUFFER;
 
194
        return options;
 
195
    }
 
196
 
 
197
 
 
198
    /**
 
199
     * @param bind The bind to set.
 
200
     */
 
201
    public void setBind(java.net.InetAddress bind) {
 
202
        this.bind = bind;
 
203
    }
 
204
 
 
205
 
 
206
    public int getTcpListenPort() {
 
207
        return this.port;
 
208
    }
 
209
 
 
210
    public boolean getDirect() {
 
211
        return direct;
 
212
    }
 
213
 
 
214
 
 
215
 
 
216
    public void setDirect(boolean direct) {
 
217
        this.direct = direct;
 
218
    }
 
219
 
 
220
 
 
221
 
 
222
    public String getHost() {
 
223
        getBind();
 
224
        return this.host;
 
225
    }
 
226
 
 
227
    public long getTcpSelectorTimeout() {
 
228
        return tcpSelectorTimeout;
 
229
    }
 
230
 
 
231
    public boolean doListen() {
 
232
        return listen;
 
233
    }
 
234
 
 
235
    public MessageListener getListener() {
 
236
        return listener;
 
237
    }
 
238
 
 
239
    public ThreadPool getPool() {
 
240
        return pool;
 
241
    }
 
242
 
 
243
    public String getTcpListenAddress() {
 
244
        return getHost();
 
245
    }
 
246
 
 
247
    public int getAutoBind() {
 
248
        return autoBind;
 
249
    }
 
250
 
 
251
    public int getMaxThreads() {
 
252
        return maxThreads;
 
253
    }
 
254
 
 
255
    public int getMinThreads() {
 
256
        return minThreads;
 
257
    }
 
258
 
 
259
    public boolean getTcpNoDelay() {
 
260
        return tcpNoDelay;
 
261
    }
 
262
 
 
263
    public boolean getSoKeepAlive() {
 
264
        return soKeepAlive;
 
265
    }
 
266
 
 
267
    public boolean getOoBInline() {
 
268
        return ooBInline;
 
269
    }
 
270
 
 
271
 
 
272
    public boolean getSoLingerOn() {
 
273
        return soLingerOn;
 
274
    }
 
275
 
 
276
    public int getSoLingerTime() {
 
277
        return soLingerTime;
 
278
    }
 
279
 
 
280
    public boolean getSoReuseAddress() {
 
281
        return soReuseAddress;
 
282
    }
 
283
 
 
284
    public int getSoTrafficClass() {
 
285
        return soTrafficClass;
 
286
    }
 
287
 
 
288
    public int getTimeout() {
 
289
        return timeout;
 
290
    }
 
291
 
 
292
    public void setTcpSelectorTimeout(long selTimeout) {
 
293
        tcpSelectorTimeout = selTimeout;
 
294
    }
 
295
 
 
296
    public void setListen(boolean doListen) {
 
297
        this.listen = doListen;
 
298
    }
 
299
 
 
300
    public void setHost(String host) {
 
301
        this.host = host;
 
302
    }
 
303
 
 
304
    public void setListener(MessageListener listener) {
 
305
        this.listener = listener;
 
306
    }
 
307
 
 
308
    public void setLog(Log log) {
 
309
        this.log = log;
 
310
    }
 
311
 
 
312
    public void setPool(ThreadPool pool) {
 
313
        this.pool = pool;
 
314
    }
 
315
 
 
316
    public void setPort(int port) {
 
317
        this.port = port;
 
318
    }
 
319
 
 
320
    public void setAutoBind(int autoBind) {
 
321
        this.autoBind = autoBind;
 
322
        if ( this.autoBind <= 0 ) this.autoBind = 1;
 
323
    }
 
324
 
 
325
    public void setMaxThreads(int maxThreads) {
 
326
        this.maxThreads = maxThreads;
 
327
    }
 
328
 
 
329
    public void setMinThreads(int minThreads) {
 
330
        this.minThreads = minThreads;
 
331
    }
 
332
 
 
333
    public void setTcpNoDelay(boolean tcpNoDelay) {
 
334
        this.tcpNoDelay = tcpNoDelay;
 
335
    }
 
336
 
 
337
    public void setSoKeepAlive(boolean soKeepAlive) {
 
338
        this.soKeepAlive = soKeepAlive;
 
339
    }
 
340
 
 
341
    public void setOoBInline(boolean ooBInline) {
 
342
        this.ooBInline = ooBInline;
 
343
    }
 
344
 
 
345
 
 
346
    public void setSoLingerOn(boolean soLingerOn) {
 
347
        this.soLingerOn = soLingerOn;
 
348
    }
 
349
 
 
350
    public void setSoLingerTime(int soLingerTime) {
 
351
        this.soLingerTime = soLingerTime;
 
352
    }
 
353
 
 
354
    public void setSoReuseAddress(boolean soReuseAddress) {
 
355
        this.soReuseAddress = soReuseAddress;
 
356
    }
 
357
 
 
358
    public void setSoTrafficClass(int soTrafficClass) {
 
359
        this.soTrafficClass = soTrafficClass;
 
360
    }
 
361
 
 
362
    public void setTimeout(int timeout) {
 
363
        this.timeout = timeout;
 
364
    }
 
365
}