SunSPOT API V2.0


com.sun.squawk.io.mailboxes
Class ServerChannel

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

public 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 a 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 seperate thread.

Example

class NewChannelTimeTest {
    public final static String MAILBOX_NAME = "NewChannelTimeTest";


    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 = TestMailboxes.msg1.getBytes();

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

                for (int i = 0; i < TestMailboxes.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 " + TestMailboxes.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 {
                            msg.getReplyAddress().send(replyEnv);
                        } catch (AddressClosedException ex) {
                            System.out.println("Client seems to have gone away. Oh well. " + msg.getToAddress());
                        }
                    }
                }
            } 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 mailbox to use or further communication.
 void close()
          Unregisters this ServerChannel and it's MailBox.
static ServerChannel create(String name)
          Creates a new ServerChannel and new Mailbox with the given name and registers it with the system.
 String getName()
          Get the name that this ServerChannel was registered under.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

accept

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

Returns:
a new Channel to the client

close

public void close()
Unregisters this ServerChannel and it's MailBox. Should this close existing Channels that came from this channel?


create

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

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

getName

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

Returns:
the name

SunSPOT API V2.0


Copyright © 2007 Sun Microsystems, Inc. All Rights Reserved.