aboutsummaryrefslogtreecommitdiff
path: root/src/TestIvySwing.java
blob: 82afe431a1c37a8c46a9bbb310bc3734cb9b3e48 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package fr.dgac.ivy ;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.* ;

/**
 * toy tool to probe the Ivy software bus.
 * it relies on the Swing toolkit, which is not standard on jdk1.1 platform.
 * if yyou don't have jdk1.2 or swing, consider downloading it. You can also
 * use TestIvy
 *
 * @see		fr.dgac.ivy.TestIvy
 * @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 TestIvySwing extends JPanel  implements IvyApplicationListener {


  private static String version ="1.0.2";
  private static int index;
  private static int nbTIS=0;
  private String localname;
  private Ivy bus ;
  private String regexp = "(.*)";
  private JLabel laRegex;
  private JTextArea  ta ;
  private JTextField tfRegex, tfSend ;
  private JButton buApply, buSend, buClear ;
  private JComboBox ports;
  private int regexp_id;
  private REGCB reg;
  private java.text.SimpleDateFormat format = new
    java.text.SimpleDateFormat("hh:mm:ss");
  private static String[] startDomainList = {
    "127.255.255.255:2010",
    "10.192.36.255:2223",
    "10.192.36.255:2333",
    "10.192.36.255:2020",
    "10.192.36.255:3110",
  };
  private static java.util.Vector domainList;

  static {
    // initialize the domainlist
    domainList = new java.util.Vector();
    for (int i = 0; i<startDomainList.length;i++)
      domainList.add(startDomainList[i]);
  }

  public static void main(String[] args) throws IvyException {newTestIvy();}

  private TestIvySwing() throws IvyException {
    super(new BorderLayout());
    nbTIS++;
    ta = new JTextArea(25,40);
    ta.setEditable(false);
    add(new JScrollPane(ta),BorderLayout.CENTER);
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JLabel("Regex:"),BorderLayout.WEST);
    tfRegex = new JTextField();
    tfRegex.addActionListener(reg=new REGCB());
    p.add(tfRegex,BorderLayout.CENTER);
    p.add(laRegex=new JLabel(regexp),BorderLayout.EAST);
    add(p,BorderLayout.NORTH);
    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(new JLabel("Msg:"),BorderLayout.WEST);
    tfSend = new JTextField("");
    tfSend.addActionListener(new SENDCB());
    p2.add(tfSend,BorderLayout.CENTER);
    p.add(p2,BorderLayout.SOUTH);
    JButton tmpb ;
    (p = new JPanel()).add(tmpb=new JButton("spawn"));
    tmpb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try { newTestIvy();
	} catch (IvyException ie) {}
      }
    });
    p.add(tmpb=new JButton("clear"));
    tmpb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ta.setText("");
      }
    });
    ports=new JComboBox();
    ports.setEditable(true);
    for (java.util.Enumeration e=domainList.elements();e.hasMoreElements();) {
      ports.addItem((String) e.nextElement());
    }
    ports.addActionListener(new ComboCB());
    p.add(ports);
    add(p,BorderLayout.SOUTH);
    tfRegex.setNextFocusableComponent(tfSend);
    tfSend.setNextFocusableComponent(tfRegex);
    tfSend.setRequestFocusEnabled(true);
    localname = "TestIvySwing "+version+" ("+index+")";
    index++;
    bus = new Ivy(localname,localname+" ready",this);
    regexp_id = bus.bindMsg(regexp,reg);
    bus.start(null);
    append( "Ivy Domain: "+ bus.getDomain(null) );
  }

  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 );
  }

  private class ComboCB implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String newDomain=(String)ports.getSelectedItem();
      try {
        append( "deconnexion from domain "+ bus.getDomain(null));
      } catch ( IvyException ie ) {
        System.err.println("deconnexion failed");
      }
      bus.stop();
      try {
        bus.start(newDomain);
	append( "Ivy Domain: "+ newDomain );
      } catch (IvyException ie ) {
        System.err.println("auuuugh "+newDomain);
      }
    }
  } // ComboCB

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

  private class SENDCB implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    int count;
      String tosend = tfSend.getText();
      tfSend.setText("");
      if ( (count = bus.sendMsg(tosend)) != 0 ) 
        append("Sending '" + tosend + "' count " + count );
      else
        append("not Sending '" + tosend + "' nobody cares");
    }
  }

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

  private static void newTestIvy() throws IvyException {
    TestIvySwing tb = new TestIvySwing();
    JFrame f = new JFrame(tb.localname);
    f.addWindowListener( tb.new WCCB(f,tb)) ;
    f.getContentPane().add(tb, BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
  }

  private class WCCB extends WindowAdapter {
    private JFrame f;
    public WCCB(JFrame f, TestIvySwing b) { this.f=f; }
    public void windowClosing(WindowEvent e) {
      bus.stop();
      f.dispose();
      // I leave when the last TestIvySwing exits
      if (--nbTIS == 0) System.exit(0);
    }
    public void windowActivated(WindowEvent e) {tfSend.grabFocus();}
  }

} // class TestIvySwing
// EOF