Skip to content

Instantly share code, notes, and snippets.

@cyberterror
Created June 8, 2016 18:27
Show Gist options
  • Save cyberterror/4ea8d92a8344f877735d9df11770c000 to your computer and use it in GitHub Desktop.
Save cyberterror/4ea8d92a8344f877735d9df11770c000 to your computer and use it in GitHub Desktop.
public class Acceptor implements Runnable{
private SelectionKey selectionKey;
private ServerSocketChannel serverSocketChannel;
private final Boolean isWithThreadPool;
private final String sessionName = "Acceptor";
private String sessionAdress;
public Acceptor(final SelectionKey selectionKey, Boolean isWithThreadPool) throws IOException {
this.selectionKey = selectionKey;
this.serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
this.sessionAdress = serverSocketChannel.getLocalAddress().toString();
this.isWithThreadPool = isWithThreadPool;
}
@Override
public void run() {
try {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
if (isWithThreadPool) {
// new HandlerWithThreadPool(selector, socketChannel);
System.out.println("not implemented");
}
else {
new Handler(selectionKey.selector(), socketChannel);
}
}
System.out.println("Connection Accepted by Command Center");
System.out.println("New client ip=" + socketChannel.getRemoteAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return sessionName + "@" + sessionAdress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment