"

2013 FRC Java API

"

com.sun.squawk.io.mailboxes
Class ServerChannel

java.lang.Object
  extended by com.sun.squawk.io.mailboxes.ServerChannel

public final class ServerChannel
extends Object

Given that a Channel is a one-to-one connection between two isolates, a ServerChannel provides a factory to create new Channels by name. It is similar to how network sockets can use a port number to accept a number of client connections.

A server can use the accept method to accept new client connections, which will return a new Channel that the server can use to talk to the client. A server may choose to service each Channel in a separate thread.

Example

class NewChannelTimeTest {
    public final static String MAILBOX_NAME = "NewChannelTimeTest";
    public static final String msg1 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
    public final static int NUM_MESSAGES = 1000;
    public final static int NUM_CLIENTS = 3;

    public static void main(String[] args) throws Exception {
        Client client =new Client();

        Server server = new Server();
        server.start();

        client.start();
        client.join();

        server.join();
    }

    static class Client extends Thread {
        public void run() {
             try {
                byte[] data = msg1.getBytes();

                long start = System.currentTimeMillis();
                Channel testChan = Channel.lookup(MAILBOX_NAME);

                for (int i = 0; i < NUM_MESSAGES; i++) {
                    Envelope cmdEnv = new ByteArrayEnvelope(data);
                    testChan.send(cmdEnv);
                    ByteArrayEnvelope replyEnv = (ByteArrayEnvelope)testChan.receive();
                    byte[] replyData = replyEnv.getData();
                    if (replyData.length != 1 || (replyData[0] != 0)) {
                        System.err.println("Reply not OK");
                    }
                }
                long time = System.currentTimeMillis() - start;

                System.err.println("Client sent " + NUM_MESSAGES + " messages of " + data.length + " bytes in " + time + "ms");
                testChan.close();

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    static class Server extends Thread {

        public void run() {
            byte[] replyData = new byte[1];
            ServerChannel serverChannel = null;
            Channel aChannel = null;
            try {
                serverChannel = ServerChannel.create(MAILBOX_NAME);
            } catch (MailboxInUseException ex) {
                throw new RuntimeException(ex.toString());
            }

            try {
                aChannel = serverChannel.accept();

                // handle messages:
                while (true) {
                    Envelope msg;
                    try {
                        msg = aChannel.receive();
                    } catch (MailboxClosedException e) {
                        System.out.println("Server seems to have gone away. Oh well. " + aChannel);
                        break;
                    }

                    if (msg instanceof ByteArrayEnvelope) {
                        ByteArrayEnvelope dataEnv = (ByteArrayEnvelope)msg;
                        byte[] data = dataEnv.getData();

                        replyData[0] = 0;
                        Envelope replyEnv = new ByteArrayEnvelope(replyData);
                        try {
                            aChannel.send(replyEnv);
                        } catch (AddressClosedException ex) {
                            System.out.println("Client seems to have gone away. Oh well. " + aChannel);
                        }
                    }
                }
            } catch (IOException ex) {
                // ok, just close server.
            } finally {
                // no way to get here:
                System.out.println("Closing server...");
                aChannel.close();
                serverChannel.close();
            }
        }
    }
}
 

See Also:
Channel

Method Summary
 Channel accept()
          Wait for a client to open a connection, then create an anonymous local Channel to use or further communication.
 void close()
          Unregisters this ServerChannel.
static ServerChannel create(String name)
          Creates a new ServerChannel with the given name and registers it with the system.
 String getName()
          Get the name that this ServerChannel was registered under.
 boolean isOpen()
          Return true if the server channel is open, registered, and can accept new connections..
 void reOpen()
          Re-opens a closed ServerChannel.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static ServerChannel create(String name)
                            throws MailboxInUseException
Creates a new ServerChannel with the given name and registers it with the system. When a client does a lookup on this ServerChannel, the ServerChannel creates a new private Channel, and tells the client to use the private Channel. Given a ServerChannel, a server may call accept(), waiting for clients to connect to it. Accept will return with a new Channel to handle communication with this client.

Parameters:
name - the name that this ServerChannel can be looked up under.
Returns:
the new ServerChannel
Throws:
MailboxInUseException - if there is already a ServerChannel registered under the name name.

reOpen

public void reOpen()
            throws MailboxInUseException
Re-opens a closed ServerChannel.

Can be called after hibernation or an explicit call to close() closed this ServerChannel.

NOTE: To avoid race conditions during hibernation, should sleep a little (100ms) between getting a MailboxClosedException and calling reOpen().

Throws:
MailboxInUseException - if there is already a ServerChannel registered under the name name.
IllegalStateException - if the ServerChannel is already open.

getName

public String getName()
Get the name that this ServerChannel was registered under.

Returns:
the name

accept

public Channel accept()
               throws MailboxClosedException
Wait for a client to open a connection, then create an anonymous local Channel to use or further communication.

Returns:
a new Channel to the client
Throws:
MailboxClosedException - if the ServerChannel is closed (either explicitly, or by isolate hibernation)

close

public void close()
Unregisters this ServerChannel. Existing Channels that came from this channel are not closed.


isOpen

public boolean isOpen()
Return true if the server channel is open, registered, and can accept new connections..

Returns:
true if open.

"

2013 FRC Java API

"

"
For updated information see the Java FRC site
"