aboutsummaryrefslogtreecommitdiff
path: root/src/TestIvy.java
blob: 27a6d082257314710e534052052119f697987e6e (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package fr.dgac.ivy ;

import java.awt.* ;
import java.awt.event.* ;

/**
 * toy tool to probe the Ivy software bus.
 * it relies on the AWT, and is less useable than TestIvySwing, which should
 * be preferred.
 *
 * @see		fr.dgac.ivy.TestIvySwing	
 * @author	François-Régis Colin
 * @author	Yannick Jestin
 * @author	<a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
 */

class TestIvy extends Panel implements IvyApplicationListener {
  private Ivy bus ;
  private String regexp="";
  private TextField tfRegex, tfSend ;
  private TextArea  ta ;
  private Button buApply, buSend, buClear ;
  private java.text.SimpleDateFormat format = new
    java.text.SimpleDateFormat("hh:mm:ss");
  private int regexp_id;

  public TestIvy() throws IvyException {
    super(new BorderLayout());
    ta = new TextArea();
    ta.setEditable(false);
    add(ta,BorderLayout.CENTER);
    Panel p = new Panel(new BorderLayout());
    p.add(new Label("Regex:"),BorderLayout.WEST);
    tfRegex = new TextField(regexp);
    tfRegex.addActionListener(new REGCB());
    p.add(tfRegex,BorderLayout.CENTER);
    add(p,BorderLayout.NORTH);
    p = new Panel(new BorderLayout());
    p.add(new Label("Msg:"),BorderLayout.WEST);
    tfSend = new TextField("");
    tfSend.addActionListener(new SENDCB());
    p.add(tfSend,BorderLayout.CENTER);
    add(p,BorderLayout.SOUTH);
    bus = new Ivy("JAVA TESTBUS","Testbus is ready",this);
    bus.start(null);
    append( "Ivy Domain: "+ bus.getDomain(null) );
  }

  public static void main(String[] args) throws IvyException {
    TestIvy tb = new TestIvy();
    Frame f = new Frame("TestIvy");
    f.addWindowListener( tb.new WCCB(f,tb)) ;
    f.add(tb, BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
  }

  public void connect(IvyClient client) {
    append(client.getApplicationName() + " connected " );
  }

  public void disconnect(IvyClient client) {
       append(client.getApplicationName() + " disconnected " );
  }
  public void die(IvyClient client, int id) {
       append(client.getApplicationName() + " die "+ id );
  }
  public void directMessage(IvyClient client, int id, String arg) {
       append(client.getApplicationName() + " direct Message "+ id + arg );
  }

  class REGCB implements ActionListener, IvyMessageListener {
    public void actionPerformed(ActionEvent e) {
      try {
        bus.unBindMsg(regexp_id);
      } catch (IvyException ie) {
        System.out.println("Big badaboum"); // this should not happen
      }
      regexp=tfRegex.getText();
      regexp.trim();
      regexp_id = bus.bindMsg(regexp,this);
      tfRegex.setText("");
    }
    public void receive(IvyClient client, String[] args) {
      String out="client " + client.getApplicationName() + " envoie: [ ";
      for (int i=0;i<args.length;i++)
        out=out+args[i]+ ((i<args.length-1)?" , ":"");
      out = out + " ]";
      append(out);
    }
  }

  class SENDCB implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int count;
      String tosend = tfSend.getText();
      tfSend.setText("");
      count = bus.sendMsg(tosend);
      append("Sending '" + tosend + "' count " + count );
    }
  }

  private void append(String s) {
    ta.append("[" + format.format(new java.util.Date()) + "] "+ s + "\n");
  }

  class WCCB extends WindowAdapter {
    private Frame f;
    public WCCB(Frame f, TestIvy b) { this.f=f; }
    public void windowClosing(WindowEvent e) {
      f.setVisible(false);
      bus.stop();
      bus = null;
      f.dispose();
      System.exit(0);
    }
  }

} // class TestIvy
// EOF