001: // Probe.java
002: // $Id: Probe.java,v 1.4 2000/08/16 21:38:05 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http.micp;
007:
008: import java.awt.Button;
009: import java.awt.Color;
010: import java.awt.Component;
011: import java.awt.Container;
012: import java.awt.Dimension;
013: import java.awt.Frame;
014: import java.awt.GridBagConstraints;
015: import java.awt.GridBagLayout;
016: import java.awt.Label;
017: import java.awt.Panel;
018: import java.awt.Window;
019:
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022:
023: import java.net.DatagramPacket;
024: import java.net.DatagramSocket;
025: import java.net.InetAddress;
026: import java.net.MulticastSocket;
027: import java.net.UnknownHostException;
028:
029: import java.io.IOException;
030: import java.io.PrintStream;
031:
032: import java.util.EventObject;
033:
034: /**
035: * A real piece of fun, try it !
036: */
037:
038: class Stats implements MICP {
039: int queries = 0;
040: int hits = 0;
041: String lasturl = null;
042:
043: final synchronized void handle(int op, String url) {
044: if (op == MICP_OP_QUERY)
045: queries++;
046: else
047: hits++;
048: lasturl = url;
049: }
050:
051: final synchronized int getQueries() {
052: return queries;
053: }
054:
055: final synchronized int getHits() {
056: return hits;
057: }
058:
059: final synchronized String getLastURL() {
060: return lasturl;
061: }
062:
063: }
064:
065: class MICPReader extends Thread {
066: InetAddress addr = null;
067: int port = -1;
068: Stats stats = null;
069: MulticastSocket socket = null;
070: MICPReadWrite micprw = null;
071:
072: public void run() {
073: byte buffer[] = new byte[4096];
074: MICPMessage msg = new MICPMessage();
075: while (true) {
076: try {
077: DatagramPacket p = new DatagramPacket(buffer,
078: buffer.length);
079: socket.receive(p);
080: micprw.decode(p.getData(), p.getLength(), msg);
081: // Stat that message:
082: stats.handle(msg.op, msg.url);
083: } catch (Exception ex) {
084: ex.printStackTrace();
085: }
086: }
087: }
088:
089: MICPReader(InetAddress a, int port, Stats stats)
090: throws UnknownHostException, IOException {
091: // Init:
092: this .micprw = new MICPReadWrite();
093: this .addr = a;
094: this .port = port;
095: this .stats = stats;
096: // Create and join:
097: this .socket = new MulticastSocket(port);
098: this .socket.joinGroup(a);
099: // Run !
100: setName("mICP listener");
101: start();
102: }
103: }
104:
105: public class Probe extends Panel implements Runnable, ActionListener {
106: MICPReader reader = null;
107: Stats stats = null;
108: long refresh = 500;
109:
110: Label hits = null;
111: Label queries = null;
112: Label url = null;
113: Button exit = null;
114:
115: /**
116: * ActionListener implementation - exit on exit button.
117: * @param e The event.
118: */
119:
120: public void actionPerformed(ActionEvent e) {
121: if (e.getSource() == exit) {
122: System.out.println("Bye !");
123: System.exit(0);
124: }
125: }
126:
127: protected synchronized void tick() {
128: try {
129: wait(refresh);
130: } catch (InterruptedException ex) {
131: }
132: }
133:
134: public void run() {
135: while (true) {
136: // Display stats:
137: hits.setText(Integer.toString(stats.getHits()));
138: queries.setText(Integer.toString(stats.getQueries()));
139: url.setText(stats.getLastURL());
140: // Wait for interval:
141: tick();
142: }
143: }
144:
145: public Probe(InetAddress addr, int port, long refresh)
146: throws UnknownHostException, IOException {
147: // Create objects:
148: this .refresh = refresh;
149: this .stats = new Stats();
150: this .reader = new MICPReader(addr, port, stats);
151: // Create widgets:
152: GridBagLayout gb = new GridBagLayout();
153: setLayout(gb);
154: GridBagConstraints ct = new GridBagConstraints();
155: GridBagConstraints cv = new GridBagConstraints();
156: // Create the title constraints:
157: ct = new GridBagConstraints();
158: ct.gridx = GridBagConstraints.RELATIVE;
159: ct.anchor = GridBagConstraints.EAST;
160: ct.weighty = 1.0;
161: // Create the value constraints:
162: cv = new GridBagConstraints();
163: cv.gridx = GridBagConstraints.RELATIVE;
164: cv.gridwidth = GridBagConstraints.REMAINDER;
165: cv.fill = GridBagConstraints.HORIZONTAL;
166: cv.anchor = GridBagConstraints.WEST;
167: cv.weightx = 1.0;
168: cv.weighty = 1.0;
169: // Add the number of queries label:
170: Label title = new Label("queries");
171: gb.setConstraints(title, ct);
172: add(title);
173: queries = new Label("0");
174: queries.setBackground(Color.white);
175: gb.setConstraints(queries, cv);
176: add(queries);
177: // Add the number of hits label:
178: title = new Label("hits");
179: gb.setConstraints(title, ct);
180: add(title);
181: hits = new Label("0");
182: hits.setBackground(Color.white);
183: gb.setConstraints(hits, cv);
184: add(hits);
185: // Add the last url label:
186: title = new Label("url");
187: gb.setConstraints(title, ct);
188: add(title);
189: url = new Label("0");
190: url.setBackground(Color.white);
191: gb.setConstraints(url, cv);
192: add(url);
193: // Add the exit button:
194: exit = new Button("Exit");
195: exit.addActionListener(this );
196: gb.setConstraints(exit, ct);
197: add(exit);
198: }
199:
200: public static void usage() {
201: PrintStream p = System.out;
202: p.println("Probe -a <addr> -p <port> -r <refresh>"
203: + " -w <width> -h <height>");
204: p.println("\taddr: multicast group address");
205: p.println("\tport: multicast port");
206: p.println("\trefresh: refresh interval in ms");
207: p.println("\twidth: width at startup (pixels)");
208: p.println("\theight: height at startup (pixels)");
209: System.exit(1);
210: }
211:
212: public static void main(String args[]) {
213: InetAddress addr = null;
214: int port = -1;
215: long refresh = 500;
216: int width = 330;
217: int height = 130;
218: try {
219: for (int i = 0; i < args.length; i++) {
220: if (args[i].equals("-a") && (i + 1 < args.length)) {
221: addr = InetAddress.getByName(args[++i]);
222: } else if (args[i].equals("-p")
223: && (i + 1 < args.length)) {
224: port = Integer.parseInt(args[++i]);
225: } else if (args[i].equals("-r")
226: && (i + 1 < args.length)) {
227: refresh = Long.parseLong(args[++i]);
228: } else if (args[i].equals("-w")
229: && (i + 1 < args.length)) {
230: width = Integer.parseInt(args[++i]);
231: } else if (args[i].equals("-h")
232: && (i + 1 < args.length)) {
233: height = Integer.parseInt(args[++i]);
234: } else {
235: usage();
236: }
237: }
238: } catch (Exception ex) {
239: usage();
240: }
241: // Check args:
242: if ((addr == null) || (port == -1))
243: usage();
244: // Run it:
245: Probe probe = null;
246: try {
247: probe = new Probe(addr, port, refresh);
248: } catch (Exception ex) {
249: ex.printStackTrace();
250: System.exit(1);
251: }
252: Frame toplevel = new Frame("mICP-Probe");
253: toplevel.add("Center", probe);
254: toplevel.setSize(new Dimension(width, height));
255: toplevel.show();
256: new Thread(probe).start();
257: }
258:
259: }
|