aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile13
-rw-r--r--tests/TESTBENCH14
-rw-r--r--tests/TestNet.java155
3 files changed, 182 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..9d130b8
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,13 @@
+JIKESPATH=/usr/lib/j2re1.3/lib/rt.jar:/usr/share/java/repository:/usr/share/java/gnu.getopt.jar
+
+.SUFFIXES: .java .class
+SRC = TestNet.java
+OBJ = $(SRC:.java=.class)
+
+.java.class:
+ jikes $<
+
+all: $(OBJ)
+
+clean:
+ /bin/rm -f $(OBJ)
diff --git a/tests/TESTBENCH b/tests/TESTBENCH
new file mode 100644
index 0000000..3cc3a5c
--- /dev/null
+++ b/tests/TESTBENCH
@@ -0,0 +1,14 @@
+How to check if it doesn't work ?
+
+- java -DIVY_DEBUG fr.dgac.ivy.Probe ...
+
+- check the rendez-vous, either in UDP broadcast or TCP multicast
+ * network available
+ * it works without ivy
+ * it works in ivy-c
+ * it should work in ivy-java
+
+- it should be symetric, whatever the order of the connections ( client A,
+ client B, client C vs A, C, B ... )
+
+- the regexps should function properly, let's build a little testbench
diff --git a/tests/TestNet.java b/tests/TestNet.java
new file mode 100644
index 0000000..e65684a
--- /dev/null
+++ b/tests/TestNet.java
@@ -0,0 +1,155 @@
+import java.lang.Thread;
+import java.net.*;
+import java.io.*;
+import java.util.StringTokenizer;
+import gnu.regexp.*;
+import gnu.getopt.*;
+
+class TestNet implements Runnable {
+ 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 void run() {
+ byte buf[] = new byte[256];
+ 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]=0; }
+ InetAddress remotehost = packet.getAddress();
+ System.out.println("Server Receive Broadcast from "+remotehost.getHostName()+":"+packet.getPort()+" ("+msg.length()+") ["+msg+"]");
+ if (msg.charAt(0)=='x') {
+ watcherrunning = false;
+ System.out.println("I leave");
+ }
+ } 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 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() );
+ }
+ }
+
+ void start(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;
+ }
+ 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;
+ broadcastListener = new Thread(this);
+ broadcastListener.start();
+ }
+
+ public static final String helpmsg = "usage: java TestNet [options]\n\t-b BUS\tspecifies the Ivy bus domain\n\t-s\tclient mode (default)\n\t-s\tserver mode\n\t-h\thelp\n\n";
+ public static void main(String[] args) {
+ Getopt opt = new Getopt("TestNet",args,"b:csh");
+ String domain = "228.0.0.0:4567";
+ boolean server=false;
+ int c;
+ while ((c = opt.getopt()) != -1) switch (c) {
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ case 'c':
+ server=false;
+ break;
+ case 's':
+ server=true;
+ break;
+ case 'h':
+ default:
+ System.out.println(helpmsg);
+ System.exit(0);
+ } // getopt
+
+ if (server) {
+ TestNet s = new TestNet();
+ s.start(domain);
+ } else {
+ TestNet.send("coucou1",domain);
+ TestNet.send("coucou2",domain);
+ TestNet.send("x",domain);
+ }
+ }
+
+} // class TestNet
+/* EOF */