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 disconnect() {
   if (session != null && session.isConnected()) {
     session.disconnect();
   }
   jToggleButtonConnect.setSelected(false);
   jToggleButtonConnect.setText("Connect");
   jToggleButtonRegister.setSelected(false);
   onlineUsers.clear();
   jToggleButtonRegister.setText("Register");
 }
 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 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);
   }
 }
 private void handleRegister() {
   if (session == null || !session.isConnected()) {
     jToggleButtonRegister.setSelected(false);
     jMenuItemOptionsDeregister.setEnabled(false);
     jMenuItemOptionsRegister.setEnabled(true);
     return;
   }
   if (jToggleButtonRegister.isSelected() || jMenuItemOptionsRegister.isEnabled()) {
     register();
     jToggleButtonRegister.setText("Deregister");
     jMenuItemOptionsDeregister.setEnabled(true);
     jMenuItemOptionsRegister.setEnabled(false);
   } else {
     deregister();
     jToggleButtonRegister.setText("Register");
     jMenuItemOptionsDeregister.setEnabled(false);
     jMenuItemOptionsRegister.setEnabled(true);
   }
 }
 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();
    }
  }
Beispiel #10
0
  // main ..........(SL: now renamed).................................................
  public void start() {

    // EditorServer server ;

    //
    // if(args.length == 1)
    // server = new EditorServer(args[0], "No Name", "No Description") ;
    // else
    // server = new EditorServer(null, args[0], args[1]) ;

    // final EditorServer self = server ;

    Session editorSession = null;
    this.textChannel = null;
    this.textChannelConsumer = null;

    URLString url = null;
    String sessionType = null;
    String hostname = null;
    int hostport = 0;

    if (EditorServer_Debug) System.err.println("EditorServer: main.");

    hostname = "localhost";
    hostport = 4461;
    sessionType = "socket";
    url = URLString.createSessionURL(hostname, hostport, sessionType, sessionName);

    if (EditorServer_Debug) System.err.println("EditorServer: main: url: " + url.toString());

    try {

      /* Registry running?  Start it if it isn't. */

      if (RegistryFactory.registryExists(sessionType) == false) {
        RegistryFactory.startRegistry(sessionType);
      }

      /* Create a session, [re]bind it and create a channel. */

      this.client = new NetworkedClient("Server");
      editorSession = SessionFactory.createSession(this.client, url, true);

      // setup text channel
      this.textChannel = editorSession.createChannel(this.client, "TextChannel", true, true, true);
      this.textChannelConsumer = new TextChannelConsumer(this);
      this.textChannel.addConsumer(this.client, this.textChannelConsumer);

      // setup client channel
      this.clientChannel =
          editorSession.createChannel(this.client, "ClientChannel", true, true, true);
      this.clientChannelConsumer = new ClientChannelConsumer(this);
      this.clientChannel.addConsumer(this.client, this.clientChannelConsumer);
      this.clientChannel.addChannelListener(
          new ChannelAdaptor() {
            public void channelConsumerAdded(ChannelEvent event) {
              self.clientAppeared();
            }
          });

      System.err.println("Setup and bound Editor server.");
    } catch (JSDTException e) {
      System.err.println("EditorServer: main: shared data exception: " + e);
      if (EditorServer_Debug) {
        e.printStackTrace();
      }
    }

    this.pack();
    // on Linux, this seems to be the only way to set JFrame sizes
    // (i.e. resize it after packing (which seems to defeat the
    // purpose of packing)!)
    this.setSize(new Dimension(800, 600));
    this.setVisible(true);
  }