KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - AbstractTransportMapping.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 org.snmp4j.TransportMapping;
26 import org.snmp4j.MessageDispatcher;
27 import java.io.IOException JavaDoc;
28 import org.snmp4j.smi.Address;
29 import java.util.Vector JavaDoc;
30 import java.nio.ByteBuffer JavaDoc;
31
32 /**
33  * The <code>AbstractTransportMapping</code> provides an abstract
34  * implementation for the message dispatcher list and the maximum inbound
35  * message size.
36  *
37  * @author Frank Fock
38  * @version 1.6
39  */

40 public abstract class AbstractTransportMapping implements TransportMapping {
41
42   protected Vector JavaDoc transportListener = new Vector JavaDoc(1);
43   protected int maxInboundMessageSize = (1 << 16) - 1;
44   protected boolean asyncMsgProcessingSupported = true;
45
46   public abstract Class JavaDoc getSupportedAddressClass();
47
48   public abstract void sendMessage(Address address, byte[] message)
49       throws IOException JavaDoc;
50
51   public void addMessageDispatcher(MessageDispatcher dispatcher) {
52     addTransportListener(dispatcher);
53   }
54
55   public void removeMessageDispatcher(MessageDispatcher dispatcher) {
56     removeTransportListener(dispatcher);
57   }
58
59   public synchronized void addTransportListener(TransportListener l) {
60     Vector JavaDoc v = (transportListener == null) ?
61         new Vector JavaDoc(2) : (Vector JavaDoc) transportListener.clone();
62     if (!v.contains(l)) {
63       v.addElement(l);
64       transportListener = v;
65     }
66   }
67
68   public synchronized void removeTransportListener(TransportListener l) {
69     if (transportListener != null && transportListener.contains(l)) {
70       Vector JavaDoc v = (Vector JavaDoc) transportListener.clone();
71       v.removeElement(l);
72       transportListener = v;
73     }
74   }
75
76   protected void fireProcessMessage(Address address, ByteBuffer JavaDoc buf) {
77     if (transportListener != null) {
78       for (int i=0; i<transportListener.size(); i++) {
79         TransportListener l;
80         synchronized (this) {
81           l = (TransportListener) transportListener.get(i);
82         }
83         l.processMessage(this, address, buf);
84       }
85     }
86   }
87
88
89   public abstract void close() throws IOException JavaDoc;
90   public abstract void listen() throws IOException JavaDoc;
91
92   public int getMaxInboundMessageSize() {
93     return maxInboundMessageSize;
94   }
95
96   /**
97    * Returns <code>true</code> if asynchronous (multi-threaded) message
98    * processing may be implemented. The default is <code>true</code>.
99    *
100    * @return
101    * if <code>false</code> is returned the
102    * {@link MessageDispatcher#processMessage}
103    * method must not return before the message has been entirely processed.
104    */

105   public boolean isAsyncMsgProcessingSupported() {
106     return asyncMsgProcessingSupported;
107   }
108
109   /**
110    * Specifies whether this transport mapping has to support asynchronous
111    * messages processing or not.
112    *
113    * @param asyncMsgProcessingSupported
114    * if <code>false</code> the {@link MessageDispatcher#processMessage}
115    * method must not return before the message has been entirely processed,
116    * because the incoming message buffer is not copied before the message
117    * is being processed. If <code>true</code> the message buffer is copied
118    * for each call, so that the message processing can be implemented
119    * asynchronously.
120    */

121   public void setAsyncMsgProcessingSupported(
122       boolean asyncMsgProcessingSupported) {
123     this.asyncMsgProcessingSupported = asyncMsgProcessingSupported;
124   }
125
126 }
127
Popular Tags