1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.socket.nio;
21
22 import java.io.IOException;
23 import java.net.DatagramPacket;
24 import java.net.DatagramSocket;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27
28 import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
29 import org.apache.mina.core.service.AbstractIoService;
30 import org.apache.mina.core.service.IoHandler;
31 import org.apache.mina.core.session.IdleStatus;
32 import org.apache.mina.core.session.IoSession;
33 import org.apache.mina.filter.logging.LoggingFilter;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37
38
39
40
41
42
43 public class DIRMINA1172Test
44 {
45 private static DatagramSocket socket;
46 private static InetAddress address;
47 private static byte[] buf;
48
49 @Before
50 public void init()
51 {
52 AbstractIoService inputSource1 = new NioDatagramAcceptor();
53 ((NioDatagramAcceptor) inputSource1).getSessionConfig().setReuseAddress(true);
54 DefaultIoFilterChainBuilder filterChainBuilderUDP = ((NioDatagramAcceptor)inputSource1).getFilterChain();
55 filterChainBuilderUDP.addLast("logger", new LoggingFilter());
56
57 ((NioDatagramAcceptor) inputSource1).getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 100000);
58 ((NioDatagramAcceptor) inputSource1).setHandler( new IoHandler()
59 {
60
61 @Override
62 public void sessionOpened( IoSession session ) throws Exception
63 {
64
65
66 }
67
68
69 @Override
70 public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
71 {
72
73
74 }
75
76
77 @Override
78 public void sessionCreated( IoSession session ) throws Exception
79 {
80
81
82 }
83
84
85 @Override
86 public void sessionClosed( IoSession session ) throws Exception
87 {
88
89
90 }
91
92
93 @Override
94 public void messageSent( IoSession session, Object message ) throws Exception
95 {
96
97
98 }
99
100
101 @Override
102 public void messageReceived( IoSession session, Object message ) throws Exception
103 {
104
105 System.out.println( "1"+session );
106
107 }
108
109
110 @Override
111 public void inputClosed( IoSession session ) throws Exception
112 {
113
114
115 }
116
117
118 @Override
119 public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
120 {
121
122
123 }
124 });
125
126 AbstractIoService inputSource2 = new NioDatagramAcceptor();
127 ((NioDatagramAcceptor) inputSource2).getSessionConfig().setReuseAddress(true);
128 DefaultIoFilterChainBuilder filterChainBuilderUDP2 = ((NioDatagramAcceptor)inputSource2).getFilterChain();
129 filterChainBuilderUDP2.addLast("logger", new LoggingFilter());
130
131 ((NioDatagramAcceptor) inputSource2).getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 100000);
132 ((NioDatagramAcceptor) inputSource2).setHandler( new IoHandler()
133 {
134
135 @Override
136 public void sessionOpened( IoSession session ) throws Exception
137 {
138
139
140 }
141
142
143 @Override
144 public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
145 {
146
147
148 }
149
150
151 @Override
152 public void sessionCreated( IoSession session ) throws Exception
153 {
154
155
156 }
157
158
159 @Override
160 public void sessionClosed( IoSession session ) throws Exception
161 {
162
163
164 }
165
166
167 @Override
168 public void messageSent( IoSession session, Object message ) throws Exception
169 {
170
171
172 }
173
174
175 @Override
176 public void messageReceived( IoSession session, Object message ) throws Exception
177 {
178
179 System.out.println( "2:"+session );
180
181 }
182
183
184 @Override
185 public void inputClosed( IoSession session ) throws Exception
186 {
187
188
189 }
190
191
192 @Override
193 public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
194 {
195
196
197 }
198 });
199
200 try {
201 ((NioDatagramAcceptor)inputSource1).bind(new InetSocketAddress(9800));
202 ((NioDatagramAcceptor)inputSource2).bind(new InetSocketAddress(9801));
203 } catch (IOException e) {
204
205 }
206 }
207
208 @Test
209 @Ignore
210 public void test() throws InterruptedException, IOException
211 {
212 socket = new DatagramSocket();
213 address = InetAddress.getByName("localhost");
214
215 int[] ports = new int[]{9800, 9801};
216
217 while(true) {
218
219 for (int port : ports ) {
220 String msg = "TEST_" + port + " " + String.valueOf(System.currentTimeMillis());
221 buf = msg.getBytes();
222 DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
223 socket.send(packet);
224 System.out.println("Send: " + msg);
225 }
226
227 Thread.sleep(5000);
228 }
229 }
230 }