View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * Tests for issue with Datagram sessions (DIRMINA-1172)
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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                  // TODO Auto-generated method stub
65                  
66              }
67              
68              
69              @Override
70              public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
71              {
72                  // TODO Auto-generated method stub
73                  
74              }
75              
76              
77              @Override
78              public void sessionCreated( IoSession session ) throws Exception
79              {
80                  // TODO Auto-generated method stub
81                  
82              }
83              
84              
85              @Override
86              public void sessionClosed( IoSession session ) throws Exception
87              {
88                  // TODO Auto-generated method stub
89                  
90              }
91              
92              
93              @Override
94              public void messageSent( IoSession session, Object message ) throws Exception
95              {
96                  // TODO Auto-generated method stub
97                  
98              }
99              
100             
101             @Override
102             public void messageReceived( IoSession session, Object message ) throws Exception
103             {
104                 // TODO Auto-generated method stub
105                 System.out.println( "1"+session );
106                 
107             }
108             
109             
110             @Override
111             public void inputClosed( IoSession session ) throws Exception
112             {
113                 // TODO Auto-generated method stub
114                 
115             }
116             
117             
118             @Override
119             public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
120             {
121                 // TODO Auto-generated method stub
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                 // TODO Auto-generated method stub
139                 
140             }
141             
142             
143             @Override
144             public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
145             {
146                 // TODO Auto-generated method stub
147                 
148             }
149             
150             
151             @Override
152             public void sessionCreated( IoSession session ) throws Exception
153             {
154                 // TODO Auto-generated method stub
155                 
156             }
157             
158             
159             @Override
160             public void sessionClosed( IoSession session ) throws Exception
161             {
162                 // TODO Auto-generated method stub
163                 
164             }
165             
166             
167             @Override
168             public void messageSent( IoSession session, Object message ) throws Exception
169             {
170                 // TODO Auto-generated method stub
171                 
172             }
173             
174             
175             @Override
176             public void messageReceived( IoSession session, Object message ) throws Exception
177             {
178                 // TODO Auto-generated method stub
179                 System.out.println( "2:"+session );
180                 
181             }
182             
183             
184             @Override
185             public void inputClosed( IoSession session ) throws Exception
186             {
187                 // TODO Auto-generated method stub
188                 
189             }
190             
191             
192             @Override
193             public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
194             {
195                 // TODO Auto-generated method stub
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             //log.error("Failed to connect {}", e);
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 }