KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfServicesComponents > SnmpAdapterProviderComposition > SnmpProtocolConverter


1 package ist.coach.coachEmfServicesComponents.SnmpAdapterProviderComposition;
2
3 /**
4  * @author administrator (digi@intracom.gr)
5  *
6  * Copyright (c) Intracom S.A., N.T., 2003
7  */

8
9 import java.lang.*;
10 import java.lang.System JavaDoc;
11 import java.net.*;
12 import org.opennms.protocols.snmp.*;
13 import java.util.Vector JavaDoc;
14
15 import ist.coach.coachEmfServices.SnmpAdapter.SnmpApplicationError;
16 import ist.coach.coachEmfCommon.ExceptionMessages;
17
18     public class SnmpProtocolConverter implements SnmpHandler{
19
20         private int m_version = SnmpSMI.SNMPV2;
21         private String JavaDoc m_community = "public";
22         private int m_retries = 5;
23         private int m_timeout = 180;
24         private int m_port = 161;
25         private String JavaDoc m_host = null;
26
27         /*For SNMP walk only*/
28         private boolean snmpWalkRequest;
29         private String JavaDoc m_startOid = null;
30         private SnmpObjectId m_stopAt = null;
31         public Vector JavaDoc resultSet = null;
32
33         /*For session timeout */
34         private boolean agentResponded;
35
36         private int commandType = -1;
37
38         public SnmpProtocolConverter (
39             int version,
40             String JavaDoc community,
41             int retries,
42             int timeout,
43             int port,
44             String JavaDoc host,
45             String JavaDoc startOid) {
46
47                 this.m_version = version;
48                 this.m_community = community;
49                 this.m_retries = retries;
50                 this.m_timeout = timeout;
51                 this.m_port = port;
52                 this.m_host = host;
53
54                 this.m_startOid = startOid;
55
56                 this.resultSet = new Vector JavaDoc();
57
58         }
59
60         public SnmpProtocolConverter (
61             String JavaDoc host,
62             String JavaDoc community,
63             int port,
64             String JavaDoc startOid) {
65
66                 this.m_version = SnmpSMI.SNMPV2;
67                 this.m_community = community;
68                 this.m_retries = 5;
69                 this.m_timeout = 500;
70                 //this.m_port = 161;
71
this.m_port = port;
72                 this.m_host = host;
73                 this.m_startOid = startOid;
74
75                 this.resultSet = new Vector JavaDoc();
76         }
77
78         public void snmpInternalError(SnmpSession session,
79             int err, SnmpSyntax pdu){
80
81                 System.out.println("An unexpected error occured with the SNMP Session");
82                 System.out.println("The error code is " + err);
83
84                 synchronized(session){
85                     session.notify();
86                 }
87         }
88
89         public void snmpTimeoutError(SnmpSession session,
90             SnmpSyntax pdu) {
91
92             System.err.println("The session timed out trying to communicate with the remote host");
93
94             synchronized(session){
95                 session.notify();
96             }
97
98             this.agentResponded = false;
99         }
100
101         public void snmpGet(SnmpVarBind[] vblist)
102         throws SnmpApplicationError {
103
104             SnmpPeer snmpPeer = null;
105             this.snmpWalkRequest = false;
106             this.agentResponded = true;
107
108             //clear all previous results
109
resultSet.clear();
110
111             try {
112                 snmpPeer = new SnmpPeer(InetAddress.getByName(this.m_host));
113             }
114             catch (UnknownHostException ue) {
115                 ue.printStackTrace();
116                 throw new SnmpApplicationError(ExceptionMessages.snmp_host_resolve_error);
117             }
118
119             snmpPeer.setPort(this.m_port);
120             snmpPeer.setTimeout(this.m_timeout);
121             snmpPeer.setRetries(this.m_retries);
122
123             SnmpParameters parms = snmpPeer.getParameters();
124             parms.setVersion(this.m_version);
125             parms.setReadCommunity(this.m_community);
126
127             SnmpSession session = null;
128
129             try {
130                 session = new SnmpSession(snmpPeer);
131             }
132             catch (SocketException se) {
133                 se.printStackTrace();
134                 throw new SnmpApplicationError(ExceptionMessages.agent_create_error);
135             }
136
137             session.setDefaultHandler(this);
138
139             SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduPacket.GET, vblist);
140
141             try {
142
143                 synchronized(session) {
144                     session.send(pdu);
145                     session.wait();
146                 }
147             }
148             catch (InterruptedException JavaDoc ie) {
149                 ie.printStackTrace();
150             }
151             finally {
152
153                 System.out.println("Closing!");
154
155                 session.close();
156
157             }
158         }
159
160         public void snmpGetNext(SnmpObjectId oid)
161         throws SnmpApplicationError {
162
163             SnmpPeer snmpPeer = null;
164             this.snmpWalkRequest = false;
165             this.agentResponded = true;
166
167             //clear all previous results
168
resultSet.clear();
169
170             try {
171                 snmpPeer = new SnmpPeer(InetAddress.getByName(this.m_host));
172             }
173             catch (UnknownHostException ue) {
174                 ue.printStackTrace();
175                 throw new SnmpApplicationError(ExceptionMessages.snmp_host_resolve_error);
176             }
177
178             snmpPeer.setPort(this.m_port);
179             snmpPeer.setTimeout(this.m_timeout);
180             snmpPeer.setRetries(this.m_retries);
181
182             SnmpParameters parms = snmpPeer.getParameters();
183             parms.setVersion(this.m_version);
184             parms.setReadCommunity(this.m_community);
185
186             SnmpSession session = null;
187
188             try {
189                 session = new SnmpSession(snmpPeer);
190             }
191             catch (SocketException se) {
192                 se.printStackTrace();
193                 throw new SnmpApplicationError(ExceptionMessages.agent_create_error);
194             }
195
196             session.setDefaultHandler(this);
197
198             SnmpVarBind [] vblist = {new SnmpVarBind(oid)};
199
200             SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduPacket.GETNEXT, vblist);
201
202             try {
203
204                 synchronized(session) {
205                     session.send(pdu);
206                     session.wait();
207                 }
208             }
209             catch (InterruptedException JavaDoc ie) {
210                 ie.printStackTrace();
211             }
212             finally {
213
214                 System.out.println("Closing!");
215
216                 session.close();
217
218             }
219         }
220
221         public void snmpWalk()
222         throws SnmpApplicationError {
223
224             SnmpPeer peer = null;
225             this.snmpWalkRequest = true;
226             this.agentResponded = true;
227
228             //clear all previous results
229
resultSet.clear();
230
231             try {
232                 peer = new SnmpPeer(InetAddress.getByName(this.m_host));
233             }
234             catch (UnknownHostException eh) {
235                 eh.printStackTrace();
236                 throw new SnmpApplicationError(ExceptionMessages.snmp_host_resolve_error);
237             }
238             peer.setPort(this.m_port);
239             peer.setTimeout(this.m_timeout);
240             peer.setRetries(this.m_retries);
241
242             SnmpParameters parms = peer.getParameters();
243             parms.setVersion(this.m_version);
244             parms.setReadCommunity(this.m_community);
245
246             SnmpSession session = null;
247             try {
248
249                 session = new SnmpSession(peer);
250             }
251             catch(SocketException e) {
252                 System.err.println("SocketException creating the SNMP session");
253                 System.err.println("SocketException: " + e.getMessage());
254                 throw new SnmpApplicationError(ExceptionMessages.agent_create_error);
255             }
256
257             session.setDefaultHandler(this);
258
259             if (m_stopAt == null) {
260                 SnmpObjectId id = new SnmpObjectId(m_startOid);
261                 int[] ids = id.getIdentifiers();
262                 ++ids[ids.length-1];
263                 id.setIdentifiers(ids);
264                 m_stopAt = id;
265             }
266
267             SnmpVarBind[] vblist = { new SnmpVarBind(m_startOid) };
268             SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduPacket.GETNEXT, vblist);
269             try {
270                 synchronized(session)
271                 {
272                     session.send(pdu);
273                     session.wait();
274                 }
275             }
276             catch(InterruptedException JavaDoc e) {
277
278                 e.printStackTrace();
279
280                         // do nothing
281
}
282             finally {
283
284                 System.out.println("Closing");
285                 session.close();
286                 //System.out.println("Got " + this.getV().size() + " results.");
287
}
288         }
289
290         public void snmpSet(SnmpVarBind[] vblist)
291         throws SnmpApplicationError {
292
293             SnmpPeer snmpPeer = null;
294             this.snmpWalkRequest = false;
295             this.agentResponded = true;
296
297             //clear all previous results
298
resultSet.clear();
299
300             try {
301
302                 snmpPeer = new SnmpPeer(InetAddress.getByName(this.m_host));
303             }
304             catch (UnknownHostException ue) {
305                 ue.printStackTrace();
306                 throw new SnmpApplicationError(ExceptionMessages.snmp_host_resolve_error);
307             }
308
309             snmpPeer.setPort(this.m_port);
310             snmpPeer.setTimeout(this.m_timeout);
311             snmpPeer.setRetries(this.m_retries);
312
313             SnmpParameters parms = snmpPeer.getParameters();
314             parms.setVersion(this.m_version);
315             parms.setWriteCommunity(this.m_community);
316
317             SnmpSession session = null;
318
319             try {
320                 session = new SnmpSession(snmpPeer);
321             }
322             catch (SocketException se) {
323                 se.printStackTrace();
324                 throw new SnmpApplicationError(ExceptionMessages.agent_create_error);
325             }
326
327             session.setDefaultHandler(this);
328
329             SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduPacket.SET, vblist);
330
331             try {
332
333                 synchronized(session) {
334                     session.send(pdu);
335                     session.wait();
336                 }
337             }
338             catch (InterruptedException JavaDoc ie) {
339                 ie.printStackTrace();
340             }
341             finally {
342
343                 session.close();
344
345             }
346         }
347
348         public void snmpReceivedPdu(SnmpSession session,
349             int cmd,
350             SnmpPduPacket pdu){
351
352             SnmpPduRequest req = null;
353
354             if(pdu instanceof SnmpPduRequest){
355                 req = (SnmpPduRequest)pdu;
356             }
357
358             System.out.println("Error index: " + req.getErrorIndex());
359             System.out.println("Error status: " + req.getErrorStatus());
360
361             if(pdu.getCommand() != SnmpPduPacket.RESPONSE){
362                 System.err.println("Error: Received non-response command " +
363                     pdu.getCommand());
364
365                 synchronized(session){
366                     session.notify();
367                 }
368
369                 return;
370             }
371
372             if(req.getErrorStatus() != 0){
373                 System.out.println("End of mib reached");
374
375                 synchronized(session){
376                     session.notify();
377                 }
378                 return;
379             }
380             /* previous request was an snmp walk request */
381             if (this.snmpWalkRequest == true) {
382
383                 SnmpVarBind vb = pdu.getVarBindAt(0);
384                 if (vb.getValue().typeId() == SnmpEndOfMibView.ASNTYPE ||
385                     (m_stopAt != null && m_stopAt.compare(vb.getName()) < 0)) {
386
387                     System.out.println("End of Mib reached");
388                     synchronized (session) {
389                         session.notify();
390                     }
391                     return;
392                 }
393
394                 this.resultSet.add(vb);
395
396                 System.out.println(vb.toString());
397
398                 SnmpVarBind[] vblist = { new SnmpVarBind(vb.getName())};
399                 SnmpPduRequest newReq = new SnmpPduRequest(SnmpPduPacket.GETNEXT, vblist);
400
401                 session.send(newReq);
402             }
403             /* previous request was not an snmp walk request */
404             else {
405
406                 System.out.println("Pdu length: " + pdu.getLength());
407
408                 for (int i = 0; i < pdu.getLength(); i++) {
409                     //System.out.println(pdu.getVarBindAt(i).toString());
410
this.resultSet.add(pdu.getVarBindAt(i));
411                 }
412
413                 synchronized (session) {
414                     session.notify();
415                 }
416
417                 return;
418             }
419         }
420
421         public boolean agentResponded() {
422
423             return this.agentResponded;
424         }
425
426 }
427
428
Popular Tags