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

« back to all changes in this revision

Viewing changes to src/jcsp-demos/altingBarriers/AltingBarrierGadget5.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
import org.jcsp.util.*;
 
4
 
 
5
import java.awt.Color;
 
6
import java.util.*;
 
7
 
 
8
public class AltingBarrierGadget5 implements CSProcess {
 
9
 
 
10
  private final AltingChannelInput in;
 
11
  private final ChannelOutput[] out;
 
12
  private final AltingBarrier bar;
 
13
  private final AltingChannelInput click;
 
14
  private final ChannelOutput configure;
 
15
  private final Color offColour, standbyColour;
 
16
  private final int offInterval, standbyInterval;
 
17
  private final int playInterval, countInterval;
 
18
  private final String[] label;
 
19
 
 
20
  public AltingBarrierGadget5 (
 
21
    AltingChannelInput in, ChannelOutput[] out, AltingBarrier bar,
 
22
    AltingChannelInput click, ChannelOutput configure,
 
23
    Color offColour, Color standbyColour,
 
24
    int offInterval, int standbyInterval,
 
25
    int playInterval, int countInterval,
 
26
    String[] label
 
27
  ) {
 
28
    this.in = in;  this.out = out; this.bar = bar;
 
29
    this.click = click;  this.configure = configure;
 
30
    this.offColour = offColour;  this.standbyColour = standbyColour;
 
31
    this.offInterval = offInterval;  this.standbyInterval = standbyInterval;
 
32
    this.playInterval = playInterval;  this.countInterval = countInterval;
 
33
    this.label = label;
 
34
  }
 
35
 
 
36
  public void run () {
 
37
 
 
38
    CSTimer tim = new CSTimer ();
 
39
 
 
40
    final Random random = new Random ();
 
41
 
 
42
    // make alting barrier and shared variables
 
43
    // for the group this gadget is leading ...
 
44
 
 
45
    final AltingBarrier[] myAltingBarrier = AltingBarrier.create (out.length + 1);
 
46
    final Any2OneChannel myChannel = Channel.any2one (new OverWritingBuffer (1));
 
47
    final Shared myShared = new Shared (myChannel.out());
 
48
 
 
49
    // parallel-I/O distribution of barriers and shared variables
 
50
 
 
51
    final AltingBarrier barDash = bar.expand ();
 
52
 
 
53
    new ProcessManager  (
 
54
      new CSProcess () {
 
55
        public void run () {
 
56
          for (int i = 0; i < out.length; i++) {
 
57
            synchronized (out[i]) {
 
58
              out[i].write (myAltingBarrier[i]);
 
59
              out[i].write (myShared);
 
60
            }
 
61
          }
 
62
          barDash.sync ();           // wait for all gadgets to finish output
 
63
          barDash.contract ();       // discard no longer needed item
 
64
        }
 
65
      }
 
66
    ).start ();
 
67
 
 
68
    ArrayList dump =                            // temporary storage
 
69
      new ArrayList (2*out.length);             // (guessed initial size)
 
70
      
 
71
    Alternative distributeAlt =
 
72
      new Alternative (new Guard[] {in, bar});
 
73
    
 
74
    final int IN = 0, BAR = 1;
 
75
    
 
76
    int count = 0;
 
77
    boolean more = true;
 
78
    
 
79
    while (more) {
 
80
      switch (distributeAlt.priSelect ()) {
 
81
        case IN:
 
82
          dump.add (in.read ());                 // an AltingBarrier
 
83
          dump.add (in.read ());                 // Shared variables
 
84
          count++;
 
85
        break;
 
86
        case BAR:
 
87
          more = false;
 
88
        break;
 
89
      }
 
90
    }
 
91
 
 
92
    distributeAlt = null;                        // discard no longer needed item
 
93
    bar.contract ();                             // discard no longer needed item
 
94
 
 
95
    // make receiving arrays for the barrier and shared variables
 
96
    // from the other group leaders
 
97
 
 
98
    final Guard[] standbyGuard = new Guard[count + 2];
 
99
 
 
100
    final int MY_INDEX = count;    
 
101
    standbyGuard[MY_INDEX] = myAltingBarrier[out.length];
 
102
 
 
103
    final int TIMEOUT = count + 1;
 
104
    standbyGuard[TIMEOUT] = tim;
 
105
 
 
106
    final Shared[] shared = new Shared[count + 1];
 
107
    shared[MY_INDEX] = myShared;
 
108
 
 
109
    Iterator iterator = dump.iterator ();
 
110
    for (int i = 0; i < count; i++) {
 
111
      standbyGuard[i] = (AltingBarrier) iterator.next ();
 
112
      shared[i] = (Shared) iterator.next ();
 
113
    }
 
114
 
 
115
    iterator = null;                             // discard no longer needed item
 
116
    dump = null;                                 // discard no longer needed item
 
117
 
 
118
    // start real work ...
 
119
    
 
120
    Alternative standbyAlt = new Alternative (standbyGuard);
 
121
 
 
122
    configure.write (Boolean.FALSE);             // disable mouse clicks
 
123
 
 
124
    while (true) {
 
125
 
 
126
      configure.write (offColour);
 
127
      tim.sleep (random.nextInt (offInterval));
 
128
 
 
129
      configure.write (standbyColour);
 
130
      tim.setAlarm (tim.read () + random.nextInt (standbyInterval));
 
131
 
 
132
      int choice = standbyAlt.fairSelect ();     // magic synchronisation
 
133
 
 
134
      if (choice == MY_INDEX) {
 
135
        playLeader (
 
136
          (AltingBarrier) standbyGuard[choice],
 
137
          shared[choice], myChannel.in(), random, tim
 
138
        );
 
139
      } 
 
140
      else if (choice != TIMEOUT) {
 
141
        play ((AltingBarrier) standbyGuard[choice], shared[choice]);
 
142
      }
 
143
 
 
144
    }
 
145
 
 
146
  }
 
147
 
 
148
  private void playLeader (
 
149
    AltingBarrier bar, Shared shared, AltingChannelInput myChannel,
 
150
    Random random, CSTimer tim
 
151
  ) {
 
152
 
 
153
    while (click.pending ()) click.read ();      // clear any buffered mouse clicks
 
154
    configure.write (Boolean.TRUE);              // enable mouse clicks
 
155
    
 
156
    while (myChannel.pending ()) myChannel.read ();   // clear any buffered cancels
 
157
    
 
158
    int count = 0;
 
159
    long countTimeout = 0;
 
160
 
 
161
    Alternative leaderAlt = new Alternative (new Guard[] {click, myChannel, tim});
 
162
    final int CLICK = 0, CANCEL = 1, TIM = 2;
 
163
    
 
164
    count = (playInterval/countInterval) - 1;
 
165
      
 
166
    shared.ok = (count >= 0);                    // initialise shared variables
 
167
    shared.label = label[count];
 
168
    shared.colour = new Color (random.nextInt (16777216));
 
169
    shared.brighter = shared.colour.brighter ();
 
170
        
 
171
    bar.sync ();                                 // allow inspection of shared variables
 
172
 
 
173
    boolean bright = true;
 
174
 
 
175
    countTimeout = tim.read () + countInterval;  // set first timeout
 
176
    tim.setAlarm (countTimeout);
 
177
 
 
178
    while (shared.ok) {
 
179
      
 
180
      configure.write (bright ? shared.brighter : shared.colour);
 
181
      bright = !bright;
 
182
 
 
183
      configure.write (shared.label);
 
184
    
 
185
      bar.sync ();                               // allow update of shared variables
 
186
 
 
187
      count--;
 
188
      if (count < 0) {
 
189
        shared.ok = false;                       // game over
 
190
      } else {
 
191
        shared.label = label[count];
 
192
      }
 
193
 
 
194
      switch (leaderAlt.priSelect ()) {
 
195
        case CLICK:                              // our button clicked
 
196
          click.read ();
 
197
          shared.ok = false;                     // game over
 
198
        break;
 
199
        case CANCEL:                             // someone else's button clicked
 
200
          myChannel.read ();                     // (they will have set shared.ok)
 
201
        break;
 
202
        case TIM:                                // timeout - move on
 
203
          countTimeout += countInterval;
 
204
          tim.setAlarm (countTimeout);
 
205
        break;
 
206
      }
 
207
 
 
208
      bar.sync ();                               // allow inspection of shared variables
 
209
 
 
210
    }
 
211
 
 
212
    configure.write (Boolean.FALSE);             // disable mouse clicks
 
213
    configure.write ("");                        // clear button label
 
214
 
 
215
  }
 
216
 
 
217
  private void play (AltingBarrier bar, Shared shared) {
 
218
 
 
219
    while (click.pending ()) click.read ();      // clear any buffered mouse clicks
 
220
    configure.write (Boolean.TRUE);              // enable mouse clicks
 
221
 
 
222
    Alternative followerAlt = new Alternative (new Guard[] {click, bar});
 
223
    final int CLICK = 0, BAR = 1;
 
224
        
 
225
    bar.sync ();                                 // allow inspection of shared variables
 
226
 
 
227
    boolean bright = true;
 
228
 
 
229
    while (shared.ok) {
 
230
      
 
231
      configure.write (bright ? shared.brighter : shared.colour);
 
232
      bright = !bright;
 
233
 
 
234
      configure.write (shared.label);
 
235
    
 
236
      bar.sync ();                               // allow update of shared variables
 
237
 
 
238
      switch (followerAlt.priSelect ()) {
 
239
        case CLICK:                              // our button clicked
 
240
          click.read ();
 
241
          shared.toLeader.write (null);          // cancel leader timeout (if any)
 
242
          shared.ok = false;                     // game over (we must set this)
 
243
          bar.sync ();                           // allow inspection of shared variables
 
244
        break;
 
245
        case BAR:                                // allow inspection of shared variables
 
246
        break;
 
247
      }
 
248
 
 
249
    }
 
250
 
 
251
    configure.write (Boolean.FALSE);             // disable mouse clicks
 
252
    configure.write ("");                        // clear button label
 
253
 
 
254
  }
 
255
 
 
256
}