Esempio n. 1
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);
    }
  }