RLPark 1.0.0
Reinforcement Learning Framework in Java

Server.java

Go to the documentation of this file.
00001 package rlpark.plugin.robot.internal.disco;
00002 
00003 import java.io.IOException;
00004 import java.net.ServerSocket;
00005 import java.net.Socket;
00006 import java.nio.ByteOrder;
00007 
00008 import rlpark.plugin.robot.internal.disco.io.DiscoSocket;
00009 
00010 import zephyr.plugin.core.api.signals.Signal;
00011 
00012 public class Server {
00013   public Signal<DiscoSocket> onAccept = new Signal<DiscoSocket>();
00014   private final ServerSocket socket;
00015   public final int port;
00016   protected Thread acceptThread = null;
00017   private final ByteOrder byteOrder;
00018 
00019   public Server() throws IOException {
00020     this(0);
00021   }
00022 
00023   public Server(int port) throws IOException {
00024     this(port, ByteOrder.BIG_ENDIAN);
00025   }
00026 
00027   public Server(int port, ByteOrder byteOrder) throws IOException {
00028     socket = new ServerSocket(port);
00029     this.port = socket.getLocalPort();
00030     this.byteOrder = byteOrder;
00031   }
00032 
00033   public DiscoSocket accept() throws IOException {
00034     Socket clientSocket = socket.accept();
00035     return new DiscoSocket(clientSocket, byteOrder);
00036   }
00037 
00038   public void runAcceptThread() {
00039     assert acceptThread == null;
00040     acceptThread = new Thread() {
00041       @Override
00042       public void run() {
00043         while (acceptThread != null)
00044           try {
00045             onAccept.fire(accept());
00046           } catch (IOException e) {
00047             e.printStackTrace();
00048             break;
00049           }
00050         acceptThread = null;
00051       }
00052     };
00053     acceptThread.start();
00054   }
00055 
00056   public void close() throws IOException {
00057     acceptThread = null;
00058     socket.close();
00059   }
00060 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark