Example #1
0
 @Test
 public void testBroadcast() {
   peer1 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true));
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions().setBroadcast(true));
   peer2.exceptionHandler(t -> fail(t.getMessage()));
   peer2.listen(
       1234,
       "0.0.0.0",
       ar1 -> {
         assertTrue(ar1.succeeded());
         Buffer buffer = TestUtils.randomBuffer(128);
         peer2.packetHandler(
             packet -> {
               assertEquals(buffer, packet.data());
               testComplete();
             });
         peer1.send(
             buffer,
             1234,
             "255.255.255.255",
             ar2 -> {
               assertTrue(ar2.succeeded());
             });
       });
   await();
 }
Example #2
0
 @Test
 public void testSendAfterCloseFails() {
   peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer1.close(
       ar -> {
         assertTrue(ar.succeeded());
         peer1.send(
             "Test",
             1234,
             "127.0.0.1",
             ar2 -> {
               assertTrue(ar2.failed());
               peer1 = null;
               peer2.close(
                   ar3 -> {
                     assertTrue(ar3.succeeded());
                     peer2.send(
                         "Test",
                         1234,
                         "127.0.0.1",
                         ar4 -> {
                           assertTrue(ar4.failed());
                           peer2 = null;
                           testComplete();
                         });
                   });
             });
       });
   await();
 }
Example #3
0
 @Test
 public void testEcho() {
   peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer1.exceptionHandler(t -> fail(t.getMessage()));
   peer2.exceptionHandler(t -> fail(t.getMessage()));
   peer2.listen(
       1234,
       "127.0.0.1",
       ar -> {
         assertTrue(ar.succeeded());
         Buffer buffer = TestUtils.randomBuffer(128);
         peer2.packetHandler(
             packet -> {
               assertEquals("127.0.0.1", packet.sender().hostAddress());
               assertEquals(1235, packet.sender().hostPort());
               assertEquals(buffer, packet.data());
               peer2.send(packet.data(), 1235, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
             });
         peer1.listen(
             1235,
             "127.0.0.1",
             ar2 -> {
               peer1.packetHandler(
                   packet -> {
                     assertEquals(buffer, packet.data());
                     assertEquals("127.0.0.1", packet.sender().hostAddress());
                     assertEquals(1234, packet.sender().hostPort());
                     testComplete();
                   });
               peer1.send(buffer, 1234, "127.0.0.1", ar3 -> assertTrue(ar3.succeeded()));
             });
       });
   await();
 }
Example #4
0
 @Test
 public void testListenSamePortMultipleTimes() {
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer2.listen(
       1234,
       "127.0.0.1",
       ar1 -> {
         assertTrue(ar1.succeeded());
         peer1.listen(
             1234,
             "127.0.0.1",
             ar2 -> {
               assertTrue(ar2.failed());
               testComplete();
             });
       });
   await();
 }
Example #5
0
 @Test
 public void testListenPort() {
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer2.listen(
       1234,
       "localhost",
       ar -> {
         assertTrue(ar.succeeded());
         testComplete();
       });
   await();
 }
Example #6
0
 protected void tearDown() throws Exception {
   if (peer1 != null) {
     CountDownLatch latch = new CountDownLatch(2);
     peer1.close(
         ar -> {
           assertTrue(ar.succeeded());
           latch.countDown();
           if (peer2 != null) {
             peer2.close(
                 ar2 -> {
                   assertTrue(ar2.succeeded());
                   latch.countDown();
                 });
           } else {
             latch.countDown();
           }
         });
     latch.await(10L, TimeUnit.SECONDS);
   }
   super.tearDown();
 }
Example #7
0
 @Test
 public void testListenInetSocketAddress() {
   peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer2.listen(
       1234,
       "127.0.0.1",
       ar -> {
         assertTrue(ar.succeeded());
         testComplete();
       });
   await();
 }
Example #8
0
 @Test
 public void testBroadcastFailsIfNotConfigured() {
   peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
   peer1.send(
       "test",
       1234,
       "255.255.255.255",
       ar -> {
         assertTrue(ar.failed());
         testComplete();
       });
   await();
 }
Example #9
0
 @Test
 public void testOptionsCopied() {
   DatagramSocketOptions options = new DatagramSocketOptions();
   options.setReuseAddress(true);
   peer1 = vertx.createDatagramSocket(options);
   peer2 = vertx.createDatagramSocket(options);
   // Listening on same address:port so will only work if reuseAddress = true
   // Set to false, but because options are copied internally should still work
   options.setReuseAddress(false);
   peer1.listen(
       1234,
       "127.0.0.1",
       ar -> {
         assertTrue(ar.succeeded());
         peer2.listen(
             1234,
             "127.0.0.1",
             ar2 -> {
               assertTrue(ar2.succeeded());
               testComplete();
             });
       });
   await();
 }
Example #10
0
  @Test
  public void testMulticastJoinLeave() throws Exception {
    Buffer buffer = TestUtils.randomBuffer(128);
    String groupAddress = "230.0.0.1";
    String iface = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1")).getName();
    AtomicBoolean received = new AtomicBoolean();
    peer1 =
        vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface));
    peer2 =
        vertx.createDatagramSocket(new DatagramSocketOptions().setMulticastNetworkInterface(iface));

    peer1.packetHandler(
        packet -> {
          assertEquals(buffer, packet.data());
          received.set(true);
        });

    peer1.listen(
        1234,
        "0.0.0.0",
        ar1 -> {
          assertTrue(ar1.succeeded());
          peer1.listenMulticastGroup(
              groupAddress,
              iface,
              null,
              ar2 -> {
                assertTrue(ar2.succeeded());
                peer2.send(
                    buffer,
                    1234,
                    groupAddress,
                    ar3 -> {
                      assertTrue(ar3.succeeded());
                      // leave group in 1 second so give it enough time to really receive the packet
                      // first
                      vertx.setTimer(
                          1000,
                          id -> {
                            peer1.unlistenMulticastGroup(
                                groupAddress,
                                iface,
                                null,
                                ar4 -> {
                                  assertTrue(ar4.succeeded());
                                  AtomicBoolean receivedAfter = new AtomicBoolean();
                                  peer1.packetHandler(
                                      packet -> {
                                        // Should not receive any more event as it left the group
                                        receivedAfter.set(true);
                                      });
                                  peer2.send(
                                      buffer,
                                      1234,
                                      groupAddress,
                                      ar5 -> {
                                        assertTrue(ar5.succeeded());
                                        // schedule a timer which will check in 1 second if we
                                        // received a message after the group
                                        // was left before
                                        vertx.setTimer(
                                            1000,
                                            id2 -> {
                                              assertFalse(receivedAfter.get());
                                              assertTrue(received.get());
                                              testComplete();
                                            });
                                      });
                                });
                          });
                    });
              });
        });
    await();
  }