aboutsummaryrefslogtreecommitdiff
path: root/tests/TestReady.java
blob: 49d9336d1d619972a19cafccf093d0051dd8beb9 (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
import fr.dgac.ivy.*;
import gnu.getopt.*;


/**
 * Ivy java ReadyMessage tester.
 * 
 * @author Francois-Regis Colin <mailto:fcolin@cena.fr>
 * 
 *         (c) CENA
 * 
 *         usage: java TestReady & java TestReady -o
 * 
 */

class TestReady implements IvyApplicationListener {

	static String me = "A";
	static String other = "B";
	static String ready_message = "A ready";
	static String ready_bind = "^B ready";
	
	private Ivy bus;
		private class Ready implements IvyMessageListener {
			public void receive (IvyClient app, String[] args)
		{
				int count = 0;
				try {
					count = bus.sendMsg ("are you there "+app.getApplicationName());
				}
				catch (IvyException ie) {
					System.err.println("can't sendMsg" + ie.getMessage());
				}
		        System.out.println("Application "+me+" received '"+ready_bind+"' from "+app.getApplicationName()+
		        		" sent question 'are you there "+app.getApplicationName()+"'= "+ count );
		}
		}
		private class Question implements IvyMessageListener {
			public void receive (IvyClient app, String[] args)
		{
				int count = 0;
				try {
					count = bus.sendMsg ("yes i am "+me);
				}
				catch (IvyException ie) {
					System.err.println("can't sendMsg" + ie.getMessage());
				}
		        System.out.println("Application "+me+" Reply to "+app.getApplicationName()+" are you there = "+count );

		}
		}
		private class Reply implements IvyMessageListener {
			public void receive (IvyClient app, String[] args)
		{
			System.out.println("Application "+app.getApplicationName()+" Reply to our question! "+args[0]);

		}
		}
	TestReady(String domain) {
		try {
		
			bus = new Ivy(me, ready_message, this);
			bus.bindMsg (ready_bind, new Ready());
			bus.bindMsg ("^are you there "+me, new Question());
			bus.bindMsg ("^(yes i am "+other+")", new Reply());

			bus.start(domain);
		} catch (IvyException ie) {
			System.err.println("can't run the Ivy bus" + ie.getMessage());
		}
	}


	public void disconnect(IvyClient client) {
	}

	public void directMessage(IvyClient client, int id, String msgarg) {
	}

	public void connect(IvyClient client) {
		
	}

	public void die(IvyClient client, int id, String msg) {
		System.out.println("argh. cya " + msg);
	}

	public static void main(String args[]) {
	Getopt opt = new Getopt("TestReady",args,"b:o");
		String domain=Ivy.getDomain(null);
		int c;
		boolean swap = false;
		while ((c = opt.getopt()) != -1) switch (c) {
		case 'b':
		  domain=opt.getOptarg();
		  break;
		case 'o':
		  swap=true;
		  break;
		default:
		  System.exit(0);
		}

		if (swap) {
			me = "B";
			other = "A";
			ready_message = "B ready";
			ready_bind = "^A ready";
		}
		System.out.println("I am "+me);
		new TestReady(domain);
	}
}