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.example.udp.perf;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.future.ConnectFuture;
26  import org.apache.mina.core.service.IoConnector;
27  import org.apache.mina.core.service.IoHandlerAdapter;
28  import org.apache.mina.core.session.IdleStatus;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.mina.transport.socket.nio.NioDatagramConnector;
31  
32  /**
33   * An UDP client taht just send thousands of small messages to a UdpServer. 
34   * 
35   * This class is used for performance test purposes. It does nothing at all, but send a message
36   * repetitly to a server.
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class UdpClient extends IoHandlerAdapter {
41      /** The connector */
42      private IoConnector connector;
43  
44      /** The session */
45      private static IoSession session;
46  
47      /**
48       * Create the UdpClient's instance
49       */
50      public UdpClient() {
51          connector = new NioDatagramConnector();
52  
53          connector.setHandler(this);
54  
55          ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", UdpServer.PORT));
56  
57          connFuture.awaitUninterruptibly();
58  
59          session = connFuture.getSession();
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
67          cause.printStackTrace();
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public void messageReceived(IoSession session, Object message) throws Exception {
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void messageSent(IoSession session, Object message) throws Exception {
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void sessionClosed(IoSession session) throws Exception {
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void sessionCreated(IoSession session) throws Exception {
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void sessionOpened(IoSession session) throws Exception {
110     }
111 
112     /**
113      * The main method : instanciates a client, and send N messages. We sleep 
114      * between each K messages sent, to avoid the server saturation.
115      * @param args The arguments
116      * @throws Exception If something went wrong
117      */
118     public static void main(String[] args) throws Exception {
119         UdpClientdp/perf/UdpClient.html#UdpClient">UdpClient client = new UdpClient();
120 
121         long t0 = System.currentTimeMillis();
122 
123         for (int i = 0; i <= UdpServer.MAX_RECEIVED; i++) {
124             Thread.sleep(1);
125 
126             String str = Integer.toString(i);
127             byte[] data = str.getBytes();
128             IoBuffer buffer = IoBuffer.allocate(data.length);
129             buffer.put(data);
130             buffer.flip();
131             session.write(buffer);
132 
133             if (i % 10000 == 0) {
134                 System.out.println("Sent " + i + " messages");
135             }
136         }
137 
138         long t1 = System.currentTimeMillis();
139 
140         System.out.println("Sent messages delay : " + (t1 - t0));
141 
142         Thread.sleep(100000);
143 
144         client.connector.dispose(true);
145     }
146 }