|
" 2013 FRC Java API " |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.squawk.io.mailboxes.ServerChannel
public final class ServerChannel
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.
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();
}
}
}
}
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 |
|---|
public static ServerChannel create(String name)
throws MailboxInUseException
name - the name that this ServerChannel can be looked up under.
MailboxInUseException - if there is already a ServerChannel registered under the name name.
public void reOpen()
throws MailboxInUseException
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().
MailboxInUseException - if there is already a ServerChannel registered under the name name.
IllegalStateException - if the ServerChannel is already open.public String getName()
public Channel accept()
throws MailboxClosedException
MailboxClosedException - if the ServerChannel is closed (either explicitly, or by isolate hibernation)public void close()
public boolean isOpen()
|
" 2013 FRC Java API " |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||