Package com.peterhi

Source Code of com.peterhi.ServerApp

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();
          }
        }
      }
    });
  }
}
TOP

Related Classes of com.peterhi.ServerApp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.