KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > transport > TcpTransportMapping


1 /*_############################################################################
2   _##
3   _## SNMP4J - TcpTransportMapping.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23 package org.snmp4j.transport;
24
25 import java.io.IOException JavaDoc;
26 import org.snmp4j.smi.Address;
27 import org.snmp4j.smi.TcpAddress;
28 import java.util.Vector JavaDoc;
29 import org.snmp4j.log.LogFactory;
30 import org.snmp4j.log.LogAdapter;
31
32 /**
33  * The <code>TcpTransportMapping</code> is the abstract base class for
34  * TCP transport mappings.
35  *
36  * @author Frank Fock
37  * @version 1.7
38  */

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 JavaDoc transportStateListeners;
48
49   public TcpTransportMapping(TcpAddress tcpAddress) {
50     this.tcpAddress = tcpAddress;
51   }
52
53   public Class JavaDoc getSupportedAddressClass() {
54     return TcpAddress.class;
55   }
56
57   /**
58    * Returns the transport address that is used by this transport mapping for
59    * sending and receiving messages.
60    * @return
61    * the <code>Address</code> used by this transport mapping. The returned
62    * instance must not be modified!
63    */

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 JavaDoc;
74
75   public abstract void listen() throws IOException JavaDoc;
76
77   public abstract void close() throws IOException JavaDoc;
78
79   /**
80    * Returns the <code>MessageLengthDecoder</code> used by this transport
81    * mapping.
82    * @return
83    * a MessageLengthDecoder instance.
84    * @since 1.7
85    */

86   public abstract MessageLengthDecoder getMessageLengthDecoder();
87
88   /**
89    * Sets the <code>MessageLengthDecoder</code> that decodes the total
90    * message length from the header of a message.
91    *
92    * @param messageLengthDecoder
93    * a MessageLengthDecoder instance.
94    * @since 1.7
95    */

96   public abstract void
97       setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder);
98
99   /**
100    * Sets the connection timeout. This timeout specifies the time a connection
101    * may be idle before it is closed.
102    * @param connectionTimeout
103    * the idle timeout in milliseconds. A zero or negative value will disable
104    * any timeout and connections opened by this transport mapping will stay
105    * opened until they are explicitly closed.
106    * @since 1.7
107    */

108   public abstract void setConnectionTimeout(long connectionTimeout);
109
110   public synchronized void addTransportStateListener(TransportStateListener l) {
111     if (transportStateListeners == null) {
112       transportStateListeners = new Vector JavaDoc(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 JavaDoc 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