2
import org.jcsp.lang.*;
5
import java.util.Random;
7
public class AltingBarrierGadget2 implements CSProcess {
9
private final AltingBarrier[] barrier;
10
private final AltingChannelInput leftIn, rightIn, click;
11
private final ChannelOutput leftOut, rightOut, configure;
12
private final Color offColour, standbyColour;
13
private final int offInterval, standbyInterval, playInterval;
15
public AltingBarrierGadget2 (
16
AltingBarrier[] barrier,
17
AltingChannelInput leftIn, ChannelOutput leftOut,
18
AltingChannelInput rightIn, ChannelOutput rightOut,
19
AltingChannelInput click, ChannelOutput configure,
20
Color offColour, Color standbyColour,
21
int offInterval, int standbyInterval, int playInterval
23
this.barrier = barrier;
24
this.leftIn = leftIn; this.leftOut = leftOut;
25
this.rightIn = rightIn; this.rightOut = rightOut;
26
this.click = click; this.configure = configure;
27
this.offColour = offColour; this.standbyColour = standbyColour;
28
this.offInterval = offInterval; this.standbyInterval = standbyInterval;
29
this.playInterval = playInterval;
34
CSTimer tim = new CSTimer ();
36
final Random random = new Random ();
38
final Guard[] standbyGuard = new Guard[barrier.length + 1];
39
for (int i = 0; i < barrier.length; i++) {
40
standbyGuard[i] = barrier[i];
42
standbyGuard[barrier.length] = tim;
43
final int TIMEOUT = barrier.length;
44
Alternative standbyAlt = new Alternative (standbyGuard);
46
configure.write (Boolean.FALSE); // disable mouse clicks
50
configure.write (offColour);
51
tim.sleep (random.nextInt (offInterval));
53
configure.write (standbyColour);
54
tim.setAlarm (tim.read () + random.nextInt (standbyInterval));
56
int choice = standbyAlt.fairSelect (); // magic synchronisation
58
if (choice != TIMEOUT) {
59
play (choice, random, tim);
66
private void play (int choice, Random random, CSTimer tim) {
68
final boolean RIGHTMOST = (choice == 0);
69
final boolean LEFTMOST = (choice == (barrier.length - 1));
73
parcel = new Parcel (new Color (random.nextInt ()), 0);
75
parcel = (Parcel) rightIn.read ();
78
configure.write (parcel.colour);
79
configure.write (String.valueOf (parcel.count));
81
while (click.pending ()) click.read (); // clear any buffered mouse clicks
82
configure.write (Boolean.TRUE); // enable mouse clicks
86
rightOut.write (parcel); // bounce
88
leftOut.write (parcel); // forward
92
final Guard[] playGuard = {click, tim, leftIn, rightIn, barrier[choice]};
93
final int CLICK = 0, TIMEOUT = 1, LEFT = 2, RIGHT = 3, BARRIER = 4;
95
final Alternative playAlt = new Alternative (playGuard);;
96
final boolean[] playCondition = {true, RIGHTMOST, true, true, !RIGHTMOST};
98
if (RIGHTMOST) tim.setAlarm (tim.read () + playInterval);
100
boolean stopping = false;
102
boolean playing = true;
105
switch (playAlt.priSelect (playCondition)) {
112
case TIMEOUT: // only RIGHTMOST sets a timeout
114
playCondition[TIMEOUT] = false; // disable timeout (only taken once)
118
parcel = (Parcel) leftIn.read ();
119
if (parcel.poisoned) stopping = true;
122
barrier[choice].sync (); // make everyone stop
123
playing = false; // we have the parcel
125
parcel.poisoned = true;
126
rightOut.write (parcel); // forward
129
configure.write (String.valueOf (parcel.count));
132
leftOut.write (parcel); // bounce
134
rightOut.write (parcel); // forward
141
parcel = (Parcel) rightIn.read ();
143
parcel.poisoned = true;
144
rightOut.write (parcel); // bounce
146
configure.write (String.valueOf (parcel.count));
149
rightOut.write (parcel); // bounce
151
leftOut.write (parcel); // forward
157
case BARRIER: // RIGHTMOST => not possible
164
configure.write (Boolean.FALSE); // disable mouse clicks
165
configure.write (""); // clear button label