Source Code Cross Referenced for SnmpLib.java in  » Net » JMIBBrowser » com » dwipal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JMIBBrowser » com.dwipal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.dwipal;
002:
003:        import java.util.*;
004:        import org.snmp4j.*;
005:        import org.snmp4j.event.*;
006:        import org.snmp4j.mp.*;
007:        import org.snmp4j.smi.*;
008:        import org.snmp4j.transport.*;
009:
010:        public class SnmpLib implements  ISnmpLib {
011:
012:            private String m_host;
013:            private int m_port;
014:            private String m_readCommunity;
015:            private String m_writeCommunity;
016:            private ISnmpResponseHandler m_snmpResponseHandler;
017:            private boolean m_sessionDestroyed;
018:            private CommunityTarget m_target;
019:
020:            public SnmpLib() {
021:                m_target = null;
022:            }
023:
024:            public String getHost() {
025:                return m_host;
026:            }
027:
028:            public int getPort() {
029:                return m_port;
030:            }
031:
032:            public String getReadCommunity() {
033:                return m_readCommunity;
034:            }
035:
036:            public String getWriteCommunity() {
037:                return m_writeCommunity;
038:            }
039:
040:            public void setCommunity(String readCommunity, String writeCommunity) {
041:                m_readCommunity = readCommunity;
042:                m_writeCommunity = writeCommunity;
043:            }
044:
045:            public void setHost(String host) {
046:                m_target = null;
047:                m_host = host;
048:            }
049:
050:            public void setPort(int port) {
051:                m_target = null;
052:                m_port = port;
053:            }
054:
055:            public void setSnmpResponseHandler(
056:                    ISnmpResponseHandler snmpResponseHandler) {
057:                m_snmpResponseHandler = snmpResponseHandler;
058:            }
059:
060:            public ISnmpResponseHandler getSnmpResponseHandler() {
061:                return m_snmpResponseHandler;
062:            }
063:
064:            public void destroySession() {
065:                m_sessionDestroyed = true;
066:            }
067:
068:            private Target getTarget(String strCommunity) {
069:                if (m_target == null) {
070:                    Address addr = GenericAddress.parse("udp:" + getHost()
071:                            + "/" + getPort());
072:                    m_target = new CommunityTarget();
073:                    m_target.setCommunity(new OctetString(strCommunity));
074:                    m_target.setAddress(addr);
075:                    m_target.setVersion(SnmpConstants.version1);
076:                    m_target.setRetries(3);
077:                }
078:                return m_target;
079:            }
080:
081:            public void snmpWalk(String oidFrom, String oidTo)
082:                    throws SnmpException {
083:                snmpWalk(oidFrom);
084:            }
085:
086:            public void snmpWalk(String oidFrom) throws SnmpException {
087:                PDU request = new PDU();
088:                request.setType(PDU.GETNEXT);
089:                request.add(new VariableBinding(new OID(oidFrom)));
090:                request.setNonRepeaters(0);
091:                OID rootOID = request.get(0).getOid();
092:                PDU response = null;
093:
094:                int objects = 0;
095:                int requests = 0;
096:                long startTime = System.currentTimeMillis();
097:
098:                try {
099:                    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
100:                    snmp.listen();
101:                    m_sessionDestroyed = false;
102:                    do {
103:                        requests++;
104:                        ResponseEvent responseEvent = snmp.send(request,
105:                                getTarget(getReadCommunity()));
106:                        response = responseEvent.getResponse();
107:                        if (response != null) {
108:                            objects += response.size();
109:                        }
110:                    } while (!processWalk(response, request, rootOID)
111:                            && !m_sessionDestroyed);
112:
113:                } catch (SnmpException e) {
114:                    throw e;
115:                } catch (Exception e) {
116:                    e.printStackTrace();
117:                    throw new SnmpException(e.getMessage());
118:                }
119:                if (m_snmpResponseHandler != null) {
120:                    m_snmpResponseHandler.requestStats(requests, objects,
121:                            System.currentTimeMillis() - startTime);
122:                }
123:            }
124:
125:            private boolean processWalk(PDU response, PDU request, OID rootOID)
126:                    throws SnmpException {
127:                if ((response == null) || (response.getErrorStatus() != 0)
128:                        || (response.getType() == PDU.REPORT)) {
129:                    return true;
130:                }
131:                boolean finished = false;
132:                OID lastOID = request.get(0).getOid();
133:                for (int i = 0; (!finished) && (i < response.size()); i++) {
134:                    VariableBinding vb = response.get(i);
135:                    if ((vb.getOid() == null)
136:                            || (vb.getOid().size() < rootOID.size())
137:                            || (rootOID.leftMostCompare(rootOID.size(), vb
138:                                    .getOid()) != 0)) {
139:                        finished = true;
140:                    } else if (Null.isExceptionSyntax(vb.getVariable()
141:                            .getSyntax())) {
142:                        outputResponse(vb);
143:                        finished = true;
144:                    } else if (vb.getOid().compareTo(lastOID) <= 0) {
145:                        throw new SnmpException(
146:                                "Variable received is not lexicographic successor of requested one:"
147:                                        + vb.toString() + " <= " + lastOID);
148:                    } else {
149:                        outputResponse(vb);
150:                        lastOID = vb.getOid();
151:                    }
152:                }
153:                if (response.size() == 0) {
154:                    finished = true;
155:                }
156:                if (!finished) {
157:                    VariableBinding next = response.get(response.size() - 1);
158:                    next.setVariable(new Null());
159:                    request.set(0, next);
160:                    request.setRequestID(new Integer32(0));
161:                }
162:                return finished;
163:            }
164:
165:            private SnmpOidValuePair outputResponse(VariableBinding vb) {
166:                SnmpOidValuePair oidval = new SnmpOidValuePair();
167:                oidval.oid = vb.getOid().toString();
168:                oidval.value_str = vb.getVariable().toString();
169:                if (m_snmpResponseHandler != null) {
170:                    m_snmpResponseHandler.responseReceived(oidval);
171:                }
172:                return oidval;
173:            }
174:
175:            void snmpSetValue(String oid, int syntax, String value)
176:                    throws SnmpException {
177:                VariableBinding varbind = getVarBindForSetRequest(oid, syntax,
178:                        value);
179:
180:                PDU request = new PDU();
181:                request.setType(PDU.SET);
182:                request.add(varbind);
183:                PDU response = null;
184:
185:                long startTime = System.currentTimeMillis();
186:
187:                try {
188:                    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
189:                    snmp.listen();
190:                    ResponseEvent responseEvent = snmp.send(request,
191:                            getTarget(getWriteCommunity()));
192:                    response = responseEvent.getResponse();
193:                    System.out.println(response);
194:                } catch (Exception e) {
195:                    e.printStackTrace();
196:                    throw new SnmpException(e.getMessage());
197:                }
198:            }
199:
200:            VariableBinding getVarBindForSetRequest(String oid, int type,
201:                    String value) {
202:                VariableBinding vb = new VariableBinding(new OID(oid));
203:
204:                if (value != null) {
205:                    Variable variable;
206:                    switch (type) {
207:                    case DwSnmpMibRecord.VALUE_TYPE_INTEGER32:
208:                        variable = new Integer32(Integer.parseInt(value));
209:                        break;
210:                    case DwSnmpMibRecord.VALUE_TYPE_UNSIGNED_INTEGER32:
211:                        variable = new UnsignedInteger32(Long.parseLong(value));
212:                        break;
213:                    case DwSnmpMibRecord.VALUE_TYPE_OCTET_STRING:
214:                        variable = new OctetString(value);
215:                        break;
216:                    case DwSnmpMibRecord.VALUE_TYPE_NULL:
217:                        variable = new Null();
218:                        break;
219:                    case DwSnmpMibRecord.VALUE_TYPE_OID:
220:                        variable = new OID(value);
221:                        break;
222:                    case DwSnmpMibRecord.VALUE_TYPE_TIMETICKS:
223:                        variable = new TimeTicks(Long.parseLong(value));
224:                        break;
225:                    case DwSnmpMibRecord.VALUE_TYPE_IP_ADDRESS:
226:                        variable = new IpAddress(value);
227:                        break;
228:                    default:
229:                        throw new IllegalArgumentException("Variable type "
230:                                + type + " not supported");
231:                    }
232:                    vb.setVariable(variable);
233:                }
234:                return vb;
235:            }
236:
237:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.