KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > agentx > AgentXMessageDispatcherImpl


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentX - AgentXMessageDispatcherImpl.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.agentx;
24
25 import java.nio.*;
26 import java.util.*;
27
28 import org.snmp4j.*;
29 import org.snmp4j.smi.*;
30 import java.io.IOException JavaDoc;
31 import org.snmp4j.agent.agentx.AgentXPDU;
32 import org.snmp4j.log.LogFactory;
33 import org.snmp4j.log.LogAdapter;
34 import org.snmp4j.mp.PduHandle;
35 import org.snmp4j.mp.PduHandleCallback;
36
37 public class AgentXMessageDispatcherImpl implements AgentXMessageDispatcher {
38
39   private static final LogAdapter logger =
40       LogFactory.getLogger(AgentXMessageDispatcherImpl.class);
41
42   private List transportMappings = new ArrayList();
43   private List commandListener = new ArrayList();
44   private volatile int nextPacketID = 0;
45
46   public AgentXMessageDispatcherImpl() {
47   }
48
49   public synchronized int getNextPacketID() {
50     int nextID = ++nextPacketID;
51     if (nextID <= 0) {
52       nextID = nextPacketID = 1;
53     }
54     return nextID;
55   }
56
57   protected PduHandle createPduHandle() {
58     return new PduHandle(getNextPacketID());
59   }
60
61   public synchronized void addTransportMapping(TransportMapping transport) {
62     transportMappings.add(transport);
63     transport.addTransportListener(this);
64   }
65
66   public Collection getTransportMappings() {
67     return new ArrayList(transportMappings);
68   }
69
70   public void processMessage(TransportMapping sourceTransport,
71                              Address incomingAddress,
72                              ByteBuffer wholeMessage) {
73     try {
74       AgentXPDU pdu = AgentXPDU.decode(wholeMessage);
75       AgentXCommandEvent event =
76           new AgentXCommandEvent(this, this,
77                                  incomingAddress, sourceTransport, pdu);
78       fireCommandEvent(event);
79     }
80     catch (IOException JavaDoc ex) {
81       if (logger.isDebugEnabled()) {
82         ex.printStackTrace();
83       }
84       logger.warn(ex);
85       if (ex instanceof AgentXParseException) {
86         // exception can be handled on AgentX protocol level
87
AgentXCommandEvent event =
88             new AgentXCommandEvent(this, this,
89                                    incomingAddress, sourceTransport,
90                                    (AgentXParseException)ex);
91         fireCommandEvent(event);
92       }
93     }
94   }
95
96   public TransportMapping removeTransportMapping(TransportMapping transport) {
97     if (transportMappings.remove(transport)) {
98       transport.removeTransportListener(this);
99       return transport;
100     }
101     return null;
102   }
103
104   public PduHandle send(TransportMapping transport,
105                         Address address, AgentXPDU message,
106                         PduHandleCallback callback) throws IOException JavaDoc {
107     PduHandle handle;
108     if (message instanceof AgentXResponsePDU) {
109       handle = new PduHandle(message.getPacketID());
110     }
111     else {
112       handle = createPduHandle();
113       message.setPacketID(handle.getTransactionID());
114     }
115     if (callback != null) {
116       callback.pduHandleAssigned(handle, message);
117     }
118     if (transport != null) {
119       sendPDU(address, message, transport);
120       return handle;
121     }
122     else {
123       for (int i = 0; i < transportMappings.size(); i++) {
124         TransportMapping t = (TransportMapping) transportMappings.get(i);
125         if (address.getClass().equals(t.getSupportedAddressClass())) {
126           sendPDU(address, message, t);
127           return handle;
128         }
129       }
130     }
131     return null;
132   }
133
134   private void sendPDU(Address address, AgentXPDU message,
135                        TransportMapping transport) throws IOException JavaDoc {
136     ByteBuffer buf =
137         ByteBuffer.allocate(message.getPayloadLength() +
138                             AgentXProtocol.HEADER_LENGTH);
139     message.encode(buf);
140     send(address, transport, buf);
141   }
142
143   public void send(Address address, TransportMapping transport,
144                    ByteBuffer message) throws IOException JavaDoc {
145     message.flip();
146     byte[] bytes = new byte[message.limit()];
147     message.get(bytes);
148     transport.sendMessage(address, bytes);
149   }
150
151   protected synchronized void fireCommandEvent(AgentXCommandEvent event) {
152     for (int i=0; i<commandListener.size(); i++) {
153       ((AgentXCommandListener)commandListener.get(i)).processCommand(event);
154       if (event.isProcessed()) {
155         return;
156       }
157     }
158   }
159
160   public synchronized void addCommandListener(AgentXCommandListener l) {
161     commandListener.add(l);
162   }
163
164   public synchronized void removeCommandListener(AgentXCommandListener l) {
165     commandListener.remove(l);
166   }
167 }
168
Popular Tags