📄 abstractsnmpcontext.java
字号:
* when run() finishes. * <p> * It does NOT close the socket. * The thread will actually stop/finish when the run() finishes. That is when * a packet arrives on the socket or when the socket times out. * </p> * * <p> * We have deprecated this method since there is no point in stopping * the context, but not destroying it. The context cannot start again anyway. * The difference between destroy() and stop() was not very clear. * </p> * * @deprecated As of version 4_12, should use {@link #destroy()} * @see #destroy() */public synchronized void stop(){ stopRequested = true;}/** * We wait for any incoming packets. After receiving one, decode * the packet into an Pdu. The Pdu will notify the observers waiting * for an response. * * <p> * Thanks to Chris Barlock <barlock@us.ibm.com> who reported a * NullPointerException in run() on variable 'me' and introduced the * variable stopRequested. * </p> */public void run(){ // while It is visible while (!stopRequested) { // block for incoming packets me.yield(); try { if (stopRequested) { break; } StreamPortItem item = soc.receive(maxRecvSize); ByteArrayInputStream in = item.getStream(); if (AsnObject.debug > 10) { int nb = in.available(); byte [] bu = new byte[nb]; in.read(bu); in.reset(); SnmpUtilities.dumpBytes(getClass().getName() + ".run(): Received from " + item.getHostAddress() + ", from port " + item.getHostPort() + ": ", bu); } processIncomingResponse(in); } catch (java.io.IOException exc) { if (exc instanceof InterruptedIOException) { if (AsnObject.debug > 15) { System.out.println(getClass().getName() + ".run(): Idle recv " + exc.getMessage()); } } else if (exc instanceof java.net.SocketException) { if (AsnObject.debug > 15) { System.out.println(getClass().getName() + ".run(): SocketException " + exc.getMessage()); } } else { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".run(): " + exc.getClass().getName() + " " + exc.getMessage()); exc.printStackTrace(); } } } catch (DecodingException exc) { if (AsnObject.debug > 1) { System.out.println(getClass().getName() + ".run(): DecodingException: " + exc.getMessage()); } } catch (Exception exc) { if (AsnObject.debug > 1) { System.out.println(getClass().getName() + ".run(): Exception: " + exc.getMessage()); exc.printStackTrace(); } } catch (Error err) { if (AsnObject.debug > 1) { System.out.println(getClass().getName() + ".run(): Error: " + err.getMessage()); err.printStackTrace(); } } } freeTransmitters(); trapSupport.empty(); pduSupport.empty(); // This used to actually create a listener. I do think this bug // has been fixed, since no socket will be created in // ListeningContextPool, unless a listener has been added. ListeningContextPool lcontext = new ListeningContextPool(ListeningContextFace.DEFAULT_TRAP_PORT, bindAddr, typeSocket); lcontext.removeRawPduListenerFromPool(this); me = null; soc = null;}/* * By moving activate() from the constructor to here, the parameter * maxRecvSize, changed in setMaxRecvSize(), gets a chance to actually * change before run() starts. * Thanks to Dave Hunt <dave.hunt@csipros.com> who reported this * problem. */public synchronized void sendPacket(byte[] p){ if (isDestroyed == false) { activate(); try { if (AsnObject.debug > 10) { SnmpUtilities.dumpBytes("Sending to " + soc.getSendToHostAddress() + ": ", p); } // Seen it throw an "java.io.IOException: Invalid argument" // when the bind address was wrong, i.e. the packet reach // the host over the interface soc.send(p); } catch (IOException exc) { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".sendPacket(): " + exc.getClass().getName() + " " + exc.getMessage()); } } }}Pdu getPdu(Integer ReqId){ return getPdu(ReqId.intValue());}Pdu getPdu(int rid){ Pdu ret = null; for (int i=0; i< MAXPDU; i++) { if ((pdus[i] != null) && (pdus[i].getReqId() == rid)) { ret = pdus[i]; break; } } return ret;}public synchronized boolean removePdu(int rid){ boolean ret = false; for (int i=0; i< MAXPDU; i++) { if ((pdus[i] != null) && (pdus[i].getReqId() == rid)) { pdus[i] = null; ret = true; break; } } return ret;}public synchronized boolean addPdu(Pdu p)throws java.io.IOException, PduException{ boolean done = false; if (isDestroyed == true) { throw new EncodingException("Context can no longer be used, since it is already destroyed"); } else { // I only want to start the receive thread when any of the // context's PDUs is actually expecting a response. See activate(). if (anyPduExpectingResponse == false) { anyPduExpectingResponse = p.isExpectingResponse(); } for (int i=0; i<MAXPDU; i++) { if (pdus[i] == null) { pdus[i] = p; pdus[i].setTrans(getTrans(i)); done = true; break; } } } return done;}public void addTrapListener(TrapListener l) throws java.io.IOException{ addTrapListener(l, ListeningContextFace.DEFAULT_TRAP_PORT);}public void removeTrapListener(TrapListener l) throws java.io.IOException{ removeTrapListener(l, ListeningContextFace.DEFAULT_TRAP_PORT);}public void addTrapListener(TrapListener l, int port) throws java.io.IOException{ ListeningContextPool lcontext = new ListeningContextPool(port, bindAddr, typeSocket); addTrapListener(l, lcontext);}public void removeTrapListener(TrapListener l, int port) throws java.io.IOException{ ListeningContextPool lcontext = new ListeningContextPool(port, bindAddr, typeSocket); removeTrapListener(l, lcontext);}public void addTrapListener(TrapListener l, ListeningContextPool lcontext) throws java.io.IOException{ trapSupport.addTrapListener(l); lcontext.addRawPduListener(this);}public void removeTrapListener(TrapListener l, ListeningContextPool lcontext)throws java.io.IOException{ trapSupport.removeTrapListener(l); if (trapSupport.getListenerCount() == 0 && pduSupport.getListenerCount() == 0) { lcontext.removeRawPduListener(this); }}public void addRequestPduListener(RequestPduListener l) throws java.io.IOException{ addRequestPduListener(l, SnmpContextBasisFace.DEFAULT_PORT);}public void removeRequestPduListener(RequestPduListener l) throws java.io.IOException{ removeRequestPduListener(l, SnmpContextBasisFace.DEFAULT_PORT);}public void addRequestPduListener(RequestPduListener l, int port) throws java.io.IOException{ ListeningContextPool lcontext = new ListeningContextPool(port, bindAddr, typeSocket); addRequestPduListener(l, lcontext);}public void removeRequestPduListener(RequestPduListener l, int port) throws java.io.IOException{ ListeningContextPool lcontext = new ListeningContextPool(port, bindAddr, typeSocket); removeRequestPduListener(l, lcontext);}public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException{ pduSupport.addRequestPduListener(l); lcontext.addRawPduListener(this);}public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException{ pduSupport.removeRequestPduListener(l); if (trapSupport.getListenerCount() == 0 && pduSupport.getListenerCount() == 0) { lcontext.removeRawPduListener(this); }}/** * Invoked when an undecoded pdu is received. * First the version and the hostaddress are checked, if correct * an attempt is made to decode the pdu. * When successful the original event is consumed and a decoded pdu event * is passed on the listeners. * * @see RawPduReceivedSupport#fireRawPduReceived * @see RequestPduReceivedSupport#fireRequestPduReceived * @see TrapReceivedSupport#fireTrapReceived */public void rawPduReceived(RawPduEvent evt){ String hostAddress = evt.getHostAddress(); int version = evt.getVersion(); if (version == this.getVersion()) { if (hostAddress != null && hostAddress.equals(this.getSendToHostAddress()) == true) { byte [] message = evt.getMessage(); Pdu pdu = null; try { pdu = processIncomingPdu(message); if (pdu != null) { evt.consume(); int port = evt.getHostPort(); if (pdu.getMsgType() == SnmpConstants.TRP_REQ_MSG || pdu.getMsgType() == SnmpConstants.TRPV2_REQ_MSG) { trapSupport.fireTrapReceived(pdu, port); } else { pduSupport.fireRequestPduReceived(pdu, port); } } else { // somehow the context matches, but the pdu type is // not recognised. } } catch(DecodingException exc) { if (AsnObject.debug > 2) { System.out.println(getClass().getName() + ".rawPduReceived(): DecodingException: " + exc.getMessage()); } } catch(IOException exc) { if (AsnObject.debug > 0) { System.out.println(getClass().getName() + ".rawPduReceived(): IOException "+ exc.getMessage()); } } } else { if (AsnObject.debug > 5) { System.out.println(getClass().getName() + ".rawPduReceived(): " + "Pdu host (" + hostAddress + "), does not correspond with context host (" + this.getSendToHostAddress() + ")"); } } } else { if (AsnObject.debug > 5) { String theirs = SnmpUtilities.getSnmpVersionString(version); String ours = SnmpUtilities.getSnmpVersionString(this.getVersion()); System.out.println(getClass().getName() + ".rawPduReceived(): " + "Pdu version " + theirs + ", does not correspond with context version " + ours); } }}Transmitter getTrans(int i){ if (transmitters[i] == null) { transmitters[i] = new Transmitter(basename+"_v"+getVersion()+"_Trans"+i); } return transmitters[i];}/** * Creates and starts the Receive thread that allows this context to * receive packets. * Subclasses may override this to adjust the threading behaviour. * * @see PassiveSnmpContext#activate() * @see PassiveSnmpContextv2c#activate() */protected void activate(){ // Only start the thread when 'me' is null (i.e. no thread is running) // AND when anyPduExpectingResponse is true. // This way a context that only sends (for example) traps, will not // start a listing thread. if (me == null && anyPduExpectingResponse == true) { me = new Thread(this, basename+"_v"+getVersion()+"_Receive"); me.setPriority(me.MAX_PRIORITY); me.start(); }}/** * Frees the transmitters. * * @see #run() * @see #destroy() * @since 5_1 */// In version 5_0, this code lived in run(). // Thanks to Vincent Deconinck <vdeconinck@tiscalinet.be>protected void freeTransmitters(){ for (int i=0;i<MAXPDU; i++) { if (transmitters[i] != null) { transmitters[i].destroy(); transmitters[i] = null; } if (pdus[i] != null) { pdus[i] = null; } }}/** * Returns a clone of this SnmpContext. * * @since 4_14 * @exception CloneNotSupportedException Thrown when the constructor * generates an IOException or when in one of the Pool classes. */public abstract Object clone() throws CloneNotSupportedException;/** * Returns the hash key. This key is built out of all properties. * * @since 4_14 * @return The hash key */public abstract String getHashKey();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -