1 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 ; 14 import java.net.InetAddress ; 15 import java.util.Set ; 16 17 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 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 58 public synchronized void start(Set initialConnectionIDs) throws IOException { 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 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 msgClass) { 94 this.msgFactory.addClassMapping(type, msgClass); 95 } 96 97 public synchronized InetAddress getBindAddress() { 98 if (!started) { throw new IllegalArgumentException ("Listener not running"); } 99 return lsnr.getBindAddress(); 100 } 101 102 public synchronized int getBindPort() { 103 if (!started) { throw new IllegalArgumentException ("Listener not running"); } 104 return lsnr.getBindPort(); 105 } 106 107 public String toString() { 108 try { 109 return getBindAddress().getHostAddress() + ":" + getBindPort(); 110 } catch (Exception e) { 111 return "Exception in toString(): " + e.getMessage(); 112 } 113 } 114 } | Popular Tags |