Skip to content

Commit 570a300

Browse files
committed
Some changes to further polish iluwatar#74
1 parent e5f1ff1 commit 570a300

File tree

8 files changed

+107
-69
lines changed

8 files changed

+107
-69
lines changed

reactor/etc/reactor.png

1.41 KB
Loading

reactor/etc/reactor.ucls

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -124,80 +124,108 @@
124124
<operations public="true" package="true" protected="true" private="false" static="true"/>
125125
</display>
126126
</class>
127-
<realization id="14">
128-
<end type="SOURCE" refId="5"/>
129-
<end type="TARGET" refId="3"/>
130-
</realization>
131-
<association id="15">
132-
<end type="SOURCE" refId="2" navigable="false">
133-
<attribute id="16" name="dispatcher"/>
134-
<multiplicity id="17" minimum="0" maximum="1"/>
127+
<association id="14">
128+
<end type="SOURCE" refId="13" navigable="false">
129+
<attribute id="15" name="dispatcher"/>
130+
<multiplicity id="16" minimum="0" maximum="1"/>
135131
</end>
136132
<end type="TARGET" refId="3" navigable="true"/>
137133
<display labels="true" multiplicity="true"/>
138134
</association>
139-
<realization id="18">
140-
<bendpoint x="309" y="348"/>
141-
<end type="SOURCE" refId="4"/>
142-
<end type="TARGET" refId="3"/>
143-
</realization>
144-
<association id="19">
145-
<bendpoint x="460" y="203"/>
146-
<end type="SOURCE" refId="13" navigable="false">
147-
<attribute id="20" name="reactor"/>
148-
<multiplicity id="21" minimum="0" maximum="1"/>
135+
<association id="17">
136+
<end type="SOURCE" refId="2" navigable="false">
137+
<attribute id="18" name="selector">
138+
<position height="0" width="0" x="0" y="0"/>
139+
</attribute>
140+
<multiplicity id="19" minimum="0" maximum="1">
141+
<position height="0" width="0" x="0" y="0"/>
142+
</multiplicity>
149143
</end>
150-
<end type="TARGET" refId="2" navigable="true"/>
144+
<end type="TARGET" refId="1" navigable="true"/>
151145
<display labels="true" multiplicity="true"/>
152146
</association>
153-
<dependency id="22">
147+
<dependency id="20">
154148
<end type="SOURCE" refId="8"/>
155149
<end type="TARGET" refId="10"/>
156150
</dependency>
157-
<generalization id="23">
151+
<generalization id="21">
158152
<end type="SOURCE" refId="7"/>
159153
<end type="TARGET" refId="6"/>
160154
</generalization>
155+
<realization id="22">
156+
<bendpoint x="309" y="348"/>
157+
<end type="SOURCE" refId="4"/>
158+
<end type="TARGET" refId="3"/>
159+
</realization>
160+
<dependency id="23">
161+
<end type="SOURCE" refId="13"/>
162+
<end type="TARGET" refId="12"/>
163+
</dependency>
161164
<association id="24">
162165
<end type="SOURCE" refId="6" navigable="false">
163-
<attribute id="25" name="handler"/>
164-
<multiplicity id="26" minimum="0" maximum="1"/>
166+
<attribute id="25" name="handler">
167+
<position height="0" width="0" x="0" y="0"/>
168+
</attribute>
169+
<multiplicity id="26" minimum="0" maximum="1">
170+
<position height="0" width="0" x="0" y="0"/>
171+
</multiplicity>
165172
</end>
166173
<end type="TARGET" refId="11" navigable="true"/>
167174
<display labels="true" multiplicity="true"/>
168175
</association>
169-
<association id="27">
170-
<end type="SOURCE" refId="6" navigable="false">
171-
<attribute id="28" name="reactor"/>
172-
<multiplicity id="29" minimum="0" maximum="1"/>
173-
</end>
174-
<end type="TARGET" refId="2" navigable="true"/>
175-
<display labels="true" multiplicity="true"/>
176-
</association>
177-
<dependency id="30">
178-
<end type="SOURCE" refId="13"/>
179-
<end type="TARGET" refId="12"/>
176+
<dependency id="27">
177+
<end type="SOURCE" refId="7"/>
178+
<end type="TARGET" refId="9"/>
180179
</dependency>
181-
<association id="31">
180+
<generalization id="28">
181+
<end type="SOURCE" refId="8"/>
182+
<end type="TARGET" refId="6"/>
183+
</generalization>
184+
<association id="29">
182185
<end type="SOURCE" refId="2" navigable="false">
183-
<attribute id="32" name="selector"/>
184-
<multiplicity id="33" minimum="0" maximum="1"/>
186+
<attribute id="30" name="dispatcher">
187+
<position height="0" width="0" x="0" y="0"/>
188+
</attribute>
189+
<multiplicity id="31" minimum="0" maximum="1">
190+
<position height="0" width="0" x="0" y="0"/>
191+
</multiplicity>
185192
</end>
186-
<end type="TARGET" refId="1" navigable="true"/>
193+
<end type="TARGET" refId="3" navigable="true"/>
187194
<display labels="true" multiplicity="true"/>
188195
</association>
189-
<realization id="34">
196+
<realization id="32">
190197
<end type="SOURCE" refId="12"/>
191198
<end type="TARGET" refId="11"/>
192199
</realization>
193-
<generalization id="35">
194-
<end type="SOURCE" refId="8"/>
195-
<end type="TARGET" refId="6"/>
196-
</generalization>
197-
<dependency id="36">
198-
<end type="SOURCE" refId="7"/>
199-
<end type="TARGET" refId="9"/>
200-
</dependency>
200+
<association id="33">
201+
<bendpoint x="460" y="203"/>
202+
<end type="SOURCE" refId="13" navigable="false">
203+
<attribute id="34" name="reactor">
204+
<position height="0" width="0" x="0" y="0"/>
205+
</attribute>
206+
<multiplicity id="35" minimum="0" maximum="1">
207+
<position height="0" width="0" x="0" y="0"/>
208+
</multiplicity>
209+
</end>
210+
<end type="TARGET" refId="2" navigable="true"/>
211+
<display labels="true" multiplicity="true"/>
212+
</association>
213+
<association id="36">
214+
<end type="SOURCE" refId="6" navigable="false">
215+
<attribute id="37" name="reactor">
216+
<position height="0" width="0" x="0" y="0"/>
217+
</attribute>
218+
<multiplicity id="38" minimum="0" maximum="1">
219+
<position height="0" width="0" x="0" y="0"/>
220+
</multiplicity>
221+
</end>
222+
<end type="TARGET" refId="2" navigable="true"/>
223+
<display labels="true" multiplicity="true"/>
224+
</association>
225+
<realization id="39">
226+
<end type="SOURCE" refId="5"/>
227+
<end type="TARGET" refId="3"/>
228+
</realization>
201229
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
202230
sort-features="false" accessors="true" visibility="true">
203231
<attributes public="true" package="true" protected="true" private="true" static="true"/>

