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.examples.coap;
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.nio.ByteBuffer;
26  import java.nio.channels.DatagramChannel;
27  import java.util.Random;
28  
29  import org.apache.mina.coap.CoapCode;
30  import org.apache.mina.coap.CoapMessage;
31  import org.apache.mina.coap.CoapOption;
32  import org.apache.mina.coap.CoapOptionType;
33  import org.apache.mina.coap.MessageType;
34  import org.apache.mina.coap.codec.CoapDecoder;
35  import org.apache.mina.coap.codec.CoapEncoder;
36  
37  public class BlockingGetClient {
38  
39      public static void main(String[] args) {
40          try {
41              DatagramChannel channel = DatagramChannel.open();
42  
43              InetSocketAddress target = new InetSocketAddress("127.0.0.1", 5683);
44  
45              channel.connect(target);
46  
47              System.err.println(channel);
48  
49              CoapEncoder encoder = new CoapEncoder();
50              CoapDecoder decoder = new CoapDecoder();
51              ByteBuffer buff = ByteBuffer.allocateDirect(2048);
52  
53              Random r = new Random();
54              byte[] url = "nlp".getBytes();
55              CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 1234, null,
56                      new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, url) }, null);
57  
58              for (int j = 0; j < 8; j++) {
59                  long start = System.currentTimeMillis();
60                  final int count = 100000;
61                  for (int i = 0; i < count; i++) {
62                      buff.position(0);
63                      buff.limit(buff.capacity());
64                      int id = r.nextInt(1024);
65                      msg.setId(id);
66                      int bytes = channel.send(encoder.encode(msg, null), target);
67  
68                      if (bytes < 1) {
69                          System.err.println("write fail :/ " + bytes);
70                      } else {
71                          buff.position(0);
72                          buff.limit(buff.capacity());
73                          SocketAddress addy = channel.receive(buff);
74                          buff.flip();
75                          CoapMessage response = decoder.decode(buff, null);
76                          if (response.getId() != id) {
77                              System.err.println("gni?");
78                          }
79                          // System.err.println("response : " + response);
80                      }
81                  }
82                  System.err.println("time : " + (System.currentTimeMillis() - start) + "ms");
83              }
84  
85          } catch (IOException e) {
86              e.printStackTrace();
87          }
88      }
89  }