JCSP.net Crash Course
I couldn't find a great deal of information on the internet about how to actually use some of the very shiny new features in JCSP, specifically the JCSP.net stuff. JCSP.net allows for you to connect up JVMs running on the same or different machines and have them communicate using channels as though they were local processes. Here's what you need to begin:
- A recent version of JCSP
- A java compiler
So, for this little example we will need a pair of processes; one to send and one to receieve a message. Let's define the Sender:
import org.jcsp.lang.*;
public class Producer implements CSProcess
{
private ChannelOutput toNet;
public Producer (ChannelOutput o)
{
toNet = o;
}
public void run ()
{
while (true)
{
toNet.write ("Hello Parallel World!");
}
}
}
Hold on a second, where's the network stuff? We don't need it. Sending messages over the network is transparent remember? :-)
Okay, let's define a Consumer to take messages from the Producer.
import org.jcsp.lang.*;
public class Consumer implements CSProcess
{
private ChannelInput fromServer;
public Consumer (ChannelInput i)
{
fromServer = i;
}
public void run ()
{
while (true)
{
System.out.println ((String) fromServer.read ());
}
}
}
Okay, that's fair enough. But we need a pair of classes to set these things off. Here's the producer controller:
import org.jcsp.lang.*;
import org.jcsp.net.*;
import org.jcsp.net.cns.*;
public class JCSPNetTest
{
public JCSPNetTest ()
{
}
public static void main (String[] args)
{
try
{
Node.getInstance ().init ();
}
catch (Exception e)
{
System.out.println ("Blergh");
}
NetChannelOutput out = CNS.createOne2Net ("jcsp-test");
new Producer (out).run ();
}
}
Node.getInstance ().init () needs to be called per-JVM when you want to use JCSP across a network, but does only need calling once. It does some magic behind the scenes and binds to the 'default CNS'. We create a one2net channel called 'jcsp-test' and spawn a Producer. Here's the code for the Consumer's controlling class:
port org.jcsp.lang.*;
import org.jcsp.net.*;
import org.jcsp.net.cns.*;
public class JCSPNetTestClient
{
public JCSPNetTestClient ()
{
}
public static void main (String[] args)
{
try
{
Node.getInstance ().init ();
}
catch (Exception e)
{
System.out.println ("Blergh");
}
NetChannelInput in = CNS.createNet2One ("jcsp-test");
new Consumer (in).run ();
}
}
As you can see, it's almost identical to the producer's controller, except that we request a net2one channel. Note that the names given to channels must be unique to that CNS, but that here we use the same name to ensure we get both the sending and receieving ends of the same networked channel. So, to run the quick and dirty test application:
- javac -cp jcsp.jar:. *.java
- java -cp jcsp.jar:. org.jcsp.net.tcpip.TCPIPCNSServer
- java -cp jcsp.jar:. JCSPNetTestClient
- java -cp jcsp.jar:. JCSPNetTest
The first line fairly obviously compiles your java to .class files. The second fires off a TCPIPCNSServer which allows processes to locate other processes using networked channels. After running the final command, your TestClient should start firing out a mass of 'hello parallel world' messages. Well done, that's a JCSP.net application! :-)
Thanks to Peter Welch for slight tweaks to the code