aboutsummaryrefslogtreecommitdiff
path: root/src/Probe.java
blob: 6e4543bd4110809180c3e04991f78074867f4c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fr.dgac.ivy ;
import gnu.getopt.Getopt;
import java.io.*;

/**
 * terminal implementation in java of the ivyprobe.
 * For a graphical version, see TestIvySwing 
 * @see		fr.dgac.ivy.TestIvySwing
 * @author	Yannick Jestin
 * @author	<a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
 * TODO BUG ! Should exit on end of user input
 */
class Probe implements IvyApplicationListener, IvyMessageListener {

  public static void main(String[] args) throws IvyException {
    Probe p = new Probe();
    Getopt opt = new Getopt("Probe",args,"b:d");
    int c;
    String domain=Ivy.getDomain(null);
    while ((c = opt.getopt()) != -1) switch (c) {
    case 'b':
      domain=opt.getOptarg();
      break;
    case 'd':
      System.setProperty("IVY_DEBUG","yes");
      break;
    default:
    }
    // connexion to the Bus
    Ivy bus;
    try {
      bus=new Ivy("JPROBE","JPROBE ready",p);
      for (int i=opt.getOptind();i<args.length;i++) {
        System.out.println("you want to subscribe to " + args[i]);
	bus.bindMsg(args[i],p);
      }
      System.out.println("broadcasting on "+domain);
      bus.start(domain);
      String s;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      // infinite loop on keyboard input
      try {
        while (p.looping() && (s=in.readLine()).length()!=0)
          System.out.println("-> Sent to " +bus.sendMsg(s)+" peers");
      } catch (NullPointerException e) {
        bus.stop();
      } catch (IOException e) {
        bus.stop();
      }
    } catch (IvyException ie) {
      System.out.println("Caught an exception. quitting. "+ie.getMessage());
    }
  }

  private boolean looping=true;

  public boolean looping() { return looping ; } // accessor

  public void connect(IvyClient client) {
    System.out.println(client.getApplicationName() + " connected " );
    for (java.util.Enumeration e=client.getRegexps();e.hasMoreElements();)
      System.out.println(client.getApplicationName() + " subscribes to "
        +e.nextElement() );
  }

  public void disconnect(IvyClient client) {
    System.out.println(client.getApplicationName() + " disconnected " );
  }

  public void die(IvyClient client, int id) {
   looping=false;
   System.out.println(client.getApplicationName() + " received die msg from "+ id );
  }

  public void directMessage(IvyClient client, int id, String arg) {
   System.out.println(client.getApplicationName() + " direct Message "+ id + arg );
  }

  public void receive(IvyClient client, String[] args) {
    String s=client.getApplicationName() + " sent";
    for (int i=0;i<args.length;i++) s+=" '"+args[i]+"'";
    System.out.println(s);
  }

} // class Probe
// EOF