CSP for Java
(JCSP) 1.0-rc4

jcsp.lang
Interface ChannelInput

All Known Subinterfaces:
Channel
All Known Implementing Classes:
AltingChannelInput

public interface ChannelInput

This defines the interface for reading from object channels.

Description

ChannelInput defines the interface for reading from object channels. The interface contains only one method - read(). This method will block the calling process until an Object has been written to the channel by a process at the other end. If an Object has already been written when this method is called, the method will return without blocking. Either way, the method returns the Object sent down the channel.

ChannelInput variables are used to hold channels that are going to be used only for input by the declaring process. This is a security matter -- by declaring a ChannelInput interface, any attempt to output to the channel will generate a compile-time error. For example, the following code fragment will not compile:

 void doWrite (ChannelInput c, Object o) {
   c.write (o);   // illegal
 }
 
When configuring a CSProcess with input channels, they should be declared as ChannelInput (or, if we wish to be able to make choices between events, as AltingChannelInput) variables. The actual channel passed, of course, may belong to any channel class that implements ChannelInput (or AltingChannelInput).

The Object returned can be cast into the actual class the reader process expects. If the reader can handle more than one class of Object (similar to tagged protocols in occam), checks should be made before casting.

Examples

Discard data

 void doRead (ChannelInput c) {
   c.read ();                       // clear the channel
 }
 

Cast data to expected type

 void doRead (ChannelInput c) {
   Boolean b = (Boolean) c.read();  // will cause a ClassCastException
                                    // if read does not return a Boolean
   ...  etc.
 }
 

Cast data after checking type

 void doRead (ChannelInput c) {
   Object o = c.read ();
   if (o instanceof Boolean) {
     System.out.println ("Boolean: " + (Boolean) o);
   }
   else if (o instanceof Integer) {
     System.out.println ("Integer: " + (Integer) o);
   }
   else {
     System.out.println ("Unexpected Class: " + o);
   }
 }
 

Author:
P.D.Austin
See Also:
AltingChannelInput, ChannelOutput

Method Summary
 Object read()
          Read an Object from the channel.
 

Method Detail

read

public Object read()
Read an Object from the channel.
Returns:
the object read from the channel

CSP for Java
(JCSP) 1.0-rc4

Submit a bug or feature to jcsp-team@ukc.ac.uk
Version 1.0-rc4 of the JCSP API Specification (Copyright 1997-2000 P.D.Austin and P.H.Welch - All Rights Reserved)
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.