Пример #1
0
    public void run() {
      StringBuffer data = new StringBuffer();
      Print.logDebug("Client:InputThread started");

      while (true) {
        data.setLength(0);
        boolean timeout = false;
        try {
          if (this.readTimeout > 0L) {
            this.socket.setSoTimeout((int) this.readTimeout);
          }
          ClientSocketThread.socketReadLine(this.socket, -1, data);
        } catch (InterruptedIOException ee) { // SocketTimeoutException ee) {
          // error("Read interrupted (timeout) ...");
          if (getRunStatus() != THREAD_RUNNING) {
            break;
          }
          timeout = true;
          // continue;
        } catch (Throwable t) {
          Print.logError("Client:InputThread - " + t);
          t.printStackTrace();
          break;
        }
        if (!timeout || (data.length() > 0)) {
          ClientSocketThread.this.handleMessage(data.toString());
        }
      }

      synchronized (this.threadLock) {
        this.isRunning = false;
        Print.logDebug("Client:InputThread stopped");
        this.threadLock.notify();
      }
    }
  /** EdiDialog constructor comment. */
  public void keyReleased(java.awt.event.KeyEvent e) {
    if (e.getSource() == list) {
      switch (e.getKeyCode()) {
        case KeyEvent.VK_DELETE:
        case KeyEvent.VK_BACK_SPACE:
          if (keys.length() > 0) keys.setLength(keys.length() - 1);
          break;
        case KeyEvent.VK_ESCAPE:
          dispose();
          break;
        case KeyEvent.VK_ENTER:
          dispose();
          actionOK();
          break;
        case KeyEvent.VK_SPACE:
          actionAdd();
          break;
        default:
          // keys.append((char) e.getKeyChar());
          list.ensureIndexIsVisible(list.getSelectedIndex());
      }

      //		if (debug)
      //			System.out.println("keys: " + keys);
      return;
    }

    switch (e.getKeyCode()) {
      case KeyEvent.VK_ENTER:
      case KeyEvent.VK_ESCAPE:
        dispose();
        break;
      default:
        break;
    }

    super.keyReleased(e);
  }
Пример #3
0
  // The main procedure
  public static void main(String args[]) {
    String s;

    initGUI();

    while (true) {
      try { // Poll every ~10 ms
        Thread.sleep(10);
      } catch (InterruptedException e) {
      }

      switch (connectionStatus) {
        case BEGIN_CONNECT:
          try {
            // Try to set up a server if host
            if (isHost) {
              hostServer = new ServerSocket(port);
              socket = hostServer.accept();
            }

            // If guest, try to connect to the server
            else {
              socket = new Socket(hostIP, port);
            }

            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            changeStatusTS(CONNECTED, true);
          }
          // If error, clean up and output an error message
          catch (IOException e) {
            cleanUp();
            changeStatusTS(DISCONNECTED, false);
          }
          break;

        case CONNECTED:
          try {
            // Send data
            if (toSend.length() != 0) {
              out.print(toSend);
              out.flush();
              toSend.setLength(0);
              changeStatusTS(NULL, true);
            }

            // Receive data
            if (in.ready()) {
              s = in.readLine();
              if ((s != null) && (s.length() != 0)) {
                // Check if it is the end of a trasmission
                if (s.equals(END_CHAT_SESSION)) {
                  changeStatusTS(DISCONNECTING, true);
                }

                // Otherwise, receive what text
                else {
                  appendToChatBox("INCOMING: " + s + "\n");
                  changeStatusTS(NULL, true);
                }
              }
            }
          } catch (IOException e) {
            cleanUp();
            changeStatusTS(DISCONNECTED, false);
          }
          break;

        case DISCONNECTING:
          // Tell other chatter to disconnect as well
          out.print(END_CHAT_SESSION);
          out.flush();

          // Clean up (close all streams/sockets)
          cleanUp();
          changeStatusTS(DISCONNECTED, true);
          break;

        default:
          break; // do nothing
      }
    }
  }
