private boolean updateClient(Client c) {
    if (c.isClosed()) {
      listener.onClientClose(this, c, null);

      return false;
    }

    try {
      c.read();
      c.update();

      listener.onClientUpdate(this, c);

      if (c.isReadyToSend()) {
        c.send();
      }

      return true;
    } catch (Exception e) {
      listener.onClientClose(this, c, e);

      c.close();

      return false;
    }
  }
Example #2
0
  public ServerManager() {
    stor = new SendableStorage();
    sen = new DataSender(stor, this, 10000);
    sen.startSending();

    rlist = new ArrayList<SocketReciever>();
    clist = new ArrayList<Connection>();
    try {
      ServerSocket s = new ServerSocket(1337);
      l = new ServerListener(s, this);
      l.startListening();
      System.out.println("Started successfully -.-");
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      boolean r = true;
      while (r) {
        String line = in.readLine();
        if (line != null) {
          // interprete
          if (line.equals("quit")) {
            l.stopListening();
            r = false;
          } else {
            // send to all clients
            // sendToAll(line);
            String[] interp = line.split(",");
            String name = interp[0];
            int posx, posy;
            if (interp.length == 3) {
              try {
                posx = Integer.parseInt(interp[1]);
                posy = Integer.parseInt(interp[2]);
              } catch (NumberFormatException ex) {
                System.out.println("Wrong input. Must be name,posx,posy");
                continue;
              }
            } else {
              posx = 0;
              posy = 0;
            }

            TestPhysObject to = new TestPhysObject(name, posx, posy);
            System.out.println("enqueuing " + to.toString());
            stor.add(to);
          }
        } else {
          r = false;
        }
      }
      l.stopListening();
      sen.stopSending();
    } catch (IOException ex) {
      System.out.println(ex);
      ex.printStackTrace(System.err);
    }
  }
  private boolean initializeClient(Client c) {
    try {
      c.init();
      c.setUpdateRate(clientUpdateRate);

      listener.onClientConnect(this, c);
      clients.add(c);

      return true;
    } catch (Exception e) {
      listener.onClientFailedConnect(this, c, e);

      return false;
    }
  }
  @Override
  public void update() throws IOException {
    if (isNotReady()) {
      return;
    }

    listener.onUpdateBegin(this);

    boolean sleep = true;

    try {
      Client acceptedClient = null;

      while ((acceptedClient = tryAccept()) != null) {
        if (initializeClient(acceptedClient)) {
          sleep = false;
        }
      }
    } catch (Exception e) {
      listener.onAcceptError(this, e);
    }

    Iterator<Client> iter = clients.iterator();

    while (iter.hasNext()) {
      Client c = iter.next();

      if (!updateClient(c)) {
        iter.remove();
      } else if ((c.getPacketsRead() > 0) || (c.getCallsSent() > 0)) {
        sleep = false;
      }
    }

    listener.onUpdateEnd(this);

    if (sleep && idleSleepMillis > 0) {
      try {
        Thread.sleep(idleSleepMillis);
      } catch (Exception e) {
        // ignore
      }
    }
  }
Example #5
0
  @Override
  public void run() {
    ServerListener servListener = new ServerListener();
    try {
      inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      outStream =
          new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())), true);
      BufferedReader kbRdr = new BufferedReader(new InputStreamReader(System.in));
      servListener.start();

      while (true) {
        outStream.println(kbRdr.readLine());
      }

    } catch (IOException e) {
      System.out.println("Server closed");
      // e.printStackTrace();
    } finally {
      servListener.interrupt();
    }
  }
 // reading a message
 public void run() {
   try {
     String line = br.readLine();
     while (line != null) {
       FactoryServerGUI.addMessage(
           socket.getInetAddress() + ":" + socket.getPort() + " - " + line);
       line = br.readLine();
     }
   } catch (IOException ioe) {
     serverListener.removeServerClientCommunicator(this);
     FactoryServerGUI.addMessage(
         socket.getInetAddress() + ":" + socket.getPort() + " - " + Constants.clientDisconnected);
     // this means that the socket is closed since no more lines are being received
     try {
       socket.close();
     } catch (IOException ioe1) {
       Util.printExceptionToCommand(ioe1);
     }
   }
 }
 @Override
 public void call(ServerListener listener) {
   listener.serverClosing(this);
 }
Example #8
0
 /** Invokes stop event to all listeners. */
 public void invokeStop() {
   for (ServerListener sl : serverListeners) {
     sl.onServerStop(this);
   }
 }
Example #9
0
 /** Invokes error event to all listeners. */
 public void invokeError(Exception e) {
   for (ServerListener sl : serverListeners) {
     sl.onServerError(this, e);
   }
 }
Example #10
0
 /** Invokes accept event to all listeners. */
 public void invokeAccept(Client client) {
   for (ServerListener sl : serverListeners) {
     sl.onServerAccept(this, client);
   }
 }
Example #11
0
 /** Invokes bind event to all listeners. */
 public void invokeBind(SocketAddress address) {
   for (ServerListener sl : serverListeners) {
     sl.onServerBind(this, address);
   }
 }