Exemplo n.º 1
0
  private void sayHello() {
    if (_helloPrx == null) {
      return;
    }

    int delay = _delaySlider.getValue();
    try {
      if (!_deliveryMode.isBatch()) {
        _status.setText("Sending request");
        final DeliveryMode mode = _deliveryMode;
        java.util.concurrent.CompletableFuture<Void> f = _helloPrx.sayHelloAsync(delay);
        final Holder<Boolean> response = new Holder<Boolean>(false);
        f.whenComplete(
            (result, ex) -> {
              if (ex == null) {
                assert (!response.value);
                response.value = true;
                _status.setText("Ready");
              } else {
                assert (!response.value);
                response.value = true;
                handleException(ex);
              }
            });
        com.zeroc.Ice.Util.getInvocationFuture(f)
            .whenSent(
                (ss, ex) -> {
                  if (ex == null) {
                    if (mode.isOneway()) {
                      _status.setText("Ready");
                    } else if (!response.value) {
                      _status.setText("Waiting for response");
                    }
                  }
                });
      } else {
        _flush.setEnabled(true);
        _helloPrx.sayHello(delay);
        _status.setText("Queued sayHello request");
      }
    } catch (com.zeroc.Ice.LocalException ex) {
      handleException(ex);
    }
  }
Exemplo n.º 2
0
  Client(String[] args) {
    //
    // Initialize an Ice communicator.
    //
    try {
      com.zeroc.Ice.InitializationData initData = new com.zeroc.Ice.InitializationData();
      initData.properties = com.zeroc.Ice.Util.createProperties();
      initData.properties.load("config.client");
      initData.dispatcher =
          (runnable, connection) -> {
            SwingUtilities.invokeLater(runnable);
          };
      _communicator = com.zeroc.Ice.Util.initialize(args, initData).communicator;
    } catch (Throwable ex) {
      handleException(ex);
    }

    Container cp = this;

    JLabel l1 = new JLabel("Hostname");
    _hostname = new JTextField();
    JLabel l2 = new JLabel("Mode");
    _mode = new JComboBox<String>();
    JLabel l3 = new JLabel("Timeout");
    _timeoutSlider = new JSlider(0, MAX_TIME);
    _timeoutLabel = new JLabel("0.0");
    JLabel l4 = new JLabel("Delay");
    _delaySlider = new JSlider(0, MAX_TIME);
    _delayLabel = new JLabel("0.0");
    JPanel buttonPanel = new JPanel();
    _hello = new JButton("Hello World!");
    _shutdown = new JButton("Shutdown");
    _flush = new JButton("Flush");
    _flush.setEnabled(false);
    JSeparator statusPanelSeparator = new JSeparator();
    _status = new JLabel();
    _status.setText("Ready");

    //
    // Default to localhost.
    //
    _hostname.setText("127.0.0.1");
    _hostname
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void changedUpdate(DocumentEvent e) {
                updateProxy();
              }

              @Override
              public void insertUpdate(DocumentEvent e) {
                if (e.getDocument().getLength() > 0) {
                  _hello.setEnabled(true);
                  _shutdown.setEnabled(true);
                }
                updateProxy();
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                if (e.getDocument().getLength() == 0) {
                  _hello.setEnabled(false);
                  _shutdown.setEnabled(false);
                  _flush.setEnabled(false);
                }
                updateProxy();
              }
            });

    _mode.setModel(new DefaultComboBoxModel<String>(DELIVERY_MODE_DESC));

    _hello.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            sayHello();
          }
        });
    _shutdown.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            shutdown();
          }
        });
    _flush.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            flush();
          }
        });
    _mode.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            changeDeliveryMode(_mode.getSelectedIndex());
          }
        });
    changeDeliveryMode(_mode.getSelectedIndex());

    _timeoutSlider.addChangeListener(new SliderListener(_timeoutSlider, _timeoutLabel));
    _timeoutSlider.addChangeListener(
        new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent ce) {
            updateProxy();
          }
        });
    _timeoutSlider.setValue(0);
    _delaySlider.addChangeListener(new SliderListener(_delaySlider, _delayLabel));
    _delaySlider.setValue(0);

    GridBagConstraints gridBagConstraints;

    cp.setMaximumSize(null);
    cp.setPreferredSize(null);
    cp.setLayout(new GridBagLayout());

    l1.setText("Hostname");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    cp.add(l1, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new Insets(5, 0, 5, 5);
    cp.add(_hostname, gridBagConstraints);

    l2.setText("Mode");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(0, 5, 5, 0);
    cp.add(l2, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new Insets(0, 0, 5, 5);
    cp.add(_mode, gridBagConstraints);

    l3.setText("Timeout");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(0, 5, 5, 0);
    cp.add(l3, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    cp.add(_timeoutSlider, gridBagConstraints);

    _timeoutLabel.setMinimumSize(new Dimension(20, 17));
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(0, 5, 5, 5);
    cp.add(_timeoutLabel, gridBagConstraints);

    l4.setText("Delay");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(0, 5, 5, 0);
    cp.add(l4, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    cp.add(_delaySlider, gridBagConstraints);

    _delayLabel.setMinimumSize(new Dimension(20, 17));
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.insets = new Insets(0, 5, 5, 5);
    cp.add(_delayLabel, gridBagConstraints);

    _hello.setText("Hello World!");
    buttonPanel.add(_hello);

    _shutdown.setText("Shutdown");
    buttonPanel.add(_shutdown);

    _flush.setText("Flush");
    buttonPanel.add(_flush);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.ipady = 5;
    cp.add(buttonPanel, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 5;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new Insets(0, 5, 5, 5);
    cp.add(statusPanelSeparator, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new Insets(0, 5, 5, 5);
    cp.add(_status, gridBagConstraints);

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

    _shutdownHook =
        new Thread("Shutdown hook") {
          @Override
          public void run() {
            destroyCommunicator();
          }
        };

    try {
      Runtime.getRuntime().addShutdownHook(_shutdownHook);
    } catch (IllegalStateException e) {
      //
      // Shutdown in progress, ignored
      //
    }

    addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
            destroyCommunicator();
            Runtime.getRuntime().removeShutdownHook(_shutdownHook);
            dispose();
            Runtime.getRuntime().exit(0);
          }
        });

    setTitle("Ice - Hello World!");
    pack();
    locateOnScreen(this);
    setVisible(true);
  }