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.bio.udp;
21
22 import java.io.IOException;
23 import java.net.DatagramPacket;
24 import java.net.DatagramSocket;
25 import java.net.InetAddress;
26 import java.util.concurrent.CountDownLatch;
27
28 import org.apache.mina.core.BenchmarkClient;
29
30 /**
31 * A client that uses a BIO datagram to communicate with the server
32 *
33 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34 */
35 public class BioUdpBenchmarkClient implements BenchmarkClient {
36 // The UDP client
37 private DatagramSocket sender;
38
39 /**
40 * {@inheritDoc}
41 */
42 public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
43 InetAddress serverAddress = InetAddress.getLocalHost();
44 byte[] buffer = new byte[65507];
45 sender = new DatagramSocket(port + 1);
46
47 DatagramPacket pduSent = new DatagramPacket(data, data.length, serverAddress, port);
48 DatagramPacket pduReceived = new DatagramPacket(buffer, data.length);
49 sender.send(pduSent);
50
51 boolean done = false;
52
53 while (!done) {
54 try {
55 sender.receive(pduReceived);
56
57 for (int i = 0; i < pduReceived.getLength(); ++i) {
58 counter.countDown();
59
60 if (counter.getCount() > 0) {
61 sender.send(pduSent);
62 break;
63 } else {
64 done = true;
65 }
66 }
67 } catch (IOException ioe) {
68 // Nothing to do
69 }
70 }
71
72 sender.close();
73 }
74
75 /**
76 * {@inheritedDoc}
77 */
78 public void stop() throws IOException {
79 }
80 }