예제 #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
 private void doQuery(String cmd) {
   // if(cmd.charAt(0) == ' ') cmd = cmd.substring(1);
   cmd = cmd.trim();
   if (onlineUsers.containsKey(cmd)) {
     ClientThread c = (ClientThread) onlineUsers.get(cmd);
     writeLine("CHAT *** " + c.getNickname() + " is from " + c.getRemoteAddr() + " ***");
   } else {
     writeLine("CHAT *** " + cmd + " is UNKNOWN ***");
   }
 } // doQuery
예제 #3
0
 private void doMsg(String cmd) {
   cmd = cmd.trim();
   int pos = cmd.indexOf(' '); // find tell whom
   if (pos < 0) return; // no message on the line
   String dst = cmd.substring(0, pos);
   String msg = cmd.substring(pos + 1);
   msg = msg.trim();
   if (msg == null) msg = " ";
   if (onlineUsers.containsKey(dst)) {
     ClientThread c = (ClientThread) onlineUsers.get(dst);
     c.writeLine("*** [" + myName + "] " + msg);
   } else {
     writeLine("CHAT *** " + dst + " not in the ROOM ***");
   }
 }
예제 #4
0
파일: Server.java 프로젝트: naory159/Ex4
  /*
   *  to broadcast a message to all Clients
   */
  private synchronized void broadcast(String message) {
    // add HH:mm:ss and \n to the message
    String time = sdf.format(new Date());
    String messageLf = time + " " + message + "\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 (!ct.writeMsg(messageLf)) {
        al.remove(i);
        display("Disconnected Client " + ct.username + " removed from list.");
      }
    }
  }
  public void start() {
    keepGoing = true;
    /* create socket server and wait for connection requests */
    try {
      // the socket used by the server
      ServerSocket serverSocket = new ServerSocket(port);

      // infinite loop to wait for connections
      while (keepGoing) {
        // format message saying we are waiting
        display("Server menunggu koneksi Client pada port " + port + ".");

        Socket socket = serverSocket.accept(); // accept connection
        // if I was asked to stop
        if (!keepGoing) break;
        ClientThread t = new ClientThread(socket); // make a thread of it
        al.add(t); // save it in the ArrayList
        t.start();
      }
      // I was asked to stop
      try {
        serverSocket.close();
        for (int i = 0; i < al.size(); ++i) {
          ClientThread tc = al.get(i);
          try {
            tc.sInput.close();
            tc.sOutput.close();
            tc.socket.close();
          } catch (IOException ioE) {
            // not much I can do
          }
        }
      } catch (Exception e) {
        display("Exception closing the server and clients: " + e);
      }
    }
    // something went bad
    catch (IOException e) {
      String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e + "\n";
      display(msg);
    }
  }
예제 #6
0
 private void doBcast(String msg) {
   for (Enumeration e = onlineUsers.elements(); e.hasMoreElements(); ) {
     ClientThread c = (ClientThread) e.nextElement();
     if (c != this) c.writeLine(msg);
   }
 }
예제 #7
0
 private void doList() {
   for (Enumeration e = onlineUsers.elements(); e.hasMoreElements(); ) {
     ClientThread c = (ClientThread) e.nextElement();
     writeLine("CHAT *** [" + c.getNickname() + "@" + c.getRemoteAddr() + "] ***");
   } // for
 } // doList
예제 #8
0
  /** Run and periodically report status. */
  public void run() {
    long st = System.currentTimeMillis();

    long lasten = st;
    long lasttotalops = 0;

    boolean alldone;

    do {
      alldone = true;

      int totalops = 0;

      // terminate this thread when all the worker threads are done
      for (Thread t : _threads) {
        if (t.getState() != Thread.State.TERMINATED) {
          alldone = false;
        }

        ClientThread ct = (ClientThread) t;
        totalops += ct.getOpsDone();
      }

      long en = System.currentTimeMillis();

      long interval = en - st;
      // double throughput=1000.0*((double)totalops)/((double)interval);

      double curthroughput =
          1000.0 * (((double) (totalops - lasttotalops)) / ((double) (en - lasten)));

      lasttotalops = totalops;
      lasten = en;

      DecimalFormat d = new DecimalFormat("#.##");

      if (totalops == 0) {
        System.err.println(
            _label
                + " "
                + (interval / 1000)
                + " sec: "
                + totalops
                + " operations; "
                + Measurements.getMeasurements().getSummary());
      } else {
        System.err.println(
            _label
                + " "
                + (interval / 1000)
                + " sec: "
                + totalops
                + " operations; "
                + d.format(curthroughput)
                + " current ops/sec; "
                + Measurements.getMeasurements().getSummary());
      }

      if (_standardstatus) {
        if (totalops == 0) {
          System.out.println(
              _label
                  + " "
                  + (interval / 1000)
                  + " sec: "
                  + totalops
                  + " operations; "
                  + Measurements.getMeasurements().getSummary());
        } else {
          System.out.println(
              _label
                  + " "
                  + (interval / 1000)
                  + " sec: "
                  + totalops
                  + " operations; "
                  + d.format(curthroughput)
                  + " current ops/sec; "
                  + Measurements.getMeasurements().getSummary());
        }
      }

      try {
        sleep(sleeptime);
      } catch (InterruptedException e) {
        // do nothing
      }

    } while (!alldone);
  }