~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/altingBarriers/AltingBarrierGadget2.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import org.jcsp.lang.*;
 
3
 
 
4
import java.awt.Color;
 
5
import java.util.Random;
 
6
 
 
7
public class AltingBarrierGadget2 implements CSProcess {
 
8
 
 
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;
 
14
 
 
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
 
22
  ) {
 
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;
 
30
  }
 
31
 
 
32
  public void run () {
 
33
 
 
34
    CSTimer tim = new CSTimer ();
 
35
 
 
36
    final Random random = new Random ();
 
37
 
 
38
    final Guard[] standbyGuard = new Guard[barrier.length + 1];
 
39
    for (int i = 0; i < barrier.length; i++) {
 
40
      standbyGuard[i] = barrier[i];
 
41
    }
 
42
    standbyGuard[barrier.length] = tim;
 
43
    final int TIMEOUT = barrier.length;
 
44
    Alternative standbyAlt = new Alternative (standbyGuard);
 
45
 
 
46
    configure.write (Boolean.FALSE);           // disable mouse clicks
 
47
 
 
48
    while (true) {
 
49
 
 
50
      configure.write (offColour);
 
51
      tim.sleep (random.nextInt (offInterval));
 
52
 
 
53
      configure.write (standbyColour);
 
54
      tim.setAlarm (tim.read () + random.nextInt (standbyInterval));
 
55
 
 
56
      int choice = standbyAlt.fairSelect ();   // magic synchronisation
 
57
 
 
58
      if (choice != TIMEOUT) {
 
59
        play (choice, random, tim);
 
60
      }
 
61
      
 
62
    }
 
63
 
 
64
  }
 
65
 
 
66
  private void play (int choice, Random random, CSTimer tim) {
 
67
 
 
68
    final boolean RIGHTMOST = (choice == 0);
 
69
    final boolean LEFTMOST = (choice == (barrier.length - 1));
 
70
 
 
71
    Parcel parcel = null;
 
72
    if (RIGHTMOST) {
 
73
      parcel = new Parcel (new Color (random.nextInt ()), 0);
 
74
    } else {
 
75
      parcel = (Parcel) rightIn.read ();
 
76
    }
 
77
 
 
78
    configure.write (parcel.colour);
 
79
    configure.write (String.valueOf (parcel.count));
 
80
 
 
81
    while (click.pending ()) click.read ();    // clear any buffered mouse clicks
 
82
    configure.write (Boolean.TRUE);            // enable mouse clicks
 
83
 
 
84
    parcel.count++;
 
85
    if (LEFTMOST) {
 
86
      rightOut.write (parcel);                 // bounce
 
87
    } else {
 
88
      leftOut.write (parcel);                  // forward
 
89
    }
 
90
    parcel = null;
 
91
  
 
92
    final Guard[] playGuard = {click, tim, leftIn, rightIn, barrier[choice]};
 
93
    final int CLICK = 0, TIMEOUT = 1, LEFT = 2, RIGHT = 3, BARRIER = 4;
 
94
 
 
95
    final Alternative playAlt = new Alternative (playGuard);;
 
96
    final boolean[] playCondition = {true, RIGHTMOST, true, true, !RIGHTMOST};
 
97
 
 
98
    if (RIGHTMOST) tim.setAlarm (tim.read () + playInterval);
 
99
 
 
100
    boolean stopping = false;
 
101
    
 
102
    boolean playing = true;
 
103
    while (playing) {
 
104
      
 
105
      switch (playAlt.priSelect (playCondition)) {
 
106
 
 
107
        case CLICK:
 
108
          click.read ();
 
109
          stopping = true;
 
110
        break;
 
111
 
 
112
        case TIMEOUT:                          // only RIGHTMOST sets a timeout
 
113
          stopping = true;
 
114
          playCondition[TIMEOUT] = false;      // disable timeout (only taken once)
 
115
        break;
 
116
 
 
117
        case LEFT:
 
118
          parcel = (Parcel) leftIn.read ();
 
119
          if (parcel.poisoned) stopping = true;
 
120
          if (stopping) {
 
121
            if (RIGHTMOST) {
 
122
              barrier[choice].sync ();         // make everyone stop
 
123
              playing = false;                 // we have the parcel
 
124
            } else {
 
125
              parcel.poisoned = true;
 
126
              rightOut.write (parcel);         // forward
 
127
            }
 
128
          } else {
 
129
            configure.write (String.valueOf (parcel.count));
 
130
            parcel.count++;
 
131
            if (RIGHTMOST) {
 
132
              leftOut.write (parcel);          // bounce
 
133
            } else {
 
134
              rightOut.write (parcel);         // forward
 
135
            }
 
136
            parcel = null;
 
137
          }
 
138
        break;
 
139
 
 
140
        case RIGHT:
 
141
          parcel = (Parcel) rightIn.read ();
 
142
          if (stopping) {
 
143
            parcel.poisoned = true;
 
144
            rightOut.write (parcel);           // bounce
 
145
          } else {
 
146
            configure.write (String.valueOf (parcel.count));
 
147
            parcel.count++;
 
148
            if (LEFTMOST) {
 
149
              rightOut.write (parcel);         // bounce
 
150
            } else {
 
151
              leftOut.write (parcel);          // forward
 
152
            }
 
153
          }
 
154
          parcel = null;
 
155
        break;
 
156
 
 
157
        case BARRIER:                          // RIGHTMOST => not possible
 
158
          playing = false;
 
159
        break;
 
160
 
 
161
      }
 
162
    }
 
163
 
 
164
    configure.write (Boolean.FALSE);           // disable mouse clicks
 
165
    configure.write ("");                      // clear button label
 
166
 
 
167
  }
 
168
 
 
169
}