KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > NetworkListenerImpl


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

5 package com.tc.net.protocol.tcm;
6
7 import com.tc.async.api.Sink;
8 import com.tc.net.TCSocketAddress;
9 import com.tc.net.core.TCListener;
10 import com.tc.net.protocol.transport.ConnectionIDFactory;
11 import com.tc.util.TCTimeoutException;
12
13 import java.io.IOException JavaDoc;
14 import java.net.InetAddress JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  * A handle to a specific server port listener
19  *
20  * @author teck
21  */

22 class NetworkListenerImpl implements NetworkListener {
23   private final ChannelManagerImpl channelManager;
24   private final TCMessageRouter tcmRouter;
25   private final CommunicationsManagerImpl commsMgr;
26   private final TCSocketAddress addr;
27   private TCListener lsnr;
28   private boolean started;
29   private final TCMessageFactory msgFactory;
30   private final boolean reuseAddr;
31   private final ConnectionIDFactory connectionIdFactory;
32   private final Sink httpSink;
33
34   // this cstr is intentionally not public, only the Comms Manager should be
35
// creating them
36
NetworkListenerImpl(TCSocketAddress addr, CommunicationsManagerImpl commsMgr, ChannelManagerImpl channelManager,
37                       TCMessageFactory msgFactory, TCMessageRouter router, boolean reuseAddr,
38                       ConnectionIDFactory connectionIdFactory, Sink httpSink) {
39     this.commsMgr = commsMgr;
40     this.channelManager = channelManager;
41     this.addr = addr;
42     this.connectionIdFactory = connectionIdFactory;
43     this.httpSink = httpSink;
44     this.started = false;
45     this.msgFactory = msgFactory;
46     this.reuseAddr = reuseAddr;
47     this.tcmRouter = router;
48   }
49
50   /**
51    * Start this listener listening on the network. You probably don't want to start a listener until you have properly
52    * setup your protocol routes, since you might miss messages between the time the listener is <code>start()</code>
53    * 'ed and the time you add your routes.
54    *
55    * @throws IOException if an IO error occurs (this will most likely be a problem binding to the specified
56    * port/address)
57    */

58   public synchronized void start(Set JavaDoc initialConnectionIDs) throws IOException JavaDoc {
59     this.lsnr = commsMgr.createCommsListener(this.addr, this.channelManager, this.reuseAddr, initialConnectionIDs,
60                                              this.connectionIdFactory, this.httpSink);
61     this.started = true;
62     commsMgr.registerListener(this);
63   }
64
65   public synchronized void stop(long timeout) throws TCTimeoutException {
66     if (!started) { return; }
67
68     try {
69       if (lsnr != null) {
70         lsnr.stop(timeout);
71       }
72     } finally {
73       started = false;
74       commsMgr.unregisterListener(this);
75     }
76   }
77
78   public void routeMessageType(TCMessageType messageType, TCMessageSink sink) {
79     tcmRouter.routeMessageType(messageType, sink);
80   }
81
82   /**
83    * Routes a TCMessage to a sink. The hydrate sink will do the hydrate() work
84    */

85   public void routeMessageType(TCMessageType messageType, Sink destSink, Sink hydrateSink) {
86     routeMessageType(messageType, new TCMessageSinkToSedaSink(destSink, hydrateSink));
87   }
88
89   public ChannelManager getChannelManager() {
90     return channelManager;
91   }
92
93   public void addClassMapping(TCMessageType type, Class JavaDoc msgClass) {
94     this.msgFactory.addClassMapping(type, msgClass);
95   }
96
97   public synchronized InetAddress JavaDoc getBindAddress() {
98     if (!started) { throw new IllegalArgumentException JavaDoc("Listener not running"); }
99     return lsnr.getBindAddress();
100   }
101
102   public synchronized int getBindPort() {
103     if (!started) { throw new IllegalArgumentException JavaDoc("Listener not running"); }
104     return lsnr.getBindPort();
105   }
106
107   public String JavaDoc toString() {
108     try {
109       return getBindAddress().getHostAddress() + ":" + getBindPort();
110     } catch (Exception JavaDoc e) {
111       return "Exception in toString(): " + e.getMessage();
112     }
113   }
114 }
Popular Tags