reactor/src/main/java/com/iluwatar/reactor/app/App.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,31 @@ public class App {
6666

6767
private NioReactor reactor;
6868
private List<AbstractNioChannel> channels = new ArrayList<>();
69+
private Dispatcher dispatcher;
70+
71+
/**
72+
* Creates an instance of App which will use provided dispatcher for dispatching events on reactor.
73+
* @param dispatcher the dispatcher that will be used to dispatch events.
74+
*/
75+
public App(Dispatcher dispatcher) {
76+
this.dispatcher = dispatcher;
77+
}
6978

7079
/**
7180
* App entry.
7281
*
7382
* @throws IOException
7483
*/
7584
public static void main(String[] args) throws IOException {
76-
new App().start(new ThreadPoolDispatcher(2));
85+
new App(new ThreadPoolDispatcher(2)).start();
7786
}
7887

7988
/**
8089
* Starts the NIO reactor.
81-
* @param threadPoolDispatcher
82-
*
90+
*
8391
* @throws IOException if any channel fails to bind.
8492
*/
85-
public void start(Dispatcher dispatcher) throws IOException {
93+
public void start() throws IOException {
8694
/*
8795
* The application can customize its event dispatching mechanism.
8896
*/
@@ -110,8 +118,9 @@ public void start(Dispatcher dispatcher) throws IOException {
110118
*/
111119
public void stop() throws InterruptedException, IOException {
112120
reactor.stop();
121+
dispatcher.stop();
113122
for (AbstractNioChannel channel : channels) {
114-
channel.getChannel().close();
123+
channel.getJavaChannel().close();
115124
}
116125
}
117126

reactor/src/main/java/com/iluwatar/reactor/framework/AbstractNioChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void setReactor(NioReactor reactor) {
4848
/**
4949
* @return the wrapped NIO channel.
5050
*/
51-
public SelectableChannel getChannel() {
51+
public SelectableChannel getJavaChannel() {
5252
return channel;
5353
}
5454

reactor/src/main/java/com/iluwatar/reactor/framework/NioDatagramChannel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public DatagramPacket read(SelectionKey key) throws IOException {
6464
* @return the underlying datagram channel.
6565
*/
6666
@Override
67-
public DatagramChannel getChannel() {
68-
return (DatagramChannel) super.getChannel();
67+
public DatagramChannel getJavaChannel() {
68+
return (DatagramChannel) super.getJavaChannel();
6969
}
7070

7171
/**
@@ -75,8 +75,8 @@ public DatagramChannel getChannel() {
7575
*/
7676
@Override
7777
public void bind() throws IOException {
78-
getChannel().socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
79-
getChannel().configureBlocking(false);
78+
getJavaChannel().socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
79+
getJavaChannel().configureBlocking(false);
8080
System.out.println("Bound UDP socket at port: " + port);
8181
}
8282

@@ -87,7 +87,7 @@ public void bind() throws IOException {
8787
@Override
8888
protected void doWrite(Object pendingWrite, SelectionKey key) throws IOException {
8989
DatagramPacket pendingPacket = (DatagramPacket) pendingWrite;
90-
getChannel().send(pendingPacket.getData(), pendingPacket.getReceiver());
90+
getJavaChannel().send(pendingPacket.getData(), pendingPacket.getReceiver());
9191
}
9292

9393
/**

reactor/src/main/java/com/iluwatar/reactor/framework/NioReactor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ public void start() throws IOException {
7474
* Stops the reactor and related resources such as dispatcher.
7575
*
7676
* @throws InterruptedException if interrupted while stopping the reactor.
77+
* @throws IOException if any I/O error occurs.
7778
*/
78-
public void stop() throws InterruptedException {
79+
public void stop() throws InterruptedException, IOException {
7980
reactorMain.shutdownNow();
8081
selector.wakeup();
8182
reactorMain.awaitTermination(4, TimeUnit.SECONDS);
82-
dispatcher.stop();
83+
selector.close();
8384
}
8485

8586
/**
@@ -94,7 +95,7 @@ public void stop() throws InterruptedException {
9495
* @throws IOException if any I/O error occurs.
9596
*/
9697
public NioReactor registerChannel(AbstractNioChannel channel) throws IOException {
97-
SelectionKey key = channel.getChannel().register(selector, channel.getInterestedOps());
98+
SelectionKey key = channel.getJavaChannel().register(selector, channel.getInterestedOps());
9899
key.attach(channel);
99100
channel.setReactor(this);
100101
return this;

reactor/src/main/java/com/iluwatar/reactor/framework/NioServerSocketChannel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public int getInterestedOps() {
4343
* @return the underlying {@link ServerSocketChannel}.
4444
*/
4545
@Override
46-
public ServerSocketChannel getChannel() {
47-
return (ServerSocketChannel) super.getChannel();
46+
public ServerSocketChannel getJavaChannel() {
47+
return (ServerSocketChannel) super.getJavaChannel();
4848
}
4949

5050
/**
@@ -71,8 +71,8 @@ public ByteBuffer read(SelectionKey key) throws IOException {
7171
*/
7272
@Override
7373
public void bind() throws IOException {
74-
((ServerSocketChannel) getChannel()).socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
75-
((ServerSocketChannel) getChannel()).configureBlocking(false);
74+
((ServerSocketChannel) getJavaChannel()).socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
75+
((ServerSocketChannel) getJavaChannel()).configureBlocking(false);
7676
System.out.println("Bound TCP socket at port: " + port);
7777
}
7878

reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class AppTest {
2222
*/
2323
@Test
2424
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
25-
App app = new App();
26-
app.start(new ThreadPoolDispatcher(2));
25+
App app = new App(new ThreadPoolDispatcher(2));
26+
app.start();
2727

2828
AppClient client = new AppClient();
2929
client.start();
@@ -48,8 +48,8 @@ public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedEx
4848
*/
4949
@Test
5050
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
51-
App app = new App();
52-
app.start(new SameThreadDispatcher());
51+
App app = new App(new SameThreadDispatcher());
52+
app.start();
5353

5454
AppClient client = new AppClient();
5555
client.start();

0 commit comments

Comments
 (0)