Exemple #1
0
  @Override
  public void update(Observable arg0, Object arg1) {
    if (arg1 instanceof String) {
      String message = arg1.toString();
      if (message.startsWith("Forwarding: ")) {
        ForwardButton.setText("Stop Forwarding");
        SetChannelButton.setEnabled(false);
        BlockButton.setEnabled(false);
        Send.setEnabled(false);
        MessageInput.setEnabled(false);
        MessageDisplay.setEnabled(false);
        Availability.setEnabled(false);
      } else if (message.startsWith("End Forwarding:")) {
        ForwardButton.setText("Forward");
        SetChannelButton.setEnabled(true);
        BlockButton.setEnabled(true);
        Send.setEnabled(true);
        MessageInput.setEnabled(true);
        MessageDisplay.setEnabled(true);
        Availability.setEnabled(true);
      } else if (message.startsWith("#send")) {
        try {
          client.sendToServer(message.substring(6));
        } catch (IOException e) {
          e.printStackTrace();
        }
        return;
      } else if (message.startsWith("#linedraw")) {
        drawPad.updateDraw(message);
        return;
      } else if (message.startsWith("Login Error:")) {
        java.awt.EventQueue.invokeLater(
            new Runnable() {
              public void run() {
                chatFrame.setVisible(false);
                LoginGUI login = new LoginGUI(true);
              }
            });
      }

      if (!message.substring(message.length() - 1, message.length()).equals("\n")) message += "\n";

      // Update UI display safely
      final String msg = message;
      SwingUtilities.invokeLater(
          new Runnable() {
            public void run() {
              MessageDisplay.append(msg);
            }
          });
    }
  }
  /**
   * This method waits for input from the console. Once it is received, it sends it to the client's
   * message handler.
   */
  public void accept() {
    try {
      BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
      String message;

      while (true) {
        message = fromConsole.readLine();
        if (client == null) {
          client = new ChatClient(this);
        }
        client.handleMessageFromClientUI(message);
      }
    } catch (Exception ex) {
      System.out.println("Unexpected error while reading from console!");
    }
  }
Exemple #3
0
  /** Creates new form GUI */
  public ClientGUI(String loginID, String password, String host, int port) {
    try {
      client = new ChatClient(loginID, password, host, port, this);
    } catch (IOException exception) {
      System.out.println("Cannot open connection. Awaiting command.");
      // System.exit(1);
    }

    try {
      client.addObserver(this);
    } catch (Exception e) {
      // server not started
      JOptionPane.showMessageDialog(
          null, "Error: Could not connect to server.  Make sure server is started and try again");
      System.exit(0);
    }
    SetupUI(loginID);
    self = this;
  }
 public void handleMessageFromClientUI(Object message) {
   String parsed = "";
   if (!((Message) message).message.startsWith("#") && isConnected()) {
     super.handleMessageFromClientUI(message);
     return;
   }
   {
     parsed = ((Message) message).message.substring(1);
     if (parsed.equals("quit")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
       System.exit(0);
     } else if (parsed.equals("logoff")) {
       try {
         closeConnection();
       } catch (IOException ex) {
         ex.printStackTrace();
         System.exit(1);
       }
     } else if (parsed.equals("login")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       try {
         openConnection();
       } catch (IOException ex) {
         System.out.println("Error: " + ex.getMessage());
         ex.printStackTrace();
       }
     } else if (parsed.startsWith("sethost")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       } else {
         String host = "";
         try {
           host = parsed.split(" ")[1];
         } catch (ArrayIndexOutOfBoundsException ex) {
           System.out.println("Error: Must specify host");
           return;
         }
         setHost(host);
       }
     } else if (parsed.startsWith("setport")) {
       if (isConnected()) {
         System.out.println("Error: Already connected");
         return;
       }
       int port = 0;
       try {
         port = Integer.parseInt(parsed.split(" ")[1]);
       } catch (ArrayIndexOutOfBoundsException ex) {
         System.out.println("Error: Must specify port");
         return;
       }
       setPort(port);
     } else if (parsed.equals("getport")) {
       System.out.println("Port: " + getPort());
     } else if (parsed.equals("gethost")) {
       System.out.println("Host: " + getHost());
     }
   }
 }