Created
November 8, 2013 15:12
-
-
Save tobias/7372368 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.vertx.java.core.Handler; | |
import org.vertx.java.core.AsyncResult; | |
import org.vertx.java.core.AsyncResultHandler; | |
import org.vertx.java.core.datagram.DatagramPacket; | |
import org.vertx.java.core.datagram.DatagramSocket; | |
import org.vertx.java.core.datagram.InternetProtocolFamily; | |
import org.vertx.java.platform.Verticle; | |
import org.vertx.java.core.buffer.Buffer; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class McastTestVerticle extends Verticle { | |
public void start() { | |
final Buffer buffer = new Buffer("foo"); | |
final String groupAddress = "230.0.0.1"; | |
final AtomicInteger receivedCount = new AtomicInteger(0); | |
final DatagramSocket peer1 = vertx.createDatagramSocket(null); | |
final DatagramSocket peer2 = vertx.createDatagramSocket(InternetProtocolFamily.IPv4); | |
peer2.dataHandler(new Handler<DatagramPacket>() { | |
@Override | |
public void handle(DatagramPacket event) { | |
System.out.println("RCVD: " + event.data().toString()); | |
System.out.println("COUNT: " + receivedCount.getAndIncrement()); | |
} | |
}); | |
vertx.setPeriodic(100, new Handler<Long>() { | |
public void handle(Long ignored) { | |
System.out.println("receivedCount is " + receivedCount.get()); | |
peer1.send(buffer, groupAddress, 1234, null); | |
} | |
}); | |
peer2.listen("127.0.0.1", 1234, new AsyncResultHandler<DatagramSocket>() { | |
@Override | |
public void handle(AsyncResult<DatagramSocket> event) { | |
peer2.listenMulticastGroup(groupAddress, new AsyncResultHandler<DatagramSocket>() { | |
@Override | |
public void handle(AsyncResult<DatagramSocket> event) { | |
System.out.println("Joined group"); | |
//stay in the group for two seconds | |
vertx.setTimer(2000, new Handler<Long>() { | |
public void handle(Long ignored) { | |
System.out.println("Leaving group"); | |
peer2.unlistenMulticastGroup(groupAddress, null); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment