KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > ManagerRecord


1 /*
2  * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This package and its source code is available at www.jboss.org
19 **/

20 package org.jboss.jmx.adaptor.snmp.agent;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.SocketException JavaDoc;
24
25 import org.jboss.logging.Logger;
26 import org.opennms.protocols.snmp.SnmpHandler;
27 import org.opennms.protocols.snmp.SnmpParameters;
28 import org.opennms.protocols.snmp.SnmpPduPacket;
29 import org.opennms.protocols.snmp.SnmpPeer;
30 import org.opennms.protocols.snmp.SnmpSMI;
31 import org.opennms.protocols.snmp.SnmpSession;
32 import org.opennms.protocols.snmp.SnmpSyntax;
33
34 /**
35  * <tt>ManagerRecord</tt> is a class that is used as a key
36  * to uniquely identify subscribing managers.
37  *
38  * @version $Revision: 44604 $
39  *
40  * @author <a HREF="mailto:spol@intracom.gr">Spyros Pollatos</a>
41  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
42 **/

43 class ManagerRecord implements SnmpHandler
44 {
45    /** The logger object */
46    private static final Logger log = Logger.getLogger(ManagerRecord.class);
47    
48    /** SNMP parameter for number of retries */
49    private int retries = 10;
50    
51    /** SNMP parameter for timeout */
52    private int timeout = 5000;
53          
54    /** Subscription target IP address */
55    private InetAddress JavaDoc address;
56    
57    /** Subscription target port */
58    private int port;
59    
60    /** The local address to bind */
61    private InetAddress JavaDoc localAddress;
62    
63    /** The local port to use */
64    private int localPort;
65    
66    /** Subscription native SNMP version */
67    private int version;
68    
69    /** The read community string*/
70    private final String JavaDoc readCommunity = "public";
71    
72    /** The session to the manager*/
73    private transient SnmpSession session;
74
75    /**
76     * Creates key for the specified values. <P>
77     *
78     * @param address the manager's IP adddress
79     * @param port the manager listening port
80     * @param localAddress the local address to bind
81     * @param localPort the local port to bind
82     * @param version the session's native SNMP version
83    **/

84    public ManagerRecord(InetAddress JavaDoc address, int port,
85                         InetAddress JavaDoc localAddress, int localPort, int version)
86       throws BadSnmpVersionException
87    {
88       this.address = address;
89       this.port = port;
90       this.localAddress = localAddress;
91       this.localPort = localPort;
92       
93       switch (version) {
94          case SnmpAgentService.SNMPV1:
95          case SnmpAgentService.SNMPV2:
96             this.version = version;
97             break;
98             
99          default:
100             throw new BadSnmpVersionException("Bad SNMP Version: " + version);
101       }
102    }
103       
104    /**
105     *
106    **/

107    public InetAddress JavaDoc getAddress()
108    {
109       return this.address;
110    }
111
112    /**
113     *
114    **/

115    public int getPort()
116    {
117       return this.port;
118    }
119
120    /**
121     *
122     */

123    public InetAddress JavaDoc getLocalAddress()
124    {
125       return this.localAddress;
126    }
127    
128    /**
129     *
130    **/

131    public int getLocalPort()
132    {
133       return this.localPort;
134    }
135    
136    /**
137     *
138    **/

139    public int getVersion()
140    {
141       return this.version;
142    }
143         
144    public void openSession()
145       throws SocketException JavaDoc
146    {
147       // Create the SNMP session to the manager
148
SnmpPeer peer = new SnmpPeer(this.address, this.port, this.localAddress, this.localPort);
149       peer.setRetries(this.retries);
150       peer.setTimeout(this.timeout);
151             
152       SnmpParameters parameters = peer.getParameters();
153             
154       switch(this.version) {
155          case SnmpAgentService.SNMPV1:
156             parameters.setVersion(SnmpSMI.SNMPV1);
157             break;
158          
159          case SnmpAgentService.SNMPV2:
160             parameters.setVersion(SnmpSMI.SNMPV2);
161             break;
162          default:
163             parameters.setVersion(SnmpSMI.SNMPV1);
164       }
165             
166       parameters.setReadCommunity(this.readCommunity);
167       peer.setParameters(parameters);
168             
169       this.session = new SnmpSession(peer);
170       this.session.setDefaultHandler(this);
171    }
172         
173    /**
174     * Close the session to the manager
175    **/

176    public void closeSession()
177    {
178       this.session.close();
179    }
180         
181    /**
182     * Returns the session to the manager
183    **/

184    public SnmpSession getSession()
185    {
186       return this.session;
187    }
188                 
189    /**
190     * Comparison operator. Keys are considered equal if all address, port
191     * and version are identical
192     *
193     * @param o the key to be compared with
194    **/

195    public boolean equals(Object JavaDoc o)
196    {
197       if (!(o instanceof ManagerRecord))
198          return false;
199             
200       ManagerRecord other = (ManagerRecord)o;
201             
202       return (this.port == other.port &&
203               this.address.equals(other.address) &&
204               this.version == other.version);
205    }
206
207    /**
208     * Hash generator
209    **/

210    public int hashCode()
211    {
212       return toString().hashCode();
213    }
214         
215    /**
216     *
217    **/

218    public String JavaDoc toString()
219    {
220       return new String JavaDoc(this.address + ":" + this.port +
221                         " (" + this.version + ")" );
222    }
223         
224    /**
225     * Stubs to be filled in if we are not only to send traps
226    **/

227    public void snmpInternalError(SnmpSession session, int err, SnmpSyntax pdu)
228    {
229       log.error("ManagerRecord::snmpInternalError, code: " + err);
230    }
231         
232    public void snmpTimeoutError(SnmpSession session, SnmpSyntax pdu)
233    {
234       log.error("ManagerRecord::snmpTimeoutError");
235    }
236     
237    public void snmpReceivedPdu(SnmpSession session, int cmd, SnmpPduPacket pdu)
238    {
239       log.error("ManagerRecord::snmpReceivedPdu");
240    }
241         
242 } // class ManagerRecord
243
Popular Tags