aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile2
-rw-r--r--tests/TestNetSwing.java252
2 files changed, 253 insertions, 1 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 2dfa6c9..ac421c9 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,7 +1,7 @@
JIKESPATH=/usr/lib/j2re1.3/lib/rt.jar:../lib/ivy-java.jar
JAVAC= jikes -classpath $(JIKESPATH)
.SUFFIXES: .java .class
-SRC = TestNet.java Bug.java BugTok.java BenchLocal.java
+SRC = TestNetSwing.java TestNet.java Bug.java BugTok.java BenchLocal.java
OBJ = $(SRC:.java=.class)
JAVAOPTS = -DIVY_PING
JAVA = java $(JAVAOPTS)
diff --git a/tests/TestNetSwing.java b/tests/TestNetSwing.java
new file mode 100644
index 0000000..ea93395
--- /dev/null
+++ b/tests/TestNetSwing.java
@@ -0,0 +1,252 @@
+/*
+ * A simple Swing application in order to check whether the chosen network is
+ * OK
+ */
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import gnu.regexp.*;
+import gnu.getopt.*;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.*;
+
+class TestNetSwing implements Runnable {
+
+ private JFrame frame;
+ private JLabel receive;
+ private JButton send;
+ private JComboBox domainBox;
+ private int alpha=0;
+ private Color color;
+ private static Color DEFAULT_COLOR = new Color(10,10,10,255);
+ private static int FADINGSTEP = 20;
+ private static int FADINGSLEEP = 400;
+
+ private static String[] domainList = {
+ "127.255.255.255:2010",
+ "10.0.0.255:2010",
+ "10.192.36.255:2010",
+ "227.1.2.3:4567"
+ };
+
+ private Thread server;
+ private Ageing age;
+ private String domain;
+ private boolean watcherrunning = false;
+ private boolean isMulticastAddress = false;
+ private Thread broadcastListener ;
+ private DatagramSocket broadcast; /* supervision socket */
+ // it can also be a MulticastSocket, which inherits from the previous
+
+ public TestNetSwing(String d) {
+ if (d==null) { d = domainList[0]; };
+ domain=d;
+ server = createBroadcastListener(domain);
+ server.start();
+ frame = new JFrame("TestNet");
+ frame.addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent e) {
+ System.exit(0);
+ }
+ });
+ Container cp = frame.getContentPane();
+ cp.setLayout(new GridLayout(0,1));
+ cp.add(send = new JButton("send packet"));
+ send.addActionListener( new ActionListener() {
+ public void actionPerformed(ActionEvent ae) {
+ send("hello",domain);
+ }
+ });
+ cp.add(receive = new JLabel(""));
+ domainBox = new JComboBox();
+ domainBox.setEditable(true);
+ boolean sel = false;
+ int index=0;
+ for (;index<domainList.length;index++) {
+ domainBox.addItem(domainList[index]);
+ if (domainList[index]==d) {
+ domainBox.setSelectedIndex(index);
+ sel=true;
+ }
+ }
+ if (!sel) {
+ domainBox.addItem(domain);
+ domainBox.setSelectedIndex(index);
+ }
+ cp.add(domainBox);
+ domainBox.addActionListener(new ComboCB());
+ frame.pack();
+ age=new Ageing();
+ frame.setVisible(true);
+ }
+
+ public void run() {
+ byte buf[] = new byte[256];
+ for (int i=0;i<buf.length;i++) { buf[i]=10; }
+ DatagramPacket packet=new DatagramPacket(buf, 256);
+ int port;
+ String s = "Server waiting for Broadcast on "+domain;
+ s+=(isMulticastAddress)?" (TCP multicast)":" (UDP broadcast)";
+ System.out.println(s);
+ while( watcherrunning ) try {
+ broadcast.receive(packet);
+ String msg = new String(buf) ;
+ // clean up the buffer after each message
+ for (int i=0;i<buf.length;i++) { buf[i]=10; }
+ InetAddress remotehost = packet.getAddress();
+ updateLabel(remotehost.getHostName()+":"+msg);
+ } catch (java.io.InterruptedIOException jii ){
+ if (!watcherrunning) break;
+ } catch (java.io.IOException ioe ){
+ System.err.println("IvyWatcher IOException "+ ioe.getMessage() );
+ }
+ broadcast.close();
+ System.out.println("Server normal shutdown");
+ }
+
+ void stop() { watcherrunning=false; }
+
+ private void updateLabel(String text) {
+ if (text==null) return;
+ int i = text.indexOf(10);
+ if (i>0) { receive.setText(text.substring(0,i)); }
+ else { receive.setText(text); }
+ alpha=255;
+ color=DEFAULT_COLOR;
+ receive.setForeground(color);
+ }
+
+ private static String getDomain(String net) {
+ int sep_index = net.lastIndexOf( ":" );
+ if ( sep_index != -1 ) { net = net.substring(0,sep_index); }
+ try {
+ net += ".255.255.255";
+ RE exp = new RE( "^(\\d+\\.\\d+\\.\\d+\\.\\d+).*");
+ net = exp.substitute( net , "$1" );
+ } catch ( REException e ){
+ System.out.println("Bad broascat addr " + net);
+ return null;
+ }
+ //System.out.println("net: "+net);
+ return net;
+ }
+
+ private static int getPort(String net) {
+ int port;
+ int sep_index = net.lastIndexOf( ":" );
+ if ( sep_index == -1 ) {
+ port = -1;
+ } else {
+ port = Integer.parseInt( net.substring( sep_index +1 ));
+ }
+ //System.out.println("port: "+port);
+ return port;
+ }
+
+ private static void send(String data, String net) {
+ int port = getPort(net);
+ net=getDomain(net);
+ try {
+ DatagramSocket send;
+ InetAddress group = InetAddress.getByName(net);
+ send = new MulticastSocket(port);
+ if (group.isMulticastAddress()) { ((MulticastSocket)send).joinGroup(group); }
+ DatagramPacket packet = new DatagramPacket(
+ data.getBytes(),
+ data.length(),
+ group,
+ send.getLocalPort() );
+ //System.out.println("Client sends Broadcast to "+net+":"+port+" ("+packet.getLength()+") ["+data+"]");
+ send.send(packet);
+ } catch ( UnknownHostException e ) {
+ System.out.println("Broadcast sent on unknown network "+ e.getMessage());
+ } catch ( IOException e ) {
+ System.out.println("Broadcast error " + e.getMessage() );
+ }
+ }
+
+ private Thread createBroadcastListener(String domain) {
+ String domainaddr=getDomain(domain);
+ int port=getPort(domain);
+ this.domain=domainaddr+":"+port;
+ try {
+ InetAddress group = InetAddress.getByName(domainaddr);
+ if (group.isMulticastAddress()) {
+ isMulticastAddress = true;
+ broadcast = new MulticastSocket(port ); // create the UDP socket
+ ((MulticastSocket)broadcast).joinGroup(group);
+ } else {
+ broadcast = new MulticastSocket(port ); // create the UDP socket
+ }
+ } catch ( IOException e ) {
+ System.out.println("MulticastSocket I/O error" + e );
+ return null;
+ }
+ try {
+ broadcast.setSoTimeout(100);
+ } catch ( java.net.SocketException jns ) {
+ System.out.println("IvyWatcher setSoTimeout error" + jns.getMessage() );
+ }
+ // starts a Thread listening on the socket
+ watcherrunning=true;
+ return new Thread(this);
+ }
+
+ private class Ageing implements Runnable {
+ boolean encore = true;
+ public Ageing() {
+ new Thread(this).start();
+ }
+ public void run() {
+ while (encore) {
+ try {
+ java.lang.Thread.sleep(FADINGSLEEP);
+ } catch (InterruptedException ie) {
+ }
+ alpha-=FADINGSTEP;
+ String s = receive.getText();
+ if (alpha<0) {
+ alpha=0;
+ receive.setText(null);
+ s=null;
+ }
+ if (s!=null) { receive.setForeground(new Color(10,10,10,alpha)); }
+ }
+ }
+ public void stop(){ encore=false; }
+ }
+
+ private class ComboCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ String newDomain=(String)domainBox.getSelectedItem();
+ // if it's the same domain, don't do anything
+ if (newDomain == domain) { return; }
+ domain=newDomain;
+ watcherrunning=false;
+ server.stop();
+ server = createBroadcastListener(domain);
+ server.start();
+ }
+ } // ComboCB
+
+ public static final String helpmsg = "usage: java TestNetSwing [options]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-h\thelp\n\n";
+ public static void main(String[] args) {
+ Getopt opt = new Getopt("TestNetSwing",args,"b:h");
+ String domain = null;
+ int c;
+ while ((c = opt.getopt()) != -1) switch (c) {
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ case 'h':
+ default:
+ System.out.println(helpmsg);
+ System.exit(0);
+ } // getopt
+ new TestNetSwing(domain);
+
+ }
+
+} // class TestNetSwing
+/* EOF */