/// <summary> /// Decode input as a control parameters in NDN-TLV and set the fields of the /// controlResponse object. /// </summary> /// /// <param name="controlResponse"></param> /// <param name="input"></param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="EncodingException">For invalid encoding</exception> public override void decodeControlResponse(ControlResponse controlResponse, ByteBuffer input, bool copy) { TlvDecoder decoder = new TlvDecoder(input); int endOffset = decoder .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_ControlResponse); controlResponse.setStatusCode((int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusCode)); // Set copy false since we just immediately get a string. Blob statusText = new Blob( decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusText), false); controlResponse.setStatusText(statusText.toString()); // Decode the body. if (decoder .peekType(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ControlParameters, endOffset)) { controlResponse.setBodyAsControlParameters(new ControlParameters()); // Decode into the existing ControlParameters to avoid copying. decodeControlParameters( controlResponse.getBodyAsControlParameters(), decoder, copy); } else controlResponse.setBodyAsControlParameters(null); decoder.finishNestedTlvs(endOffset); }
public void Decode() { ControlResponse response = new ControlResponse(); response.wireDecode(TestControlResponse1); Assert.AssertEquals(response.getStatusCode(), 404); Assert.AssertEquals(response.getStatusText(), "Nothing not found"); Assert.AssertEquals(response.getBodyAsControlParameters().getFaceId(), 10); }
/// <summary> /// Create a new ControlResponse as a deep copy of the given ControlResponse. /// </summary> /// /// <param name="controlResponse">The ControlResponse to copy.</param> public ControlResponse(ControlResponse controlResponse) { this.statusCode_ = -1; this.statusText_ = ""; this.bodyAsControlParameters_ = null; statusCode_ = controlResponse.statusCode_; statusText_ = controlResponse.statusText_; bodyAsControlParameters_ = (controlResponse.bodyAsControlParameters_ == null) ? null : new ControlParameters( controlResponse.bodyAsControlParameters_); }
public void Encode() { ControlResponse response = new ControlResponse(); response.setStatusCode(404); response.setStatusText("Nothing not found"); response.setBodyAsControlParameters(new ControlParameters()); response.getBodyAsControlParameters().setFaceId(10); Blob wire = response.wireEncode(); Assert.AssertEquals(wire.buf(), TestControlResponse1); }
/// <summary> /// We received the response. /// </summary> /// /// <param name="interest"></param> /// <param name="responseData"></param> public virtual void onData(Interest interest, Data responseData) { // Decode responseData.getContent() and check for a success code. ControlResponse controlResponse = new ControlResponse(); try { controlResponse.wireDecode(responseData.getContent(), net.named_data.jndn.encoding.TlvWireFormat.get()); } catch (EncodingException ex) { net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix failed: Error decoding the NFD response: {0}", ex); try { info_.onRegisterFailed_.onRegisterFailed(info_.prefix_); } catch (Exception exception) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterFailed", exception); } return; } // Status code 200 is "OK". if (controlResponse.getStatusCode() != 200) { net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix failed: Expected NFD status code 200, got: {0}", controlResponse.getStatusCode()); try { info_.onRegisterFailed_.onRegisterFailed(info_.prefix_); } catch (Exception ex_0) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterFailed", ex_0); } return; } // Success, so we can add to the registered prefix table. if (info_.registeredPrefixId_ != 0) { long interestFilterId = 0; if (info_.onInterest_ != null) { // registerPrefix was called with the "combined" form that includes the // callback, so add an InterestFilterEntry. interestFilterId = parent_.getNextEntryId(); parent_.setInterestFilter(interestFilterId, new InterestFilter(info_.prefix_), info_.onInterest_, info_.face_); } if (!parent_.registeredPrefixTable_.add( info_.registeredPrefixId_, info_.prefix_, interestFilterId)) { // removeRegisteredPrefix was already called with the registeredPrefixId. if (interestFilterId > 0) { // Remove the related interest filter we just added. parent_.unsetInterestFilter(interestFilterId); } return; } } net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix succeeded with the NFD forwarder for prefix {0}", info_.prefix_.toUri()); if (info_.onRegisterSuccess_ != null) { try { info_.onRegisterSuccess_.onRegisterSuccess(info_.prefix_, info_.registeredPrefixId_); } catch (Exception ex_1) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterSuccess", ex_1); } } }
/// <summary> /// Encode controlResponse in NDN-TLV and return the encoding. /// </summary> /// /// <param name="controlResponse">The ControlResponse object to encode.</param> /// <returns>A Blob containing the encoding.</returns> public override Blob encodeControlResponse(ControlResponse controlResponse) { TlvEncoder encoder = new TlvEncoder(256); int saveLength = encoder.getLength(); // Encode backwards. // Encode the body. if (controlResponse.getBodyAsControlParameters() != null) encodeControlParameters( controlResponse.getBodyAsControlParameters(), encoder); encoder.writeBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusText, new Blob( controlResponse.getStatusText()).buf()); encoder.writeNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusCode, controlResponse.getStatusCode()); encoder.writeTypeAndLength(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_ControlResponse, encoder.getLength() - saveLength); return new Blob(encoder.getOutput(), false); }
/// <summary> /// We received the response. /// </summary> /// /// <param name="interest"></param> /// <param name="responseData"></param> public virtual void onData(Interest interest, Data responseData) { // Decode responseData.getContent() and check for a success code. ControlResponse controlResponse = new ControlResponse(); try { controlResponse.wireDecode(responseData.getContent(), net.named_data.jndn.encoding.TlvWireFormat.get()); } catch (EncodingException ex) { net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix failed: Error decoding the NFD response: {0}", ex); try { info_.onRegisterFailed_.onRegisterFailed(info_.prefix_); } catch (Exception exception) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterFailed", exception); } return; } // Status code 200 is "OK". if (controlResponse.getStatusCode() != 200) { net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix failed: Expected NFD status code 200, got: {0}", controlResponse.getStatusCode()); try { info_.onRegisterFailed_.onRegisterFailed(info_.prefix_); } catch (Exception ex_0) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterFailed", ex_0); } return; } // Success, so we can add to the registered prefix table. if (info_.registeredPrefixId_ != 0) { long interestFilterId = 0; if (info_.onInterest_ != null) { // registerPrefix was called with the "combined" form that includes the // callback, so add an InterestFilterEntry. interestFilterId = parent_.getNextEntryId(); parent_.setInterestFilter(interestFilterId, new InterestFilter(info_.prefix_), info_.onInterest_, info_.face_); } if (!parent_.registeredPrefixTable_.add( info_.registeredPrefixId_, info_.prefix_, interestFilterId)) { // removeRegisteredPrefix was already called with the registeredPrefixId. if (interestFilterId > 0) // Remove the related interest filter we just added. parent_.unsetInterestFilter(interestFilterId); return; } } net.named_data.jndn.Node.logger_.log( ILOG.J2CsMapping.Util.Logging.Level.INFO, "Register prefix succeeded with the NFD forwarder for prefix {0}", info_.prefix_.toUri()); if (info_.onRegisterSuccess_ != null) { try { info_.onRegisterSuccess_.onRegisterSuccess(info_.prefix_, info_.registeredPrefixId_); } catch (Exception ex_1) { net.named_data.jndn.Node.logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, "Error in onRegisterSuccess", ex_1); } } }
/// <summary> /// Encode controlResponse and return the encoding. /// Your derived class should override. /// </summary> /// /// <param name="controlResponse">The ControlResponse object to encode.</param> /// <returns>A Blob containing the encoding.</returns> /// <exception cref="System.NotSupportedException">for unimplemented if the derivedclass does not override.</exception> public virtual Blob encodeControlResponse(ControlResponse controlResponse) { throw new NotSupportedException( "encodeControlResponse is not implemented"); }
/// <summary> /// Decode input as a control parameters and set the fields of the /// controlResponse object. Copy from the input when making new Blob values. /// Your derived class should override. /// </summary> /// /// <param name="controlResponse"></param> /// <param name="input"></param> /// <exception cref="System.NotSupportedException">for unimplemented if the derivedclass does not override.</exception> /// <exception cref="EncodingException">For invalid encoding.</exception> public void decodeControlResponse(ControlResponse controlResponse, ByteBuffer input) { decodeControlResponse(controlResponse, input, true); }
/// <summary> /// Decode input as a control parameters and set the fields of the /// controlResponse object. Your derived class should override. /// </summary> /// /// <param name="controlResponse"></param> /// <param name="input"></param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="System.NotSupportedException">for unimplemented if the derivedclass does not override.</exception> /// <exception cref="EncodingException">For invalid encoding.</exception> public virtual void decodeControlResponse(ControlResponse controlResponse, ByteBuffer input, bool copy) { throw new NotSupportedException( "decodeControlResponse is not implemented"); }