Exemplo n.º 1
0
  /*
   *  to broadcast a private message to all Clients
   */
  private synchronized void broadcastOnlyOne(String message, ChatMessage cm) {

    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\r\n";
    System.out.print(messageLf);
    serverGUI.serverTextArea.append(messageLf);

    // we loop in reverse order in case we would have to remove a Client
    // because it has disconnected
    for (int i = al.size(); --i >= 0; ) {
      ClientThread ct = al.get(i);
      // try to write to the Client if it fails remove it from the list
      if (cm.getType() == ChatMessage.ESTABLISHCONNECTION
          || cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY) {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(cm)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      } else {
        if (cm.getTO().equalsIgnoreCase(ct.username) && !ct.writeMsg(message)) {
          al.remove(i);
          display("Disconnected Client " + ct.username + " removed from list.");
          break;
        }
      }
    }
  }
Exemplo n.º 2
0
    // what will run forever
    public void run() {
      // to loop until LOGOUT
      boolean keepGoing = true;
      while (keepGoing) {
        // read a String (which is an object)
        try {
          cm = (ChatMessage) sInput.readObject();
        } catch (IOException e) {
          display(username + " Exception reading Streams: " + e);
          break;
        } catch (ClassNotFoundException e2) {
          break;
        }
        // the messaage part of the ChatMessage
        String message = cm.getMessage();

        // Switch on the type of message receive
        switch (cm.getType()) {
          case ChatMessage.MESSAGE:
            broadcast(username + ": " + message);
            break;
          case ChatMessage.LOGOUT:
            display(username + " disconnected with a LOGOUT message.");
            keepGoing = false;
            break;
          case ChatMessage.WHOISIN:
            writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n");
            // scan al the users connected
            for (int i = 0; i < al.size(); ++i) {
              ClientThread ct = al.get(i);
              writeMsg((i + 1) + ") " + ct.username + " since " + ct.date);
            }
            break;
          case ChatMessage.TO:
            broadcastOnlyOne(username + ": " + message, cm);
            break;
          case ChatMessage.SENDFILE:
            sendFile(username + ": " + message, cm);
            break;
          case ChatMessage.PROCEED:
            proceedSendFile();
            break;
          case ChatMessage.SENDMEDIAFILE:
            String fileList = getFileList();
            writeMsg(new ChatMessage(ChatMessage.SENDMEDIAFILE, fileList));
            break;
          case ChatMessage.ESTABLISHCONNECTIONANDPLAY:
            sendMediaFile(username + ": " + message, cm);
            break;
        }
      }
      // remove myself from the arrayList containing the list of the
      // connected Clients
      remove(id);
      close();
    }
Exemplo n.º 3
0
 /*
  * Write a String to the Client output stream
  */
 private boolean writeMsg(ChatMessage cm) {
   // if Client is still connected send the message to it
   if (!socket.isConnected()) {
     close();
     return false;
   }
   // write the message to the stream
   try {
     boolean sendChatMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTION;
     boolean sendMediaFileMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY;
     if (sendChatMessage || sendMediaFileMessage) sOutput.writeObject(cm);
     else sOutput.writeObject(cm.getMessage());
   }
   // if an error occurs, do not abort just inform the user
   catch (IOException e) {
     display("Error sending message to " + username);
     display(e.toString());
   }
   return true;
 }
Exemplo n.º 4
0
  @Override
  public void update(ChatMessage message) throws IOException {

    System.out.println("Inside update() " + number + " received message " + message.getMessage());
    System.out.println("sending message...");

    // ChatClient client = (ChatClient) message.sender;

    // char chars[] = (message.getSender().getSenderName() + ": " +
    // message.getMessage()).toCharArray();
    char chars[] = getMessageForSend(message);
    // TODO convert message to function
    // char chars[] = message.getMessage().toCharArray();
    for (char c : chars) {
      // outputStream.write((int)c);
      // System.out.println("Sending " + c);
      outputStream.write((int) c);
    }
    outputStream.flush();
    System.out.println("Sent: " + message.getMessage());
  }
    // what will run forever
    public void run() {
      // to loop until LOGOUT
      boolean keepGoing = true;
      while (keepGoing) {
        // read a String (which is an object)
        try {
          cm = (ChatMessage) sInput.readObject();
        } catch (IOException e) {
          display(username + " Exception reading Streams: " + e);
          break;
        } catch (ClassNotFoundException e2) {
          break;
        }
        // the messaage part of the ChatMessage
        String message = cm.getMessage();

        // Switch on the type of message receive
        switch (cm.getType()) {
          case ChatMessage.MESSAGE:
            broadcast(username + ": " + message);
            break;
          case ChatMessage.LOGOUT:
            display(username + " LOGOUT dari chat.");
            keepGoing = false;
            break;
          case ChatMessage.WHOISIN:
            writeMsg("Daftar Client yang terkoneksi pada " + sdf.format(new Date()) + "\n");
            // scan al the users connected
            for (int i = 0; i < al.size(); ++i) {
              ClientThread ct = al.get(i);
              writeMsg((i + 1) + ") " + ct.username + " sejak " + ct.date);
            }
            break;
        }
      }
      // remove myself from the arrayList containing the list of the
      // connected Clients
      remove(id);
      close();
    }
Exemplo n.º 6
0
  /**
   * Composes messages for send (using message body, sender name etc.)
   *
   * @param message
   * @return
   */
  private char[] getMessageForSend(ChatMessage message) {

    char chars[] =
        (message.getSender().getSenderName() + ": " + message.getMessage()).toCharArray();
    return chars;
  }