Пример #4
0
  // Checks the current state and sets the enables/disables
  // accordingly
  public void run() {
    switch (connectionStatus) {
      case DISCONNECTED:
        connectButton.setEnabled(true);
        disconnectButton.setEnabled(false);
        ipField.setEnabled(true);
        portField.setEnabled(true);
        hostOption.setEnabled(true);
        guestOption.setEnabled(true);
        chatLine.setText("");
        chatLine.setEnabled(false);
        statusColor.setBackground(Color.red);
        break;

      case DISCONNECTING:
        connectButton.setEnabled(false);
        disconnectButton.setEnabled(false);
        ipField.setEnabled(false);
        portField.setEnabled(false);
        hostOption.setEnabled(false);
        guestOption.setEnabled(false);
        chatLine.setEnabled(false);
        statusColor.setBackground(Color.orange);
        break;

      case CONNECTED:
        connectButton.setEnabled(false);
        disconnectButton.setEnabled(true);
        ipField.setEnabled(false);
        portField.setEnabled(false);
        hostOption.setEnabled(false);
        guestOption.setEnabled(false);
        chatLine.setEnabled(true);
        statusColor.setBackground(Color.green);
        break;

      case BEGIN_CONNECT:
        connectButton.setEnabled(false);
        disconnectButton.setEnabled(false);
        ipField.setEnabled(false);
        portField.setEnabled(false);
        hostOption.setEnabled(false);
        guestOption.setEnabled(false);
        chatLine.setEnabled(false);
        chatLine.grabFocus();
        statusColor.setBackground(Color.orange);
        break;
    }

    // Make sure that the button/text field states are consistent
    // with the internal states
    ipField.setText(hostIP);
    portField.setText((new Integer(port)).toString());
    hostOption.setSelected(isHost);
    guestOption.setSelected(!isHost);
    statusField.setText(statusString);
    chatText.append(toAppend.toString());
    toAppend.setLength(0);

    mainFrame.repaint();
  }
Пример #5
0
  public void echoCmd(LibgistCommand cmd) {
    if (!showCmdsItem.getState()) {
      return;
    }

    echoBuffer.setLength(0); // reset
    switch (cmd.cmdType) {
      case LibgistCommand.INSERT:
        echoBuffer.append("insert \"").append(cmd.key);
        echoBuffer.append("\" \"").append(cmd.data).append("\"");
        break;

      case LibgistCommand.REMOVE:
        echoBuffer.append("delete \"").append(cmd.qual);
        echoBuffer.append("\"");
        break;

      case LibgistCommand.FETCH:
        echoBuffer.append("select ");
        if (cmd.fetchLimit > 0) {
          echoBuffer.append(cmd.fetchLimit).append(" ");
        }
        echoBuffer.append("\"").append(cmd.qual).append("\"");
        break;

      case LibgistCommand.CREATE:
        echoBuffer.append("create ").append(cmd.indexName);
        echoBuffer.append(" ").append(cmd.extension);
        break;

      case LibgistCommand.OPEN:
        echoBuffer.append("open ").append(cmd.indexName);
        break;

      case LibgistCommand.CLOSE:
        echoBuffer.append("close");
        break;

      case LibgistCommand.FLUSH:
        echoBuffer.append("flush");
        break;

      case LibgistCommand.OPENANL:
        echoBuffer.append("openanl ").append(cmd.analysisFile.getPath());
        break;

      case LibgistCommand.CLOSEANL:
        echoBuffer.append("closeanl");
        break;

      case LibgistCommand.CREATEANL:
        echoBuffer.append("createanl");
        break;

      case LibgistCommand.SCRIPT:
        echoBuffer.append("executing script...");
        break;

      default:
        // something wrong here
    }
    consoleFrame.echoCmd(echoBuffer.toString());
  }