KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPInformRequestSenderInterface


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.io.*;
34 import java.net.*;
35
36
37 /**
38 * The class SNMPInformRequestSenderInterface implements an interface for sending SNMPv2 inform request
39 * messages to a remote SNMP manager. The approach is that from version 2c of SNMP, using no encryption of data.
40 * Communication occurs via UDP, using port 162, the standard SNMP trap and inform request port, as the destination port.
41 */

42
43 public class SNMPInformRequestSenderInterface
44 {
45     public static final int SNMP_TRAP_PORT = 162;
46     
47     private DatagramSocket dSocket;
48     
49     
50     /**
51     * Construct a new inform request sender object to send inform requests to remote SNMP hosts.
52     */

53     
54     public SNMPInformRequestSenderInterface()
55         throws SocketException
56     {
57         dSocket = new DatagramSocket();
58     }
59     
60
61     
62     /**
63     * Construct a new inform request sender object to send inform requests to remote SNMP hosts, binding to
64     * the specified local port.
65     */

66     
67     public SNMPInformRequestSenderInterface(int localPort)
68         throws SocketException
69     {
70         dSocket = new DatagramSocket(localPort);
71     }
72     
73
74     
75     /**
76     * Send the supplied SNMPv2 inform request pdu to the specified host, using the supplied version number
77     * and community name.
78     */

79     
80     public void sendInformRequest(int version, InetAddress hostAddress, String JavaDoc community, SNMPv2InformRequestPDU pdu)
81         throws IOException
82     {
83         SNMPMessage message = new SNMPMessage(version, community, pdu);
84         
85         byte[] messageEncoding = message.getBEREncoding();
86         
87         /*
88         System.out.println("Request Message bytes:");
89         
90         for (int i = 0; i < messageEncoding.length; ++i)
91             System.out.print(hexByte(messageEncoding[i]) + " ");
92         */

93         
94         DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT);
95         
96         /*
97         System.out.println("Message bytes length (out): " + outPacket.getLength());
98         
99         System.out.println("Message bytes (out):");
100         for (int i = 0; i < messageEncoding.length; ++i)
101         {
102             System.out.print(hexByte(messageEncoding[i]) + " ");
103         }
104         System.out.println("\n");
105         */

106         
107         dSocket.send(outPacket);
108         
109     }
110     
111     
112     
113     /**
114     * Send the supplied inform request pdu to the specified host, using the supplied community name and
115     * using 1 for the version field in the SNMP message.
116     */

117     
118     public void sendInformRequest(InetAddress hostAddress, String JavaDoc community, SNMPv2InformRequestPDU pdu)
119         throws IOException
120     {
121         int version = 1;
122     
123         sendInformRequest(version, hostAddress, community, pdu);
124     }
125     
126     
127     
128     private String JavaDoc hexByte(byte b)
129     {
130         int pos = b;
131         if (pos < 0)
132             pos += 256;
133         String JavaDoc returnString = new String JavaDoc();
134         returnString += Integer.toHexString(pos/16);
135         returnString += Integer.toHexString(pos%16);
136         return returnString;
137     }
138     
139     
140     
141     
142     
143     
144     private String JavaDoc getHex(byte theByte)
145     {
146         int b = theByte;
147         
148         if (b < 0)
149             b += 256;
150         
151         String JavaDoc returnString = new String JavaDoc(Integer.toHexString(b));
152         
153         // add leading 0 if needed
154
if (returnString.length()%2 == 1)
155             returnString = "0" + returnString;
156             
157         return returnString;
158     }
159     
160     
161     
162 }
Popular Tags