blob: c8b5ec97f296703e66e00cbf81f0da59942c970c [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include "nlsr.hpp"
2#include "sync-logic-handler.hpp"
Yingdi Yu0c217822014-04-24 14:22:42 -07003// #include "security/key-manager.hpp"
akmhoque53353462014-04-22 08:43:45 -05004#include "utility/tokenizer.hpp"
5
6
7namespace nlsr {
8
akmhoquefdbddb12014-05-02 18:35:19 -05009using namespace ndn;
10using namespace std;
11
akmhoque53353462014-04-22 08:43:45 -050012void
13SyncLogicHandler::createSyncSocket(Nlsr& pnlsr)
14{
15 std::cout << "Creating Sync socket ......" << std::endl;
16 std::cout << "Sync prefix: " << m_syncPrefix.toUri() << std::endl;
17 m_syncSocket = make_shared<Sync::SyncSocket>(m_syncPrefix, m_validator,
18 m_syncFace,
19 bind(&SyncLogicHandler::nsyncUpdateCallBack, this,
20 _1, _2, boost::ref(pnlsr)),
21 bind(&SyncLogicHandler::nsyncRemoveCallBack, this,
22 _1, boost::ref(pnlsr)));
23}
24
25void
26SyncLogicHandler::nsyncUpdateCallBack(const vector<Sync::MissingDataInfo>& v,
27 Sync::SyncSocket* socket, Nlsr& pnlsr)
28{
29 std::cout << "nsyncUpdateCallBack called ...." << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -050030 int32_t n = v.size();
31 for (int32_t i = 0; i < n; i++)
akmhoque53353462014-04-22 08:43:45 -050032 {
33 std::cout << "Data Name: " << v[i].prefix << " Seq: " << v[i].high.getSeq() <<
34 endl;
35 processUpdateFromSync(v[i].prefix, v[i].high.getSeq(), pnlsr);
36 }
37}
38
39void
40SyncLogicHandler::nsyncRemoveCallBack(const string& prefix, Nlsr& pnlsr)
41{
42 std::cout << "nsyncRemoveCallBack called ...." << std::endl;
43}
44
45void
akmhoquefdbddb12014-05-02 18:35:19 -050046SyncLogicHandler::removeRouterFromSyncing(const string& routerPrefix)
akmhoque53353462014-04-22 08:43:45 -050047{
48}
49
50void
akmhoquefdbddb12014-05-02 18:35:19 -050051SyncLogicHandler::processUpdateFromSync(const std::string& updateName,
akmhoque53353462014-04-22 08:43:45 -050052 uint64_t seqNo, Nlsr& pnlsr)
53{
54 Tokenizer nt(updateName, "/");
55 string chkString("LSA");
56 if (nt.doesTokenExist(chkString))
57 {
58 //process LSA Update here
59 string routerName = nt.getTokenString(nt.getTokenPosition(chkString) + 1);
60 processRoutingUpdateFromSync(routerName, seqNo, pnlsr);
61 }
62 chkString = "keys";
63 if (nt.doesTokenExist(chkString))
64 {
65 //process keys update here
66 std::string certName = nt.getTokenString(0);
Yingdi Yu0c217822014-04-24 14:22:42 -070067 // processKeysUpdateFromSync(certName, seqNo, pnlsr);
akmhoque53353462014-04-22 08:43:45 -050068 }
69}
70
71void
akmhoquefdbddb12014-05-02 18:35:19 -050072SyncLogicHandler::processRoutingUpdateFromSync(const std::string& routerName,
akmhoque53353462014-04-22 08:43:45 -050073 uint64_t seqNo, Nlsr& pnlsr)
74{
75 if (routerName != pnlsr.getConfParameter().getRouterPrefix())
76 {
77 SequencingManager sm(seqNo);
78 std::cout << sm;
79 std::cout << "Router Name: " << routerName << endl;
80 if (pnlsr.getLsdb().isNameLsaNew(routerName + "/1", sm.getNameLsaSeq()))
81 {
82 std::cout << "Updated Name LSA. Need to fetch it" << std::endl;
83 string lsaPrefix =
84 pnlsr.getConfParameter().getChronosyncLsaPrefix() +
85 routerName + "/1/" +
86 boost::lexical_cast<std::string>(sm.getNameLsaSeq());
akmhoquec8a10f72014-04-25 18:42:55 -050087 pnlsr.getInterestManager().expressInterest(lsaPrefix, 3,
88 pnlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -050089 }
90 if (pnlsr.getLsdb().isAdjLsaNew(routerName + "/2", sm.getAdjLsaSeq()))
91 {
92 std::cout << "Updated Adj LSA. Need to fetch it" << std::endl;
93 string lsaPrefix =
94 pnlsr.getConfParameter().getChronosyncLsaPrefix() +
95 routerName + "/2/" +
96 boost::lexical_cast<std::string>(sm.getAdjLsaSeq());
akmhoquec8a10f72014-04-25 18:42:55 -050097 pnlsr.getInterestManager().expressInterest(lsaPrefix, 3,
98 pnlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -050099 }
akmhoqueb6450b12014-04-24 00:01:03 -0500100 if (pnlsr.getLsdb().isCoordinateLsaNew(routerName + "/3", sm.getCorLsaSeq()))
akmhoque53353462014-04-22 08:43:45 -0500101 {
102 std::cout << "Updated Cor LSA. Need to fetch it" << std::endl;
103 string lsaPrefix =
104 pnlsr.getConfParameter().getChronosyncLsaPrefix() +
105 routerName + "/3/" +
106 boost::lexical_cast<std::string>(sm.getCorLsaSeq());
akmhoquec8a10f72014-04-25 18:42:55 -0500107 pnlsr.getInterestManager().expressInterest(lsaPrefix, 3,
108 pnlsr.getConfParameter().getInterestResendTime());
akmhoque53353462014-04-22 08:43:45 -0500109 }
110 }
111}
112
Yingdi Yu0c217822014-04-24 14:22:42 -0700113// void
114// SyncLogicHandler::processKeysUpdateFromSync(std::string certName,
115// uint64_t seqNo, Nlsr& pnlsr)
116// {
117// std::cout << "Cert Name: " << certName << std::endl;
118// // if (pnlsr.getKeyManager().isNewCertificate(certName, seqNo))
119// {
120// string certNamePrefix = certName + "/" +
121// boost::lexical_cast<string>(seqNo);
122// pnlsr.getIm().expressInterest(certNamePrefix, 3,
123// pnlsr.getConfParameter().getInterestResendTime());
124// }
125// }
akmhoque53353462014-04-22 08:43:45 -0500126
127void
128SyncLogicHandler::publishRoutingUpdate(SequencingManager& sm,
akmhoquefdbddb12014-05-02 18:35:19 -0500129 const string& updatePrefix)
akmhoque53353462014-04-22 08:43:45 -0500130{
131 sm.writeSeqNoToFile();
132 publishSyncUpdate(updatePrefix, sm.getCombinedSeqNo());
133}
134
Yingdi Yu0c217822014-04-24 14:22:42 -0700135// void
136// SyncLogicHandler::publishKeyUpdate(KeyManager& km)
137// {
138// publishSyncUpdate(km.getProcessCertName().toUri(), km.getCertSeqNo());
139// }
akmhoque53353462014-04-22 08:43:45 -0500140
141void
akmhoquefdbddb12014-05-02 18:35:19 -0500142SyncLogicHandler::publishIdentityUpdate(const string& identityName)
akmhoque53353462014-04-22 08:43:45 -0500143{
144 publishSyncUpdate(identityName, 0);
145}
146
147void
akmhoquefdbddb12014-05-02 18:35:19 -0500148SyncLogicHandler::publishSyncUpdate(const string& updatePrefix, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500149{
150 std::cout << "Publishing Sync Update ......" << std::endl;
151 std::cout << "Update in prefix: " << updatePrefix << std::endl;
152 std::cout << "Seq No: " << seqNo << std::endl;
153 ndn::Name updateName(updatePrefix);
154 string data("NoData");
155 m_syncSocket->publishData(updateName, 0, data.c_str(), data.size(), 1000,
156 seqNo);
157}
158
159}//namespace nlsr