1 import zeus.actors.*;
2 import zeus.util.*;
3 import zeus.concepts.*;
4 import zeus.agents.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8
9 public class pingExternal extends JFrame implements ZeusExternal,ActionListener {
10 JButton sendLausButton = new JButton("Ping Lausanne");
11 JButton sendMontButton = new JButton ("Ping Montpellier");
12 JButton sendSaarButton = new JButton ("Ping Saarbrucken");
13 JButton sendSanButton = new JButton ("Ping SanFrancisco");
14 JButton sendParisButton = new JButton("Ping Paris");
15 JButton sendParmaButton = new JButton("Ping Parma");
16 JButton sendBarcaButton = new JButton("Ping Barcelona");
17 JButton sendQMULButton = new JButton("Ping QMLU");
18 JButton sendLisboaButton = new JButton("Ping Lisboa");
19 JButton sendICSTMButton = new JButton("Ping ICSTM");
20 JButton sendAgentScpButton = new JButton("Ping Agentscape");
21 JButton sendSendiButton = new JButton("Ping Sendi");
22 JButton sendDublinButton = new JButton("Ping Dublin");
23
24
25
26 AgentContext context = null;
27
28 public pingExternal () {
29 setSize (200,200);
30
31 GridLayout layout = new GridLayout(13,1);
32 getContentPane().setLayout(layout);
33
34 getContentPane().add (sendLausButton);
35 getContentPane().add (sendParisButton);
36 getContentPane().add (sendMontButton);
37 getContentPane().add (sendSaarButton);
38 getContentPane().add (sendSanButton);
39 getContentPane().add (sendParmaButton);
40 getContentPane().add (sendBarcaButton);
41 getContentPane().add (sendQMULButton);
42 getContentPane().add (sendLisboaButton);
43 getContentPane().add (sendICSTMButton);
44 getContentPane().add (sendAgentScpButton);
45 getContentPane().add (sendSendiButton);
46 getContentPane().add (sendDublinButton);
47
48
49 sendLausButton.addActionListener(this);
50 sendParisButton.addActionListener(this);
51 sendMontButton.addActionListener(this);
52 sendSaarButton.addActionListener(this);
53 sendSanButton.addActionListener(this);
54 sendParmaButton.addActionListener(this);
55 sendBarcaButton.addActionListener(this);
56 sendQMULButton.addActionListener(this);
57 sendLisboaButton.addActionListener(this);
58 sendICSTMButton.addActionListener(this);
59 sendAgentScpButton.addActionListener(this);
60 sendSendiButton.addActionListener(this);
61 sendDublinButton.addActionListener(this);
62 repaint();
63
64 }
65
66
67 public void actionPerformed (ActionEvent ae) {
68 Object source = ae.getSource();
69 if (source == sendLausButton) {
70 System.out.println("Trying Lausanne");
71 sendMessage("LausannePing");
72 }
73 if (source == sendMontButton) {
74 System.out.println("Trying Montpellier");
75 sendMessage ("MontpellierPing");
76 }
77 if (source == sendParisButton) {
78 System.out.println("Trying Paris");
79 sendMessage ("ParisPing");
80 }
81 if (source == sendSanButton) {
82 System.out.println("Trying Sanfrancisco");
83 sendMessage ("SanfranciscoPing");
84 }
85 if (source == sendParmaButton) {
86 System.out.println("Trying Parma");
87 sendMessage ("ParmaPing");
88 }
89 if (source == sendBarcaButton) {
90 System.out.println("Trying Barcalona");
91 sendMessage ("BarcalonaPing");
92 }
93 if (source == sendQMULButton) {
94 System.out.println("Trying QMUL (London 1)");
95 sendMessage ("QMULPing");
96 }
97 if (source == sendLisboaButton) {
98 System.out.println("Trying Lisboa");
99 sendMessage ("LisboaPing");
100 }
101 if (source == sendAgentScpButton) {
102 System.out.println("Trying AgentScape (Berlin)");
103 sendMessage ("BerlinPing");
104 }
105 if (source == sendSendiButton) {
106 System.out.println("Trying Sendi");
107 sendMessage ("SendiPing");
108 }
109 if (source == sendDublinButton){
110 System.out.println("Trying Dublin");
111 sendMessage ("DublinPing");
112 }
113 if (source == sendICSTMButton){
114 System.out.println("Trying ICSTM");
115 sendMessage ("ICSTMPing");
116 }
117 if (source == sendSaarButton){
118 System.out.println("Trying Saarbrucken");
119 sendMessage ("SaarbruckenPing");
120 }
121
122 }
123
124
125 public void exec(AgentContext context) {
126 this.context = context;
127 setVisible (true);
128 setResponse ();
129
130 }
131
132
133 public void sendMessage(String receiver) {
134 Performative ping = new Performative ("query-ref") ;
135 ping.setReceiver(receiver);
136 ping.setContent ("ping");
137 ping.setLanguage ("PlainText");
138 context.getMailBox().sendMsg(ping);
139 }
140
141
142 public void setResponse () {
143 SimpleAPI api = new SimpleAPI(context);
144 api.setHandler ("query-ref",this,"respond");
145 }
146
147
148 public void setRedo () {
149 SimpleAPI api = new SimpleAPI (context);
150 api.setHandler ("inform", this, "redo");
151 }
152
153 public void redo (Performative msg) {
154 try {
155 String content = msg.getContent();
156 content = content.toLowerCase ();
157 if (content == "alive")
158 Thread.sleep(6000) ; }
159 catch (Exception e) {
160 e.printStackTrace();
161 }
162 sendMessage(msg.getSender());
163 }
164
165
166 public void respond(Performative msg) {
167
168
169 System.out.println("type = " + msg.getClass());
170 Performative resp = new Performative ("inform");
171
172 resp.setContent ("alive");
173
174 resp.setSender (msg.getReceiver());
175
176 resp.setReceiver(msg.getSender());
177
178 resp.send(context);
179 }
180 }