1 20 21 22 23 package org.snmp4j.transport; 24 25 import java.io.IOException ; 26 import org.snmp4j.smi.Address; 27 import org.snmp4j.smi.TcpAddress; 28 import java.util.Vector ; 29 import org.snmp4j.log.LogFactory; 30 import org.snmp4j.log.LogAdapter; 31 32 39 public abstract class TcpTransportMapping extends AbstractTransportMapping 40 implements ConnectionOrientedTransportMapping 41 { 42 43 private static final LogAdapter logger = 44 LogFactory.getLogger(TcpTransportMapping.class); 45 46 protected TcpAddress tcpAddress; 47 private transient Vector transportStateListeners; 48 49 public TcpTransportMapping(TcpAddress tcpAddress) { 50 this.tcpAddress = tcpAddress; 51 } 52 53 public Class getSupportedAddressClass() { 54 return TcpAddress.class; 55 } 56 57 64 public TcpAddress getAddress() { 65 return tcpAddress; 66 } 67 68 public Address getListenAddress() { 69 return tcpAddress; 70 } 71 72 public abstract void sendMessage(Address address, byte[] message) 73 throws IOException ; 74 75 public abstract void listen() throws IOException ; 76 77 public abstract void close() throws IOException ; 78 79 86 public abstract MessageLengthDecoder getMessageLengthDecoder(); 87 88 96 public abstract void 97 setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder); 98 99 108 public abstract void setConnectionTimeout(long connectionTimeout); 109 110 public synchronized void addTransportStateListener(TransportStateListener l) { 111 if (transportStateListeners == null) { 112 transportStateListeners = new Vector (2); 113 } 114 transportStateListeners.add(l); 115 } 116 117 public synchronized void removeTransportStateListener(TransportStateListener 118 l) { 119 if (transportStateListeners != null) { 120 transportStateListeners.remove(l); 121 } 122 } 123 124 protected void fireConnectionStateChanged(TransportStateEvent change) { 125 if (logger.isDebugEnabled()) { 126 logger.debug("Firing transport state event: "+change); 127 } 128 if (transportStateListeners != null) { 129 Vector listeners = transportStateListeners; 130 int count = listeners.size(); 131 for (int i = 0; i < count; i++) { 132 ((TransportStateListener) 133 listeners.get(i)).connectionStateChanged(change); 134 } 135 } 136 } 137 138 } 139 | Popular Tags |