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

« back to all changes in this revision

Viewing changes to src/jcsp-demos/altingBarriers/AltingBarrierGadget3.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.Random;
 
7
 
 
8
public class AltingBarrierGadget3 implements CSProcess {
 
9
 
 
10
  private final ChannelInput in;
 
11
  private final ChannelOutput[] out;
 
12
  private final AltingChannelInput click;
 
13
  private final ChannelOutput configure;
 
14
  private final Color offColour, standbyColour;
 
15
  private final int offInterval, standbyInterval;
 
16
  private final int playInterval, countInterval;
 
17
  private final String[] label;
 
18
 
 
19
  public AltingBarrierGadget3 (
 
20
    ChannelInput in,
 
21
    ChannelOutput[] out,
 
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;
 
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
    // make receiving arrays for the barrier and shared variables
 
50
    // from the other group leaders
 
51
 
 
52
    final Guard[] standbyGuard = new Guard[out.length + 2];
 
53
 
 
54
    final int MY_INDEX = out.length;    
 
55
    standbyGuard[MY_INDEX] = myAltingBarrier[MY_INDEX];
 
56
 
 
57
    final int TIMEOUT = out.length + 1;
 
58
    standbyGuard[TIMEOUT] = tim;
 
59
    
 
60
    final Shared[] shared = new Shared[out.length + 1];
 
61
    shared[MY_INDEX] = myShared;
 
62
 
 
63
    // parallel-I/O distribution of barriers and shared variables
 
64
 
 
65
    new Parallel (
 
66
      new CSProcess[] {
 
67
      
 
68
        new CSProcess () {
 
69
          public void run () {
 
70
            for (int i = 0; i < out.length; i++) {
 
71
              synchronized (out[i]) {
 
72
                out[i].write (myAltingBarrier[i]);
 
73
                out[i].write (myShared);
 
74
              }
 
75
            }
 
76
          }
 
77
        },
 
78
        
 
79
        new CSProcess () {
 
80
          public void run () {
 
81
            for (int i = 0; i < out.length; i++) {
 
82
              standbyGuard[i] = (AltingBarrier) in.read ();
 
83
              shared[i] = (Shared) in.read ();
 
84
            }
 
85
          }
 
86
        }
 
87
        
 
88
      }
 
89
    ).run ();
 
90
 
 
91
    // start real work ...
 
92
    
 
93
    Alternative standbyAlt = new Alternative (standbyGuard);
 
94
 
 
95
    configure.write (Boolean.FALSE);           // disable mouse clicks
 
96
 
 
97
    while (true) {
 
98
 
 
99
      configure.write (offColour);
 
100
      tim.sleep (random.nextInt (offInterval));
 
101
 
 
102
      configure.write (standbyColour);
 
103
      tim.setAlarm (tim.read () + random.nextInt (standbyInterval));
 
104
 
 
105
      int choice = standbyAlt.fairSelect ();   // magic synchronisation
 
106
 
 
107
      if (choice == MY_INDEX) {
 
108
        playLeader (
 
109
          (AltingBarrier) standbyGuard[choice],
 
110
          shared[choice], myChannel.in(), random, tim
 
111
        );
 
112
      } 
 
113
      else if (choice != TIMEOUT) {
 
114
        play ((AltingBarrier) standbyGuard[choice], shared[choice]);
 
115
      }
 
116
 
 
117
      
 
118
    }
 
119
 
 
120
  }
 
121
 
 
122
  private void playLeader (
 
123
    AltingBarrier bar, Shared shared, AltingChannelInput myChannel,
 
124
    Random random, CSTimer tim
 
125
  ) {
 
126
 
 
127
    while (click.pending ()) click.read ();      // clear any buffered mouse clicks
 
128
    configure.write (Boolean.TRUE);              // enable mouse clicks
 
129
    
 
130
    while (myChannel.pending ()) myChannel.read ();   // clear any buffered cancels
 
131
    
 
132
    int count = 0;
 
133
    long countTimeout = 0;
 
134
 
 
135
    Alternative leaderAlt = new Alternative (new Guard[] {click, myChannel, tim});
 
136
    final int CLICK = 0, CANCEL = 1, TIM = 2;
 
137
    
 
138
    count = (playInterval/countInterval) - 1;
 
139
      
 
140
    shared.ok = (count >= 0);                    // initialise shared variables
 
141
    shared.label = label[count];
 
142
    shared.colour = new Color (random.nextInt ());
 
143
    shared.brighter = shared.colour.brighter ();
 
144
        
 
145
    bar.sync ();                                 // allow inspection of shared variables
 
146
 
 
147
    boolean bright = true;
 
148
 
 
149
    countTimeout = tim.read () + countInterval;  // set first timeout
 
150
    tim.setAlarm (countTimeout);
 
151
 
 
152
    while (shared.ok) {
 
153
      
 
154
      configure.write (bright ? shared.brighter : shared.colour);
 
155
      bright = !bright;
 
156
 
 
157
      configure.write (shared.label);
 
158
 
 
159
      bar.sync ();                               // allow update of shared variables
 
160
 
 
161
      count--;
 
162
      if (count < 0) {
 
163
        shared.ok = false;                       // game over
 
164
      } else {
 
165
        shared.label = label[count];
 
166
      }
 
167
 
 
168
      switch (leaderAlt.priSelect ()) {
 
169
        case CLICK:                              // our button clicked
 
170
          click.read ();
 
171
          shared.ok = false;                     // game over
 
172
        break;
 
173
        case CANCEL:                             // someone else's button clicked
 
174
          myChannel.read ();                     // (they will have set shared.ok)
 
175
        break;
 
176
        case TIM:                                // timeout - move on
 
177
          countTimeout += countInterval;
 
178
          tim.setAlarm (countTimeout);
 
179
        break;
 
180
      }
 
181
 
 
182
      bar.sync ();                               // allow inspection of shared variables
 
183
 
 
184
    }
 
185
 
 
186
    configure.write (Boolean.FALSE);             // disable mouse clicks
 
187
    configure.write ("");                        // clear button label
 
188
 
 
189
  }
 
190
 
 
191
  private void play (AltingBarrier bar, Shared shared) {
 
192
 
 
193
    while (click.pending ()) click.read ();      // clear any buffered mouse clicks
 
194
    configure.write (Boolean.TRUE);              // enable mouse clicks
 
195
 
 
196
    Alternative followerAlt = new Alternative (new Guard[] {click, bar});
 
197
    final int CLICK = 0, BAR = 1;
 
198
        
 
199
    bar.sync ();                                 // allow inspection of shared variables
 
200
 
 
201
    boolean bright = true;
 
202
 
 
203
    while (shared.ok) {
 
204
      
 
205
      configure.write (bright ? shared.brighter : shared.colour);
 
206
      bright = !bright;
 
207
 
 
208
      configure.write (shared.label);
 
209
    
 
210
      bar.sync ();                               // allow update of shared variables
 
211
 
 
212
      switch (followerAlt.priSelect ()) {
 
213
        case CLICK:                              // our button clicked
 
214
          click.read ();
 
215
          shared.toLeader.write (null);          // cancel leader timeout (if any)
 
216
          shared.ok = false;                     // game over (we must set this)
 
217
          bar.sync ();                           // allow inspection of shared variables
 
218
        break;
 
219
        case BAR:                                // allow inspection of shared variables
 
220
        break;
 
221
      }
 
222
 
 
223
    }
 
224
 
 
225
    configure.write (Boolean.FALSE);             // disable mouse clicks
 
226
    configure.write ("");                        // clear button label
 
227
 
 
228
  }
 
229
 
 
230
}