yoursunny / NDNts

NDN libraries for the Modern Web
https://ndnts-docs.ndn.today
ISC License
31 stars 9 forks source link

failed to generate response on server side #16

Closed radara09 closed 1 year ago

radara09 commented 1 year ago

I'm still trying to make development for (https://yoursunny.com/t/2020/NDNts-webpack-start/) this time I try to make server from python ndn and I got an issue where when I use appParameters the interest is not received on the server but if I don't use it, the server received the interest

this is my code on main.js

import { connectToNetwork, connectToRouter } from "@ndn/autoconfig";
import { Endpoint } from "@ndn/endpoint";
import { AltUri, Interest, Name } from "@ndn/packet";
import { WsTransport } from "@ndn/ws-transport";

async function ping(evt) {
  evt.preventDefault();
  // Disable the submit button during function execution.
  const $button = document.querySelector("#app_button");
  $button.disabled = true;

  try {
    // Construct the name prefix <user-input>+/ping
    const prefix = new Name(document.querySelector("#app_prefix").value);
    const app = document.querySelector("#app_param").value;
    const $log = document.querySelector("#app_log");
    $log.textContent = `Check Data \n${AltUri.ofName(prefix)}\n`;

    const endpoint = new Endpoint();
    const encoder = new TextEncoder();
    // Generate a random number as initial sequence number.
    let seqNum = Math.trunc(Math.random() * 1e8);
    for (let i = 0; i < 3; ++i) {
      ++seqNum;
      // Construct an Interest with prefix + seqNum.
      const interest = new Interest();
      interest.name = prefix;
      interest.mustBeFresh = true; 
      interest.lifetime = 1000;
      interest.appParameters = encoder.encode(app);
      $log.textContent += `\n${encoder.encode(app)}\n`;
      const t0 = Date.now();
      try {
        // Retrieve Data and compute round-trip time.
        const data = await endpoint.consume(interest);
        const rtt = Date.now() - t0;
        const dataContent = data.content;
        //console.log(dataContent);
        $log.textContent += `\n${AltUri.ofName(data.name)} rtt=${rtt}ms content=${String.fromCharCode(...dataContent)}`;
      } catch(err) {
        // Report Data retrieval error.
        $log.textContent += `\n${AltUri.ofName(interest.name)} ${err}`;
      }

      // Delay 500ms before sending the next Interest.
      await new Promise((r) => setTimeout(r, 500));
    }
  } finally {
    // Re-enable the submit button.
    $button.disabled = false;
  }
}

async function main() {
  // Connect to the global NDN network in one line.
  // This function queries the NDN-FCH service, and connects to the nearest router.
  //await connectToRouter("wss://192.168.56.106:9696/ws/", {});
  //await WsTransport.createFace({}, "wss://testbed-ndn-rg.stei.itb.ac.id/ws/");
  await WsTransport.createFace({}, "ws://192.168.56.106:9696/ws/");
  //await WsTransport.createFace({}, "ws://coba.ndntel-u.my.id/ws/");

  // Enable the form after connection was successful.
  document.querySelector("#app_button").disabled = false;
  document.querySelector("#app_form").addEventListener("submit", ping);
}

window.addEventListener("load", main);

this is my producer.py code

@app.route('/data/user')
def on_interest(name: FormalName, param: InterestParam, ap: Optional[BinaryStr]):
    print(f'>> I: {Name.to_str(name)}, {param}')
    content = f'Hello, world! {bytes(ap)}'.encode()
    app.put_data(name, content=content, freshness_period=10000)
    print(f'<< D: {Name.to_str(name)}')
    print(MetaInfo(freshness_period=10000))
    print(f'Content: (size: {len(content)})')
    print('')

thank you for your help

yoursunny commented 1 year ago

Are you accessing the webpage from a Secure Context? Basically, either https: or http://localhost.

radara09 commented 1 year ago

we accessing the webpage by http://localhost:3333 we also tested from http://10.0.2.15:3333 and still got no respond on the server any recommendation to fix this? thank you

yoursunny commented 1 year ago

http://localhost:3333

This is OK because http://localhost is a Secure Context.

http://10.0.2.15:3333

This is not a Secure Context and many features of NDNts will not work. You should deliver your webpage over HTTPS with a valid certificate.

yoursunny commented 1 year ago

interest.appParameters = encoder.encode(app);

The issue is indeed related to ApplicationParameters field. The NDN protocol requires a ParametersSha256DigestComponent in the name that matches in ApplicationParameters field. However, you set the ApplicationParameters field without updating the name with ParametersSha256DigestComponent field, so that the Interest is dropped by the network.

The easiest way to update the name with a correct ParametersSha256DigestComponent is adding this line:

await interest.updateParamsDigest();

After that, the Interest should pass through the network successfully.

radara09 commented 1 year ago

thanks its solved