KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > TCConnectionManagerJDK14


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.core;
5
6 import com.tc.net.TCSocketAddress;
7 import com.tc.net.core.event.TCConnectionEventListener;
8 import com.tc.net.protocol.ProtocolAdaptorFactory;
9 import com.tc.net.protocol.TCProtocolAdaptor;
10
11 import java.io.IOException JavaDoc;
12 import java.net.InetSocketAddress JavaDoc;
13 import java.net.ServerSocket JavaDoc;
14 import java.nio.channels.ServerSocketChannel JavaDoc;
15
16 /**
17  * JDK 1.4 implementation of TCConnectionManager interface
18  *
19  * @author teck
20  */

21 class TCConnectionManagerJDK14 extends AbstractTCConnectionManager {
22   TCConnectionManagerJDK14(TCComm comm) {
23     super(comm);
24
25     if (comm != null) {
26       if (!(comm instanceof TCCommJDK14)) { throw new IllegalArgumentException JavaDoc("Illegal TCComm instance type: "
27                                                                                + comm.getClass().getName()); }
28     }
29   }
30
31   protected TCConnection createConnectionImpl(TCProtocolAdaptor adaptor, TCConnectionEventListener listener) {
32     return new TCConnectionJDK14(listener, (TCCommJDK14) comm, adaptor, this);
33   }
34
35   protected TCListener createListenerImpl(TCSocketAddress addr, ProtocolAdaptorFactory factory, int backlog, boolean reuseAddr)
36       throws IOException JavaDoc {
37     ServerSocketChannel JavaDoc ssc = ServerSocketChannel.open();
38     ssc.configureBlocking(false);
39     ServerSocket JavaDoc serverSocket = ssc.socket();
40     serverSocket.setReuseAddress(reuseAddr);
41     serverSocket.setReceiveBufferSize(64 * 1024);
42
43     try {
44       serverSocket.bind(new InetSocketAddress JavaDoc(addr.getAddress(), addr.getPort()), backlog);
45     } catch (IOException JavaDoc ioe) {
46       logger.warn("Unable to bind socket on address " + addr.getAddress() + ", port " + addr.getPort() + ", " + ioe.getMessage());
47       throw ioe;
48     }
49
50     if (logger.isDebugEnabled()) {
51       logger.debug("Bind: " + serverSocket.getLocalSocketAddress());
52     }
53
54     TCListenerJDK14 rv = new TCListenerJDK14(ssc, factory, (TCCommJDK14) comm, getConnectionListener(), this);
55     ((TCCommJDK14) comm).requestAcceptInterest(rv, ssc);
56
57     return rv;
58   }
59 }
Popular Tags