Пример #1
0
 private void handleConnect() {
   if (jToggleButtonConnect.isSelected() || jMenuItemServerConnect.isEnabled())
     try {
       jTextPaneDisplayMessages.setText("");
       session = new Session();
       try {
         String userid = jTextFieldUser.getText();
         if (jPasswordFieldPwd.getPassword() != null) {
           String pwd = String.valueOf(jPasswordFieldPwd.getPassword()).trim();
           if (!pwd.equals("")) {
             userid += ":" + pwd;
           }
         }
         if (session.connect(jTextFieldServer.getText(), userid)) {
           displayMessage("Connected\n", incomingMsgAttrSet);
           session.addListener(this);
           jToggleButtonConnect.setText("Disconnect");
           jMenuItemServerConnect.setEnabled(false);
           jMenuItemServerDisconnect.setEnabled(true);
         } else {
           jToggleButtonConnect.setSelected(false);
           jMenuItemServerConnect.setEnabled(true);
           jMenuItemServerDisconnect.setEnabled(false);
           displayMessage("Connection attempt failed\n", offlineClientAttrSet);
         }
       } catch (ConnectionException ex1) {
         jToggleButtonConnect.setSelected(false);
         jMenuItemServerConnect.setEnabled(true);
         jMenuItemServerDisconnect.setEnabled(false);
         displayMessage(
             "Connection attempt failed: " + ex1.getMessage() + "\n", offlineClientAttrSet);
       }
     } catch (Exception ex) {
       ex.printStackTrace();
     }
   else {
     disconnect();
     jMenuItemServerConnect.setEnabled(true);
     jMenuItemServerDisconnect.setEnabled(false);
     displayMessage("Disconnected\n", offlineClientAttrSet);
   }
 }
Пример #2
0
  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();
    }
  }