KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > server > DeviceList


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.mrtg.server;
26
27 import java.util.Vector JavaDoc;
28
29 class DeviceList {
30     private Vector JavaDoc routers = new Vector JavaDoc();
31
32     DeviceList() { }
33
34     Vector JavaDoc getRouters() {
35         return routers;
36     }
37
38     void setRouters(Vector JavaDoc routers) {
39         this.routers = routers;
40     }
41
42     public String JavaDoc toString() {
43         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
44         for(int i = 0; i < routers.size(); i++) {
45             buff.append(routers.get(i));
46         }
47         return buff.toString();
48     }
49
50     Device getRouterByHost(String JavaDoc host) {
51         for(int i = 0; i < routers.size(); i++) {
52             Device router = (Device) routers.get(i);
53             if(router.getHost().equalsIgnoreCase(host)) {
54                 return router;
55             }
56         }
57         return null;
58     }
59
60     int addRouter(String JavaDoc host, String JavaDoc community, String JavaDoc descr, boolean active) {
61         Device router = getRouterByHost(host);
62         if(router == null) {
63             // not found
64
Device newRouter = new Device();
65             newRouter.setHost(host);
66             newRouter.setCommunity(community);
67             newRouter.setDescr(descr);
68             newRouter.setActive(active);
69             routers.add(newRouter);
70             // added
71
return 0;
72         }
73         // error, already exists
74
return -1;
75     }
76
77     int updateRouter(String JavaDoc host, String JavaDoc community, String JavaDoc descr, boolean active) {
78         Device router = getRouterByHost(host);
79         if(router != null) {
80             router.setCommunity(community);
81             router.setDescr(descr);
82             router.setActive(active);
83             return 0;
84         }
85         // not found, not updated
86
return -1;
87     }
88
89     int removeRouter(String JavaDoc host) {
90         Device router = getRouterByHost(host);
91         if(router == null) {
92             // not found, cannot remove
93
return -1;
94         }
95         // remove router only if no links are attached
96
if(router.getLinkCount() > 0) {
97             return -2;
98         }
99         routers.remove(router);
100         return 0;
101     }
102
103     int addLink(String JavaDoc host, String JavaDoc ifDescr, String JavaDoc descr, int samplingInterval, boolean active) {
104         Device router = getRouterByHost(host);
105         if(router == null) {
106             // router not found, link cannot be added
107
return -1;
108         }
109         Port link = router.getLinkByIfDescr(ifDescr);
110         if(link != null) {
111             // such link already exists, link cannot be added
112
return -2;
113         }
114         Port newLink = new Port();
115         newLink.setDescr(descr);
116         newLink.setIfDescr(ifDescr);
117         newLink.setSamplingInterval(samplingInterval);
118         newLink.setActive(active);
119         router.addLink(newLink);
120         return 0;
121     }
122
123     int updateLink(String JavaDoc host, String JavaDoc ifDescr, String JavaDoc descr, int samplingInterval, boolean active) {
124         Device router = getRouterByHost(host);
125         if(router == null) {
126             // router not found, link cannot be updated
127
return -1;
128         }
129         Port link = router.getLinkByIfDescr(ifDescr);
130         if(link == null) {
131             // such link cannot be found and updated
132
return -2;
133         }
134         link.setDescr(descr);
135         link.setSamplingInterval(samplingInterval);
136         link.setActive(active);
137         return 0;
138     }
139
140     int removeLink(String JavaDoc host, String JavaDoc ifDescr) {
141         Device router = getRouterByHost(host);
142         if(router == null) {
143             // router not found, link cannot be removed
144
return -1;
145         }
146         Port link = router.getLinkByIfDescr(ifDescr);
147         if(link == null) {
148             // such link cannot be found and removed
149
return -2;
150         }
151         router.removeLink(link);
152         return 0;
153     }
154 }
155
156
Popular Tags