KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.xmlrpc.WebServer;
28 import org.jrobin.mrtg.Debug;
29 import org.jrobin.mrtg.MrtgConstants;
30 import org.jrobin.mrtg.MrtgException;
31
32 import java.util.Date JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 class Listener implements MrtgConstants {
37     private WebServer webServer;
38
39     Listener(String JavaDoc[] clients) {
40         webServer = new WebServer(SERVER_PORT);
41         webServer.addHandler("mrtg", new EventHandler());
42         if(clients != null && clients.length > 0) {
43             webServer.setParanoid(true);
44             for(int i = 0; i < clients.length; i++) {
45                 webServer.acceptClient(clients[i]);
46             }
47         }
48         webServer.start();
49         Debug.print("XmlRpcServer started on port " + SERVER_PORT);
50     }
51
52     void terminate() {
53         if(webServer != null) {
54             webServer.shutdown();
55             Debug.print("XmlRpcServer closed");
56             webServer = null;
57         }
58     }
59
60     protected void finalize() {
61         terminate();
62     }
63
64     public class EventHandler {
65         public int addRouter(String JavaDoc host, String JavaDoc community, String JavaDoc descr, boolean active) {
66             try {
67                 int status = Server.getInstance().addRouter(host, community, descr, active);
68                 Debug.print("Router " + host + " added [" + status + "]");
69                 return status;
70             } catch (MrtgException e) {
71                 Debug.print("Event handler error: " + e);
72                 return -10;
73             }
74         }
75
76         public int updateRouter(String JavaDoc host, String JavaDoc community, String JavaDoc descr, boolean active) {
77             try {
78                 int status = Server.getInstance().updateRouter(host, community, descr, active);
79                 Debug.print("Router " + host + " updated [" + status + "]");
80                 return status;
81             } catch (MrtgException e) {
82                 Debug.print("Event handler error: " + e);
83                 return -10;
84             }
85         }
86
87         public int removeRouter(String JavaDoc host) {
88             try {
89                 int status = Server.getInstance().removeRouter(host);
90                 Debug.print("Router " + host + " removed [" + status + "]");
91                 return status;
92             } catch (MrtgException e) {
93                 Debug.print("Event handler error: " + e);
94                 return -10;
95             }
96         }
97
98         public int addLink(String JavaDoc host, String JavaDoc ifDescr, String JavaDoc descr, int samplingInterval,
99                            boolean active) {
100             try {
101                 int status =
102                     Server.getInstance().addLink(host, ifDescr, descr, samplingInterval, active);
103                 Debug.print("Interface " + ifDescr + "@" + host + " added [" + status + "]");
104                 return status;
105             } catch (MrtgException e) {
106                 Debug.print("Event handler error: " + e);
107                 return -10;
108             }
109         }
110
111         public int updateLink(String JavaDoc host, String JavaDoc ifDescr, String JavaDoc descr,
112                               int samplingInterval, boolean active) {
113             try {
114                 int status =
115                     Server.getInstance().updateLink(host, ifDescr, descr, samplingInterval, active);
116                 Debug.print("Interface " + ifDescr + "@" + host + " updated [" + status + "]");
117                 return status;
118             } catch (MrtgException e) {
119                 Debug.print("Event handler error: " + e);
120                 return -10;
121             }
122         }
123
124         public int removeLink(String JavaDoc host, String JavaDoc ifDescr) {
125             try {
126                 int status = Server.getInstance().removeLink(host, ifDescr);
127                 Debug.print("Interface " + ifDescr + "@" + host + " removed [" + status + "]");
128                 return status;
129             } catch (MrtgException e) {
130                 Debug.print("Event handler error: " + e);
131                 return -10;
132             }
133         }
134
135         public byte[] getPngGraph(String JavaDoc host, String JavaDoc ifDescr, Date JavaDoc startDate, Date JavaDoc stopDate) {
136             byte[] graph = new byte[0];
137             long start = startDate.getTime() / 1000L;
138             long stop = stopDate.getTime() / 1000L;
139             try {
140                 graph = Server.getInstance().getPngGraph(host, ifDescr, start, stop);
141             } catch (MrtgException e) {
142                 Debug.print("Event handler error: " + e);
143             }
144             Debug.print("Graph for interface " + ifDescr + "@" + host +
145                 " generated [" + graph.length + " bytes]");
146             return graph;
147         }
148
149         public Vector JavaDoc getAvailableLinks(String JavaDoc host) {
150             Vector JavaDoc result = new Vector JavaDoc();
151             try {
152                 String JavaDoc[] links = Server.getInstance().getAvailableLinks(host);
153                 for (int i = 0; i < links.length; i++) {
154                     result.add(links[i]);
155                 }
156             } catch (MrtgException e) {
157                 Debug.print("Event handler error: " + e);
158             }
159             Debug.print(result.size() + " interfaces found on " + host);
160             return result;
161         }
162
163         public Vector JavaDoc getRouters() {
164             Vector JavaDoc result = new Vector JavaDoc();
165             Device[] routers = Server.getInstance().getRouters();
166             for (int i = 0; i < routers.length; i++) {
167                 result.add(routers[i].getRouterInfo());
168             }
169             Debug.print("Sending router data [" + result.size() + " routers found]");
170             return result;
171         }
172
173         public Hashtable JavaDoc getServerInfo() {
174             Hashtable JavaDoc hash = new Hashtable JavaDoc();
175             Server server = Server.getInstance();
176             hash = server.getServerInfo();
177             Debug.print("Sending MRTG server info");
178             return hash;
179         }
180
181         public Hashtable JavaDoc getMrtgInfo() {
182             Hashtable JavaDoc mrtgInfo = new Hashtable JavaDoc();
183             mrtgInfo.put("serverInfo", getServerInfo());
184             mrtgInfo.put("routers", getRouters());
185             return mrtgInfo;
186         }
187     }
188 }
189
Popular Tags