NIO_CHANGEREQUEST
--------------------------------------------------------------------------
import java.nio.channels.SocketChannel;
public class ChangeRequest {
public static final int REGISTER = 1;
public static final int CHANGEOPS = 2;
public SocketChannel socket;
public int type;
public int ops;
public ChangeRequest(SocketChannel socket, int type, int ops) {
this.socket = socket;
this.type = type;
this.ops = ops;
}
}
NIO_SERVEREVENT
--------------------------------------------------------------------------
import java.nio.channels.SocketChannel;
class ServerDataEvent {
public NioServer server;
public SocketChannel socket;
public byte[] data;
public ServerDataEvent(NioServer server, SocketChannel socket, byte[] data) {
this.server = server;
this.socket = socket;
this.data = data;
}
}
NIO_ECHOWORKER
--------------------------------------------------------------------------
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;
public class EchoWorker implements Runnable {
private List queue = new LinkedList();
public void processData(NioServer server, SocketChannel socket, byte[] data, int count) {
byte[] dataCopy = new byte[count];
System.arraycopy(data, 0, dataCopy, 0, count);
synchronized(queue) {
queue.add(new ServerDataEvent(server, socket, dataCopy));
queue.notify();
}
}
public void run() {
ServerDataEvent dataEvent;
while(true) {
// Wait for data to become available
synchronized(queue) {
while(queue.isEmpty()) {
try {
queue.wait();
} catch (InterruptedException e) {
}
}
dataEvent = (ServerDataEvent) queue.remove(0);
}
// Return to sender
dataEvent.server.send(dataEvent.socket, dataEvent.data);
}
}
}