void jTextFieldSendMessages_keyPressed(KeyEvent e) {
   if (e.getKeyCode() == e.VK_DOWN) {
     String s = msgHistory.forward();
     if (s != null) {
       jTextFieldSendMessages.setText(s);
     }
   } else if (e.getKeyCode() == e.VK_UP) {
     String s = msgHistory.back();
     if (s != null) {
       jTextFieldSendMessages.setText(s);
     }
   } else if (e.getKeyChar() == '\n') {
     String body = jTextFieldSendMessages.getText();
     if (body.length() > 0) {
       if (body.charAt(body.length() - 1) == '\n') body = body.substring(0, body.length() - 1);
       String subject = jTextFieldSendSubject.getText();
       if (subject.length() > 0) {
         if (subject.charAt(subject.length() - 1) == '\n')
           subject = subject.substring(0, subject.length() - 1);
       }
       if (session != null && session.isConnected()) {
         session.postMessage(jTextFieldTargetUser.getText(), subject, body);
         displaySendMessage(subject, jTextFieldTargetUser.getText(), body, outgoingMsgAttrSet);
       }
       msgHistory.add(body);
       subjectHistory.add(subject);
       subjectHistory.reset();
       msgHistory.reset();
       jTextFieldSendMessages.setText("");
       jTextFieldSendSubject.setText("");
     }
   }
 }
 private void register() {
   if (session != null && session.isConnected()) {
     onlineUsers.clear();
     // get the current list
     Message msg = session.sendMessage("", "list", "");
     System.out.println("LIST: " + msg.getBody());
     StringTokenizer st = new StringTokenizer(msg.getBody(), "\n");
     while (st.hasMoreTokens()) {
       onlineUsers.addElement(new HostItem(st.nextToken()));
     }
     session.postMessage("", "register", "");
   }
 }
  private void uneavesdrop() {
    if (session == null || !session.isConnected()) return;
    // get the list of selected hosts from the online list
    Object[] values = jListOnlineUsers.getSelectedValues();
    if (values == null) return;

    // iterate through the list of hosts
    for (int i = 0; i < values.length; i++) {
      // if a host is not being eavesdropped on, skip it
      HostItem hi = (HostItem) values[i];
      if (hi.eavesDroppingEnabled) {
        session.postMessage("", "uneavesdrop", hi.name);
      }
    }
  }
 private void globalUneavesdrop() {
   if (session == null || !session.isConnected()) return;
   jMenuItemOptionsGlobalEavesdrop.setEnabled(true);
   jMenuItemOptionsGlobalUneavesdrop.setEnabled(false);
   session.postMessage("", "unglobaleavesdrop", "");
 }
 private void deregister() {
   onlineUsers.clear();
   if (session != null && session.isConnected()) {
     session.postMessage("", "deregister", "");
   }
 }
  public static void main(String[] args) {
    if (args.length > 0) {
      try {
        if (args[0].equals("-stress")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          if (session.connect(server, userName)) {
            long msgCount = 0;
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              Thread.currentThread().sleep(2000);
            }
          }
        } else if (args[0].equals("-stress2")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              session.disconnect();
            }
            Thread.currentThread().sleep(2000);
          }

        } else if (args[0].equals("-stress3")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(1000));
              }
              session.dropConnection();
            }
            Thread.currentThread().sleep(2000);
          }
        } else if (args[0].equals("-stress4")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          while (true) {
            if (session.connect(server, userName)) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.fixedString(300000));
              }
              session.dropConnection();
            }
            Thread.currentThread().sleep(5000);
          }

        } else if (args[0].equals("-stress5")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          if (session.connect(server, userName)) {
            long msgCount = 0;
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                session.postMessage(
                    targetUser, ("subject " + String.valueOf(msgCount)), Util.randomString(10));
              }
              Thread.currentThread().sleep(2000);
            }
          }
        } else if (args[0].equals("-monitor")) {
          String userName = args[1];
          String server = args[2];
          long interval = Long.parseLong(args[3]);
          Session session = new Session();
          long msgCount = 0;
          if (session.connect(server, userName)) {
            while (true) {
              Message msg = session.sendMessage("", "get send queue stats", "");
              System.out.println("SEND QUEUE STATS");
              System.out.println(msg.getBody());
              Thread.currentThread().sleep(interval);
            }
          }
        } else if (args[0].equals("-responder")) {
          String userName = args[1];
          String server = args[2];
          long interval = Long.parseLong(args[3]);
          final Session session = new Session();
          if (session.connect(server, userName)) {
            session.addListener(
                new AsyncMessageReceiverListener() {
                  public void receiveMsg(Message msg) {
                    session.postMessage(
                        msg.getFrom(), "RE: " + msg.getFrom(), msg.getThread(), msg.getBody());
                  }

                  public void messageReceiverClosed(MessageReceiver mr) {}
                });
            while (true) {
              Thread.currentThread().sleep(interval);
            }
          }
        } else if (args[0].equals("-threaded-sender")) {
          String userName = args[1];
          String server = args[2];
          String targetUser = args[3];
          int rate = Integer.parseInt(args[4]);
          Session session = new Session();
          long msgCount = 0;
          if (session.connect(server, userName)) {
            while (true) {
              for (int i = 0; i < rate; i++) {
                msgCount++;
                Message reply =
                    session.sendMessage(
                        targetUser, ("subject " + String.valueOf(msgCount)), "body", 10000);
                if (reply == null) {
                  System.out.println("reply not received");
                }
              }
              Thread.currentThread().sleep(5000);
            }
          }
        }
      } catch (Exception e) {
        System.out.println(
            "Unable to start stress test.  Format: -stress <user name> <server> <target user> <msg rate>");
      }
    } else {
      Client client = new Client();
      client.setSize(800, 420);
      center(client);
      client.show();
    }
  }