public void onResponse(ResponseEvent re) {
    // Always cancel async request when response has been received
    // otherwise a memory leak is created! Not canceling a request
    // immediately can be useful when sending a request to a broadcast
    // address.
    try {
      Object source = re.getSource();

      // test to ignore REPORTS from DISCOVERY messages in SNMPv3
      if (!(source instanceof Snmp)) return;

      ((Snmp) source).cancel(re.getRequest(), this);

      // create the SnmpMsg received
      MsgSnmp msg = new MsgSnmp();
      msg.setPdu(re.getResponse());

      // TODO: how to know the version here to set communityTarget or UserTarget
      Target target = new CommunityTarget();
      //           ((CommunityTarget)target).setCommunity(new
      // OctetString(re.getStateReference().getSecurityName()));
      target.setAddress(re.getPeerAddress());
      msg.setTarget((AbstractTarget) target);
      UdpAddress add = (UdpAddress) re.getPeerAddress();
      msg.setRemotePort(add.getPort());
      msg.setRemoteHost(add.getInetAddress().getHostAddress());
      msg.setListenpoint(listenpoint);

      StackFactory.getStack(StackFactory.PROTOCOL_SNMP).receiveMessage(msg);
    } catch (Exception ex) {
    }
  }
  // process request and analyze content with mib file
  public void processPdu(CommandResponderEvent cre) {

    try {
      // create the SnmpMsg received
      MsgSnmp msg = new MsgSnmp();
      msg.setPdu(cre.getPDU());
      // TODO: how to know the version here to set communityTarget or UserTarget
      AbstractTarget target = null;
      if ((cre.getMessageProcessingModel() == MessageProcessingModel.MPv1)
          || (cre.getMessageProcessingModel() == MessageProcessingModel.MPv2c)) {
        target = new CommunityTarget();
        ((CommunityTarget) target)
            .setCommunity(new OctetString(cre.getStateReference().getSecurityName()));
      } else // TODO: manage of snmpV3 not done
      target = new UserTarget();

      if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv1) {
        target.setVersion(SnmpConstants.version1);
      } else if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv2c) {
        target.setVersion(SnmpConstants.version2c);
      } else if (cre.getMessageProcessingModel() == MessageProcessingModel.MPv3) {
        target.setVersion(SnmpConstants.version3);
      }

      target.setAddress(cre.getPeerAddress());
      msg.setTarget((AbstractTarget) target);
      UdpAddress add = (UdpAddress) cre.getPeerAddress();
      msg.setRemotePort(add.getPort());
      msg.setRemoteHost(add.getInetAddress().getHostAddress());
      msg.setListenpoint(listenpoint);

      StackFactory.getStack(StackFactory.PROTOCOL_SNMP).receiveMessage(msg);
    } catch (Exception ex) {
    }
  }
 /**
  * Creates a UDP transport with optional reusing the address if is currently in timeout state
  * (TIME_WAIT) after the connection is closed.
  *
  * @param udpAddress the local address for sending and receiving of UDP messages.
  * @param reuseAddress if <code>true</code> addresses are reused which provides faster socket
  *     binding if an application is restarted for instance.
  * @throws IOException if socket binding fails.
  * @since 1.7.3
  */
 public DefaultUdpTransportMapping(UdpAddress udpAddress, boolean reuseAddress)
     throws IOException {
   super(udpAddress);
   socket = new DatagramSocket(null);
   socket.setReuseAddress(reuseAddress);
   final SocketAddress addr =
       new InetSocketAddress(udpAddress.getInetAddress(), udpAddress.getPort());
   socket.bind(addr);
 }
 public void sendMessage(
     UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference)
     throws java.io.IOException {
   InetSocketAddress targetSocketAddress =
       new InetSocketAddress(targetAddress.getInetAddress(), targetAddress.getPort());
   if (logger.isDebugEnabled()) {
     logger.debug(
         "Sending message to "
             + targetAddress
             + " with length "
             + message.length
             + ": "
             + new OctetString(message).toHexString());
   }
   DatagramSocket s = ensureSocket();
   s.send(new DatagramPacket(message, message.length, targetSocketAddress));
 }
 /**
  * Creates a UDP transport on the specified address. The address will not be reused if it is
  * currently in timeout state (TIME_WAIT).
  *
  * @param udpAddress the local address for sending and receiving of UDP messages.
  * @throws IOException if socket binding fails.
  */
 public DefaultUdpTransportMapping(UdpAddress udpAddress) throws IOException {
   super(udpAddress);
   socket = new DatagramSocket(udpAddress.getPort(), udpAddress.getInetAddress());
 }