Java Socket实战(四)

2014-11-24 10:46:19 · 作者: · 浏览: 3
serverSocketChannel.socket().bind(new InetSocketAddress(port));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
try {
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey readyKey = it.next();
it.remove();
invoke((ServerSocketChannel) readyKey.channel());
}
} catch(Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
}
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
selector.close();
} catch(Exception ex) {}
try {
serverSocketChannel.close();
} catch(Exception ex) {}
}
}
}).start();
}
@Override
public void shutdown() throws Exception {
// Implement me
}
private void invoke(ServerSocketChannel serverSocketChannel) throws Exception {
SocketChannel socketChannel = null;
try {
socketChannel = serverSocketChannel.accept();
MyRequest myRequest = receiveData(socketChannel);
MyResponse myResponse = execute(myRequest);
sendData(socketChannel, myResponse);
} finally {
try {
socketChannel.close();
} catch(Exception ex) {}
}
}
private MyResponse execute(MyRequest request) throws Exception {
Class clazz = request.getRequestClass();
String methodName = request.getRequestMethod();
Class< >[] parameterTypes = request.getRequestParameterTypes();
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
Object[] parameterValues = request.getRequestParameterValues();
final Object obj = method.invoke(clazz.newInstance(), parameterValues);
return new MyGenericResponse(obj);
}
private MyRequest receiveData(SocketChannel socketChannel) throws IOException {
MyRequest myRequest = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
byte[] bytes;
int size = 0;
while ((size = socketChannel.read(buffer)) >= 0) {
buffer.flip();
bytes = new byte[size];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
}
bytes = baos.toByteArray();
Object obj = SerializableUtil.toObject(bytes);
myRequest = (MyRequest)obj;
} finally {
try {
baos.close();
} catch(Exception ex) {}
}
return myRequest;
}
private void sendData(SocketChannel socketChannel, MyResponse myResponse) throws IOException {
byte[] bytes = SerializableUtil.toBytes(myResponse);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
socketChannel.write(buffer);