Exemplo n.º 1
0
  public static void listen(StringTokenizer st) {
    try {
      if (port[0] == -1) {
        if (st.hasMoreTokens()) {

          try {
            port[0] = Integer.parseInt(st.nextToken());
          } catch (NumberFormatException nfe) {
            System.err.println("listen: incorrect port name");
            return;
          }

          if (port[0] < 0 || port[0] > 65535) {
            System.err.println("listen: port name is out of range 0 - 65535");
            port[0] = -1;
            return;
          }

          if (!serverSocket.isOpen()) {
            serverSocket = ServerSocketChannel.open();
            serverSocket.configureBlocking(false);
          }
          serverSocket.socket().bind(new InetSocketAddress(port[0]));
          serverSocket.register(selector, SelectionKey.OP_ACCEPT);
        } else {
          System.err.println("listen: no port name");
        }
      } else {
        System.err.println("Error: already listening port " + port[0]);
      }
    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }
Exemplo n.º 2
0
 public static String getMessageFromTokens(StringTokenizer st) {
   StringBuilder sb = new StringBuilder();
   while (st.hasMoreTokens()) {
     sb.append(st.nextToken()).append(" ");
   }
   return sb.toString();
 }
Exemplo n.º 3
0
  public static void kill(StringTokenizer st) {
    try {
      if (st.hasMoreTokens()) {
        String clientName = st.nextToken();

        if (clients.containsKey(clientName)) {
          SocketChannel clientChannel = clients.get(clientName);
          clients.remove(clientName);
          sendMessage(clientChannel, MessageUtils.bye());
          notifyAllClients(clientName + " leave this chatroom");
          System.out.println(clientName + " leave this chatroom");
          Utils.tryClose(clientChannel);
        } else {
          System.err.println("kill: no such client");
        }
      } else {
        System.err.println("kill: no client name");
      }
    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }
Exemplo n.º 4
0
  public static void readCommand() {
    try {

      if (!reader.ready()) {
        return;
      }

      StringTokenizer tokens = new StringTokenizer(reader.readLine());

      if (tokens.hasMoreTokens()) {

        switch (tokens.nextToken()) {
          case "/listen":
            listen(tokens);
            break;

          case "/stop":
            stop();
            break;

          case "/list":
            if (port[0] == -1) {
              System.err.println("Error: start listening before");
            }

            for (Entry<String, SocketChannel> client : clients.entrySet()) {
              System.out.println(client.getKey());
            }

            if (clients.entrySet().isEmpty()) {
              System.out.println("list: no clients online");
            }
            break;

          case "/send":
            if (tokens.hasMoreTokens()) {
              String name = tokens.nextToken();
              if (clients.containsKey(name)) {
                if (tokens.hasMoreTokens()) {
                  sendMessage(
                      clients.get(name),
                      MessageUtils.message("<server>", getMessageFromTokens(tokens)));
                } else {
                  System.err.println("send: message is empty");
                }
              } else {
                System.err.println("send: no such client");
              }
            } else {
              System.err.println("send: no client name");
            }
            break;

          case "/sendall":
            if (tokens.hasMoreTokens()) {
              notifyAllClients(getMessageFromTokens(tokens));
            } else {
              System.err.println("sendall: message is empty");
            }
            break;

          case "/kill":
            kill(tokens);
            break;

          case "/exit":
            if (port[0] != -1) {
              stop();
            }
            Utils.tryClose(selector);
            System.exit(0);
            break;

          default:
            System.err.print("Error: unknown command");
            break;
        }

      } else {
        System.err.println("Error: empty input");
      }

    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }