Some changes to further polish #74

This commit is contained in:
Narendra Pathai
2015-09-16 13:24:21 +05:30
parent e5f1ff1be1
commit 570a30099e
8 changed files with 119 additions and 81 deletions

View File

@ -66,6 +66,15 @@ public class App {
private NioReactor reactor;
private List<AbstractNioChannel> channels = new ArrayList<>();
private Dispatcher dispatcher;
/**
* Creates an instance of App which will use provided dispatcher for dispatching events on reactor.
* @param dispatcher the dispatcher that will be used to dispatch events.
*/
public App(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}
/**
* App entry.
@ -73,16 +82,15 @@ public class App {
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new App().start(new ThreadPoolDispatcher(2));
new App(new ThreadPoolDispatcher(2)).start();
}
/**
* Starts the NIO reactor.
* @param threadPoolDispatcher
*
*
* @throws IOException if any channel fails to bind.
*/
public void start(Dispatcher dispatcher) throws IOException {
public void start() throws IOException {
/*
* The application can customize its event dispatching mechanism.
*/
@ -110,8 +118,9 @@ public class App {
*/
public void stop() throws InterruptedException, IOException {
reactor.stop();
dispatcher.stop();
for (AbstractNioChannel channel : channels) {
channel.getChannel().close();
channel.getJavaChannel().close();
}
}

View File

@ -48,7 +48,7 @@ public abstract class AbstractNioChannel {
/**
* @return the wrapped NIO channel.
*/
public SelectableChannel getChannel() {
public SelectableChannel getJavaChannel() {
return channel;
}

View File

@ -64,8 +64,8 @@ public class NioDatagramChannel extends AbstractNioChannel {
* @return the underlying datagram channel.
*/
@Override
public DatagramChannel getChannel() {
return (DatagramChannel) super.getChannel();
public DatagramChannel getJavaChannel() {
return (DatagramChannel) super.getJavaChannel();
}
/**
@ -75,8 +75,8 @@ public class NioDatagramChannel extends AbstractNioChannel {
*/
@Override
public void bind() throws IOException {
getChannel().socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
getChannel().configureBlocking(false);
getJavaChannel().socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
getJavaChannel().configureBlocking(false);
System.out.println("Bound UDP socket at port: " + port);
}
@ -87,7 +87,7 @@ public class NioDatagramChannel extends AbstractNioChannel {
@Override
protected void doWrite(Object pendingWrite, SelectionKey key) throws IOException {
DatagramPacket pendingPacket = (DatagramPacket) pendingWrite;
getChannel().send(pendingPacket.getData(), pendingPacket.getReceiver());
getJavaChannel().send(pendingPacket.getData(), pendingPacket.getReceiver());
}
/**

View File

@ -74,12 +74,13 @@ public class NioReactor {
* Stops the reactor and related resources such as dispatcher.
*
* @throws InterruptedException if interrupted while stopping the reactor.
* @throws IOException if any I/O error occurs.
*/
public void stop() throws InterruptedException {
public void stop() throws InterruptedException, IOException {
reactorMain.shutdownNow();
selector.wakeup();
reactorMain.awaitTermination(4, TimeUnit.SECONDS);
dispatcher.stop();
selector.close();
}
/**
@ -94,7 +95,7 @@ public class NioReactor {
* @throws IOException if any I/O error occurs.
*/
public NioReactor registerChannel(AbstractNioChannel channel) throws IOException {
SelectionKey key = channel.getChannel().register(selector, channel.getInterestedOps());
SelectionKey key = channel.getJavaChannel().register(selector, channel.getInterestedOps());
key.attach(channel);
channel.setReactor(this);
return this;

View File

@ -43,8 +43,8 @@ public class NioServerSocketChannel extends AbstractNioChannel {
* @return the underlying {@link ServerSocketChannel}.
*/
@Override
public ServerSocketChannel getChannel() {
return (ServerSocketChannel) super.getChannel();
public ServerSocketChannel getJavaChannel() {
return (ServerSocketChannel) super.getJavaChannel();
}
/**
@ -71,8 +71,8 @@ public class NioServerSocketChannel extends AbstractNioChannel {
*/
@Override
public void bind() throws IOException {
((ServerSocketChannel) getChannel()).socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
((ServerSocketChannel) getChannel()).configureBlocking(false);
((ServerSocketChannel) getJavaChannel()).socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
((ServerSocketChannel) getJavaChannel()).configureBlocking(false);
System.out.println("Bound TCP socket at port: " + port);
}

View File

@ -22,8 +22,8 @@ public class AppTest {
*/
@Test
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
App app = new App();
app.start(new ThreadPoolDispatcher(2));
App app = new App(new ThreadPoolDispatcher(2));
app.start();
AppClient client = new AppClient();
client.start();
@ -48,8 +48,8 @@ public class AppTest {
*/
@Test
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
App app = new App();
app.start(new SameThreadDispatcher());
App app = new App(new SameThreadDispatcher());
app.start();
AppClient client = new AppClient();
client.start();