Some changes to further polish #74
This commit is contained in:
parent
e5f1ff1be1
commit
570a30099e
Binary file not shown.
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 123 KiB |
@ -124,80 +124,108 @@
|
||||
<operations public="true" package="true" protected="true" private="false" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="14">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<association id="15">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="16" name="dispatcher"/>
|
||||
<multiplicity id="17" minimum="0" maximum="1"/>
|
||||
<association id="14">
|
||||
<end type="SOURCE" refId="13" navigable="false">
|
||||
<attribute id="15" name="dispatcher"/>
|
||||
<multiplicity id="16" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="18">
|
||||
<bendpoint x="309" y="348"/>
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<association id="19">
|
||||
<bendpoint x="460" y="203"/>
|
||||
<end type="SOURCE" refId="13" navigable="false">
|
||||
<attribute id="20" name="reactor"/>
|
||||
<multiplicity id="21" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<dependency id="22">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</dependency>
|
||||
<generalization id="23">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</generalization>
|
||||
<association id="24">
|
||||
<end type="SOURCE" refId="6" navigable="false">
|
||||
<attribute id="25" name="handler"/>
|
||||
<multiplicity id="26" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="11" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="27">
|
||||
<end type="SOURCE" refId="6" navigable="false">
|
||||
<attribute id="28" name="reactor"/>
|
||||
<multiplicity id="29" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<dependency id="30">
|
||||
<end type="SOURCE" refId="13"/>
|
||||
<end type="TARGET" refId="12"/>
|
||||
</dependency>
|
||||
<association id="31">
|
||||
<association id="17">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="32" name="selector"/>
|
||||
<multiplicity id="33" minimum="0" maximum="1"/>
|
||||
<attribute id="18" name="selector">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="19" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="34">
|
||||
<end type="SOURCE" refId="12"/>
|
||||
<end type="TARGET" refId="11"/>
|
||||
</realization>
|
||||
<generalization id="35">
|
||||
<dependency id="20">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</dependency>
|
||||
<generalization id="21">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</generalization>
|
||||
<dependency id="36">
|
||||
<realization id="22">
|
||||
<bendpoint x="309" y="348"/>
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<dependency id="23">
|
||||
<end type="SOURCE" refId="13"/>
|
||||
<end type="TARGET" refId="12"/>
|
||||
</dependency>
|
||||
<association id="24">
|
||||
<end type="SOURCE" refId="6" navigable="false">
|
||||
<attribute id="25" name="handler">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="26" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="11" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<dependency id="27">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="9"/>
|
||||
</dependency>
|
||||
<generalization id="28">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</generalization>
|
||||
<association id="29">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="30" name="dispatcher">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="31" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="32">
|
||||
<end type="SOURCE" refId="12"/>
|
||||
<end type="TARGET" refId="11"/>
|
||||
</realization>
|
||||
<association id="33">
|
||||
<bendpoint x="460" y="203"/>
|
||||
<end type="SOURCE" refId="13" navigable="false">
|
||||
<attribute id="34" name="reactor">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="35" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="36">
|
||||
<end type="SOURCE" refId="6" navigable="false">
|
||||
<attribute id="37" name="reactor">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="38" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="39">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</realization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ public abstract class AbstractNioChannel {
|
||||
/**
|
||||
* @return the wrapped NIO channel.
|
||||
*/
|
||||
public SelectableChannel getChannel() {
|
||||
public SelectableChannel getJavaChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user