My favorites | Sign in
Logo
                
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* This class is part of the JGroups project (www.jgroups.org) and is
* (c) its creators. It was released under the LGPL v2.1 which allows
* for its modification and redistribution. This class was modified
* on 2008/09/15.
*/

/**
* Copyright (C) 2007, 2008 Carnegie Mellon University and others.
*
* This file is part of Plural.
*
* Plural is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Plural is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Plural; if not, see <http://www.gnu.org/licenses>.
*
* Linking Plural statically or dynamically with other modules is
* making a combined work based on Plural. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* In addition, as a special exception, the copyright holders of Plural
* give you permission to combine Plural with free software programs or
* libraries that are released under the GNU LGPL and with code
* included in the standard release of Eclipse under the Eclipse Public
* License (or modified versions of such code, with unchanged license).
* You may copy and distribute such a system following the terms of the
* GNU GPL for Plural and the licenses of the other code concerned.
*
* Note that people who make modified versions of Plural are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/

package edu.cmu.cs.nimby.test.oopsla;

//$Id: CausalDemo.java,v 1.2 2008/09/17 14:12:28 nbeckman Exp $
import java.util.Random;
import java.util.Vector;

import edu.cmu.cs.crystal.annotations.PassingTest;
import edu.cmu.cs.crystal.annotations.UseAnalyses;
import edu.cmu.cs.plural.annot.ClassStates;
import edu.cmu.cs.plural.annot.Perm;
import edu.cmu.cs.plural.annot.Pure;
import edu.cmu.cs.plural.annot.State;
import edu.cmu.cs.plural.annot.Unique;
import edu.cmu.cs.plural.annot.Use;



/**
* This class is part of JGroups (www.jgroups.org) and is (c) its creators<br>
* <br>
* Simple causal demo where each member bcast a consecutive letter from the
* alphabet and picks the next member to transmit the next letter. Start a
* few instances of CausalDemo and pass a paramter "-start" to a CausalDemo
* that initiates transmission of a letter A. All participanting members should
* have correct alphabet. DISCARD layer has been added to simulate lost messages,
* thus forcing delaying of delivery of a certain alphabet letter until the causally
* prior one has been received. Remove CAUSAL from the stack and witness how FIFO
* alone doesn't provide this guarantee.<br>
* <br>
* 9/15/08<br>
* This class has been changed to include atomic primitives. It has also been
* modified since OOPSLA publication to include the new-style PLURAL annotations.
*
* @author Vladimir Blagojevic
*/
@PassingTest
@UseAnalyses("NIMBYChecker")
public class CausalDemo implements Runnable
{
//private Channel channel;
private final Vector alphabet = new Vector();
private boolean starter = false;
private int doneCount=0;
private Log log=LogFactory.getLog(getClass());

private final String props = "causal.xml";

@Perm(ensures="unique(this!fr)")
public CausalDemo(boolean start)
{
starter = start;
}

public String getNext(String c)
{
char letter = c.charAt(0);
return new String(new char[]{++letter});
}

@Pure
public void listAlphabet()
{
System.out.println(alphabet);
}

//@Unique(use = Use.FIELDS, returned=false)
//@Full(use = Use.FIELDS)
@Perm(requires="pure(this) * full(this!fr)")
public void run()
{
Object obj;
Message msg;
Random r = new Random();

//JChannel channel = null;

try
{
final JChannel channel = new JChannel(props);
channel.connect("CausalGroup");
System.out.println("View:" + channel.getView());
if (starter)
channel.send(new Message(null, null, new CausalMessage("A", (Address) channel.getView().getMembers().get(0))));


Runtime.getRuntime().addShutdownHook(
new Thread2(new ShutDownThread(channel, this)));

while (true)
{
try
{
CausalMessage cm = null;

atomic: {
if( channel.isConnected() )
obj = channel.receive(0); // no timeout
else
return;
}

if (obj instanceof Message)
{
msg = (Message) obj;
cm = (CausalMessage) msg.getObject();

Vector members;
atomic: {
if( channel.isConnected() )
members = channel.getView().getMembers();
else
return;
}

String receivedLetter = cm.message;

if("Z".equals(receivedLetter))
{
atomic: {
Message to_send = new Message(null, null, new CausalMessage("done", null));
if( channel.isConnected() )
channel.send(to_send);
else
return;
}
}
if("done".equals(receivedLetter))
{
if(++doneCount >= members.size())
{
System.exit(0);
}
continue;
}

alphabet.add(receivedLetter);
listAlphabet();

//am I chosen to transmit next letter?
Address addr;
atomic: {
if( channel.isConnected() )
addr = channel.getLocalAddress();
else
return;
}

if (cm.member.equals(addr))
{
int nextTarget = r.nextInt(members.size());

//chose someone other than yourself
while (nextTarget == members.indexOf(addr))
{
nextTarget = r.nextInt(members.size());
}
Address next = (Address) members.get(nextTarget);
String nextChar = getNext(receivedLetter);
if (nextChar.compareTo("Z") < 1)
{
System.out.println("Sending " + nextChar);

atomic: {
Message to_send = new Message(null, null, new CausalMessage("done", null));
if( channel.isConnected() )
channel.send(to_send);
else
return;
}
}
}
}
}
catch (ChannelNotConnectedException conn)
{
break;
}
catch (Exception e)
{
log.error(e);
}
}

}
catch (Exception e)
{
System.out.println("Exception" + e);
}
}


public static void main(String args[])
{
CausalDemo test = null;
boolean start=false;

for(int i=0; i < args.length; i++) {
if("-help".equals(args[i])) {
System.out.println("CausalDemo [-help] [-start]");
return;
}
if("-start".equals(args[i])) {
start=true;
continue;
}
}

//if parameter start is passed , start the demo
test = new CausalDemo(start);
try
{
new Thread2(test).start();
}
catch (Exception e)
{
System.err.println(e);
}

}

}

/**
* In the original example, this class was a nested inner class. Due to
* limitations in our tool, it was extracted, and the CausalDemo field
* was added to this one.
*/
@ClassStates(@State(name="alive", inv="share(channel) * pure(causalDemo)"))
class ShutDownThread implements Runnable {

final JChannel channel;
final CausalDemo causalDemo; // New field

@Perm(requires="share(#0) in connected * pure(#1)", ensures="unique(this!fr)")
public ShutDownThread(JChannel channel,
CausalDemo causalDemo) {
this.channel = channel;
this.causalDemo = causalDemo;
}

@Unique(use = Use.FIELDS)
public void run()
{
this.causalDemo.listAlphabet();
this.channel.disconnect();
this.channel.close();
}
}

/*
* The following are classes that were originally defined in the JGroups
* project but that I didn't want to include here in the test Project, or
* library classes that I needed to annotate.
*/

class Thread2 extends java.lang.Thread {

@Perm(ensures="unique(this!fr)")
public Thread2(@Unique(returned=false) Runnable r) {
super(r);
}

public Thread2(ThreadGroup globalThreadGroup, String string) {
}
}
Show details Hide details

Change log

r264 by kevin.bierhoff on Mar 26, 2009   Diff
Port to new "use" attribute, replacing
fieldAccess flag
Go to: 
Project members, sign in to write a code review

Older revisions

r50 by nels.beckman on Sep 17, 2008   Diff
[license] Updated the copyright notice
for the two JGroups programs.
r49 by nels.beckman on Sep 17, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 8726 bytes, 310 lines
Hosted by Google Code