~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/transport/multiplex/MasterServerSocket.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * JBoss, Home of Professional Open Source
 
3
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
 * by the @authors tag. See the copyright.txt in the distribution for a
 
5
 * full listing of individual contributors.
 
6
 *
 
7
 * This is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU Lesser General Public License as
 
9
 * published by the Free Software Foundation; either version 2.1 of
 
10
 * the License, or (at your option) any later version.
 
11
 *
 
12
 * This software is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this software; if not, write to the Free
 
19
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
 */
 
22
 
 
23
/*
 
24
 * Created on Jul 24, 2005
 
25
 */
 
26
 
 
27
package org.jboss.remoting.transport.multiplex;
 
28
 
 
29
import org.jboss.logging.Logger;
 
30
 
 
31
import javax.net.ServerSocketFactory;
 
32
import java.io.IOException;
 
33
import java.io.Serializable;
 
34
import java.net.InetAddress;
 
35
import java.net.InetSocketAddress;
 
36
import java.net.ServerSocket;
 
37
import java.net.Socket;
 
38
import java.net.SocketAddress;
 
39
import java.net.SocketException;
 
40
import java.net.SocketTimeoutException;
 
41
import java.nio.channels.ServerSocketChannel;
 
42
import java.util.HashMap;
 
43
import java.util.Map;
 
44
 
 
45
 
 
46
/**
 
47
 * <code>MasterServerSocket</code> is one of the two implementations of a server socket in the
 
48
 * Multiplex project.  For each socket created in the <code>accept()</code> method, it builds
 
49
 * a virtual socket group.  For more details, see the Multiplex documentation on
 
50
 * the labs.jboss.org web site.
 
51
 * 
 
52
 * <p>
 
53
 * Most of the methods in <code>MasterServerSocket</code> override those in its parent class,
 
54
 * <code>java.net.ServerSocket</code>.  For method descriptions, see the <code>ServerSocket</code> javadoc.
 
55
 * 
 
56
 * <p>
 
57
 * Copyright (c) 2005
 
58
 * <p>
 
59
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
 
60
 * 
 
61
 * @deprecated As of release 2.4.0 the multiplex transport will no longer be actively supported.
 
62
 */
 
63
public class MasterServerSocket extends ServerSocket implements Serializable
 
