예제 #1
0
파일: Server.java 프로젝트: naory159/Ex4
  /*
   *  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;
        }
      }
    }
  }
예제 #2
0
파일: Server.java 프로젝트: naory159/Ex4
    // 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();
    }
  public Connection saveChatMessage(Connection con, String message)
      throws NoSuchConnectionException { // load connection, checking if it exists
    if (con == null) throw new IllegalArgumentException("connectionURI is not set");
    if (message == null) throw new IllegalArgumentException("message is not set");

    // construct chatMessage object to store in the db
    ChatMessage chatMessage = new ChatMessage();
    chatMessage.setCreationDate(new Date());
    chatMessage.setLocalConnectionURI(con.getConnectionURI());
    chatMessage.setMessage(message);
    chatMessage.setOriginatorURI(con.getNeedURI());
    // save in the db
    chatMessageRepository.saveAndFlush(chatMessage);

    return con;
  }
예제 #4
0
파일: Server.java 프로젝트: naory159/Ex4
 /*
  * 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();
    }
예제 #6
0
 @OnMessage
 public void handleChatMessage(ChatMessage message) {
   switch (message.getType()) {
     case NewUserMessage.USERNAME_MESSAGE:
       this.processNewUser((NewUserMessage) message);
       break;
     case ChatMessage.CHAT_DATA_MESSAGE:
       this.processChatUpdate((ChatUpdateMessage) message);
       break;
     case ChatMessage.SIGNOFF_REQUEST:
       this.processSignoffRequest((UserSignoffMessage) message);
   }
 }