fix:将解析类放在message文件夹中
This commit is contained in:
+1
-1
@@ -7,4 +7,4 @@ add_compile_options(-DBOOST_LOG_DYN_LINK)
|
||||
add_subdirectory(MINWebServer)
|
||||
|
||||
add_executable(demo test_minweb_server_library.cpp)
|
||||
target_link_libraries(demo ndn-cxx boost_system pthread boost_thread boost_log_setup boost_log MINWebServer jsoncpp)
|
||||
target_link_libraries(demo ndn-cxx boost_system pthread boost_thread boost_log_setup boost_log MINWebServer jsoncpp)
|
||||
@@ -18,8 +18,7 @@ set(CURRENT_SOURCES
|
||||
http/MINHttpServer.cpp
|
||||
http/MINHttpServerImpl.cpp
|
||||
http/MINHttpSession.cpp
|
||||
http/MINWebRequest.cpp
|
||||
http/MINWebResponse.cpp
|
||||
http/MINHttpSession.h
|
||||
server/FileCache.cpp
|
||||
base/MBase.cpp
|
||||
)
|
||||
|
||||
@@ -24,6 +24,12 @@ namespace MIN {
|
||||
*/
|
||||
virtual void replyResponse(std::shared_ptr<MIN::MINWebResponse> response) = 0;
|
||||
|
||||
/**
|
||||
* 返回response的body长度
|
||||
* @param response
|
||||
*/
|
||||
//virtual void returnResponseLength(std::shared_ptr<MIN::MINWebResponse> response) = 0;
|
||||
|
||||
/**
|
||||
* 启动MINHttpServer服务
|
||||
* @param wait 是否阻塞运行
|
||||
|
||||
@@ -19,316 +19,608 @@ using namespace std ;
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "MINWebRequest.h"
|
||||
#include "MINHttpServerImpl.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "MINHttpSession.h"
|
||||
#include <stdlib.h>
|
||||
using namespace ndn ;
|
||||
using namespace std ;
|
||||
|
||||
class Sessionpro
|
||||
|
||||
|
||||
|
||||
void Sessionpro::run(){
|
||||
|
||||
cout << "Service build listen start " << endl;
|
||||
Name namePrefix="/MIN/Nginx/test2";
|
||||
|
||||
m_face.registerPrefix(namePrefix,
|
||||
bind(&Sessionpro::onSuccess, this, _1),
|
||||
RegisterPrefixFailureCallback());
|
||||
|
||||
m_face.processEvents();
|
||||
}
|
||||
|
||||
vector<string> Sessionpro::split(const string& s, vector<string>& tokens, char delim = '-') {
|
||||
tokens.clear();
|
||||
auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t {
|
||||
for (size_t i = pos; i < s.size(); i++) {
|
||||
if (s[i] != delim) return i;
|
||||
}
|
||||
return string::npos;
|
||||
};
|
||||
size_t lastPos = string_find_first_not(0);
|
||||
size_t pos = s.find(delim, lastPos);
|
||||
while (lastPos != string::npos) {
|
||||
tokens.emplace_back(s.substr(lastPos, pos - lastPos));
|
||||
lastPos = string_find_first_not(pos);
|
||||
pos = s.find(delim, lastPos);
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
string readFileIntoString(string filename)
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
void run(){
|
||||
cout << "Service build listen start " << endl;
|
||||
Name namePrefix="/MIN/Nginx/test2";
|
||||
m_face.registerPrefix(namePrefix,
|
||||
bind(&Sessionpro::onSuccess, this, _1),
|
||||
RegisterPrefixFailureCallback());
|
||||
m_face.processEvents();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
vector<string> split(const string& s, vector<string>& tokens, char delim = '-') {
|
||||
tokens.clear();
|
||||
auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t {
|
||||
for (size_t i = pos; i < s.size(); i++) {
|
||||
if (s[i] != delim) return i;
|
||||
}
|
||||
return string::npos;
|
||||
};
|
||||
size_t lastPos = string_find_first_not(0);
|
||||
size_t pos = s.find(delim, lastPos);
|
||||
while (lastPos != string::npos) {
|
||||
tokens.emplace_back(s.substr(lastPos, pos - lastPos));
|
||||
lastPos = string_find_first_not(pos);
|
||||
pos = s.find(delim, lastPos);
|
||||
ifstream ifile(filename);
|
||||
//将文件读入到ostringstream对象buf中
|
||||
ostringstream buf;
|
||||
char ch;
|
||||
while(buf&&ifile.get(ch))
|
||||
buf.put(ch);
|
||||
//返回与流对象buf关联的字符串
|
||||
return buf.str();
|
||||
}
|
||||
shared_ptr<SignatureInfo> getSignatureInfo(const Interest &interest) {
|
||||
auto &name = interest.getName();
|
||||
if (name.size() > ndn::signed_interest::MIN_SIZE) {
|
||||
try {
|
||||
return make_shared<SignatureInfo>(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue());
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void onSuccess(const Name& name){
|
||||
void Sessionpro::onSuccess(const Name& name){
|
||||
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/index.html/MIN_SERVICE_DETECT",
|
||||
bind(&Sessionpro::onInterestDetect, this, _1, _2));
|
||||
//TODO: index.html
|
||||
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION",
|
||||
bind(&Sessionpro::onInterestBuild, this, _1, _2));
|
||||
cout << " onsucess " << endl;
|
||||
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION/sessionId",
|
||||
bind(&Sessionpro::onInterest, this, _1, _2));
|
||||
}
|
||||
|
||||
void onInterestBuild(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
if(interest.getName()[-3].toUri()=="MIN_HTTP_SESSION") {
|
||||
std::cout << ">> Session I: " << interest << std::endl;
|
||||
uu = boost::uuids::random_generator()();
|
||||
uuid_string = boost::uuids::to_string(uu);
|
||||
std::cout << uuid_string << std::endl;
|
||||
sessionIdMap[uuid_string]=true;
|
||||
if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
|
||||
Name dataName(interest.getName());
|
||||
Json::Reader reader;
|
||||
Json::Value root1;
|
||||
|
||||
root1["sessionId"] = uuid_string;
|
||||
cout << root1["sessionId"].asString() << endl;
|
||||
cout << root1.toStyledString() << endl;
|
||||
|
||||
std::string content = root1.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
//data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
data->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << "<< D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2", [this](const InterestFilter& filter, const Interest& interest) {
|
||||
auto name = interest.getName();
|
||||
auto signatureInfoPtr = getSignatureInfo(interest);
|
||||
int commandIndex = -1;
|
||||
if (signatureInfoPtr) {
|
||||
commandIndex -= 2;
|
||||
}
|
||||
else if(sessionIdMap.find(interest.getName()[4].toUri())->second){//interest.getName()[4].toUri()==uuid_string&&(!uuid_string.empty()
|
||||
/*
|
||||
* assume GR request packet only once
|
||||
*/
|
||||
if(interest.getName()[-3].toUri().substr(0,2)=="GR"){
|
||||
//TODO:loopnum requestid duoci
|
||||
std::cout << ">> request I: " << interest << std::endl;
|
||||
string requestId="webtest";
|
||||
loopNum=0;
|
||||
vector<string> want;
|
||||
//cout << "parse name: " << split(interest.getName()[-3].toUri(),want)[1] <<endl;
|
||||
//cout << "loop num" << split(interest.getName()[-3].toUri(),want)[2] <<endl;
|
||||
loopNum=stoi(split(interest.getName()[-3].toUri(),want)[2]);
|
||||
cout << want[0] << want[1] << want[2] << endl;
|
||||
string finalId=requestId.append(want[1]);
|
||||
if(!requestMap.count(finalId)){
|
||||
requestSeq++;//split(interest.getName()[-3].toUri(),want)[1]
|
||||
requestMap[finalId]=loopNum;
|
||||
if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
std::string command = name[commandIndex].toUri();
|
||||
if (command == "MIN_SERVICE_DETECT") {
|
||||
this->onInterestDetect(filter, interest);
|
||||
} else {
|
||||
this->onInterestBuild(filter, interest);
|
||||
}
|
||||
});
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/index.html/MIN_SERVICE_DETECT",
|
||||
bind(&Sessionpro::onInterestDetect, this, _1, _2));
|
||||
|
||||
Name dataName(interest.getName());
|
||||
Json::Reader reader;
|
||||
Json::Value root1;
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/getUserInfo/MIN_SERVICE_DETECT",
|
||||
bind(&Sessionpro::onInterestUserDetect, this, _1, _2));
|
||||
|
||||
root1["requestId"] = finalId;
|
||||
//seqMap[requestMap[split(interest.getName()[-3].toUri(),want)[1]]]=atoi(split(interest.getName()[-3].toUri(),want)[2].c_str());
|
||||
cout << root1["requestId"].asString() << endl;
|
||||
cout << root1.toStyledString() << endl;
|
||||
m_face.setInterestFilter("/MIN/Nginx/test2/MIN_HTTP_SESSION",
|
||||
bind(&Sessionpro::onInterestBuild, this, _1, _2));
|
||||
}
|
||||
|
||||
std::string content = root1.toStyledString();
|
||||
void Sessionpro::onInterestBuild(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
cout << "fuck " << endl;
|
||||
if(interest.getName()[-3].toUri()=="MIN_HTTP_SESSION" && interest.getName()[4].toUri()!="MIN_HTTP_RESPONSE") {
|
||||
std::cout << ">> Session I: " << interest << std::endl;
|
||||
uu = boost::uuids::random_generator()();
|
||||
uuid_string = boost::uuids::to_string(uu);
|
||||
std::cout << uuid_string << std::endl;
|
||||
sessionIdMap[uuid_string]=true;
|
||||
if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
data->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// 数据缓存在节点中,立即变旧
|
||||
Name dataName(interest.getName());
|
||||
Json::Reader reader;
|
||||
Json::Value root1;
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
|
||||
root1["sessionId"] = uuid_string;
|
||||
cout << root1["sessionId"].asString() << endl;
|
||||
cout << root1.toStyledString() << endl;
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << "<< D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
cout << requestMap[finalId] << endl;
|
||||
std::string content = root1.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
//data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
data->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << "<< D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
}
|
||||
else if(sessionIdMap.find(interest.getName()[4].toUri())->second){//interest.getName()[4].toUri()==uuid_string&&(!uuid_string.empty()
|
||||
/*
|
||||
* assume GR request packet only once
|
||||
*/
|
||||
if(interest.getName()[-3].toUri().substr(0,2)=="GR"){
|
||||
//TODO:loopnum requestid duoci
|
||||
std::cout << ">> request I: " << interest << std::endl;
|
||||
string requestId="webtest";
|
||||
requestIdSeq++;
|
||||
loopNum=0;
|
||||
vector<string> want;
|
||||
//cout << "parse name: " << split(interest.getName()[-3].toUri(),want)[1] <<endl;
|
||||
//cout << "loop num" << split(interest.getName()[-3].toUri(),want)[2] <<endl;
|
||||
loopNum=stoi(split(interest.getName()[-3].toUri(),want)[2]);
|
||||
cout << want[0] << want[1] << want[2] << endl;
|
||||
string finalId=requestId.append(to_string(requestIdSeq));
|
||||
//responsePacketState[finalId]=false;
|
||||
if(!requestMap.count(finalId)){
|
||||
requestSeq++;//split(interest.getName()[-3].toUri(),want)[1]
|
||||
requestMap[finalId]=loopNum;
|
||||
if (interest.hasApplicationParameters()) { // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
else{
|
||||
cerr << "GR request duplicate" << endl;
|
||||
}
|
||||
}
|
||||
else if(interest.getName()[-3].toUri().substr(0,3)=="SEQ" && requestMap.count(interest.getName()[-4].toUri())){
|
||||
cout << "SEQ I>>==========================" << endl;
|
||||
vector<string> ackSeq;
|
||||
split(interest.getName()[-3].toUri(),ackSeq);
|
||||
cout << ackSeq[1] << endl;
|
||||
|
||||
//makeAck makeack;
|
||||
//TODO:sessionidmap judge
|
||||
Name dataName(interest.getName());
|
||||
Json::Reader reader;
|
||||
Json::Value root1;
|
||||
|
||||
string storeInterest((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
localMap[stoi(ackSeq[1])]=storeInterest;
|
||||
storeRequestIdSeq[interest.getName()[-4].toUri()]=localMap;
|
||||
Json::Reader reader ;
|
||||
Json::Value root1 ;
|
||||
string ack="ACK-";
|
||||
root1["ACK"]=ack.append(ackSeq[1]);
|
||||
cout << root1["ACK"].asString() << endl ;
|
||||
cout << root1.toStyledString() << endl ;
|
||||
root1["requestId"] = finalId;
|
||||
//seqMap[requestMap[split(interest.getName()[-3].toUri(),want)[1]]]=atoi(split(interest.getName()[-3].toUri(),want)[2].c_str());
|
||||
cout << root1["requestId"].asString() << endl;
|
||||
cout << root1.toStyledString() << endl;
|
||||
|
||||
std::string content = root1.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
//data->setName(dataName.append(root1["ACK"].asString()));
|
||||
data->setName(interest.getName());
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
data->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
std::cout << "<< D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
cout << "output of seq content" << localMap[stoi(ackSeq[1])] << endl;
|
||||
if(storeRequestIdSeq[interest.getName()[-4].toUri()].size() == requestMap[interest.getName()[-4].toUri()]){
|
||||
//TODO:feed to minhttp parser
|
||||
parseStr="";
|
||||
auto it = storeRequestIdSeq[interest.getName()[-4].toUri()].begin();
|
||||
while(it != storeRequestIdSeq[interest.getName()[-4].toUri()].end()) {
|
||||
cout<<it->second<<endl;
|
||||
parseStr.append(it->second);
|
||||
it++;
|
||||
}
|
||||
MIN::MINWebRequest minWebRequest;
|
||||
MIN::MINWebMessage minWebMessage;
|
||||
minWebRequest.ybParse(minWebMessage,minWebRequest,parseStr,interest);
|
||||
//MIN::MINWebServer::OnRequestReceiveListener(minWebRequest);
|
||||
}
|
||||
cout << requestMap[finalId] << endl;
|
||||
}
|
||||
else{
|
||||
cerr << "GR request duplicate" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(interest.getName()[-3].toUri().substr(0,3)=="SEQ" && requestMap.count(interest.getName()[-4].toUri())){
|
||||
cout << "SEQ I>>==========================" << endl;
|
||||
vector<string> ackSeq;
|
||||
split(interest.getName()[-3].toUri(),ackSeq);
|
||||
cout << ackSeq[1] << endl;
|
||||
|
||||
void onInterestDetect(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
std::cout << "<< I: " << interest << std::endl;
|
||||
if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
//makeAck makeack;
|
||||
//TODO:sessionidmap judge
|
||||
|
||||
string storeInterest((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
localMap[stoi(ackSeq[1])]=storeInterest;
|
||||
storeRequestIdSeq[interest.getName()[-4].toUri()]=localMap;
|
||||
Json::Reader reader ;
|
||||
Json::Value root1 ;
|
||||
string ack="ACK-";
|
||||
root1["ACK"]=ack.append(ackSeq[1]);
|
||||
cout << root1["ACK"].asString() << endl ;
|
||||
cout << root1.toStyledString() << endl ;
|
||||
|
||||
std::string content = root1.toStyledString();
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
//data->setName(dataName.append(root1["ACK"].asString()));
|
||||
data->setName(interest.getName());
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
data->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << "<< D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
cout << "output of seq content" << localMap[stoi(ackSeq[1])] << endl;
|
||||
cout << "output of seq content" << storeRequestIdSeq[interest.getName()[-4].toUri()].size() << endl;
|
||||
cout << "output of seq content" << requestMap[interest.getName()[-4].toUri()] << endl;
|
||||
if(storeRequestIdSeq[interest.getName()[-4].toUri()].size() == requestMap[interest.getName()[-4].toUri()]){
|
||||
//TODO:feed to minhttp parser
|
||||
parseStr="";
|
||||
auto it = storeRequestIdSeq[interest.getName()[-4].toUri()].begin();
|
||||
while(it != storeRequestIdSeq[interest.getName()[-4].toUri()].end()) {
|
||||
cout<<it->second<<endl;
|
||||
parseStr.append(it->second);
|
||||
it++;
|
||||
}
|
||||
minWebRequest->ybParse(minWebMessage,minWebRequest,parseStr,interest);
|
||||
//MIN::MINWebServer::OnRequestReceiveListener(minWebRequest);
|
||||
pair<string,string> localPair(interest.getName()[-5].toUri(),interest.getName()[-4].toUri());
|
||||
//localPair[interest.getName()[4].toUri()]=interest.getName()[5].toUri();
|
||||
requestPacketId[localPair]=minWebRequest;
|
||||
responsePacketState[requestPacketId[localPair]]=true;
|
||||
//this->listener(minWebRequest);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Name dataName(interest.getName());
|
||||
|
||||
Json::Reader reader ;
|
||||
Json::Value root ;
|
||||
|
||||
string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ;
|
||||
reader.parse(str,root) ;
|
||||
cout << root["serverPrefix"].asString() << endl ;
|
||||
cout << root.toStyledString() << endl ;
|
||||
|
||||
static const std::string content = root.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
//TODO: /MIN/Nginx/test2/MIN_HTTP_SESSION/abcdxxx/RES-ccc
|
||||
}
|
||||
|
||||
void onInterest(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
std::cout << "<< I: " << interest << std::endl;
|
||||
// uuid_t uu;
|
||||
boost::uuids::uuid uu = boost::uuids::random_generator()();
|
||||
string uuid_string = boost::uuids::to_string(uu);
|
||||
std::cout << uu << std::endl;
|
||||
|
||||
if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
else if(interest.getName()[4].toUri()=="MIN_HTTP_RESPONSE"){
|
||||
minWebRequest->context["feature"]=interest.getName()[-3].toUri();
|
||||
minWebRequest->context["prefix"]=interest.getName().toUri();
|
||||
if(interest.getName()[-3].toUri()=="pull"){
|
||||
minWebRequest->context["number"]=stoi(interest.getName()[-4].toUri());
|
||||
}
|
||||
this->listener(minWebRequest);
|
||||
|
||||
Name dataName(interest.getName());
|
||||
// Get Response from blocking queue
|
||||
|
||||
Json::Reader reader ;
|
||||
Json::Value root1 ;
|
||||
|
||||
|
||||
root1["sessionId"]=uuid_string;
|
||||
cout << root1["sessionId"].asString() << endl ;
|
||||
cout << root1.toStyledString() << endl ;
|
||||
|
||||
static const std::string content = root1.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
// shared_ptr<MIN::MINWebResponse> response=make_shared<MIN::MINWebResponse>();
|
||||
// int length=0;
|
||||
// std::string responseStr;
|
||||
// std::vector<std::string> responseSeq;
|
||||
// ///MIN/Nginx/test2/MIN_HTTP_SESSION/MIN_HTTP_RESPONSE/sessionId/RES-requstId/ask
|
||||
// //response chunk data
|
||||
// if(interest.getName()[-3].toUri()=="ask"){
|
||||
// minWebRequest->context["feature"]=interest.getName()[-3].toUri();
|
||||
// minWebRequest->context["prefix"]=interest.getName().toUri();
|
||||
// vector<string> responseRequestId;
|
||||
// split(interest.getName()[6].toUri(),responseRequestId);
|
||||
// pair<string,string> localPair2(interest.getName()[5].toUri(),responseRequestId[1]);
|
||||
// cout << "responsestate" << responsePacketState[requestPacketId[localPair2]] << endl;
|
||||
// //cout << responseRequestId[0] << responseRequestId[1] << endl;
|
||||
// if(responsePacketState[requestPacketId[localPair2]]) {
|
||||
// // TODO 生成一个回复
|
||||
// Json::Value rootChunk ;
|
||||
// response->status = MIN::MINWebServer::MIN_HTTP_STATUS_OK;
|
||||
// response->contentType = MIN::MINWebServer::TEXT_HTML;
|
||||
// response->contentLength = 15;
|
||||
// response->body = R"({\"name\":\"wgh\", \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100
|
||||
//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100
|
||||
//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100
|
||||
//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100
|
||||
//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100
|
||||
//, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100, \"age\":100})";
|
||||
// // TODO deal response
|
||||
//// ifstream infile;
|
||||
//// string buffstr;
|
||||
//// infile.open("/home/yb/yb/index/index.html", ios::in);
|
||||
////// read line and line
|
||||
//// while(getline(infile, buffstr))
|
||||
//// {
|
||||
//// cout << buffstr << endl;// red line string
|
||||
//// break;
|
||||
//// }
|
||||
//// infile.close();
|
||||
//// if(minWebRequest->path=="/index.html")
|
||||
//// response->body="fuckbad";
|
||||
//// else
|
||||
//// response->body="fuckbad";
|
||||
// cout << "fuck bad " << endl;
|
||||
// responseStr.append("MIN-HTTP/1.0").append(" ").append("200").append(" ").append(
|
||||
// MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code;
|
||||
// responseStr.append("\r\n");
|
||||
// responseStr.append("Content-Type").append(" ").append(
|
||||
// MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType));
|
||||
// responseStr.append("\r\n");
|
||||
// responseStr.append("Content-Length").append(" ").append("15");
|
||||
// responseStr.append("\r\n\r\n");
|
||||
// responseStr.append(response->body);
|
||||
// storeResponseStr[localPair2]=responseStr;
|
||||
// cout << " fuck bad bda " << endl;
|
||||
// length=responseStr.length();
|
||||
// if(length<=7000){
|
||||
//// responseSeq[0]=storeResponseStr[localPair3];
|
||||
// storeResponseSeq[localPair2].push_back(responseStr);
|
||||
// cout << "fuck test" << endl;
|
||||
// }
|
||||
// else {
|
||||
// size_t sendSeq = responseStr.length() / 7000;
|
||||
// for (int i = 0; i <= sendSeq; i++) {
|
||||
// if (i == 0)
|
||||
// storeResponseSeq[localPair2].push_back(responseStr.substr(0, 6996));
|
||||
// else if (i==sendSeq-1)
|
||||
// storeResponseSeq[localPair2].push_back(responseStr.substr((7000 * i) , responseStr.length()));
|
||||
// else
|
||||
// storeResponseSeq[localPair2].push_back(responseStr.substr((7000 * i) + 1, 7000*(i+1)));
|
||||
// }
|
||||
// }
|
||||
// cout << " test split" << storeResponseSeq[localPair2][1] << endl;
|
||||
// /*
|
||||
// * SEND RESPONSE TO BROWSER CLIENT
|
||||
// */
|
||||
// Name dataName(interest.getName()); //TODO: need requestId
|
||||
// shared_ptr<Data> resData = make_shared<Data>();
|
||||
// resData->setName(dataName);
|
||||
// resData->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// lp::CachePolicy noCachePolicy;
|
||||
// noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
// resData->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// string urlStr="/MIN_HTTP_RESPONSE/";
|
||||
// rootChunk["type"]="0";
|
||||
// rootChunk["url"]=urlStr.append(responseRequestId[1]);
|
||||
// rootChunk["length"]=to_string(length);
|
||||
// //length=responseStr.length();
|
||||
// rootChunk["chunkSize"]="7000";
|
||||
// //string contentStr =rootChunk.asString();
|
||||
// cout << responseStr << endl;
|
||||
// resData->setContent(reinterpret_cast<const uint8_t *>(rootChunk.toStyledString().data()), rootChunk.toStyledString().size());
|
||||
// m_keyChain.sign(*resData);
|
||||
// std::cout << ">> D: " << "chunkdata packet :" << " " << *resData << std::endl;
|
||||
// m_face.put(*resData);
|
||||
// }
|
||||
// }
|
||||
// else if(interest.getName()[-3].toUri()=="pull"){
|
||||
// pair<string,string> localPair3(interest.getName()[5].toUri(),interest.getName()[6].toUri());
|
||||
// cout << storeResponseStr[localPair3].length() << endl;
|
||||
// cout << storeResponseSeq[localPair3][0] << endl;
|
||||
// Name dataName(interest.getName()); //TODO: need requestId
|
||||
// shared_ptr<Data> resData = make_shared<Data>();
|
||||
// resData->setName(dataName);
|
||||
// resData->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// lp::CachePolicy noCachePolicy;
|
||||
// noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
// resData->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
// cout << storeResponseSeq[localPair3][stoi(interest.getName()[-4].toUri())].length() << endl;
|
||||
// string contentStr1=storeResponseSeq[localPair3][stoi(interest.getName()[-4].toUri())];
|
||||
// resData->setContent(reinterpret_cast<const uint8_t*>(contentStr1.data()), contentStr1.size());
|
||||
// m_keyChain.sign(*resData);
|
||||
// std::cout << "<< D: " << "data packet :" << stoi(interest.getName()[-4].toUri()) << "th" << " " << *resData << std::endl;
|
||||
// m_face.put(*resData);
|
||||
// sessionIdMap[interest.getName()[5].toUri()]= false;
|
||||
// }
|
||||
}
|
||||
void onRegisterFailed(const Name& prefix, const std::string& reason)
|
||||
{
|
||||
std::cerr << "ERROR: Failed to register prefix \""
|
||||
<< prefix << "\" in local hub's daemon (" << reason << ")"
|
||||
<< std::endl;
|
||||
m_face.shutdown();
|
||||
}
|
||||
|
||||
private:
|
||||
Face m_face;
|
||||
KeyChain m_keyChain;
|
||||
boost::uuids::uuid uu;
|
||||
string uuid_string;
|
||||
string parseStr;
|
||||
int seq=0;
|
||||
map<string,int> requestMap; //GR-0->requestId0
|
||||
map<string,int> seqMap; //requestId0->loopNum(seq-i)
|
||||
int requestSeq=-1;
|
||||
map<string,map<int,string>> storeRequestIdSeq;
|
||||
map<int,string> localMap;
|
||||
map<int,string> storeSeq;
|
||||
map<string,bool> sessionIdMap;
|
||||
int loopNum;//TODO: loopnummap<requestId,loopnum>
|
||||
int seqNum;
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Sessionpro producer;
|
||||
try {
|
||||
producer.run();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "ERROR: " << e.what() << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Sessionpro::onInterestUserDetect(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
std::cout << ">> I: " << interest << std::endl;
|
||||
if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
|
||||
Name dataName(interest.getName());
|
||||
|
||||
Json::Reader reader ;
|
||||
Json::Value root ;
|
||||
|
||||
string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ;
|
||||
reader.parse(str,root) ;
|
||||
cout << root["serverPrefix"].asString() << endl ;
|
||||
cout << root.toStyledString() << endl ;
|
||||
|
||||
static const std::string content = root.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
}
|
||||
|
||||
void Sessionpro::onInterestDetect(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
std::cout << ">> I: " << interest << std::endl;
|
||||
if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
|
||||
Name dataName(interest.getName());
|
||||
|
||||
Json::Reader reader ;
|
||||
Json::Value root ;
|
||||
|
||||
string str = "{\"serverPrefix\":\"/MIN/Nginx/test2\"}" ;
|
||||
reader.parse(str,root) ;
|
||||
cout << root["serverPrefix"].asString() << endl ;
|
||||
cout << root.toStyledString() << endl ;
|
||||
|
||||
static const std::string content = root.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
}
|
||||
|
||||
void Sessionpro::onInterest(const InterestFilter& filter, const Interest& interest)
|
||||
{
|
||||
std::cout << "<< I: " << interest << std::endl;
|
||||
// uuid_t uu;
|
||||
boost::uuids::uuid uu = boost::uuids::random_generator()();
|
||||
string uuid_string = boost::uuids::to_string(uu);
|
||||
std::cout << uu << std::endl;
|
||||
|
||||
if(interest.hasApplicationParameters()){ // 0.6.6版本ndn-cxx
|
||||
string intParaStr((char *)
|
||||
interest.getApplicationParameters().value(),
|
||||
interest.getApplicationParameters().value_size());
|
||||
}
|
||||
|
||||
Name dataName(interest.getName());
|
||||
|
||||
Json::Reader reader ;
|
||||
Json::Value root1 ;
|
||||
|
||||
|
||||
root1["sessionId"]=uuid_string;
|
||||
cout << root1["sessionId"].asString() << endl ;
|
||||
cout << root1.toStyledString() << endl ;
|
||||
|
||||
static const std::string content = root1.toStyledString();
|
||||
|
||||
shared_ptr<Data> data = make_shared<Data>();
|
||||
data->setName(dataName);
|
||||
data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// 数据缓存在节点中,立即变旧
|
||||
|
||||
data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
||||
|
||||
|
||||
m_keyChain.sign(*data);
|
||||
std::cout << ">> D: " << *data << std::endl;
|
||||
m_face.put(*data);
|
||||
}
|
||||
void Sessionpro::onRegisterFailed(const Name& prefix, const std::string& reason)
|
||||
{
|
||||
std::cerr << "ERROR: Failed to register prefix \""
|
||||
<< prefix << "\" in local hub's daemon (" << reason << ")"
|
||||
<< std::endl;
|
||||
m_face.shutdown();
|
||||
}
|
||||
|
||||
void Sessionpro::setOnRequestListener(const MIN::MINWebServer::OnRequestReceiveListener &onRequestReceiveListener) {
|
||||
this->listener = onRequestReceiveListener;
|
||||
}
|
||||
|
||||
void Sessionpro::replyResponse(std::shared_ptr<MIN::MINWebResponse> response) {
|
||||
cout << response->context["feature"] << endl;
|
||||
cout << response->contentType <<endl;
|
||||
vector<string> responseSeq;
|
||||
std::string responseStr;
|
||||
responseStr.append("MIN-HTTP/1.0").append(" ").append("200").append(" ").append(
|
||||
MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code;
|
||||
responseStr.append("\r\n");
|
||||
responseStr.append("Content-Type").append(" ").append(
|
||||
MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType));
|
||||
responseStr.append("\r\n");
|
||||
responseStr.append("Content-Length").append(" ").append(to_string(response->contentLength));
|
||||
responseStr.append("\r\n\r\n");
|
||||
std::cout << "-----------------------------------reply" << endl;
|
||||
std::cout << std::string((char *)response->content, response->contentLength) << std::endl;
|
||||
if(response->body.empty())
|
||||
responseStr.append(std::string((char *)response->content, response->contentLength));
|
||||
else
|
||||
responseStr.append(response->body);
|
||||
// std::cout << response->body << std::endl;
|
||||
cout << "this is str: " << responseStr << endl;
|
||||
int length=responseStr.length();
|
||||
cout << response->contentLength << endl;
|
||||
cout << "this is length " << length << endl;
|
||||
if(length<=7000){
|
||||
// responseSeq[0]=storeResponseStr[localPair3];
|
||||
responseSeq.push_back(responseStr);
|
||||
cout << "fuck test" << endl;
|
||||
cout << responseStr << endl;
|
||||
cout << responseSeq[0] <<endl;
|
||||
}
|
||||
else {
|
||||
size_t sendSeq = responseStr.length() / 7000;
|
||||
for (int i = 0; i <= sendSeq; i++) {
|
||||
if (i == 0)
|
||||
responseSeq.push_back(responseStr.substr(0, 7000));
|
||||
else if (i==sendSeq)
|
||||
responseSeq.push_back(responseStr.substr((7000 * i) , responseStr.length()));
|
||||
else
|
||||
responseSeq.push_back(responseStr.substr((7000 * i) + 1, 7000*(i+1)));
|
||||
}
|
||||
}
|
||||
cout << " test split" << responseSeq[0] << endl;
|
||||
if(response->context["feature"]=="ask"){
|
||||
|
||||
|
||||
/*
|
||||
* SEND RESPONSE TO BROWSER CLIENT
|
||||
*/
|
||||
Name dataName(Name(response->context["prefix"])); //TODO: need requestId
|
||||
shared_ptr<Data> resData = make_shared<Data>();
|
||||
Json::Value rootChunk ;
|
||||
resData->setName(dataName);
|
||||
resData->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
resData->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
string urlStr="";
|
||||
rootChunk["type"]="0";
|
||||
rootChunk["url"]=urlStr.append(response->context["prefix"]);
|
||||
rootChunk["length"]=to_string(length);
|
||||
//length=responseStr.length();
|
||||
rootChunk["chunkSize"]="7000";
|
||||
//string contentStr =rootChunk.asString();
|
||||
cout << rootChunk.toStyledString().size()<< endl;
|
||||
resData->setContent(reinterpret_cast<const uint8_t *>(rootChunk.toStyledString().data()), rootChunk.toStyledString().size());
|
||||
m_keyChain.sign(*resData);
|
||||
std::cout << "<< D: " << "chunkdata packet :" << " " << *resData << std::endl;
|
||||
m_face.put(*resData);
|
||||
}
|
||||
else if(response->context["feature"]=="pull"){
|
||||
// pair<string,string> localPair3(interest.getName()[5].toUri(),interest.getName()[6].toUri());
|
||||
// cout << storeResponseStr[localPair3].length() << endl;
|
||||
// cout << storeResponseSeq[localPair3][0] << endl;
|
||||
Name dataName(Name(response->context["prefix"])); //TODO: need requestId
|
||||
shared_ptr<Data> resData = make_shared<Data>();
|
||||
resData->setName(dataName);
|
||||
resData->setFreshnessPeriod(0_s); // 10 seconds
|
||||
lp::CachePolicy noCachePolicy;
|
||||
noCachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
|
||||
resData->setTag(make_shared<lp::CachePolicyTag>(noCachePolicy));
|
||||
cout << response->context["number"] <<endl;
|
||||
cout << responseSeq[0] << endl;
|
||||
//cout << storeResponseSeq[localPair3][stoi(interest.getName()[-4].toUri())].length() << endl;
|
||||
string contentStr1=responseSeq[response->context["number"]];
|
||||
resData->setContent(reinterpret_cast<const uint8_t*>(contentStr1.data()), contentStr1.size());
|
||||
m_keyChain.sign(*resData);
|
||||
std::cout << "<< D: " << "data packet :" << response->context["number"] << "th" << " " << *resData << std::endl;
|
||||
m_face.put(*resData);
|
||||
//sessionIdMap[interest.getName()[5].toUri()]= false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
//
|
||||
//int main(int argc, char** argv)
|
||||
//{
|
||||
// Sessionpro producer;
|
||||
// try {
|
||||
// producer.run();
|
||||
// }
|
||||
// catch (const std::exception& e) {
|
||||
// std::cerr << "ERROR: " << e.what() << std::endl;
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
//if(!storeSeq.count(stoi(ackSeq[1]))){
|
||||
// seqNum++;
|
||||
// std::cout << "SEQ >> I: " << interest << std::endl;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// Created by yb on 11/27/20.
|
||||
//
|
||||
|
||||
#ifndef MIN_NGINX_MINHTTPSESSION_H
|
||||
#define MIN_NGINX_MINHTTPSESSION_H
|
||||
#include <stdio.h>
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
#include <boost/uuid/uuid_generators.hpp>
|
||||
#include <ndn-cxx/face.hpp>
|
||||
#include <ndn-cxx/lp/tags.hpp>
|
||||
#include <iostream>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "../message/MINWebRequest.h"
|
||||
#include "../message/MINWebResponse.h"
|
||||
#include "MINHttpServer.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
using namespace ndn ;
|
||||
using namespace std ;
|
||||
|
||||
class Sessionpro {
|
||||
public:
|
||||
void run();
|
||||
shared_ptr<MIN::MINWebRequest> minWebRequest= make_shared<MIN::MINWebRequest>();
|
||||
MIN::MINWebMessage minWebMessage;
|
||||
//typedef std::function<void(std::shared_ptr<MIN::MINWebRequest> &request)> OnRequestReceiveListener;
|
||||
|
||||
void setOnRequestListener(const MIN::MINWebServer::OnRequestReceiveListener &onRequestReceiveListener);
|
||||
void replyResponse(std::shared_ptr<MIN::MINWebResponse> response);
|
||||
|
||||
private:
|
||||
vector<string> split(const string& s, vector<string>& tokens, char delim );
|
||||
void onSuccess(const Name& name);
|
||||
void onInterestBuild(const InterestFilter& filter, const Interest& interest);
|
||||
void onInterestUserDetect(const InterestFilter& filter, const Interest& interest);
|
||||
void onInterestDetect(const InterestFilter& filter, const Interest& interest);
|
||||
void onInterest(const InterestFilter& filter, const Interest& interest);
|
||||
void onRegisterFailed(const Name& prefix, const std::string& reason);
|
||||
private:
|
||||
Face m_face;
|
||||
KeyChain m_keyChain;
|
||||
boost::uuids::uuid uu;
|
||||
string uuid_string;
|
||||
string parseStr;
|
||||
int seq=0;
|
||||
map<string,int> requestMap; //GR-0->requestId0
|
||||
map<string,int> seqMap; //requestId0->loopNum(seq-i)
|
||||
int requestSeq=-1;
|
||||
map<string,map<int,string>> storeRequestIdSeq;
|
||||
map<int,string> localMap;
|
||||
map<int,string> storeSeq;
|
||||
map<string,bool> sessionIdMap;
|
||||
int loopNum;//TODO: loopnummap<requestId,loopnum>
|
||||
int seqNum;
|
||||
int requestIdSeq=0;
|
||||
map<shared_ptr<MIN::MINWebRequest>,bool> responsePacketState;
|
||||
map<pair<string,string>,shared_ptr<MIN::MINWebRequest>> requestPacketId;
|
||||
map<pair<string,string>,string> storeResponseStr;
|
||||
map<pair<string,string>,vector<string>> storeResponseSeq;
|
||||
MIN::MINWebServer::OnRequestReceiveListener listener;
|
||||
};
|
||||
|
||||
|
||||
#endif //MIN_NGINX_MINHTTPSESSION_H
|
||||
@@ -1,78 +0,0 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:26.
|
||||
//
|
||||
|
||||
#ifndef MIN_NGINX_MINWEBMESSAGE_H
|
||||
#define MIN_NGINX_MINWEBMESSAGE_H
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <ndn-cxx/face.hpp>
|
||||
#include "../utils/StringUtils.h"
|
||||
#include "../common/MINHttpDef.h"
|
||||
#include "../utils/json.h"
|
||||
|
||||
namespace MIN {
|
||||
#define MIN_MAP std::map
|
||||
|
||||
enum class MessageParserState : uint8_t
|
||||
{
|
||||
NONE,
|
||||
PARSING_START_LINE,
|
||||
START_LINE_REQUEST,
|
||||
START_LINE_RESPONSE,
|
||||
HEADER_KEY,
|
||||
HEADER_VALUE,
|
||||
PARSING_BODY,
|
||||
};
|
||||
|
||||
enum MINParserType {
|
||||
MIN_WEB_REQUEST = 0,
|
||||
MIN_WEB_RESPONSE = 1,
|
||||
};
|
||||
|
||||
typedef std::map<std::string, std::string, StringCaseLess> MINHttpHeaders;
|
||||
typedef std::string MINHttpBody;
|
||||
|
||||
class MINWebMessage {
|
||||
public:
|
||||
// /MIN/Nginx/test/getData?size=1&page=2
|
||||
// /MIN/Nginx/test/getPost/:filedId => /MIN/Nginx/test/getPost/2
|
||||
std::string url;
|
||||
int type;
|
||||
|
||||
MessageParserState state;
|
||||
|
||||
// SignatureInfo
|
||||
std::shared_ptr<ndn::SignatureInfo> signatureInfo;
|
||||
|
||||
// headers
|
||||
MINHttpHeaders headers;
|
||||
|
||||
// body
|
||||
MINHttpBody body;
|
||||
|
||||
// structured content
|
||||
void *content; // DATA_NO_COPY
|
||||
int contentLength;
|
||||
MINWebServer::MINHttpContentType contentType;
|
||||
nlohmann::json json; // APPLICATION_JSON
|
||||
|
||||
virtual MINWebMessage& SetHeader(const std::string& name, const std::string& value)
|
||||
{
|
||||
headers[name] = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
virtual void reset() {
|
||||
type = MIN_WEB_REQUEST;
|
||||
state= MessageParserState::NONE;
|
||||
url = "";
|
||||
signatureInfo = std::make_shared<ndn::SignatureInfo>();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MIN_NGINX_MINWEBMESSAGE_H
|
||||
@@ -1,192 +0,0 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:21.
|
||||
//
|
||||
|
||||
#include <ndn-cxx/interest.hpp>
|
||||
#include <ndn-cxx/signature-info.hpp>
|
||||
#include "MINWebRequest.h"
|
||||
#include <ndn-cxx/face.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "../common/logger.h"
|
||||
|
||||
namespace MIN {
|
||||
using namespace ndn;
|
||||
using namespace std;
|
||||
MIN_LOG_INIT(MINWebRequest);
|
||||
|
||||
// MINWebMessage minWebMessage;
|
||||
// MINWebRequest request;
|
||||
// MINWebServer::MINHttpMethod httpMethod;
|
||||
shared_ptr<SignatureInfo> getSignatureInfo(const Interest &interest) {
|
||||
auto &name = interest.getName();
|
||||
if (name.size() > ndn::signed_interest::MIN_SIZE) {
|
||||
try {
|
||||
return make_shared<SignatureInfo>(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue());
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string& buf,const ndn::Interest& interest){
|
||||
minWebMessage.reset();
|
||||
std::vector<uint8_t> buffer = std::vector<uint8_t>(buf.begin(), buf.end());
|
||||
minWebMessage.state=MessageParserState::PARSING_START_LINE;
|
||||
std::string temp="";
|
||||
bool skipNext=false;
|
||||
std::string headerKey = "";
|
||||
MIN_LOG_DEBUG("??????????????????");
|
||||
// whether or not a message body is present
|
||||
bool hasMessageBody = false;
|
||||
|
||||
Name name = interest.getName();
|
||||
// 判断有没有签名
|
||||
auto signatureInfoPtr = getSignatureInfo(interest);
|
||||
if (signatureInfoPtr) {
|
||||
// 有签名则去掉后两个Component
|
||||
name = interest.getName().getSubName(0, interest.getName().size() - 2);
|
||||
}
|
||||
request.url = name.toUri();
|
||||
auto lastComponentStr = name[-1].toUri();
|
||||
auto position = lastComponentStr.find(std::string(MIN_WEB_CHUNK_KEY) + "%3A");
|
||||
if (position != std::basic_string<char>::npos) {
|
||||
// 最后一个Component表示块号
|
||||
name = name.getSubName(0, name.size() - 1);
|
||||
try {
|
||||
request.queryParams[MIN_WEB_CHUNK_KEY] = lastComponentStr.substr(strlen(MIN_WEB_CHUNK_KEY) + 3);
|
||||
} catch (...) {
|
||||
std::cerr << "Interest Parser error: " << interest.toUri();
|
||||
}
|
||||
}
|
||||
cout << " this is url :" << request.url;
|
||||
request.requestType=MIN_WEB_REQUEST_TYPE_STATIC;
|
||||
request.service="/MIN/Nginx/test2";
|
||||
|
||||
// the index at which the message body begins
|
||||
size_t bodyStartIndex = 0;
|
||||
for(size_t index=0;index<buffer.size();index++){
|
||||
uint8_t character=buffer[index];
|
||||
if(skipNext){
|
||||
skipNext= false;
|
||||
break;
|
||||
}
|
||||
if(minWebMessage.state==MessageParserState::PARSING_BODY){
|
||||
hasMessageBody=true;
|
||||
bodyStartIndex=index;
|
||||
for(size_t bodyindex=bodyStartIndex;bodyindex<buffer.size();bodyindex++){
|
||||
uint8_t bodycharacter=buffer[index];
|
||||
temp+=bodycharacter;
|
||||
}
|
||||
if(request.headers["Content-Type"]=="application/json"){
|
||||
request.body = request.json.parse(temp);
|
||||
}
|
||||
else{
|
||||
request.body=temp;
|
||||
}
|
||||
|
||||
cout << " this is headers :" << request.body << endl;
|
||||
cout << " this is method :" << request.method << endl;
|
||||
break;
|
||||
}
|
||||
if(minWebMessage.state==MessageParserState::PARSING_START_LINE){
|
||||
if(character==' '){
|
||||
request.method=MINWebServer::getMINHttpMethodEnumFromStr(temp);
|
||||
minWebMessage.state = MessageParserState::START_LINE_REQUEST;
|
||||
temp = "";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if(minWebMessage.state==MessageParserState::START_LINE_REQUEST){
|
||||
if(character == ' '){
|
||||
request.path=temp;
|
||||
temp="";
|
||||
continue;
|
||||
}
|
||||
else if(character=='/r'){
|
||||
request.version=temp;
|
||||
temp="";
|
||||
minWebMessage.state=MessageParserState::HEADER_KEY;
|
||||
skipNext=true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (minWebMessage.state == MessageParserState::START_LINE_RESPONSE)
|
||||
{
|
||||
// if we are at a space, then we have hit the status code for the response
|
||||
if (character == ' ')
|
||||
{
|
||||
int code = std::stoi(temp);
|
||||
//minWebMessage->SetStatusCode(static_cast<uint16_t>(code));
|
||||
temp = "";
|
||||
continue;
|
||||
}
|
||||
// if we are at a carriage return start, then set the status message for
|
||||
// the response, this can be blank in which it will use a generated status
|
||||
//
|
||||
// this will also set the state of the parser to move on to headers
|
||||
else if (character == '\r')
|
||||
{
|
||||
//minWebMessage->SetStatusMessage(temp);
|
||||
temp = "";
|
||||
minWebMessage.state = MessageParserState::HEADER_KEY;
|
||||
skipNext = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == ':')
|
||||
{
|
||||
headerKey = temp;
|
||||
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::HEADER_VALUE;
|
||||
|
||||
// HTTP defines that the next character in a header should be a space
|
||||
// so skip that for parsing the value of the header
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
// if we are parsing header values and hit the beginning of a carriage return then
|
||||
// it is time to add the header to the message with the key and value, and move the
|
||||
// state back to parsing keys
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_VALUE && character == '\r')
|
||||
{
|
||||
request.SetHeader(headerKey, temp);
|
||||
|
||||
headerKey = "";
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::HEADER_KEY;
|
||||
|
||||
// skip the next character as it will just be a newline
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
// if we are parsing header keys and we hit a carriage return, then we should assume
|
||||
// that the headers have ended, and that we are now parsing a message body.
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == '\r')
|
||||
{
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::PARSING_BODY;
|
||||
|
||||
// skip the next character as it'll be a newline
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
temp+=character;
|
||||
}
|
||||
}
|
||||
// std::shared_ptr<MINWebRequest> MINWebRequest::Parse(MINWebMessage MINwebmessage,const std::string &buffer,const Interest& interest){
|
||||
// return Parser(MINwebmessage, std::vector<uint8_t>(buffer.begin(), buffer.end()),interest);
|
||||
// }
|
||||
|
||||
void MINWebRequest::ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string &buffer, const Interest &interest) {
|
||||
std::cout << "111" << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:21.
|
||||
//
|
||||
|
||||
#ifndef MIN_NGINX_MINWEBREQUEST_H
|
||||
#define MIN_NGINX_MINWEBREQUEST_H
|
||||
|
||||
#include "MINWebMessage.h"
|
||||
#include <ndn-cxx/security/signing-info.hpp>
|
||||
#include "../common/MINHttpDef.h"
|
||||
|
||||
#define MIN_WEB_CHUNK_KEY "MIN_WEB_CHUNK"
|
||||
|
||||
namespace MIN {
|
||||
|
||||
using namespace ndn;
|
||||
|
||||
enum MINWebRequestType {
|
||||
MIN_WEB_REQUEST_TYPE_STATIC = 0, // 静态请求
|
||||
};
|
||||
|
||||
class MINWebRequest : public MINWebMessage {
|
||||
public:
|
||||
|
||||
// /MIN/Nginx/test
|
||||
std::string service;
|
||||
|
||||
// /index.html
|
||||
std::string path;
|
||||
|
||||
// 0
|
||||
MINWebRequestType requestType;
|
||||
|
||||
// method
|
||||
MINWebServer::MINHttpMethod method;
|
||||
|
||||
//string method
|
||||
std::string strmethod;
|
||||
//version
|
||||
//MINWebServer::MINHttpVersion version;
|
||||
std::string version;
|
||||
|
||||
// queryParams
|
||||
MIN_MAP<std::string, std::string> queryParams;
|
||||
|
||||
MINWebRequest& SetPath(std::string &mpath){
|
||||
this->path=mpath;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
MINWebRequest& SetHeader(const std::string& name, const std::string& value)
|
||||
{
|
||||
headers[name] = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void init() {
|
||||
type = MIN_WEB_REQUEST;
|
||||
}
|
||||
|
||||
void reset() override {
|
||||
MINWebMessage::reset();
|
||||
service = "";
|
||||
path = "";
|
||||
requestType = MIN_WEB_REQUEST_TYPE_STATIC;
|
||||
queryParams.clear();
|
||||
}
|
||||
|
||||
void ybParse(MINWebMessage minWebMessage,MINWebRequest request, const std::string &buf,const Interest& interest);
|
||||
// {
|
||||
// renturn std::shared_ptr<MINWebRequest> Parser(MINWebMessage MINwebmessage,const std::vector<uint8_t>& buffer,const Interest& interest);
|
||||
// }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //MIN_NGINX_MINWEBREQUEST_H
|
||||
@@ -1,8 +0,0 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:21.
|
||||
//
|
||||
|
||||
#include "MINWebResponse.h"
|
||||
|
||||
namespace MIN {
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:21.
|
||||
//
|
||||
|
||||
#ifndef MIN_NGINX_MINWEBRESPONSE_H
|
||||
#define MIN_NGINX_MINWEBRESPONSE_H
|
||||
|
||||
#include "MINWebMessage.h"
|
||||
#include "../common/MINHttpDef.h"
|
||||
|
||||
namespace MIN {
|
||||
|
||||
class MINWebResponse : public MINWebMessage {
|
||||
public:
|
||||
MINWebServer::MINHttpStatus status = MINWebServer::MIN_HTTP_STATUS_NOT_FOUND;
|
||||
|
||||
void init() {
|
||||
type = MIN_WEB_RESPONSE;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //MIN_NGINX_MINWEBRESPONSE_H
|
||||
@@ -1,16 +1,214 @@
|
||||
//
|
||||
// Created by SunnyQjm on 2020/10/14 上午10:21.
|
||||
// Created by yb on 2020/11/14
|
||||
//
|
||||
|
||||
#include <ndn-cxx/interest.hpp>
|
||||
#include <ndn-cxx/signature-info.hpp>
|
||||
#include <ndn-cxx/ndn-cxx/interest.hpp>
|
||||
#include <ndn-cxx/ndn-cxx/signature-info.hpp>
|
||||
#include "MINWebRequest.h"
|
||||
#include <ndn-cxx/face.hpp>
|
||||
#include <string>
|
||||
#include "../common/logger.h"
|
||||
#include <jsoncpp/json/json.h>
|
||||
|
||||
namespace MIN {
|
||||
using namespace ndn;
|
||||
using namespace std;
|
||||
|
||||
// MINWebMessage minWebMessage;
|
||||
// MINWebRequest request;
|
||||
// MINWebServer::MINHttpMethod httpMethod;
|
||||
shared_ptr<SignatureInfo> getSignatureInfo(const Interest &interest) {
|
||||
auto &name = interest.getName();
|
||||
if (name.size() > ndn::signed_interest::MIN_SIZE) {
|
||||
try {
|
||||
return make_shared<SignatureInfo>(name[ndn::signed_interest::POS_SIG_INFO].blockFromValue());
|
||||
} catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MINWebRequest::ybParse(MINWebMessage minWebMessage,std::shared_ptr<MIN::MINWebRequest> request,const std::string& buf,const ndn::Interest& interest){
|
||||
minWebMessage.reset();
|
||||
std::vector<uint8_t> buffer = std::vector<uint8_t>(buf.begin(), buf.end());
|
||||
cout << " ?????????????????" << endl;
|
||||
minWebMessage.state=MessageParserState::PARSING_START_LINE;
|
||||
std::string temp="";
|
||||
bool skipNext=false;
|
||||
std::string headerKey = "";
|
||||
|
||||
// whether or not a message body is present
|
||||
bool hasMessageBody = false;
|
||||
|
||||
Name name = interest.getName();
|
||||
// 判断有没有签名
|
||||
cout << name.toUri() << endl;
|
||||
auto signatureInfoPtr = getSignatureInfo(interest);
|
||||
if (signatureInfoPtr) {
|
||||
// 有签名则去掉后两个Component
|
||||
name = interest.getName().getSubName(0, interest.getName().size() - 2);
|
||||
}
|
||||
string serviceprefix="/MIN/Web/test";
|
||||
request->url = "";
|
||||
cout << request->url << endl;
|
||||
auto lastComponentStr = name[-1].toUri();
|
||||
auto position = lastComponentStr.find(std::string(MIN_WEB_CHUNK_KEY) + "%3A");
|
||||
cout << position << endl;
|
||||
if (position != std::basic_string<char>::npos) {
|
||||
// 最后一个Component表示块号
|
||||
name = name.getSubName(0, name.size() - 1);
|
||||
try {
|
||||
request->queryParams[MIN_WEB_CHUNK_KEY] = lastComponentStr.substr(strlen(MIN_WEB_CHUNK_KEY) + 3);
|
||||
} catch (...) {
|
||||
std::cerr << "Interest Parser error: " << interest.toUri();
|
||||
}
|
||||
}
|
||||
cout << " this is url :" << request->url << endl;
|
||||
request->requestType=MIN_WEB_REQUEST_TYPE_STATIC;
|
||||
request->service="/MIN/Web/test";
|
||||
cout << " fuck :" << request->url << endl;
|
||||
|
||||
// the index at which the message body begins
|
||||
size_t bodyStartIndex = 0;
|
||||
for(size_t index=0;index<buffer.size();index++){
|
||||
uint8_t character=buffer[index];
|
||||
|
||||
if(skipNext){
|
||||
skipNext= false;
|
||||
continue;
|
||||
}
|
||||
if(minWebMessage.state==MessageParserState::PARSING_BODY){
|
||||
hasMessageBody=true;
|
||||
bodyStartIndex=index;
|
||||
for(size_t bodyindex=bodyStartIndex;bodyindex<buffer.size();bodyindex++){
|
||||
uint8_t bodycharacter=buffer[bodyindex];
|
||||
cout << bodycharacter << endl;
|
||||
temp+=bodycharacter;
|
||||
}
|
||||
cout << temp ;
|
||||
if(request->headers["Content-Type"]=="application/json"){
|
||||
//request->body = request->json.parse(temp);
|
||||
cout << "this is body " << temp;
|
||||
Json::Reader reader ;
|
||||
Json::Value root ;
|
||||
reader.parse(temp,root) ;
|
||||
cout << root.toStyledString() << endl ;
|
||||
request->body=root.toStyledString();
|
||||
}
|
||||
else{
|
||||
request->body=temp;
|
||||
}
|
||||
// request->url=request->service.append(request->path);
|
||||
cout << " this is url :" << request->url << endl;
|
||||
cout << " this is headers :" << request->body << endl;
|
||||
cout << " this is method :" << request->strmethod << endl;
|
||||
break;
|
||||
}
|
||||
if(minWebMessage.state==MessageParserState::PARSING_START_LINE){
|
||||
cout << "character" << character << endl;
|
||||
if(character==' '){
|
||||
request->method=MINWebServer::getMINHttpMethodEnumFromStr(temp);
|
||||
request->strmethod=temp;
|
||||
minWebMessage.state = MessageParserState::START_LINE_REQUEST;
|
||||
temp = "";
|
||||
cout << "this is method" << request->strmethod << endl;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if(minWebMessage.state==MessageParserState::START_LINE_REQUEST){
|
||||
if(character == ' '){
|
||||
request->path=temp;
|
||||
temp="";
|
||||
cout << "this is path " << request->path << endl;
|
||||
request->url=request->service.append(request->path);
|
||||
continue;
|
||||
}
|
||||
else if(character== '\r'){
|
||||
request->version=temp;
|
||||
cout << " this is version" << request->version << endl;
|
||||
temp="";
|
||||
minWebMessage.state=MessageParserState::HEADER_KEY;
|
||||
skipNext=true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (minWebMessage.state == MessageParserState::START_LINE_RESPONSE)
|
||||
{
|
||||
// if we are at a space, then we have hit the status code for the response
|
||||
if (character == ' ')
|
||||
{
|
||||
int code = std::stoi(temp);
|
||||
//minWebMessage->SetStatusCode(static_cast<uint16_t>(code));
|
||||
temp = "";
|
||||
continue;
|
||||
}
|
||||
// if we are at a carriage return start, then set the status message for
|
||||
// the response, this can be blank in which it will use a generated status
|
||||
//
|
||||
// this will also set the state of the parser to move on to headers
|
||||
else if (character == '\r')
|
||||
{
|
||||
//minWebMessage->SetStatusMessage(temp);
|
||||
temp = "";
|
||||
minWebMessage.state = MessageParserState::HEADER_KEY;
|
||||
skipNext = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == ':')
|
||||
{
|
||||
headerKey = temp;
|
||||
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::HEADER_VALUE;
|
||||
|
||||
// HTTP defines that the next character in a header should be a space
|
||||
// so skip that for parsing the value of the header
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
// if we are parsing header values and hit the beginning of a carriage return then
|
||||
// it is time to add the header to the message with the key and value, and move the
|
||||
// state back to parsing keys
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_VALUE && character == '\r')
|
||||
{
|
||||
request->SetHeader(headerKey, temp);
|
||||
cout << "this is header" << request->headers[headerKey] << endl;
|
||||
headerKey = "";
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::HEADER_KEY;
|
||||
|
||||
// skip the next character as it will just be a newline
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
// if we are parsing header keys and we hit a carriage return, then we should assume
|
||||
// that the headers have ended, and that we are now parsing a message body.
|
||||
else if (minWebMessage.state == MessageParserState::HEADER_KEY && character == '\r')
|
||||
{
|
||||
temp = "";
|
||||
|
||||
minWebMessage.state = MessageParserState::PARSING_BODY;
|
||||
|
||||
// skip the next character as it'll be a newline
|
||||
skipNext = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
temp+=character;
|
||||
cout << temp << endl;
|
||||
}
|
||||
}
|
||||
// std::shared_ptr<MINWebRequest> MINWebRequest::Parse(MINWebMessage MINwebmessage,const std::string &buffer,const Interest& interest){
|
||||
// return Parser(MINwebmessage, std::vector<uint8_t>(buffer.begin(), buffer.end()),interest);
|
||||
// }
|
||||
|
||||
// void MINWebRequest::ybParse(MINWebMessage minWebMessage,MINWebRequest request,const std::string &buffer, const Interest &interest) {
|
||||
// std::cout << "111" << std::endl;
|
||||
// }
|
||||
}
|
||||
@@ -47,6 +47,20 @@ namespace MIN {
|
||||
// 可以再 preprocessor -> controller -> postprocessor 这条处理链的任意缓解向上下文中存取信息
|
||||
nlohmann::json context;
|
||||
|
||||
|
||||
MINWebRequest& SetPath(std::string &mpath){
|
||||
this->path=mpath;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
MINWebRequest& SetHeader(const std::string& name, const std::string& value)
|
||||
{
|
||||
headers[name] = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void init() {
|
||||
type = MIN_WEB_REQUEST;
|
||||
}
|
||||
@@ -59,8 +73,11 @@ namespace MIN {
|
||||
queryParams.clear();
|
||||
}
|
||||
|
||||
void ybParse(MINWebMessage minWebMessage,std::shared_ptr<MINWebRequest> request, const std::string &buf,const Interest& interest);
|
||||
// {
|
||||
// renturn std::shared_ptr<MINWebRequest> Parser(MINWebMessage MINwebmessage,const std::vector<uint8_t>& buffer,const Interest& interest);
|
||||
// }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //MIN_NGINX_MINWEBREQUEST_H
|
||||
#endif
|
||||
|
||||
@@ -51,4 +51,4 @@ namespace MIN {
|
||||
}
|
||||
|
||||
|
||||
#endif //MIN_NGINX_MINWEBRESPONSE_H
|
||||
#endif //MIN_NGINX_MINWEBRESPONSE_H
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <filesystem>
|
||||
//#include <filesystem>
|
||||
#include <sys/stat.h>
|
||||
#include "../base/MBuf.h"
|
||||
|
||||
|
||||
@@ -7,34 +7,84 @@
|
||||
#include "MINWebServer/server/Server.h"
|
||||
#include "MINWebServer/message/MINWebRequest.h"
|
||||
#include "MINWebServer/message/MINWebResponse.h"
|
||||
#include "MINWebServer/http/MINHttpSession.h"
|
||||
#include "MINWebServer/http/MINHttpServerImpl.h"
|
||||
|
||||
using namespace MIN;
|
||||
using namespace MIN::MINWebServer;
|
||||
|
||||
|
||||
class MINHttpServerImpl : public MINHttpServer {
|
||||
class MINHttpServerImpl1 : public MINHttpServer {
|
||||
public:
|
||||
void setOnRequestListener(const OnRequestReceiveListener &onRequestReceiveListener) override {
|
||||
this->listener = onRequestReceiveListener;
|
||||
}
|
||||
|
||||
void replyResponse(std::shared_ptr<MIN::MINWebResponse> response) override {
|
||||
std::cout << std::string((char *)response->content, response->contentLength) << std::endl;
|
||||
std::cout << response->body << std::endl;
|
||||
void replyResponse(std::shared_ptr<MIN::MINWebResponse> response) override{
|
||||
// TODO deal response
|
||||
sessionpro.replyResponse(response);
|
||||
// std::string responseStr;
|
||||
// responseStr.append("MIN-HTTP/1.0").append(" ").append(MIN::MINWebServer::getMINHttpStatusStr(response->status)).append(" ").append(MIN::MINWebServer::getMINHttpStatusStr(response->status));//get status code;
|
||||
// responseStr.append("\r\n");
|
||||
// responseStr.append("Content-Type").append(" ").append(MIN::MINWebServer::getMINHttpContentTypeStr(response->contentType));
|
||||
// responseStr.append("Content-Length").append(" ").append(" ");
|
||||
// responseStr.append("\r\n\r\n");
|
||||
// responseStr.append(response->body);
|
||||
//
|
||||
// /*
|
||||
// * SEND RESPONSE TO BROWSER CLIENT
|
||||
// */
|
||||
// if(responseStr.length()<=7000){
|
||||
// ndn::KeyChain m_keyChain;
|
||||
// ndn::Face m_face;
|
||||
// //Name dataName(interest.getName()); //TODO: need requestId
|
||||
// shared_ptr<Data> data = make_shared<Data>();
|
||||
// //data->setName(dataName);
|
||||
// data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// // 数据缓存在节点中,立即变旧
|
||||
// data->setContent(reinterpret_cast<const uint8_t*>(responseStr.data()), responseStr.size());
|
||||
// m_keyChain.sign(*data);
|
||||
// std::cout << ">> D: " << "data packet :" << " " << *data << std::endl;
|
||||
// m_face.put(*data);
|
||||
// }
|
||||
// else {
|
||||
// size_t sendSeq = responseStr.length() / 7000;
|
||||
// std::vector<std::string> responseSeq;
|
||||
// for (int i = 0; i < sendSeq; i++) {
|
||||
// if (i == 0)
|
||||
// responseSeq[i] = responseStr.substr(0, 7000);
|
||||
// else
|
||||
// responseSeq[i] = responseStr.substr((7000 * i) + 1, 7000 * (i + 1));
|
||||
// }
|
||||
// ndn::KeyChain m_keyChain;
|
||||
// ndn::Face m_face;
|
||||
// //Name dataName(interest.getName()); //TODO: need requestId
|
||||
// shared_ptr<Data> data = make_shared<Data>();
|
||||
// //data->setName(dataName);
|
||||
// data->setFreshnessPeriod(0_s); // 10 seconds
|
||||
// // 数据缓存在节点中,立即变旧
|
||||
// for (int i = 0; i < sendSeq; i++) {
|
||||
// data->setContent(reinterpret_cast<const uint8_t *>(responseSeq[i].data()), responseSeq[i].size());
|
||||
// m_keyChain.sign(*data);
|
||||
// std::cout << ">> D: " << "data packet :" << i << " " << *data << std::endl;
|
||||
// m_face.put(*data);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// void replyResponse(std::shared_ptr<MIN::MINWebResponse> response) override {
|
||||
// std::cout << "-----------------------------------reply" << endl;
|
||||
// std::cout << std::string((char *)response->content, response->contentLength) << std::endl;
|
||||
// std::cout << response->body << std::endl;
|
||||
// }
|
||||
|
||||
void start(bool wait) override {
|
||||
MINWebRequest request;
|
||||
request.service = "/MIN/Web/test";
|
||||
request.url = "/MIN/Web/test/getUserInfo?id=1";
|
||||
request.path = "/getUserInfo?id=1";
|
||||
// request.path = "/test.cpp";
|
||||
request.method = MIN_HTTP_GET;
|
||||
auto requestPtr = make_shared<MINWebRequest>(request);
|
||||
this->listener(requestPtr);
|
||||
sessionpro.setOnRequestListener(this->listener);
|
||||
sessionpro.run();
|
||||
}
|
||||
|
||||
private:
|
||||
Sessionpro sessionpro;
|
||||
OnRequestReceiveListener listener;
|
||||
};
|
||||
|
||||
@@ -45,6 +95,8 @@ int main () {
|
||||
service.homePage = "index.html";
|
||||
service.baseUrl = "/MIN/Web/test";
|
||||
|
||||
std::cout << getMINHttpMethodEnumFromStr("GET") << std::endl;
|
||||
std::cout << getMINHttpStatusEnumFromStr("Continue") << std::endl;
|
||||
service.GET("/getUserInfo", [](std::shared_ptr<MINWebRequest> &request,
|
||||
std::shared_ptr<MINWebResponse> &response) {
|
||||
nlohmann::json result = {
|
||||
@@ -56,7 +108,7 @@ int main () {
|
||||
return 0;
|
||||
});
|
||||
|
||||
MINHttpServerImpl minHttpServer;
|
||||
MINHttpServerImpl1 minHttpServer;
|
||||
|
||||
Server::ServerConfig serverConfig;
|
||||
serverConfig.service = &service;
|
||||
@@ -64,4 +116,14 @@ int main () {
|
||||
Server server(serverConfig);
|
||||
|
||||
server.run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MINWebRequest request;
|
||||
// request.service = "/MIN/Web/test";
|
||||
// request.url = "/MIN/Web/test/getUserInfo";
|
||||
//// request.path = "/sunnyqjm.github.io/2017/";
|
||||
// request.path = "/getUserInfo";
|
||||
// request.method = MIN_HTTP_GET;
|
||||
// auto requestPtr = make_shared<MINWebRequest>(request);
|
||||
// this->listener(requestPtr);
|
||||
Reference in New Issue
Block a user