1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package uk.co.westhawk.snmp.beans;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 import uk.co.westhawk.snmp.stack.*;
51 import uk.co.westhawk.snmp.pdu.*;
52
53 import javax.swing.tree.*;
54 import java.awt.*;
55 import java.util.*;
56 import java.text.*;
57 import java.lang.*;
58 import java.io.*;
59 import java.beans.*;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public class AnnexModemStatusBean extends SNMPRunBean
128 implements Observer, TreeNode {
129 private static final String version_id = "@(#)$Id: AnnexModemStatusBean.java,v 1.22 2006/03/23 14:54:09 birgit Exp $ Copyright Westhawk Ltd";
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public final static String charPortIndex = "1.3.6.1.2.1.19.2.1.1";
144
145
146
147
148
149
150
151
152
153
154 public final static String charPortName = "1.3.6.1.2.1.19.2.1.2";
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public final static String charPortOperStatus = "1.3.6.1.2.1.19.2.1.7";
188
189
190
191
192
193
194
195
196
197
198 public final static String rs232InSigState = "1.3.6.1.2.1.10.33.5.1.3";
199
200 private final static int NR_PORT_OID = 3;
201
202
203 public final static int portUP = 1;
204
205 public final static int portDOWN = 2;
206
207 public final static int portMAINTENANCE = 3;
208
209 public final static int portABSENT = 4;
210
211 public final static int portACTIVE = 5;
212
213
214
215
216
217 public final static int DCD = 6;
218
219
220 public final static int sigNONE = 1;
221
222 public final static int sigON = 2;
223
224 public final static int sigOFF = 3;
225
226 public final static String[] sig_state = {
227 "unknown",
228 "none",
229 "in use",
230 "not in use"
231 };
232
233 private GetNextPdu_vec pduGetNext;
234 private Hashtable modemIndexStatusHash;
235 private Hashtable modemHash;
236
237 private TreeNode parent;
238 private DefaultTreeModel treeModel;
239
240 private boolean isGetNextInFlight;
241 private Date lastUpdateDate = null;
242
243 private int deviceType = 0;
244 private int openCount = 0;
245 private int modemStatus = 0;
246
247
248
249
250 public AnnexModemStatusBean() {
251 modemIndexStatusHash = new Hashtable();
252 modemHash = new Hashtable();
253 }
254
255
256
257
258
259
260
261
262
263 public AnnexModemStatusBean(String h, int p) {
264 this(h, p, null);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public AnnexModemStatusBean(String h, int p, String b) {
281 this();
282 setHost(h);
283 setPort(p);
284 setBindAddress(b);
285 }
286
287
288
289
290
291
292
293 public Date getLastUpdateDate() {
294 return lastUpdateDate;
295 }
296
297
298
299
300
301
302
303
304
305
306
307 public Enumeration getModemIndexes() {
308 return modemIndexStatusHash.keys();
309 }
310
311
312
313
314 public synchronized int getModemCount() {
315 return modemIndexStatusHash.size();
316 }
317
318
319
320
321
322
323
324
325 public String getPortName(Long index) {
326 String name = "";
327 Object obj = modemHash.get(index);
328 if (obj != null) {
329 PortInfo pInfo = (PortInfo) obj;
330 name = pInfo.getName();
331 }
332 return name;
333 }
334
335
336
337
338
339
340
341
342
343 public int getPortStatus(Long index) {
344 int status = 0;
345 Object obj = modemIndexStatusHash.get(index);
346 if (obj != null) {
347 status = ((Integer) obj).intValue();
348 }
349 return status;
350 }
351
352
353
354
355
356
357
358
359
360 public String getPortStatusString(Long index) {
361 int status = getPortStatus(index);
362 String str = sig_state[status];
363 return str;
364 }
365
366
367
368
369 public Enumeration children() {
370 return modemHash.elements();
371 }
372
373
374
375
376
377 public int getChildCount() {
378 int sz = modemHash.size();
379 return sz;
380 }
381
382
383
384
385
386 public TreeNode getChildAt(int childIndex) {
387 TreeNode node = null;
388 if (childIndex < modemHash.size()) {
389 Enumeration e = modemHash.elements();
390 for (int i = 0; i <= childIndex; i++) {
391 node = (TreeNode) e.nextElement();
392 }
393 }
394 return node;
395 }
396
397
398
399
400
401
402 public int getIndex(TreeNode node) {
403 int ret = -1;
404 if (modemHash.contains(node)) {
405 boolean found = false;
406 Enumeration e = modemHash.elements();
407 while (e.hasMoreElements() && !found) {
408 TreeNode n = (TreeNode) e.nextElement();
409 found = (n == node);
410 ret++;
411 }
412 }
413 return ret;
414 }
415
416
417
418
419 public TreeNode getParent() {
420 return parent;
421 }
422
423
424
425
426 public boolean getAllowsChildren() {
427 return true;
428 }
429
430
431
432
433 public boolean isLeaf() {
434 return false;
435 }
436
437
438
439
440
441 public void action() {
442 if (isHostPortReachable()) {
443 lastUpdateDate = new Date();
444 isGetNextInFlight = false;
445 setRunning(true);
446 }
447 }
448
449
450
451
452
453
454
455
456 public void run() {
457 while (context != null && isRunning()) {
458 if (isGetNextInFlight == false) {
459
460 isGetNextInFlight = true;
461
462 pduGetNext = new GetNextPdu_vec(context, NR_PORT_OID);
463 pduGetNext.addObserver(this);
464
465 pduGetNext.addOid(charPortIndex);
466 pduGetNext.addOid(charPortName);
467 pduGetNext.addOid(charPortOperStatus);
468 try {
469 pduGetNext.send();
470 } catch (PduException exc) {
471 System.out.println("PduException " + exc.getMessage());
472 } catch (IOException exc) {
473 System.out.println("IOException " + exc.getMessage());
474 }
475 }
476
477 try {
478 Thread.sleep(interval);
479 } catch (InterruptedException ix) {
480 ;
481 }
482 }
483 }
484
485
486
487
488
489
490 public void update(Observable obs, Object ov) {
491 boolean loopHasEnded = false;
492
493 if (obs instanceof GetNextPdu_vec) {
494 int portIndex, portStatus;
495 String portName;
496
497 varbind[] var;
498 pduGetNext = (GetNextPdu_vec) obs;
499
500 if (pduGetNext.isTimedOut() == false) {
501 if (pduGetNext.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
502 var = (varbind[]) ov;
503 if (var[0].getOid().toString().startsWith(charPortIndex)) {
504 portIndex = ((AsnInteger) var[0].getValue()).getValue();
505 portName = ((AsnOctets) var[1].getValue()).getValue();
506 portStatus = ((AsnInteger) var[2].getValue()).getValue();
507
508 Long indexInt = new Long(portIndex);
509 if (portStatus == portUP) {
510
511
512
513 PortInfo pStatus = (PortInfo) modemHash.get(indexInt);
514
515 if (pStatus == null) {
516 pStatus = new PortInfo(portIndex, portName, this);
517 modemHash.put(indexInt, pStatus);
518 }
519
520
521 String oid = rs232InSigState + "." + String.valueOf(portIndex)
522 + "." + String.valueOf(DCD);
523
524 try {
525 GetPdu pduGet = new GetPdu(context);
526 pduGet.addOid(oid);
527 pduGet.addObserver(this);
528 pduGet.send();
529 } catch (PduException exc) {
530 System.out.println("PduException " + exc.getMessage());
531 } catch (IOException exc) {
532 System.out.println("IOException " + exc.getMessage());
533 }
534 } else {
535
536
537 modemIndexStatusHash.remove(indexInt);
538 modemHash.remove(indexInt);
539 }
540
541
542 pduGetNext = new GetNextPdu_vec(context, NR_PORT_OID);
543 pduGetNext.addObserver(this);
544
545 pduGetNext.addOid(var[0].getOid().toString());
546 pduGetNext.addOid(var[1].getOid().toString());
547 pduGetNext.addOid(var[2].getOid().toString());
548 try {
549 pduGetNext.send();
550 } catch (PduException exc) {
551 System.out.println("PduException " + exc.getMessage());
552 } catch (IOException exc) {
553 System.out.println("IOException " + exc.getMessage());
554 }
555 } else {
556 loopHasEnded = true;
557 }
558 } else {
559
560
561
562
563 loopHasEnded = true;
564 }
565 } else {
566 modemIndexStatusHash.clear();
567 modemHash.clear();
568 loopHasEnded = true;
569 }
570 } else if (obs instanceof GetPdu) {
571 GetPdu pduGet = (GetPdu) obs;
572 if (pduGet.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
573 varbind var = (varbind) ov;
574
575 AsnObjectId oid = var.getOid();
576 int sigStatus = ((AsnInteger) var.getValue()).getValue();
577
578
579 int len = oid.getSize();
580 long portIndex = oid.getElementAt(len - 2);
581
582 Long indexInt = new Long(portIndex);
583 modemIndexStatusHash.put(indexInt,
584 new Integer(sigStatus));
585
586 PortInfo pStatus = (PortInfo) modemHash.get(indexInt);
587
588 if (pStatus != null) {
589 pStatus.setStatus(sigStatus);
590 }
591 }
592 }
593
594 if (loopHasEnded) {
595 lastUpdateDate = new Date();
596 javax.swing.SwingUtilities.invokeLater(new TreeUpdate());
597 isGetNextInFlight = false;
598 }
599
600 }
601
602
603
604
605
606 public void setParent(TreeNode p) {
607 parent = p;
608 }
609
610
611
612
613
614 public void setDefaultTreeModel(DefaultTreeModel model) {
615 treeModel = model;
616 }
617
618
619
620
621
622
623
624 protected void fireTreeModelChanged() {
625 if (treeModel != null) {
626 treeModel.nodeStructureChanged(this);
627 }
628 }
629
630 class PortInfo extends Object implements TreeNode {
631 private TreeNode parent;
632 private int portIndex, sigStatus;
633 private String portName;
634
635 public PortInfo(int ind, String nm, TreeNode par) {
636 this(ind, 0, nm, par);
637 }
638
639 public PortInfo(int ind, int st, String nm, TreeNode par) {
640 portIndex = ind;
641 sigStatus = st;
642 portName = nm;
643 parent = par;
644 }
645
646 public int getIndex() {
647 return portIndex;
648 }
649
650 public int getStatus() {
651 return sigStatus;
652 }
653
654 public void setStatus(int st) {
655 sigStatus = st;
656 }
657
658 public String getName() {
659 return portName;
660 }
661
662 public String toString() {
663 return "" + portIndex + " " + portName + " " + sig_state[sigStatus];
664 }
665
666
667
668
669 public Enumeration children() {
670 return null;
671 }
672
673
674
675
676
677 public int getChildCount() {
678 return 0;
679 }
680
681
682
683
684
685 public TreeNode getChildAt(int childIndex) {
686 return null;
687 }
688
689
690
691
692
693
694 public int getIndex(TreeNode node) {
695 return -1;
696 }
697
698
699
700
701 public TreeNode getParent() {
702 return parent;
703 }
704
705
706
707
708 public boolean getAllowsChildren() {
709 return true;
710 }
711
712
713
714
715 public boolean isLeaf() {
716 return true;
717 }
718
719 }
720
721 class TreeUpdate implements Runnable {
722 public void run() {
723 fireTreeModelChanged();
724 firePropertyChange("modems", null, null);
725 }
726 }
727
728 }