aboutsummaryrefslogtreecommitdiff
path: root/tests/TestNet.java
diff options
context:
space:
mode:
authorjestin2002-06-05 09:48:51 +0000
committerjestin2002-06-05 09:48:51 +0000
commitda83e117b8ec556bf42488d5aeb889f6e2afc150 (patch)
tree4527adeb9d657ed0ec210d269fa1f3119c538749 /tests/TestNet.java
parent2df14befd571ad9a36ecca968d4926d9059667e1 (diff)
downloadivy-java-da83e117b8ec556bf42488d5aeb889f6e2afc150.zip
ivy-java-da83e117b8ec556bf42488d5aeb889f6e2afc150.tar.gz
ivy-java-da83e117b8ec556bf42488d5aeb889f6e2afc150.tar.bz2
ivy-java-da83e117b8ec556bf42488d5aeb889f6e2afc150.tar.xz
Premiers vecteurs de tests pour non regression. Version initiale.
Diffstat (limited to 'tests/TestNet.java')
-rw-r--r--tests/TestNet.java155
1 files changed, 155 insertions, 0 deletions
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 */