Ejemplo n.º 1
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();
    }
Ejemplo n.º 2
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());
  }
Ejemplo 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;
 }
    // 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();
    }
Ejemplo n.º 5
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;
  }