2
/*************************************************************************
4
* JCSP ("CSP for Java") libraries *
5
* Copyright (C) 1996-2008 Peter Welch and Paul Austin. *
7
* This library is free software; you can redistribute it and/or *
8
* modify it under the terms of the GNU Lesser General Public *
9
* License as published by the Free Software Foundation; either *
10
* version 2.1 of the License, or (at your option) any later version. *
12
* This library is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15
* Lesser General Public License for more details. *
17
* You should have received a copy of the GNU Lesser General Public *
18
* License along with this library; if not, write to the Free Software *
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, *
22
* Author contact: P.H.Welch@ukc.ac.uk *
24
*************************************************************************/
26
// package org.jcsp.plugNplay;
28
// This class *is* in the above package. It is reproduced here because
29
// this source is quoted in the documentation of org.jcsp.lang.Alternative.
30
// The above package declaration is commented out since, otherwise,
31
// compiling the *java files in this directory would fail (because of
32
// an attempt to duplicate Regulate in org.jcsp.plugNplay).
36
* This process controls the flow of traffic from its in to out channels.
37
* <H2>Description</H2>
38
* <tt>Regulate</tt> produces a constant rate of output flow, regardless of
39
* the rate of its input. At the end of each timeslice defined by the required
40
* output rate, it outputs the last object input during that timeslice.
41
* If nothing has come in during a timeslice, the previous output will be repeated
42
* (note: this will be a null if nothing has ever arrived).
43
* If the input flow is greater than the required output flow, data will be discarded.
45
* The interval (in msecs) defining the output flow rate is given by a constructor
46
* argument; but this can be changed at any time by sending a new interval (as a <tt>Long</tt>)
47
* down its <tt>reset</tt> channel.
48
* <H2>Description</H2>
49
* See {@link Alternative#STFR here}.
51
* @see org.jcsp.plugNplay.FixedDelay
52
* @see org.jcsp.plugNplay.Regular
59
import org.jcsp.lang.*;
61
public class Regulate implements CSProcess {
63
private final AltingChannelInput in, reset;
64
private final ChannelOutput out;
65
private final long initialInterval;
68
* Construct the process.
70
* @param in the input channel
71
* @param out the output channel
72
* @param initialInterval the initial interval between outputs (in milliseconds)
73
* @param reset send a <tt>Long</tt> down this to change the interval between outputs (in milliseconds)
75
public Regulate (final AltingChannelInput in, final AltingChannelInput reset,
76
final ChannelOutput out, final long initialInterval) {
80
this.initialInterval = initialInterval;
84
* The main body of this process.
88
final CSTimer tim = new CSTimer ();
90
final Guard[] guards = {reset, tim, in}; // prioritised order
91
final int RESET = 0; // index into guards
92
final int TIM = 1; // index into guards
93
final int IN = 2; // index into guards
95
final Alternative alt = new Alternative (guards);
97
Object x = null; // holding object
99
long interval = initialInterval;
101
long timeout = tim.read () + interval;
102
tim.setAlarm (timeout);
105
switch (alt.priSelect ()) {
107
interval = ((Long) reset.read ()).longValue ();
108
timeout = tim.read (); // fall through
112
tim.setAlarm (timeout);