package com.peterhi;
import java.net.DatagramPacket;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class ServerApp {
public static void main(String[] args) throws Exception {
ScheduledExecutorService receiver = Executors.newSingleThreadScheduledExecutor();
ScheduledExecutorService sender = Executors.newSingleThreadScheduledExecutor();
final LocalEndpoint endpoint = new LocalEndpoint(22222);
endpoint.addEndpointListener((e) -> {
System.out.println(e.getData().length + " bytes of " +
(e.isReliable() ? "reliable" : "unreliable") + " data received. " + System.currentTimeMillis());
});
receiver.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (true) {
try {
DatagramPacket datagram = endpoint.doReceive();
if (datagram != null) {
endpoint.doProcess(datagram);
} else {
Thread.sleep(10);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
sender.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (true) {
try {
endpoint.doDispatch();
Thread.sleep(10);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
}
}