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.core.nio.udp;
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.nio.ByteBuffer;
25  import java.util.concurrent.CountDownLatch;
26  import java.util.concurrent.ExecutionException;
27  
28  import org.apache.mina.api.IdleStatus;
29  import org.apache.mina.api.IoFuture;
30  import org.apache.mina.api.IoHandler;
31  import org.apache.mina.api.IoService;
32  import org.apache.mina.api.IoSession;
33  import org.apache.mina.core.BenchmarkClient;
34  import org.apache.mina.transport.nio.NioUdpClient;
35  
36  /**
37   * A MINA 3 based UDP client
38   * 
39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
40   */
41  public class Mina3UdpBenchmarkClient implements BenchmarkClient {
42      // The UDP client
43      private NioUdpClient udpClient;
44  
45      /**
46       * {@inheritDoc}
47       */
48      public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
49          udpClient = new NioUdpClient();
50          udpClient.setIoHandler(new IoHandler() {
51              private void sendMessage(IoSession session, byte[] data) {
52                  ByteBuffer iobuf = ByteBuffer.wrap(data);
53                  session.write(iobuf);
54              }
55  
56              public void sessionOpened(IoSession session) {
57                  sendMessage(session, data);
58              }
59  
60              public void messageReceived(IoSession session, Object message) {
61                  if (message instanceof ByteBuffer) {
62                      ByteBuffer buffer = (ByteBuffer) message;
63                      for (int i = 0; i < buffer.remaining(); ++i) {
64                          counter.countDown();
65                          long count = counter.getCount();
66                          if (count > 0) {
67                              sendMessage(session, data);
68                          }
69                      }
70                  }
71              }
72  
73              public void exceptionCaught(IoSession session, Exception cause) {
74                  cause.printStackTrace();
75              }
76  
77              @Override
78              public void sessionClosed(IoSession session) {
79              }
80  
81              @Override
82              public void sessionIdle(IoSession session, IdleStatus status) {
83              }
84  
85              @Override
86              public void messageSent(IoSession session, Object message) {
87              }
88  
89              @Override
90              public void serviceActivated(IoService service) {
91              }
92  
93              @Override
94              public void serviceInactivated(IoService service) {
95              }
96          });
97  
98          IoFuture<IoSession> future = udpClient.connect(new InetSocketAddress(port));
99  
100         try {
101             IoSession session = future.get();
102         } catch (InterruptedException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         } catch (ExecutionException e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         }
109     }
110 
111     /**
112      * {@inheritedDoc}
113      */
114     public void stop() throws IOException {
115     }
116 }