use protobuf instead of xml for sync msg
diff --git a/include/sync-state.h b/include/sync-state.h
index 826e84d..c96160e 100644
--- a/include/sync-state.h
+++ b/include/sync-state.h
@@ -26,6 +26,7 @@
#include "sync-state-leaf-container.h"
#include <boost/exception/all.hpp>
#include "boost/tuple/tuple.hpp"
+#include "sync-state.pb.h"
/**
* \defgroup sync SYNC protocol
@@ -74,29 +75,31 @@
LeafContainer m_leaves;
};
-/**
- * @brief Formats an XML representation of the state
- * @param os output stream
- * @param state state
- * @returns output stream
- */
-std::ostream &
-operator << (std::ostream &os, const State &state);
/**
- * @brief Parses an XML representation to the state
- * @param in input data stream
+ * @brief Formats a protobuf SyncStateMsg msg
+ * @param oss output SyncStateMsg msg
* @param state state
- * @returns input stream
+ * @returns output SyncStateMsg msg
*/
-std::istream &
-operator >> (std::istream &in, State &state);
+SyncStateMsg &
+operator << (SyncStateMsg &ossm, const State &state);
+
+
+/**
+ * @brief Parse a protobuf SyncStateMsg msg
+ * @param iss input SyncStateMsg msg
+ * @param state state
+ * @returns SyncStateMsg msg
+ */
+SyncStateMsg &
+operator >> (SyncStateMsg &issm, const State &state);
namespace Error {
/**
- * @brief Will be thrown when XML cannot be properly decoded to State
+ * @brief Will be thrown when data cannot be properly decoded to SyncStateMsg
*/
-struct SyncXmlDecodingFailure : virtual boost::exception, virtual std::exception { };
+struct SyncStateMsgDecodingFailure : virtual boost::exception, virtual std::exception { };
}
} // Sync
diff --git a/model/sync-logic.cc b/model/sync-logic.cc
index 860f119..943126b 100644
--- a/model/sync-logic.cc
+++ b/model/sync-logic.cc
@@ -178,7 +178,7 @@
}
void
-SyncLogic::respondSyncData (const std::string &name, const std::string &dataBuffer)
+SyncLogic::respondSyncData (const std::string &name, const char *wireData, size_t len)
{
try
{
@@ -190,13 +190,13 @@
if (type == "normal")
{
- processSyncData (name, digest, dataBuffer);
+ processSyncData (name, digest, wireData, len);
}
else
{
// timer is always restarted when we schedule recovery
m_scheduler.cancel (REEXPRESSING_RECOVERY_INTEREST);
- processSyncData (name, digest, dataBuffer);
+ processSyncData (name, digest, wireData, len);
}
}
catch (Error::DigestCalculationError &e)
@@ -263,7 +263,7 @@
}
void
-SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const string &dataBuffer)
+SyncLogic::processSyncData (const std::string &name, DigestConstPtr digest, const char *wireData, size_t len)
{
DiffStatePtr diffLog = make_shared<DiffState> ();
bool ownInterestSatisfied = false;
@@ -277,8 +277,14 @@
ownInterestSatisfied = (name == m_outstandingInterestName);
DiffState diff;
- istringstream ss (dataBuffer);
- ss >> diff;
+ SyncStateMsg msg;
+ if (!msg.parseFromArray(wireData, len) || !msg.IsInitialized())
+ {
+ //Throw
+ BOOST_THROW_EXCEPTION (SyncStateMsgDecodingFailure () << info_str ("Can not decode data"));
+ }
+ msg >> diff;
+
vector<MissingDataInfo> v;
BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
{
@@ -332,7 +338,7 @@
insertToDiffLog (diffLog);
}
- catch (Error::SyncXmlDecodingFailure &e)
+ catch (Error::SyncStateMsgDecodingFailure &e)
{
_LOG_TRACE ("Something really fishy happened during state decoding " <<
diagnostic_information (e));
@@ -486,8 +492,8 @@
bind (&SyncLogic::sendSyncInterest, this),
REEXPRESSING_INTEREST);
- m_ccnxHandle->sendInterestForString (os.str (),
- bind (&SyncLogic::respondSyncData, this, _1, _2));
+ m_ccnxHandle->sendInterest (os.str (),
+ bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
}
void
@@ -508,8 +514,8 @@
REEXPRESSING_RECOVERY_INTEREST);
}
- m_ccnxHandle->sendInterestForString (os.str (),
- bind (&SyncLogic::respondSyncData, this, _1, _2));
+ m_ccnxHandle->sendInterest (os.str (),
+ bind (&SyncLogic::respondSyncData, this, _1, _2, _3));
}
@@ -518,9 +524,15 @@
{
_LOG_TRACE (">> D " << name);
// sending
- m_ccnxHandle->publishStringData (name,
- lexical_cast<string> (*state),
+ SyncStateMsg ssm;
+ ssm << (*state);
+ char *wireData = new char[ssm.size()];
+ ssm.SerializedToArray(wireData, ssm.size());
+ m_ccnxHandle->publishRawData (name,
+ wireData,
+ ssm.size(),
m_syncResponseFreshness); // in NS-3 it doesn't have any effect... yet
+ delete wireData;
// checking if our own interest got satisfied
bool satisfiedOwnInterest = false;
diff --git a/model/sync-state.cc b/model/sync-state.cc
index f6db146..45a7ab7 100644
--- a/model/sync-state.cc
+++ b/model/sync-state.cc
@@ -30,7 +30,6 @@
#include <boost/throw_exception.hpp>
#include <boost/lexical_cast.hpp>
-#define TIXML_USE_STL
#include <tinyxml.h>
using namespace std;
@@ -42,12 +41,7 @@
namespace Sync {
-#ifdef _DEBUG
-#define DEBUG_ENDL os << "\n";
-#else
-#define DEBUG_ENDL
-#endif
-
+/*
std::ostream &
operator << (std::ostream &os, const State &state)
{
@@ -73,7 +67,40 @@
}
os << "</state>";
}
+*/
+SyncStateMsg &
+operator << (SyncStateMsg &ossm, const State &state)
+{
+ BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
+ {
+ SyncState *oss = ossm->add_ss();
+ shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
+ if (diffLeaf != 0 && diffLeaf->getOperation != UPDATE)
+ {
+ oss->set_type(SyncState::DELETE);
+ }
+ else
+ {
+ oss->set_type(SyncState::UPDATE);
+ }
+
+ std::ostringstream os;
+ os << *leaf->getInfo();
+ oss->set_name(os.str());
+
+ if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
+ {
+ SyncState::SeqNo seqNo;
+ seqNo->set_session(leaf->getSeq()->getSession());
+ seqNo->set_seq(leaf->getSeq()->getSeq());
+ oss->set_seqNo(seqNo);
+ }
+ }
+ return ossm;
+}
+
+/*
std::istream &
operator >> (std::istream &in, State &state)
{
@@ -120,5 +147,29 @@
return in;
}
+*/
+
+SyncStateMsg &
+operator >> (SyncStateMsg &issm, const State &state)
+{
+ int n = issm.ss_size();
+ for (int i = 0; i < n; i++)
+ {
+ const SyncState &ss = issm.ss(i);
+ NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name());
+ if (ss.type() == SyncState::UPDATE)
+ {
+ state.update(info, SeqNo(
+ lexical_cast<uint32_t> (ss.seqNo().session()),
+ lexical_cast<uint32_t> (ss.seqNo().seq()),
+ ));
+ }
+ else
+ {
+ state.remove(info);
+ }
+ }
+ return issm;
+}
}
diff --git a/proto/sync-state.proto b/proto/sync-state.proto
new file mode 100644
index 0000000..71bd1e7
--- /dev/null
+++ b/proto/sync-state.proto
@@ -0,0 +1,24 @@
+package Sync;
+
+message SyncState
+{
+ required string name = 1;
+ enum ActionType
+ {
+ UPDATE = 0;
+ DELETE = 1;
+ OTHER = 2;
+ }
+ required ActionType type = 2;
+ message SeqNo
+ {
+ required uint32 seq = 1;
+ required uint32 session = 2;
+ }
+ optional SeqNo seqNo = 3;
+}
+
+message SyncStateMsg
+{
+ repeated SyncState ss = 1;
+}