Last active
August 15, 2019 12:27
-
-
Save dev-ritik/d5ba9ea2bbebad0a2bfd9cb0fe03bb0f to your computer and use it in GitHub Desktop.
Code for checking interest.setApplicationParameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.named_data.jndn.*; | |
import net.named_data.jndn.util.SegmentFetcher; | |
import java.io.IOException; | |
import net.named_data.jndn.util.Blob; | |
import net.named_data.jndn.security.v2.ValidationPolicyAcceptAll; | |
import net.named_data.jndn.security.v2.Validator; | |
import net.named_data.jndn.NetworkNack; | |
import net.named_data.jndn.OnData; | |
import net.named_data.jndn.OnNetworkNack; | |
import net.named_data.jndn.OnTimeout; | |
import java.nio.ByteBuffer; | |
import net.named_data.jndn.encoding.tlv.TlvEncoder; | |
import org.json.JSONObject; | |
public class Client { | |
static Interest generateProbeInterest() { | |
Name interestName = new Name("/ndn"); | |
interestName.append("CA").append("_PROBE"); | |
Interest interest = new Interest(interestName); | |
interest.setMustBeFresh(true); | |
interest.setCanBePrefix(false); | |
JSONObject jo = new JSONObject(); | |
jo.put("name", "ritik"); | |
Blob blob = paramFromJson(jo.toString()); | |
System.out.println("Sending blob: " + jo.toString()); | |
System.out.println("Sending blob with size: " + blob.size()); | |
interest.setApplicationParameters(blob); | |
interest.appendParametersDigestToName(); | |
return interest; | |
} | |
private static ByteBuffer toBuffer(int[] array) | |
{ | |
ByteBuffer result = ByteBuffer.allocate(array.length); | |
for (int i = 0; i < array.length; ++i) | |
result.put((byte)(array[i] & 0xff)); | |
result.flip(); | |
return result; | |
} | |
final static OnData onData = new OnData() { | |
@Override | |
public void onData(Interest interest, Data data) { | |
System.out.println("Got data"); | |
} | |
}; | |
final static OnTimeout onTimeout = new OnTimeout() { | |
@Override | |
public void onTimeout(Interest interest) { | |
System.out.println("Timeout"); | |
} | |
}; | |
final static OnNetworkNack onNack = new OnNetworkNack() { | |
@Override | |
public void onNetworkNack(Interest interest, NetworkNack networkNack) { | |
System.out.println("Got Nack"); | |
} | |
}; | |
public static void main(String[] argv) { | |
Face face = new Face(); | |
Interest interest = generateProbeInterest(); | |
System.out.println("interest: " + interest.getName()); | |
try { | |
face.expressInterest(interest, onData, onTimeout); | |
} catch (IOException e) { | |
System.out.println("error"); | |
e.printStackTrace(); | |
} | |
// Loop calling processEvents until a callback sets enabled[0] = false. | |
while (true) { | |
try { | |
face.processEvents(); | |
// We need to sleep for a few milliseconds so we don't use 100% of | |
// the CPU. | |
Thread.sleep(500); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
static Blob paramFromJson(final String json) | |
{ | |
TlvEncoder encoder = new TlvEncoder(); | |
encoder.writeBlobTlv(2, ByteBuffer.wrap(json.getBytes())); | |
return new Blob(encoder.getOutput(), true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
/** | |
* Copyright 2013,2015 Alexander Afanasyev | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
#include <ndn-cxx/face.hpp> | |
#include <ndn-cxx/interest.hpp> | |
#include <ndn-cxx/data.hpp> | |
#include <ndn-cxx/security/key-chain.hpp> | |
#include <ndn-cxx/encoding/block.hpp> | |
#include <boost/property_tree/ptree.hpp> | |
#include <boost/property_tree/json_parser.hpp> | |
#include <iostream> | |
#include <string> | |
class Server | |
{ | |
public: | |
Server(ndn::Face& face) | |
: m_face(face) | |
, m_baseName("/ndn/CA/_PROBE") | |
, m_counter(0) | |
{ | |
ndn::Interest::setAutoCheckParametersDigest(false); | |
m_face.setInterestFilter(m_baseName, | |
std::bind(&Server::onInterest, this, _2), | |
std::bind([] { | |
std::cerr << "Prefix registered" << std::endl; | |
}), | |
[] (const ndn::Name& prefix, const std::string& reason) { | |
std::cerr << "Failed to register prefix: " << reason << std::endl; | |
}); | |
} | |
private: | |
void | |
onInterest(const ndn::Interest& interest) | |
{ | |
std::cerr << "<< interest for " << interest << std::endl; | |
const auto& parameterJson = jsonFromBlock(interest.getApplicationParameters()); | |
for (const auto& kv : parameterJson) { | |
std::cerr << kv.first << "\n"; | |
} | |
// std::cerr << parameterJson.get<std::string>("name") << "\n"; | |
} | |
boost::property_tree::ptree | |
jsonFromBlock(const ndn::Block& block) | |
{ | |
// ndn::Block block = ndn::makeStringBlock(ndn::tlv::ApplicationParameters, "{\"filename\":\"ritik\"}"); | |
std::cerr << "Received Block Size :" << block.size() << "\n"; | |
std::string jsonString; | |
try { | |
jsonString = ndn::encoding::readString(block); | |
std::istringstream ss(jsonString); | |
std::cerr << "Received Block :" << ss.str() << "\n"; | |
boost::property_tree::ptree json; | |
boost::property_tree::json_parser::read_json(ss, json); | |
return json; | |
} | |
catch (const std::exception& e) { | |
std::cerr << "error" << "\n"; | |
return boost::property_tree::ptree(); | |
} | |
} | |
private: | |
ndn::Face& m_face; | |
ndn::KeyChain m_keyChain; | |
ndn::Name m_baseName; | |
uint64_t m_counter; | |
}; | |
int | |
main(int argc, char** argv) | |
{ | |
try { | |
// create Face instance | |
ndn::Face face; | |
// create server instance | |
Server server(face); | |
// start processing loop (it will block forever) | |
face.processEvents(); | |
} | |
catch (const std::exception& e) { | |
std::cerr << "ERROR: " << e.what() << std::endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.named_data.jndn.*; | |
import java.io.IOException; | |
import net.named_data.jndn.util.Blob; | |
import net.named_data.jndn.security.*; | |
import net.named_data.jndn.security.pib.*; | |
import net.named_data.jndn.security.identity.*; | |
public class Server { | |
final static OnData onData = new OnData() { | |
@Override | |
public void onData(Interest interest, Data data) { | |
System.out.println("Got data"); | |
} | |
}; | |
final static OnTimeout onTimeout = new OnTimeout() { | |
@Override | |
public void onTimeout(Interest interest) { | |
System.out.println("expressing interest after timeout"); | |
} | |
}; | |
final static OnNetworkNack onNack = new OnNetworkNack() { | |
@Override | |
public void onNetworkNack(Interest interest, NetworkNack networkNack) { | |
System.out.println("Got Nack"); | |
} | |
}; | |
public static void main(String[] argv) throws Exception { | |
Face face = new Face(); | |
KeyChain keyChain; | |
try { | |
keyChain = buildTestKeyChain(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return; | |
} | |
Name defaultCertificateName; | |
try { | |
defaultCertificateName = keyChain.getDefaultCertificateName(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
defaultCertificateName = new Name("/bogus/certificate/name"); | |
} | |
face.setCommandSigningInfo(keyChain, defaultCertificateName); | |
try{ | |
face.registerPrefix(new Name("/ndn/CA/_PROBE"), onCertInterest, | |
new OnRegisterFailed() { | |
@Override | |
public void onRegisterFailed(Name prefix) { | |
System.out.println("Registration Failure"); | |
} | |
}, | |
new OnRegisterSuccess() { | |
@Override | |
public void onRegisterSuccess(Name prefix, long registeredPrefixId) { | |
System.out.println("Registration Success for prefix: " + prefix.toUri() + ", id: " + registeredPrefixId); | |
} | |
} | |
); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("Error"); | |
} | |
// Loop calling processEvents until a callback sets enabled[0] = false. | |
while (true) { | |
try { | |
face.processEvents(); | |
// System.out.println("beep"); | |
// We need to sleep for a few milliseconds so we don't use 100% of | |
// the CPU. | |
Thread.sleep(500); | |
// System.out.println("whiler"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* Setup an in-memory KeyChain with a default identity. | |
* | |
* @return | |
* @throws net.named_data.jndn.security.SecurityException | |
*/ | |
public static KeyChain buildTestKeyChain() throws net.named_data.jndn.security.SecurityException { | |
MemoryIdentityStorage identityStorage = new MemoryIdentityStorage(); | |
MemoryPrivateKeyStorage privateKeyStorage = new MemoryPrivateKeyStorage(); | |
IdentityManager identityManager = new IdentityManager(identityStorage, privateKeyStorage); | |
KeyChain keyChain = new KeyChain(identityManager); | |
try { | |
keyChain.getDefaultCertificateName(); | |
} catch (net.named_data.jndn.security.SecurityException e) { | |
keyChain.createIdentityAndCertificate(new Name("/test/identity")); | |
keyChain.getIdentityManager().setDefaultIdentity(new Name("/test/identity")); | |
} | |
return keyChain; | |
} | |
public static final OnInterestCallback onCertInterest = new OnInterestCallback() { | |
@Override | |
public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { | |
System.out.println("Called onCertInterest with Interest: "+ interest.getName().toUri()); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment