private void sendRequest(BlockPdu pdu) {
   setButton.setEnabled(false);
   getButton.setEnabled(false);
   getNextButton.setEnabled(false);
   lmessage.setText("Sending request ..");
   lmessage.setBackground(Color.white);
   try {
     varbind var = pdu.getResponseVariableBinding();
     AsnObjectId oid = var.getOid();
     AsnObject res = var.getValue();
     if (res != null) {
       toid.setText(oid.toString());
       tvalue.setText(res.toString());
       lmessage.setText("Received aswer ");
       lmessage.setBackground(Color.white);
     } else {
       lmessage.setText("Received no aswer ");
       lmessage.setBackground(Color.red);
     }
   } catch (PduException exc) {
     lmessage.setText("PduException: " + exc.getMessage());
     lmessage.setBackground(Color.red);
     exc.printStackTrace();
   } catch (java.io.IOException exc) {
     lmessage.setText("IOException: " + exc.getMessage());
     lmessage.setBackground(Color.red);
     exc.printStackTrace();
   }
   setButton.setEnabled(true);
   getButton.setEnabled(true);
   getNextButton.setEnabled(true);
 }
  public void actionPerformed(ActionEvent evt) {
    Object src = evt.getSource();

    try {
      if (src == propDialog) {
        propDialog_actionPerformed(evt);
      } else {
        pdu = new BlockPdu(context);
        if (src == setButton) {
          pdu.setPduType(BlockPdu.SET);
          pdu.addOid(toid.getText(), new AsnOctets(tvalue.getText()));
        } else if (src == getButton) {
          pdu.setPduType(BlockPdu.GET);
          pdu.addOid(toid.getText());
        } else if (src == getNextButton) {
          pdu.setPduType(BlockPdu.GETNEXT);
          pdu.addOid(toid.getText());
        }
        sendRequest(pdu);
      }
    } catch (Exception exc) {
      exc.printStackTrace();
      lmessage.setText("Exception: " + exc.getMessage());
      lmessage.setBackground(Color.red);
    }
  }
示例#3
0
 /**
  * Implementing the Observer interface. Receiving the response from the Pdu.
  *
  * @param obs the UpSincePdu variable
  * @param ov the date
  * @see uk.co.westhawk.snmp.pdu.UpSincePdu
  */
 public void update(Observable obs, Object ov) {
   Pdu pdu = (Pdu) obs;
   if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
     Date dres = (Date) ov;
     if (dres != null) {
       // TODO: invokeLater
       v.setText(dres.toString());
     }
   } else {
     // TODO: invokeLater
     v.setText(pdu.getErrorStatusString());
   }
 }
  private void createContext(String host, int port, String comm, String socketType) {
    myFrame.setTitle("Set One  - " + host);

    if (context != null) {
      context.destroy();
    }
    try {
      context = new SnmpContextPool(host, port, socketType);
      context.setCommunity(comm);
    } catch (java.io.IOException exc) {
      lmessage.setText("IOException: " + exc.getMessage());
      lmessage.setBackground(Color.red);
    }
  }
示例#5
0
  /**
   * Implementing the Observer interface. Receiving the response from the Pdu.
   *
   * @param obs the InterfacePdu variable
   * @param ov the varbind
   * @see uk.co.westhawk.snmp.pdu.InterfacePdu
   * @see uk.co.westhawk.snmp.stack.varbind
   */
  public void update(Observable obs, Object ov) {
    up = (InterfacePdu) obs;
    if (prev != null) {
      // TODO invokeLater
      v.setText(up.getSpeed(prev) + " bytes/sec ");
    }

    // keep the previous...
    prev = up;
  }
  public void init() {
    try {
      createPropertyDialog();
      jbInit();

      propDialog.setVisible(true);
      propDialog.toFront();

      createContext(
          propDialog.getHost(),
          propDialog.getPort(),
          propDialog.getCommunityName(),
          propDialog.getSocketType());
      sendGetRequest(toid.getText());
    } catch (Exception exc) {
      exc.printStackTrace();
      lmessage.setText("Exception: " + exc.getMessage());
      lmessage.setBackground(Color.red);
    }
  }
  private void jbInit() throws Exception {
    JLabel loid, lvalue;

    loid = new JLabel("OID: ");
    lvalue = new JLabel("Value: ");
    lmessage = new JLabel("");
    lmessage.setOpaque(true);

    String oid = util.getOid(sysContact);
    toid = new JTextField(oid);
    tvalue = new JTextField();

    setButton = new JButton("Set");
    getButton = new JButton("Get");
    getNextButton = new JButton("GetNext");

    Vector sw = new Vector(2);
    sw.addElement(setButton);
    sw.addElement(getButton);

    GridBagLayout grid = new GridBagLayout();
    Container cont = this;
    cont.setLayout(grid);

    addToGridBag(grid, cont, loid, 0, 0, 0.33, 1.0);
    addToGridBag(grid, cont, toid, 1, 0, 2, 1, 0.33, 1.0);
    addToGridBag(grid, cont, lvalue, 0, 1, 0.33, 1.0);
    addToGridBag(grid, cont, tvalue, 1, 1, 2, 1, 0.33, 1.0);
    addToGridBag(grid, cont, setButton, 0, 2, 0.33, 1.0);
    addToGridBag(grid, cont, getButton, 1, 2, 0.33, 1.0);
    addToGridBag(grid, cont, getNextButton, 2, 2, 0.33, 1.0);
    addToGridBag(grid, cont, lmessage, 0, 3, 3, 1, 0.33, 1.0);

    lmessage.setBackground(Color.white);

    setButton.addActionListener(this);
    getButton.addActionListener(this);
    getNextButton.addActionListener(this);
    this.addMouseListener(this);
  }