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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.jcsp.lang.*;
import java.util.Random;

class SymmetricA implements CSProcess {

  private final AltingChannelInputInt in;
  private final AltingChannelOutputInt out;

  public SymmetricA (AltingChannelInputInt in, AltingChannelOutputInt out) {
    this.in = in;
    this.out = out;
  }

  public void run () {
    
    final Alternative alt = new Alternative (new Guard[] {in , out});
    final int IN = 0, OUT = 1;

    final Random rand = new Random ();
    final CSTimer tim = new CSTimer ();
    
    int a = -1, b = -1;
    
    while (true) {
      int period = (rand.nextInt ()) % 16;
      tim.sleep (period);
      a++;
      switch (alt.fairSelect ()) {
        case IN:
          b = in.read ();
	  System.out.println (b);
        break;
        case OUT:
          out.write (a);
	  System.out.println ("\t\t" + a);
        break;
      }
    }
  }

}