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

« back to all changes in this revision

Viewing changes to src/jcsp-demos/outputGuards/SymmetricA.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 java.util.Random;
 
4
 
 
5
class SymmetricA implements CSProcess {
 
6
 
 
7
  private final AltingChannelInputInt in;
 
8
  private final AltingChannelOutputInt out;
 
9
 
 
10
  public SymmetricA (AltingChannelInputInt in, AltingChannelOutputInt out) {
 
11
    this.in = in;
 
12
    this.out = out;
 
13
  }
 
14
 
 
15
  public void run () {
 
16
    
 
17
    final Alternative alt = new Alternative (new Guard[] {in , out});
 
18
    final int IN = 0, OUT = 1;
 
19
 
 
20
    final Random rand = new Random ();
 
21
    final CSTimer tim = new CSTimer ();
 
22
    
 
23
    int a = -1, b = -1;
 
24
    
 
25
    while (true) {
 
26
      int period = (rand.nextInt ()) % 16;
 
27
      tim.sleep (period);
 
28
      a++;
 
29
      switch (alt.fairSelect ()) {
 
30
        case IN:
 
31
          b = in.read ();
 
32
          System.out.println (b);
 
33
        break;
 
34
        case OUT:
 
35
          out.write (a);
 
36
          System.out.println ("\t\t" + a);
 
37
        break;
 
38
      }
 
39
    }
 
40
  }
 
41
 
 
42
}