1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
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 //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
29
package org.jcsp.lang;
31
import java.io.Serializable;
34
* This is the super-class for one-to-any <TT>interface</TT>-specific CALL channels,
35
* safe for use by one client and many servers.
37
* <A HREF="#constructor_summary">Shortcut to the Constructor and Method Summaries.</A>
39
* <H2>Description</H2>
40
* Please see {@link One2OneCallChannel} for general information about CALL channels.
41
* Documented here is information specific to this <I>1-any</I> version.
43
* <H3><A NAME="Convert">Converting a Method Interface into a Variant CALL Channel</H3>
44
* Constructing a <I>1-any</I> CALL channel for a specific <TT>interface</TT>
45
* follows exactly the same pattern as in the <I>1-1</I> case. Of course, it must
46
* extend <TT>One2AnyCallChannel</TT> rather than <TT>One2OneCallChannel</TT>.
48
* For example, using the same <A HREF="One2OneCallChannel.html#Foo"><TT>Foo</TT></A>
49
* interface as before, we derive:
51
* import org.jcsp.lang.*;
53
* public class One2AnyFooChannel extends One2AnyCallChannel implements Foo {
55
* ... same body as <A HREF="One2OneCallChannel.html#One2OneFooChannel"><TT>One2OneFooChannel</TT></A>
60
* <H3><A NAME="Call">Calling a CALL Channel</H3>
61
* All the <I>client</I> needs to see is the method <TT>interface</TT>
62
* implemented by the CALL channel. So far as the <I>client</I> is concerned, therefore,
63
* there is <I>no</I> difference between any of the varieties of CALL channel
64
* - it just <A HREF="One2OneCallChannel.html#Call">makes the call</A>.
66
* <H3><A NAME="Accept">Accepting a CALL Channel</H3>
67
* The mechanics of accepting a CALL channel are the same for all varieties.
68
* However, the <I>server</I> should declare which kind (or kinds) it allows
71
* import org.jcsp.lang.*;
73
* class B implements CSProcess, Foo {
75
* private final ChannelAccept in;
77
* public B (final One2OneFooChannel in) { // original constructor
81
* public B (final One2AnyFooChannel in) { // additional constructor
85
* ... rest <A HREF="One2OneCallChannel.html#Accept">as before</A>
89
* When wrapping the above to hide its raw method interface, don't forget to include
90
* the extra constructor(s):
92
* import org.jcsp.lang.*;
94
* public class B2 implements CSProcess { // no Foo interface
98
* public B2 (final One2OneFooChannel in) { // original constructor
102
* public B2 (final One2AnyFooChannel in) { // additional constructor
106
* public void run () {
113
* <H3><A NAME="ALTing">ALTing on a CALL Channel</H3>
114
* As for <I>ordinary</I> channels, ALTing over <I>1-Any</I> or <I>Any-Any</I> versions
115
* is not supported. Hence, a server can only choose to {@link #accept <TT>accept</TT>}
116
* or not to <TT>accept</TT> a <TT>One2AnyFooChannel</TT> - it cannot back off because
117
* of some other event.
119
* <H3><A NAME="Network">Building a CALL Channel Network</H3>
120
* Network building with CALL channels is the same as building with <I>ordinary</I>
121
* channels. First construct the channels and, then, construct the processes
122
* - plugging in the channels as required and running them in {@link Parallel}.
124
* For example, the network consisting of one <I>client</I> and several <I>servers</I>:
125
* <p><IMG SRC="doc-files\One2AnyCallChannel1.gif"></p>
126
* where <TT>A</TT> is unchanged from its definition
127
* in <A HREF="One2OneCallChannel.html#Call"><TT>One2OneCallChannel</TT></A>,
130
* One2AnyFooChannel c = new One2AnyFooChannel ();
132
* final B2[] bServers = new B2[n_bClients];
133
* for (int i = 0; i < bServers.length; i++) {
134
* bServers[i] = new B2 (c);
140
* new Parallel (bServers)
144
* [Reminder: <I>XXX-any</I> channels are not broadcasters of information.
145
* In the above, when <TT>A</TT> makes a CALL on <TT>c</TT>, it must not care
146
* <I>which</I> of the <TT>B2</TT> servers picks it up. The servers compete
147
* with each other to service the client.]
149
* <H2><A NAME="Example">Example</H2>
150
* Please see <A HREF="Any2AnyCallChannel.html#Example"><TT>Any2AnyCallChannel</TT></A>
151
* for an example that includes many <I>clients</I> and many <I>servers</I> competing for
152
* each other's attention.
154
* @see org.jcsp.lang.One2OneCallChannel
155
* @see org.jcsp.lang.Any2OneCallChannel
156
* @see org.jcsp.lang.Any2AnyCallChannel
157
* @see org.jcsp.lang.Alternative
162
public abstract class One2AnyCallChannel implements ChannelAccept, Serializable
165
* This is used to synchronise the calling and accepting process.
167
final private One2OneChannelImpl c = new One2OneChannelImpl();
170
* This holds a reference to a <I>server</I> process so that a <I>client</I> may
171
* make the call. The reference is only valid between the {@link #join <TT>join</TT>}
172
* and {@link #fork <TT>fork</TT>} elements of the standard
173
* <A HREF="One2OneCallChannel.html#One2OneFooChannel">calling sequence</A>.
174
* As shown in that sequence, it will need casting up to the relevant interface
175
* supported by the specific CALL channel derived from this class.
177
protected CSProcess server;
180
* This may be set during the standard <A HREF="One2OneCallChannel.html#One2OneFooChannel">calling sequence</A> to record
181
* which method was invoked by a <I>client</I>. It is only safe to do this between
182
* the {@link #join <TT>join</TT>} and {@link #fork <TT>fork</TT>} elements of
183
* that sequence. Either <I>all</I> the CALL
184
* channel methods should do this or <I>none</I> - in the latter case, its default
185
* value remains as zero. Its value is returned to a <I>server</I> as the result
186
* the <I>server</I>'s invocation of {@link #accept <TT>accept</TT>}.
188
protected int selected = 0;
191
* This is invoked by a <I>server</I> when it commits to accepting a CALL
192
* from a <I>client</I>. The parameter supplied must be a reference to this <I>server</I>
193
* - see the <A HREF="One2OneCallChannel.html#Accept">example</A> from {@link One2OneCallChannel}.
194
* It will not complete until a CALL has been made. If the derived CALL channel has set
195
* the {@link #selected} field in the way defined by the standard
196
* <A HREF="One2OneCallChannel.html#One2OneFooChannel">calling sequence</A>,
197
* the value returned by this method will indicate which method was called.
199
* @param server the <I>server</I> process receiving the CALL.
201
public synchronized int accept(CSProcess server)
203
this.server = server;
204
c.read(); // ready to ACCEPT the CALL
205
c.read(); // wait until the CALL is complete
210
* This is invoked by a <I>client</I> during the standard <A HREF="One2OneCallChannel.html#One2OneFooChannel">calling
211
* sequence</A>. It will not complete until a <I>server</I> invokes
212
* an {@link #accept <TT>accept</TT>} on this channel. In turn, that <TT>accept</TT>
213
* will not complete until the <I>client</I> invokes a {@link #fork <TT>fork</TT>},
214
* after having made its CALL on the <I>server</I>.
216
protected void join()
222
* This is invoked by a <I>client</I> during the standard <A HREF="One2OneCallChannel.html#One2OneFooChannel">calling
223
* sequence</A>. A <I>server</I> must have invoked an {@link #accept <TT>accept</TT>}
224
* for the <I>client</I> to have got this far in the sequence - see
225
* the {@link #join <TT>join</TT>}. This call unblocks that <TT>accept</TT>,
226
* releasing the <I>server</I> and <I>client</I> to resume separate lives.
228
protected void fork()