64
{
 
65
   private static final Logger log = Logger.getLogger(MasterServerSocket.class);
 
66
   private Map configuration = new HashMap();
 
67
   private ServerSocket ss;
 
68
   private ServerSocketChannel ssc;
 
69
   private static final long serialVersionUID = 402293949935889044L;
 
70
 
 
71
   /**
 
72
 * @throws java.io.IOException
 
73
 */
 
74
   public MasterServerSocket() throws IOException
 
75
   {
 
76
      this(true);
 
77
   }
 
78
   
 
79
/**
 
80
 * @param port
 
81
 * @throws java.io.IOException
 
82
 */
 
83
   public MasterServerSocket(int port) throws IOException
 
84
   {
 
85
      this(true, port);
 
86
   }
 
87
   
 
88
/**
 
89
 * @param port
 
90
 * @param backlog
 
91
 * @throws java.io.IOException
 
92
 */
 
93
   public MasterServerSocket(int port, int backlog) throws IOException
 
94
   {
 
95
      this(true, port, backlog);
 
96
   }
 
97
   
 
98
/**
 
99
 * @param port
 
100
 * @param backlog
 
101
 * @param bindAddr
 
102
 * @throws java.io.IOException
 
103
 */
 
104
   public MasterServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
 
105
   {
 
106
      this(true, port, backlog, bindAddr);
 
107
   }
 
108
   
 
109
/**
 
110
 * @throws java.io.IOException
 
111
 */
 
112
   public MasterServerSocket(boolean nio) throws IOException
 
113
   {
 
114
      if (nio)
 
115
      {
 
116
         ssc = ServerSocketChannel.open();
 
117
         ss = ssc.socket();
 
118
      }
 
119
      else
 
120
      {
 
121
         ss = new ServerSocket();
 
122
      }
 
123
   }
 
124
   
 
125
/**
 
126
 * @param port
 
127
 * @throws java.io.IOException
 
128
 */
 
129
   public MasterServerSocket(boolean nio, int port) throws IOException
 
130
   {
 
131
      if (nio)
 
132
      {
 
133
         ssc = ServerSocketChannel.open();
 
134
         ss = ssc.socket();
 
135
         ss.bind(new InetSocketAddress(port));
 
136
      }
 
137
      else
 
138
      {
 
139
         ss = new ServerSocket(port);
 
140
      }
 
141
   }
 
142
   
 
143
/**
 
144
 * @param port
 
145
 * @param backlog
 
146
 * @throws java.io.IOException
 
147
 */
 
148
   public MasterServerSocket(boolean nio, int port, int backlog) throws IOException
 
149
   {
 
150
      if (nio)
 
151
      {
 
152
         ssc = ServerSocketChannel.open();
 
153
         ss = ssc.socket();
 
154
         ss.bind(new InetSocketAddress(port), backlog);
 
155
      }
 
156
      else
 
157
      {
 
158
         ss = new ServerSocket(port, backlog);
 
159
      }
 
160
   }
 
161
   
 
162
/**
 
163
 * @param port
 
164
 * @param backlog
 
165
 * @param bindAddr
 
166
 * @throws java.io.IOException
 
167
 */
 
168
   public MasterServerSocket(boolean nio, int port, int backlog, InetAddress bindAddr) throws IOException
 
169
   {
 
170
      if (nio)
 
171
      {
 
172
         ssc = ServerSocketChannel.open();
 
173
         ss = ssc.socket();
 
174
         ss.bind(new InetSocketAddress(bindAddr, port), backlog);
 
175
      }
 
176
      else
 
177
      {
 
178
         ss = new ServerSocket(port, backlog, bindAddr);
 
179
      }
 
180
   }
 
181
   
 
182
   
 
183
/**
 
184
 * 
 
185
 * Create a new <code>MasterServerSocket</code>. 
 
186
 * If <code>Map</code> conf contains key <code>Multiplex.SERVER_SOCKET_FACTORY</code>, will use it.
 
187
 * Otherwise, will create an NIO <code>ServerSocket</code>.
 
188
 * 
 
189
 * @param port
 
190
 * @param backlog
 
191
 * @param bindAddr
 
192
 * @param conf
 
193
 * @throws IOException
 
194
 */
 
195
   public MasterServerSocket(int port, int backlog, InetAddress bindAddr, Map conf) throws IOException
 
196
   {
 
197
      if (conf != null)
 
198
         configuration.putAll((conf));
 
199
      
 
200
      if (conf == null || conf.get(Multiplex.SERVER_SOCKET_FACTORY) == null)
 
201
      {
 
202
         ssc = ServerSocketChannel.open();
 
203
         ss = ssc.socket();
 
204
         ss.bind(new InetSocketAddress(bindAddr, port), backlog);
 
205
      }
 
206
      else
 
207
      {
 
208
         Object obj = conf.get(Multiplex.SERVER_SOCKET_FACTORY);
 
209
         if (!(obj instanceof ServerSocketFactory))
 
210
         {
 
211
            String msg = "configuration map contains invalid entry for Multiplex.SERVER_SOCKET_FACTORY: " + obj;
 
212
            log.error(msg);
 
213
            throw new IOException(msg);
 
214
         }
 
215
         ServerSocketFactory ssf = (ServerSocketFactory) obj;
 
216
         ss = ssf.createServerSocket(port, backlog, bindAddr);
 
217
      }
 
218
   }
 
219
   
 
220
   
 
221
/**
 
222
 * @param ssf
 
223
 * @throws IOException
 
224
 */
 
225
   public MasterServerSocket(ServerSocketFactory ssf) throws IOException
 
226
   {
 
227
      ss = ssf.createServerSocket();
 
228
   }
 
229
   
 
230
   
 
231
/**
 
232
 * @param ssf
 
233
 * @param port
 
234
 * @throws IOException
 
235
 */
 
236
   public MasterServerSocket(ServerSocketFactory ssf, int port) throws IOException
 
237
   {
 
238
      ss = ssf.createServerSocket(port);
 
239
   }
 
240
   
 
241
   
 
242
/**
 
243
 * @param ssf
 
244
 * @param port
 
245
 * @param backlog
 
246
 * @throws IOException
 
247
 */
 
248
   public MasterServerSocket(ServerSocketFactory ssf, int port, int backlog) throws IOException
 
249
   {
 
250
      ss = ssf.createServerSocket(port, backlog);
 
251
   }
 
252
   
 
253
   
 
254
/**
 
255
 * @param ssf
 
256
 * @param port
 
257
 * @param backlog
 
258
 * @param bindAddr
 
259
 * @throws IOException
 
260
 */
 
261
   public MasterServerSocket(ServerSocketFactory ssf, int port, int backlog, InetAddress bindAddr)
 
262
   throws IOException
 
263
   {
 
264
      ss = ssf.createServerSocket(port, backlog, bindAddr);
 
265
   }
 
266
   
 
267
   
 
268
   //////////////////////////////////////////////////////////////////////////////////////////////////
 
269
   ///                  The following methods are required of any ServerSocket                   '///
 
270
   //////////////////////////////////////////////////////////////////////////////////////////////////
 
271
   
 
272
   
 
273
   /*
 
274
    ok: public Socket              accept() throws IOException;
 
275
    ok: public void                bind(SocketAddress endpoint) throws IOException;
 
276
    ok: public void                bind(SocketAddress endpoint, int backlog) throws IOException;
 
277
    ok: public void                close() throws IOException;
 
278
    ok: public ServerSocketChannel getChannel();
 
279
    ok: public InetAddress         getInetAddress();
 
280
    ok: public int                 getLocalPort();
 
281
    ok: public SocketAddress       getLocalSocketAddress();
 
282
    ok: public int                 getReceiveBufferSize() throws SocketException;
 
283
    ok: public boolean             getReuseAddress() throws SocketException;
 
284
    ok: public int                 getSoTimeout() throws IOException;
 
285
    ok: public boolean             isBound();
 
286
    ok: public boolean             isClosed();
 
287
    ok: public void                setReceiveBufferSize(int size) throws SocketException;
 
288
    ok: public void                setReuseAddress(boolean on) throws SocketException;
 
289
    ok: public void                setSoTimeout(int timeout) throws SocketException;
 
290
    ok: public String              toString();
 
291
    */
 
292
 
 
293
   
 
294
/**
 
295
 * See <code>java.net.ServerSocket</code> javadoc.
 
296
 */
 
297
   public Socket accept() throws IOException
 
298
   {
 
299
      long start = System.currentTimeMillis();
 
300
      int timeout = getSoTimeout();
 
301
      int savedTimeout = timeout;
 
302
      int timeLeft = 0;
 
303
      Socket socket = null;
 
304
      SocketTimeoutException savedException = null;
 
305
      
 
306
      while (true)
 
307
      {
 
308
         if (timeout > 0)
 
309
            if ((timeLeft = timeout - (int) (System.currentTimeMillis() - start)) <= 0)
 
310
               throw new SocketTimeoutException("Accept timed out");
 
311
         
 
312
         setSoTimeout(timeLeft);
 
313
         
 
314
         try
 
315
         {
 
316
            socket = ss.accept();
 
317
         }
 
318
         catch (SocketTimeoutException e)
 
319
         {
 
320
            // NIO ServerSocket doesn't set message.
 
321
            savedException = new SocketTimeoutException("Accept timed out");
 
322
            throw savedException;
 
323
         }
 
324
         finally
 
325
         {
 
326
            try
 
327
            {
 
328
               setSoTimeout(savedTimeout);
 
329
            }
 
330
            catch (Exception e) {}
 
331
            
 
332
            if (savedException != null)
 
333
               throw savedException;
 
334
         }
 
335
         
 
336
         MultiplexingManager manager = MultiplexingManager.getaManager(socket, configuration);
 
337
         MultiplexingInputStream is = null;
 
338
         Protocol protocol = null;
 
339
         SocketId clientPort = null;
 
340
         
 
341
         try
 
342
         {
 
343
            is = manager.getAnInputStream(SocketId.SERVER_SOCKET_ID, null);
 
344
            protocol = manager.getProtocol();
 
345
            
 
346
            if (timeout > 0)
 
347
               if ((timeLeft = timeout - (int) (System.currentTimeMillis() - start)) <= 0)
 
348
                  throw new SocketTimeoutException("Accept timed out");
 
349
            
 
350
            clientPort = protocol.acceptConnect(is, timeLeft);
 
351
         }
 
352
         catch (SocketTimeoutException e)
 
353
         {
 
354
            log.debug("i/o exception in MasterServerSocket.accept()");
 
355
            manager.decrementReferences();
 
356
            throw e;
 
357
         }
 
358
         catch (IOException e)
 
359
         {
 
360
            log.error("i/o exception in MasterServerSocket.accept()", e);
 
361
            manager.decrementReferences();
 
362
            throw e;
 
363
         }
 
364
         
 
365
         if (log.isDebugEnabled())
 
366
            log.debug("accept(): clientPort:  " + clientPort.getPort());
 
367
         
 
368
         // connection from independent VirtualServerSocket
 
369
         if (clientPort.getPort() < 0)
 
370
         {
 
371
            MultiplexingOutputStream os = new MultiplexingOutputStream(manager, SocketId.SERVER_SOCKET_CONNECT_ID);
 
372
            
 
373
            try
 
374
            {
 
375
               protocol.answerConnect(os, SocketId.SERVER_SOCKET_CONNECT_PORT);
 
376
            }
 
377
            catch (IOException e)
 
378
            {
 
379
               // If this connect timed out at the other end, there may not be an OutputStream to write to.
 
380
               log.error("unable to respond to connect request");
 
381
               manager.decrementReferences();
 
382
               
 
383
               if (e instanceof SocketTimeoutException)
 
384
                  throw new SocketTimeoutException("Accept timed out");
 
385
               
 
386
               throw e;
 
387
            }
 
388
            
 
389
            // Keept trying to accept a request for a VirtualSocket connection.
 
390
            continue;
 
391
         }
 
392
         
 
393
         VirtualSocket virtualSocket = null;
 
394
         
 
395
         try
 
396
         {
 
397
            virtualSocket = new VirtualSocket(manager, clientPort, configuration);
 
398
         }
 
399
         catch (IOException e)
 
400
         {
 
401
            manager.decrementReferences();
 
402
            throw e;
 
403
         }
 
404
         
 
405
         int localPort = virtualSocket.getLocalVirtualPort();
 
406
         
 
407
         try
 
408
         {
 
409
            protocol.answerConnect((MultiplexingOutputStream)virtualSocket.getOutputStream(), localPort);
 
410
         }
 
411
         catch (IOException e)
 
412
         {
 
413
            // If this connect timed out at the other end, there may not be an OutputStream to write to.
 
414
            log.error("unable to respond to connect request");
 
415
            virtualSocket.close();
 
416
            throw e;
 
417
         }
 
418
 
 
419
         return virtualSocket;
 
420
      }
 
421
   }
 
422
   
 
423
   
 
424
   /**
 
425
    * See <code>java.net.ServerSocket</code> javadoc.
 
426
    */
 
427
   public void bind(SocketAddress endpoint) throws IOException
 
428
   {
 
429
      ss.bind(endpoint);
 
430
   }
 
431
   
 
432
   
 
433
   /**
 
434
    * See <code>java.net.ServerSocket</code> javadoc. 
 
435
    */
 
436
   public void bind(SocketAddress endpoint, int backlog) throws IOException
 
437
   {
 
438
      ss.bind(endpoint, backlog);
 
439
   }
 
440
   
 
441
   
 
442
   /**
 
443
    * See <code>java.net.ServerSocket</code> javadoc. 
 
444
    */
 
445
   public void close() throws IOException
 
446
   {
 
447
      log.debug("MasterServerSocket: closing");
 
448
      ss.close();
 
449
   }
 
450
   
 
451
   /**
 
452
    * See <code>java.net.ServerSocket</code> javadoc.
 
453
    */
 
454
   public ServerSocketChannel getChannel()
 
455
   {
 
456
      return ss.getChannel();
 
457
   }
 
458
   
 
459
   
 
460
   /**
 
461
    * See <code>java.net.ServerSocket</code> javadoc. 
 
462
    */
 
463
   public InetAddress getInetAddress()
 
464
   {
 
465
      return ss.getInetAddress();
 
466
   }
 
467
   
 
468
   
 
469
   /**
 
470
    * See <code>java.net.ServerSocket</code> javadoc.
 
471
    */
 
472
   public int getLocalPort()
 
473
   {
 
474
      return ss.getLocalPort();
 
475
   }
 
476
   
 
477
   
 
478
   /**
 
479
    * See <code>java.net.ServerSocket</code> javadoc.
 
480
    */
 
481
   public SocketAddress getLocalSocketAddress()
 
482
   {
 
483
      return ss.getLocalSocketAddress();
 
484
   }
 
485
   
 
486
   
 
487
   /**
 
488
    * See <code>java.net.ServerSocket</code> javadoc.
 
489
    */
 
490
   public int getReceiveBufferSize() throws SocketException
 
491
   {
 
492
      return ss.getReceiveBufferSize();
 
493
   }
 
494
   
 
495
   
 
496
   /**
 
497
    * See <code>java.net.ServerSocket</code> javadoc.
 
498
    */
 
499
   public boolean getReuseAddress() throws SocketException
 
500
   {
 
501
      return ss.getReuseAddress();
 
502
   }
 
503
   
 
504
   
 
505
   /**
 
506
    * See <code>java.net.ServerSocket</code> javadoc.
 
507
    */
 
508
   public int getSoTimeout() throws IOException
 
509
   {
 
510
      return ss.getSoTimeout();
 
511
   }
 
512
   
 
513
   
 
514
   /**
 
515
    * See <code>java.net.ServerSocket</code> javadoc.
 
516
    */
 
517
   public boolean isBound()
 
518
   {
 
519
      return ss.isBound();
 
520
   }
 
521
   
 
522
   
 
523
   /**
 
524
    * See <code>java.net.ServerSocket</code> javadoc.
 
525
    */
 
526
   public boolean isClosed()
 
527
   {
 
528
      return ss.isClosed();
 
529
   }
 
530
   
 
531
   
 
532
   /**
 
533
    * See <code>java.net.ServerSocket</code> javadoc.
 
534
    */
 
535
   public void setReceiveBufferSize(int size) throws SocketException
 
536
   {
 
537
      ss.setReceiveBufferSize(size);
 
538
   }
 
539
   
 
540
   
 
541
   /**
 
542
    * See <code>java.net.ServerSocket</code> javadoc.
 
543
    */
 
544
   public void setReuseAddress(boolean on) throws SocketException
 
545
   {
 
546
      ss.setReuseAddress(on);
 
547
   }
 
548
   
 
549
   
 
550
   /**
 
551
    * See <code>java.net.ServerSocket</code> javadoc.
 
552
    */
 
553
   public void setSoTimeout(int timeout) throws SocketException
 
554
   {
 
555
      ss.setSoTimeout(timeout);
 
556
   }
 
557
   
 
558
   
 
559
   /**
 
560
    * See <code>java.net.ServerSocket</code> javadoc.
 
561
    */
 
562
   public String toString()
 
563
   {
 
564
      if (!isBound())
 
565
         return "MasterServerSocket[unbound]";
 
566
      
 
567
      return "MasterServerSocket[" + ss.toString()  + "]";
 
568
   }
 
569
   
 
570
   
 
571
//////////////////////////////////////////////////////////////////////////////////////////////////
 
572
// /              The following methods are specific to MasterServerSocket           '///
 
573
//////////////////////////////////////////////////////////////////////////////////////////////////
 
574
   
 
575
/**
 
576
 * Accepts a connection from a remote <code>VirtualServerSocket</code>.
 
577
 */
 
578
   public int acceptServerSocketConnection() throws IOException
 
579
   {     
 
580
      long start = System.currentTimeMillis();
 
581
      int timeout = getSoTimeout();
 
582
      int savedTimeout = timeout;
 
583
      int timeLeft = 0;
 
584
      
 
585
//    Socket socket = ss.accept();
 
586
      Socket socket = null;
 
587
      
 
588
      if (ssc == null)
 
589
         socket = ss.accept();
 
590
      else
 
591
         socket = ssc.accept().socket();
 
592
      
 
593
      MultiplexingManager manager = MultiplexingManager.getaManager(socket, configuration);
 
594
      manager.setCreatedForRemoteServerSocket();
 
595
      MultiplexingInputStream is = null;
 
596
      Protocol protocol = null;
 
597
      SocketId clientPort = null;
 
598
      
 
599
      try
 
600
      {
 
601
         is = manager.getAnInputStream(SocketId.SERVER_SOCKET_ID, null);
 
602
         protocol = manager.getProtocol();
 
603
         
 
604
         if (timeout > 0)
 
605
            if ((timeLeft = timeout - (int) (System.currentTimeMillis() - start)) <= 0)
 
606
               throw new SocketTimeoutException("Accept timed out");
 
607
         
 
608
         clientPort = protocol.acceptConnect(is, timeLeft);
 
609
      }
 
610
      catch (IOException e)
 
611
      {
 
612
         log.error("i/o exception in MasterServerSocket.acceptServerSocketConnection()", e);
 
613
         manager.decrementReferences();
 
614
         
 
615
         if (e instanceof SocketTimeoutException)
 
616
            throw new SocketTimeoutException("Accept timed out");
 
617
         
 
618
         throw e;
 
619
      }
 
620
      
 
621
      if (clientPort.getPort() != SocketId.SERVER_SOCKET_PORT)
 
622
      {
 
623
         manager.decrementReferences();
 
624
         String message = "received connect request not from a VirtualServerSocket";
 
625
         log.error(message);
 
626
         throw new IOException(message);
 
627
      }
 
628
      
 
629
      MultiplexingOutputStream os = new MultiplexingOutputStream(manager, SocketId.SERVER_SOCKET_CONNECT_ID);
 
630
      
 
631
      try
 
632
      {
 
633
         protocol.answerConnect(os, SocketId.SERVER_SOCKET_CONNECT_PORT);
 
634
      }
 
635
      catch (IOException e)
 
636
      {
 
637
         // If this connect timed out at the other end, there may not be an OutputStream to write to.
 
638
         log.error("unable to respond to connect request");
 
639
         manager.decrementReferences();
 
640
         throw e;
 
641
      }
 
642
      
 
643
      return manager.getSocket().getLocalPort();
 
644
   }
 
645
   
 
646
   
 
647
   public void setConfiguration(Map configuration)
 
648
   {
 
649
      this.configuration.putAll(configuration);
 
650
   }
 
651
}
 
 
b'\\ No newline at end of file'