fetch: Switch code to use ndn-cxx
This commit also moves code to ndn::chronoshare namespace and changes
logging to show file and line number of the logging statement.
Change-Id: I075320644166cea9d5d3ef65bb26a2cabfd4dc5a
diff --git a/core/logging.hpp b/core/logging.hpp
index b7c3f34..d81e8a0 100644
--- a/core/logging.hpp
+++ b/core/logging.hpp
@@ -28,9 +28,9 @@
#define _LOG_INIT(name) NDN_LOG_INIT(chronoshare.name)
-#define _LOG_DEBUG(x) NDN_LOG_DEBUG(x)
+#define _LOG_DEBUG(x) NDN_LOG_DEBUG(__FILE__ << ":" << __LINE__ << ":" << " " << x)
-#define _LOG_TRACE(x) NDN_LOG_TRACE(x)
+#define _LOG_TRACE(x) NDN_LOG_TRACE(__FILE__ << ":" << __LINE__ << ":" << " " << x)
#define _LOG_ERROR(x) NDN_LOG_ERROR(x)
diff --git a/src/fetch-manager.cpp b/src/fetch-manager.cpp
index 15ecb32..d8dd6a6 100644
--- a/src/fetch-manager.cpp
+++ b/src/fetch-manager.cpp
@@ -19,21 +19,16 @@
*/
#include "fetch-manager.hpp"
-#include <boost/lexical_cast.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/ref.hpp>
-#include <boost/throw_exception.hpp>
+#include "core/logging.hpp"
-#include "logging.hpp"
-#include "simple-interval-generator.hpp"
+#include <ndn-cxx/face.hpp>
+
+namespace ndn {
+namespace chronoshare {
_LOG_INIT(FetchManager);
-using namespace boost;
-using namespace std;
-using namespace Ndnx;
-
-//The disposer object function
+// The disposer object function
struct fetcher_disposer
{
void
@@ -43,54 +38,42 @@
}
};
-static const string SCHEDULE_FETCHES_TAG = "ScheduleFetches";
-
-FetchManager::FetchManager(Ccnx::CcnxWrapperPtr ccnx,
- const Mapping& mapping,
- const Name& broadcastForwardingHint,
+FetchManager::FetchManager(Face& face, const Mapping& mapping, const Name& broadcastForwardingHint,
uint32_t parallelFetches, // = 3
const SegmentCallback& defaultSegmentCallback,
- const FinishCallback& defaultFinishCallback,
- const FetchTaskDbPtr& taskDb)
- : m_ccnx(ccnx)
+ const FinishCallback& defaultFinishCallback, const FetchTaskDbPtr& taskDb)
+ : m_face(face)
, m_mapping(mapping)
, m_maxParallelFetches(parallelFetches)
, m_currentParallelFetches(0)
- , m_scheduler(new Scheduler)
- , m_executor(new Executor(1))
+ , m_scheduler(m_face.getIoService())
+ , m_scheduledFetchesEvent(m_scheduler)
, m_defaultSegmentCallback(defaultSegmentCallback)
, m_defaultFinishCallback(defaultFinishCallback)
, m_taskDb(taskDb)
, m_broadcastHint(broadcastForwardingHint)
+ , m_ioService(m_face.getIoService())
{
- m_scheduler->start();
- m_executor->start();
+ // no need to check to often. if needed, will be rescheduled
+ m_scheduledFetchesEvent =
+ m_scheduler.scheduleEvent(time::seconds(300), bind(&FetchManager::ScheduleFetches, this));
- m_scheduleFetchesTask =
- Scheduler::schedulePeriodicTask(m_scheduler,
- make_shared<SimpleIntervalGenerator>(
- 300), // no need to check to often. if needed, will be rescheduled
- bind(&FetchManager::ScheduleFetches, this),
- SCHEDULE_FETCHES_TAG);
// resume un-finished fetches if there is any
if (m_taskDb) {
- m_taskDb->foreachTask(bind(&FetchManager::Enqueue, this, _1, _2, _3, _4, _5));
+ m_taskDb->foreachTask(
+ [this](const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
+ int priority) { this->Enqueue(deviceName, baseName, minSeqNo, maxSeqNo, priority); });
}
}
FetchManager::~FetchManager()
{
- m_scheduler->shutdown();
- m_executor->shutdown();
-
- m_ccnx.reset();
-
m_fetchList.clear_and_dispose(fetcher_disposer());
}
// Enqueue using default callbacks
void
-FetchManager::Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo,
+FetchManager::Enqueue(const Name& deviceName, const Name& baseName, uint64_t minSeqNo,
uint64_t maxSeqNo, int priority)
{
Enqueue(deviceName, baseName, m_defaultSegmentCallback, m_defaultFinishCallback, minSeqNo,
@@ -98,7 +81,7 @@
}
void
-FetchManager::Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName,
+FetchManager::Enqueue(const Name& deviceName, const Name& baseName,
const SegmentCallback& segmentCallback, const FinishCallback& finishCallback,
uint64_t minSeqNo, uint64_t maxSeqNo, int priority /*PRIORITY_NORMAL*/)
{
@@ -115,14 +98,14 @@
m_taskDb->addTask(deviceName, baseName, minSeqNo, maxSeqNo, priority);
}
- unique_lock<mutex> lock(m_parellelFetchMutex);
+ std::unique_lock<std::mutex> lock(m_parellelFetchMutex);
_LOG_TRACE("++++ Create fetcher: " << baseName);
Fetcher* fetcher =
- new Fetcher(m_ccnx, m_executor, segmentCallback, finishCallback,
+ new Fetcher(m_face, segmentCallback, finishCallback,
bind(&FetchManager::DidFetchComplete, this, _1, _2, _3),
bind(&FetchManager::DidNoDataTimeout, this, _1), deviceName, baseName, minSeqNo,
- maxSeqNo, boost::posix_time::seconds(30), forwardingHint);
+ maxSeqNo, time::seconds(30), forwardingHint);
switch (priority) {
case PRIORITY_HIGH:
@@ -138,19 +121,17 @@
}
_LOG_DEBUG("++++ Reschedule fetcher task");
- m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0);
- // ScheduleFetches (); // will start a fetch if m_currentParallelFetches is less than max, otherwise does nothing
+ m_scheduledFetchesEvent =
+ m_scheduler.scheduleEvent(time::seconds(0), bind(&FetchManager::ScheduleFetches, this));
}
void
FetchManager::ScheduleFetches()
{
- unique_lock<mutex> lock(m_parellelFetchMutex);
+ std::unique_lock<std::mutex> lock(m_parellelFetchMutex);
- boost::posix_time::ptime currentTime =
- date_time::second_clock<boost::posix_time::ptime>::universal_time();
- boost::posix_time::ptime nextSheduleCheck =
- currentTime + posix_time::seconds(300); // no reason to have anything, but just in case
+ auto currentTime = time::steady_clock::now();
+ auto nextSheduleCheck = currentTime + time::seconds(300); // no reason to have anything, but just in case
for (FetchList::iterator item = m_fetchList.begin();
m_currentParallelFetches < m_maxParallelFetches && item != m_fetchList.end();
@@ -166,8 +147,9 @@
}
if (currentTime < item->GetNextScheduledRetry()) {
- if (item->GetNextScheduledRetry() < nextSheduleCheck)
+ if (item->GetNextScheduledRetry() < nextSheduleCheck) {
nextSheduleCheck = item->GetNextScheduledRetry();
+ }
_LOG_DEBUG("Item is delayed");
continue;
@@ -180,8 +162,8 @@
item->RestartPipeline();
}
- m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask,
- (nextSheduleCheck - currentTime).total_seconds());
+ m_scheduledFetchesEvent = m_scheduler.scheduleEvent(nextSheduleCheck - currentTime,
+ bind(&FetchManager::ScheduleFetches, this));
}
void
@@ -191,7 +173,7 @@
<< fetcher.GetForwardingHint());
{
- unique_lock<mutex> lock(m_parellelFetchMutex);
+ std::unique_lock<std::mutex> lock(m_parellelFetchMutex);
m_currentParallelFetches--;
// no need to do anything with the m_fetchList
}
@@ -220,27 +202,26 @@
fetcher.SetForwardingHint(m_broadcastHint);
}
- double delay = fetcher.GetRetryPause();
- if (delay < 1) // first time
+ time::seconds delay = fetcher.GetRetryPause();
+ if (delay < time::seconds(1)) // first time
{
- delay = 1;
+ delay = time::seconds(1);
}
else {
- delay = std::min(2 * delay, 300.0); // 5 minutes max
+ delay = std::min(2 * delay, time::seconds(300)); // 5 minutes max
}
fetcher.SetRetryPause(delay);
- fetcher.SetNextScheduledRetry(date_time::second_clock<boost::posix_time::ptime>::universal_time() +
- posix_time::seconds(delay));
+ fetcher.SetNextScheduledRetry(time::steady_clock::now() + time::seconds(delay));
- m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0);
+ m_scheduledFetchesEvent = m_scheduler.scheduleEvent(time::seconds(0), bind(&FetchManager::ScheduleFetches, this));
}
void
FetchManager::DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName)
{
{
- unique_lock<mutex> lock(m_parellelFetchMutex);
+ std::unique_lock<std::mutex> lock(m_parellelFetchMutex);
m_currentParallelFetches--;
if (m_taskDb) {
@@ -249,17 +230,17 @@
}
// like TCP timed-wait
- m_scheduler->scheduleOneTimeTask(m_scheduler, 10,
- boost::bind(&FetchManager::TimedWait, this, ref(fetcher)),
- boost::lexical_cast<string>(baseName));
-
- m_scheduler->rescheduleTaskAt(m_scheduleFetchesTask, 0);
+ m_scheduler.scheduleEvent(time::seconds(10), bind(&FetchManager::TimedWait, this, ref(fetcher)));
+ m_scheduledFetchesEvent = m_scheduler.scheduleEvent(time::seconds(0), bind(&FetchManager::ScheduleFetches, this));
}
void
FetchManager::TimedWait(Fetcher& fetcher)
{
- unique_lock<mutex> lock(m_parellelFetchMutex);
+ std::unique_lock<std::mutex> lock(m_parellelFetchMutex);
_LOG_TRACE("+++++ removing fetcher: " << fetcher.GetName());
m_fetchList.erase_and_dispose(FetchList::s_iterator_to(fetcher), fetcher_disposer());
}
+
+} // namespace chronoshare
+} // namespace ndn
diff --git a/src/fetch-manager.hpp b/src/fetch-manager.hpp
index 8372e9d..5dae0ff 100644
--- a/src/fetch-manager.hpp
+++ b/src/fetch-manager.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
*
* This file is part of ChronoShare, a decentralized file sharing application over NDN.
*
@@ -21,31 +21,38 @@
#ifndef FETCH_MANAGER_H
#define FETCH_MANAGER_H
-#include "ccnx-wrapper.h"
-#include "executor.h"
-#include "fetch-task-db.h"
-#include "scheduler.h"
-#include <boost/exception/all.hpp>
-#include <boost/function.hpp>
-#include <boost/shared_ptr.hpp>
-#include <list>
-#include <stdint.h>
-#include <string>
+#include "fetch-task-db.hpp"
+#include "fetcher.hpp"
+#include "core/chronoshare-common.hpp"
-#include "fetcher.h"
+#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
+
+#include <list>
+
+namespace ndn {
+namespace chronoshare {
class FetchManager
{
public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+
enum { PRIORITY_NORMAL, PRIORITY_HIGH };
- typedef boost::function<Ccnx::Name(const Ccnx::Name&)> Mapping;
- typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName, uint64_t seq, Ccnx::PcoPtr pco)>
- SegmentCallback;
- typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName)> FinishCallback;
- FetchManager(Ccnx::CcnxWrapperPtr ccnx,
- const Mapping& mapping,
- const Ccnx::Name& broadcastForwardingHint,
+ typedef function<Name(const Name&)> Mapping;
+ typedef function<void(Name& deviceName, Name& baseName, uint64_t seq, shared_ptr<Data> data)> SegmentCallback;
+ typedef function<void(Name& deviceName, Name& baseName)> FinishCallback;
+
+public:
+ FetchManager(Face& face, const Mapping& mapping, const Name& broadcastForwardingHint,
uint32_t parallelFetches = 3,
const SegmentCallback& defaultSegmentCallback = SegmentCallback(),
const FinishCallback& defaultFinishCallback = FinishCallback(),
@@ -53,30 +60,26 @@
virtual ~FetchManager();
void
- Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName,
- const SegmentCallback& segmentCallback, const FinishCallback& finishCallback,
- uint64_t minSeqNo, uint64_t maxSeqNo, int priority = PRIORITY_NORMAL);
+ Enqueue(const Name& deviceName, const Name& baseName, const SegmentCallback& segmentCallback,
+ const FinishCallback& finishCallback, uint64_t minSeqNo, uint64_t maxSeqNo,
+ int priority = PRIORITY_NORMAL);
// Enqueue using default callbacks
void
- Enqueue(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo,
- uint64_t maxSeqNo, int priority = PRIORITY_NORMAL);
-
- // only for Fetcher
- inline Ccnx::CcnxWrapperPtr
- GetCcnx();
+ Enqueue(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
+ int priority = PRIORITY_NORMAL);
private:
// Fetch Events
void
- DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Ccnx::Name& basename,
- const Ccnx::Name& name, Ccnx::PcoPtr data);
+ DidDataSegmentFetched(Fetcher& fetcher, uint64_t seqno, const Name& basename, const Name& name,
+ shared_ptr<Data> data);
void
DidNoDataTimeout(Fetcher& fetcher);
void
- DidFetchComplete(Fetcher& fetcher, const Ccnx::Name& deviceName, const Ccnx::Name& baseName);
+ DidFetchComplete(Fetcher& fetcher, const Name& deviceName, const Name& baseName);
void
ScheduleFetches();
@@ -85,44 +88,33 @@
TimedWait(Fetcher& fetcher);
private:
- Ndnx::NdnxWrapperPtr m_ndnx;
+ Face& m_face;
Mapping m_mapping;
uint32_t m_maxParallelFetches;
uint32_t m_currentParallelFetches;
- boost::mutex m_parellelFetchMutex;
+ std::mutex m_parellelFetchMutex;
// optimized list structure for fetch queue
typedef boost::intrusive::member_hook<Fetcher, boost::intrusive::list_member_hook<>,
- &Fetcher::m_managerListHook>
- MemberOption;
+ &Fetcher::m_managerListHook> MemberOption;
typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
FetchList m_fetchList;
- SchedulerPtr m_scheduler;
- ExecutorPtr m_executor;
- TaskPtr m_scheduleFetchesTask;
+ Scheduler m_scheduler;
+ util::scheduler::ScopedEventId m_scheduledFetchesEvent;
+
SegmentCallback m_defaultSegmentCallback;
FinishCallback m_defaultFinishCallback;
FetchTaskDbPtr m_taskDb;
- const Ndnx::Name m_broadcastHint;
+ const Name m_broadcastHint;
+ boost::asio::io_service& m_ioService;
};
-Ccnx::CcnxWrapperPtr
-FetchManager::GetCcnx()
-{
- return m_ndnx;
-}
+typedef shared_ptr<FetchManager> FetchManagerPtr;
-typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
-namespace Error {
-struct FetchManager : virtual boost::exception, virtual std::exception
-{
-};
-}
-
-typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
-
+} // namespace chronoshare
+} // namespace ndn
#endif // FETCHER_H
diff --git a/src/fetch-task-db.cpp b/src/fetch-task-db.cpp
index 912f063..e753f95 100644
--- a/src/fetch-task-db.cpp
+++ b/src/fetch-task-db.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
*
* This file is part of ChronoShare, a decentralized file sharing application over NDN.
*
@@ -17,15 +17,16 @@
*
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
+
#include "fetch-task-db.hpp"
#include "db-helper.hpp"
-using namespace std;
-using namespace boost;
-using namespace Ndnx;
+namespace ndn {
+namespace chronoshare {
+
namespace fs = boost::filesystem;
-const string INIT_DATABASE = "\
+const std::string INIT_DATABASE = "\
CREATE TABLE IF NOT EXISTS \n\
Task( \n\
deviceName BLOB NOT NULL, \n\
@@ -45,14 +46,12 @@
int res = sqlite3_open((actualFolder / tag).c_str(), &m_db);
if (res != SQLITE_OK) {
- BOOST_THROW_EXCEPTION(
- Error::Db() << errmsg_info_str("Cannot open database: " + (actualFolder / tag).string()));
+ BOOST_THROW_EXCEPTION(Error("Cannot open database: " + (actualFolder / tag).string()));
}
char* errmsg = 0;
res = sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, &errmsg);
if (res != SQLITE_OK && errmsg != 0) {
- // _LOG_TRACE ("Init \"error\": " << errmsg);
sqlite3_free(errmsg);
}
else {
@@ -73,12 +72,13 @@
{
sqlite3_stmt* stmt;
sqlite3_prepare_v2(m_db,
- "INSERT OR IGNORE INTO Task (deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES (?, ?, ?, ?, ?)",
+ "INSERT OR IGNORE INTO Task(deviceName, baseName, minSeqNo, maxSeqNo, priority) VALUES(?, ?, ?, ?, ?)",
-1, &stmt, 0);
- CcnxCharbufPtr deviceBuf = CcnxCharbufPtr(deviceName);
- CcnxCharbufPtr baseBuf = CcnxCharbufPtr(baseName);
- sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC);
- sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC);
+
+ sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(),
+ SQLITE_STATIC);
+ sqlite3_bind_blob(stmt, 2, baseName.wireEncode().wire(), baseName.wireEncode().size(),
+ SQLITE_STATIC);
sqlite3_bind_int64(stmt, 3, minSeqNo);
sqlite3_bind_int64(stmt, 4, maxSeqNo);
sqlite3_bind_int(stmt, 5, priority);
@@ -94,10 +94,12 @@
{
sqlite3_stmt* stmt;
sqlite3_prepare_v2(m_db, "DELETE FROM Task WHERE deviceName = ? AND baseName = ?;", -1, &stmt, 0);
- NdnxCharbufPtr deviceBuf = NdnxCharbufPtr(deviceName);
- NdnxCharbufPtr baseBuf = NdnxCharbufPtr(baseName);
- sqlite3_bind_blob(stmt, 1, deviceBuf->buf(), deviceBuf->length(), SQLITE_STATIC);
- sqlite3_bind_blob(stmt, 2, baseBuf->buf(), baseBuf->length(), SQLITE_STATIC);
+
+ sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(),
+ SQLITE_STATIC);
+ sqlite3_bind_blob(stmt, 2, baseName.wireEncode().wire(), baseName.wireEncode().size(),
+ SQLITE_STATIC);
+
int res = sqlite3_step(stmt);
if (res == SQLITE_OK) {
}
@@ -110,8 +112,10 @@
sqlite3_stmt* stmt;
sqlite3_prepare_v2(m_db, "SELECT * FROM Task;", -1, &stmt, 0);
while (sqlite3_step(stmt) == SQLITE_ROW) {
- Name deviceName(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
- Name baseName(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1));
+ Name deviceName(Block(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)));
+ Name baseName(Block(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)));
+
+ std::cout << "deviceName: " << deviceName << " baseName: " << baseName << std::endl;
uint64_t minSeqNo = sqlite3_column_int64(stmt, 2);
uint64_t maxSeqNo = sqlite3_column_int64(stmt, 3);
int priority = sqlite3_column_int(stmt, 4);
@@ -120,3 +124,6 @@
sqlite3_finalize(stmt);
}
+
+} // namespace chronoshare
+} // namespace ndn
diff --git a/src/fetch-task-db.hpp b/src/fetch-task-db.hpp
index ab3fdc9..45d405d 100644
--- a/src/fetch-task-db.hpp
+++ b/src/fetch-task-db.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
*
* This file is part of ChronoShare, a decentralized file sharing application over NDN.
*
@@ -17,32 +17,48 @@
*
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
+
#ifndef FETCH_TASK_DB_H
#define FETCH_TASK_DB_H
-#include <boost/filesystem.hpp>
-#include <boost/shared_ptr.hpp>
-#include <ccnx-common.h>
-#include <ccnx-name.h>
+#include "db-helper.hpp"
+#include "core/chronoshare-common.hpp"
+
+#include <ndn-cxx/name.hpp>
+
#include <sqlite3.h>
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace chronoshare {
+
class FetchTaskDb
{
public:
+ class Error : public DbHelper::Error
+ {
+ public:
+ explicit Error(const std::string& what)
+ : DbHelper::Error(what)
+ {
+ }
+ };
+
+ typedef function<void(const Name&, const Name&, uint64_t, uint64_t, int)> FetchTaskCallback;
+
+public:
FetchTaskDb(const boost::filesystem::path& folder, const std::string& tag);
~FetchTaskDb();
// task with same deviceName and baseName combination will be added only once
// if task already exists, this call does nothing
void
- addTask(const Ccnx::Name& deviceName, const Ccnx::Name& baseName, uint64_t minSeqNo,
- uint64_t maxSeqNo, int priority);
+ addTask(const Name& deviceName, const Name& baseName, uint64_t minSeqNo, uint64_t maxSeqNo,
+ int priority);
void
- deleteTask(const Ccnx::Name& deviceName, const Ccnx::Name& baseName);
-
- typedef boost::function<void(const Ccnx::Name&, const Ccnx::Name&, uint64_t, uint64_t, int)>
- FetchTaskCallback;
+ deleteTask(const Name& deviceName, const Name& baseName);
void
foreachTask(const FetchTaskCallback& callback);
@@ -51,6 +67,9 @@
sqlite3* m_db;
};
-typedef boost::shared_ptr<FetchTaskDb> FetchTaskDbPtr;
+typedef shared_ptr<FetchTaskDb> FetchTaskDbPtr;
+
+} // namespace chronoshare
+} // namespace ndn
#endif // FETCH_TASK_DB_H
diff --git a/src/fetcher.cpp b/src/fetcher.cpp
index 83f8491..56899de 100644
--- a/src/fetcher.cpp
+++ b/src/fetcher.cpp
@@ -18,30 +18,26 @@
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
-#include "fetcher.h"
-#include "ccnx-pco.h"
-#include "fetch-manager.h"
-#include "logging.h"
+#include "fetcher.hpp"
+#include "fetch-manager.hpp"
+#include "core/logging.hpp"
+#include <boost/asio/io_service.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/ref.hpp>
-#include <boost/throw_exception.hpp>
+
+namespace ndn {
+namespace chronoshare {
_LOG_INIT(Fetcher);
-using namespace boost;
-using namespace std;
-using namespace Ndnx;
-
-Fetcher::Fetcher(Ccnx::CcnxWrapperPtr ccnx, ExecutorPtr executor,
- const SegmentCallback& segmentCallback, const FinishCallback& finishCallback,
- OnFetchCompleteCallback onFetchComplete, OnFetchFailedCallback onFetchFailed,
- const Ccnx::Name& deviceName, const Ccnx::Name& name, int64_t minSeqNo,
- int64_t maxSeqNo,
- boost::posix_time::time_duration timeout /* = boost::posix_time::seconds (30)*/,
- const Ccnx::Name& forwardingHint /* = Ccnx::Name ()*/)
- : m_ccnx(ccnx)
+Fetcher::Fetcher(Face& face,
+ const SegmentCallback& segmentCallback,
+ const FinishCallback& finishCallback,
+ const OnFetchCompleteCallback& onFetchComplete,
+ const OnFetchFailedCallback& onFetchFailed,
+ const Name& deviceName, const Name& name, int64_t minSeqNo, int64_t maxSeqNo,
+ time::milliseconds timeout, const Name& forwardingHint)
+ : m_face(face)
, m_segmentCallback(segmentCallback)
, m_onFetchComplete(onFetchComplete)
@@ -57,14 +53,20 @@
, m_minSendSeqNo(minSeqNo - 1)
, m_maxInOrderRecvSeqNo(minSeqNo - 1)
- , m_minSeqNo(minSeqNo)
+ // , m_minSeqNo(minSeqNo)
, m_maxSeqNo(maxSeqNo)
, m_pipeline(6) // initial "congestion window"
, m_activePipeline(0)
- , m_retryPause(0)
- , m_nextScheduledRetry(date_time::second_clock<boost::posix_time::ptime>::universal_time())
- , m_executor(executor) // must be 1
+
+ , m_slowStart(false)
+ , m_threshold(32767) // TODO make these values dynamic
+ , m_roundCount(32767)
+
+ , m_retryPause(time::seconds::zero())
+ , m_nextScheduledRetry(time::steady_clock::now())
+
+ , m_ioService(m_face.getIoService())
{
}
@@ -78,13 +80,13 @@
m_active = true;
m_minSendSeqNo = m_maxInOrderRecvSeqNo;
// cout << "Restart: " << m_minSendSeqNo << endl;
- m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
+ m_lastPositiveActivity = time::steady_clock::now();
- m_executor->execute(bind(&Fetcher::FillPipeline, this));
+ m_ioService.post(bind(&Fetcher::FillPipeline, this));
}
void
-Fetcher::SetForwardingHint(const Ccnx::Name& forwardingHint)
+Fetcher::SetForwardingHint(const Name& forwardingHint)
{
m_forwardingHint = forwardingHint;
}
@@ -93,7 +95,7 @@
Fetcher::FillPipeline()
{
for (; m_minSendSeqNo < m_maxSeqNo && m_activePipeline < m_pipeline; m_minSendSeqNo++) {
- unique_lock<mutex> lock(m_seqNoMutex);
+ std::unique_lock<std::mutex> lock(m_seqNoMutex);
if (m_outOfOrderRecvSeqNo.find(m_minSendSeqNo + 1) != m_outOfOrderRecvSeqNo.end())
continue;
@@ -103,72 +105,66 @@
m_inActivePipeline.insert(m_minSendSeqNo + 1);
- _LOG_DEBUG(" >>> i " << Name(m_forwardingHint)(m_name) << ", seq = " << (m_minSendSeqNo + 1));
+ _LOG_DEBUG(
+ " >>> i " << Name(m_forwardingHint).append(m_name) << ", seq = " << (m_minSendSeqNo + 1));
// cout << ">>> " << m_minSendSeqNo+1 << endl;
- m_ccnx->sendInterest(Name(m_forwardingHint)(m_name)(m_minSendSeqNo + 1),
- Closure(bind(&Fetcher::OnData, this, m_minSendSeqNo + 1, _1, _2),
- bind(&Fetcher::OnTimeout, this, m_minSendSeqNo + 1, _1, _2, _3)),
- Selectors().interestLifetime(1)); // Alex: this lifetime should be changed to RTO
+
+ Interest interest(
+ Name(m_forwardingHint).append(m_name).appendNumber(m_minSendSeqNo + 1)); // Alex: this lifetime should be changed to RTO
+ _LOG_DEBUG("interest Name: " << interest);
+ interest.setInterestLifetime(time::seconds(1));
+ m_face.expressInterest(interest,
+ bind(&Fetcher::OnData, this, m_minSendSeqNo + 1, _1, _2),
+ bind(&Fetcher::OnTimeout, this, m_minSendSeqNo + 1, _1));
+
_LOG_DEBUG(" >>> i ok");
m_activePipeline++;
}
}
-
void
-Fetcher::OnData(uint64_t seqno, const Ccnx::Name& name, PcoPtr data)
+Fetcher::OnData(uint64_t seqno, const Interest& interest, Data& data)
{
- m_executor->execute(bind(&Fetcher::OnData_Execute, this, seqno, name, data));
-}
+ const Name& name = data.getName();
+ _LOG_DEBUG(" <<< d " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno);
-void
-Fetcher::OnData_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data)
-{
- _LOG_DEBUG(" <<< d " << name.getPartialName(0, name.size() - 1) << ", seq = " << seqno);
+ shared_ptr<Data> pco = make_shared<Data>(data.wireEncode());
if (m_forwardingHint == Name()) {
// TODO: check verified!!!!
if (true) {
- if (!m_segmentCallback.empty()) {
- m_segmentCallback(m_deviceName, m_name, seqno, data);
+ if (m_segmentCallback != nullptr) {
+ m_segmentCallback(m_deviceName, m_name, seqno, pco);
}
}
else {
- _LOG_ERROR("Can not verify signature content. Name = " << data->name());
+ _LOG_ERROR("Can not verify signature content. Name = " << data.getName());
// probably needs to do more in the future
}
// we don't have to tell FetchManager about this
}
else {
// in this case we don't care whether "data" is verified, in fact, we expect it is unverified
- try {
- PcoPtr pco = make_shared<ParsedContentObject>(*data->contentPtr());
- // we need to verify this pco and apply callback only when verified
- // TODO: check verified !!!
- if (true) {
- if (!m_segmentCallback.empty()) {
- m_segmentCallback(m_deviceName, m_name, seqno, pco);
- }
- }
- else {
- _LOG_ERROR("Can not verify signature content. Name = " << pco->name());
- // probably needs to do more in the future
+ // we need to verify this pco and apply callback only when verified
+ // TODO: check verified !!!
+ if (true) {
+ if (m_segmentCallback != nullptr) {
+ m_segmentCallback(m_deviceName, m_name, seqno, pco);
}
}
- catch (MisformedContentObjectException& e) {
- cerr << "MisformedContentObjectException..." << endl;
- // no idea what should do...
- // let's ignore for now
+ else {
+ _LOG_ERROR("Can not verify signature content. Name = " << pco->getName());
+ // probably needs to do more in the future
}
}
m_activePipeline--;
- m_lastPositiveActivity = date_time::second_clock<boost::posix_time::ptime>::universal_time();
+ m_lastPositiveActivity = time::steady_clock::now();
{
- unique_lock<mutex> lock (m_pipelineMutex);
+ std::unique_lock<std::mutex> lock(m_seqNoMutex);
if(m_slowStart){
m_pipeline++;
if(m_pipeline == m_threshold)
@@ -184,16 +180,16 @@
}
}
- _LOG_DEBUG ("slowStart: " << boolalpha << m_slowStart << " pipeline: " << m_pipeline << " threshold: " << m_threshold);
+ _LOG_DEBUG ("slowStart: " << std::boolalpha << m_slowStart << " pipeline: " << m_pipeline << " threshold: " << m_threshold);
////////////////////////////////////////////////////////////////////////////
- unique_lock<mutex> lock(m_seqNoMutex);
+ std::unique_lock<std::mutex> lock(m_seqNoMutex);
m_outOfOrderRecvSeqNo.insert(seqno);
m_inActivePipeline.erase(seqno);
_LOG_DEBUG("Total segments received: " << m_outOfOrderRecvSeqNo.size());
- set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin();
+ std::set<int64_t>::iterator inOrderSeqNo = m_outOfOrderRecvSeqNo.begin();
for (; inOrderSeqNo != m_outOfOrderRecvSeqNo.end(); inOrderSeqNo++) {
_LOG_TRACE("Checking " << *inOrderSeqNo << " and " << m_maxInOrderRecvSeqNo + 1);
if (*inOrderSeqNo == m_maxInOrderRecvSeqNo + 1) {
@@ -209,54 +205,47 @@
m_outOfOrderRecvSeqNo.erase(m_outOfOrderRecvSeqNo.begin(), inOrderSeqNo);
////////////////////////////////////////////////////////////////////////////
- _LOG_TRACE("Max in order received: " << m_maxInOrderRecvSeqNo
- << ", max seqNo to request: " << m_maxSeqNo);
+ _LOG_TRACE("Max in order received: " << m_maxInOrderRecvSeqNo << ", max seqNo to request: " << m_maxSeqNo);
if (m_maxInOrderRecvSeqNo == m_maxSeqNo) {
_LOG_TRACE("Fetch finished: " << m_name);
m_active = false;
// invoke callback
- if (!m_finishCallback.empty()) {
+ if (m_finishCallback != nullptr) {
_LOG_TRACE("Notifying callback");
m_finishCallback(m_deviceName, m_name);
}
// tell FetchManager that we have finish our job
- // m_onFetchComplete (*this);
+ // m_onFetchComplete(*this);
// using executor, so we won't be deleted if there is scheduled FillPipeline call
- if (!m_onFetchComplete.empty()) {
+ if (m_onFetchComplete != nullptr) {
m_timedwait = true;
- m_executor->execute(bind(m_onFetchComplete, ref(*this), m_deviceName, m_name));
+ m_ioService.post(bind(m_onFetchComplete, std::ref(*this), m_deviceName, m_name));
}
}
else {
- m_executor->execute(bind(&Fetcher::FillPipeline, this));
+ m_ioService.post(bind(&Fetcher::FillPipeline, this));
}
}
void
-Fetcher::OnTimeout(uint64_t seqno, const Ccnx::Name& name, const Closure& closure, Selectors selectors)
+Fetcher::OnTimeout(uint64_t seqno, const Interest& interest)
{
- _LOG_DEBUG(this << ", " << m_executor.get());
- m_executor->execute(bind(&Fetcher::OnTimeout_Execute, this, seqno, name, closure, selectors));
-}
-
-void
-Fetcher::OnTimeout_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure,
- Ccnx::Selectors selectors)
-{
- _LOG_DEBUG(" <<< :( timeout " << name.getPartialName(0, name.size() - 1) << ", seq = " << seqno);
+ const Name name = interest.getName();
+ _LOG_DEBUG(" <<< :( timeout " << name.getSubName(0, name.size() - 1) << ", seq = " << seqno);
// cout << "Fetcher::OnTimeout: " << name << endl;
// cout << "Last: " << m_lastPositiveActivity << ", config: " << m_maximumNoActivityPeriod
// << ", now: " << date_time::second_clock<boost::posix_time::ptime>::universal_time()
- // << ", oldest: " << (date_time::second_clock<boost::posix_time::ptime>::universal_time() - m_maximumNoActivityPeriod) << endl;
+ // << ", oldest: " <<(date_time::second_clock<boost::posix_time::ptime>::universal_time() -
+ // m_maximumNoActivityPeriod) << endl;
- if (m_lastPositiveActivity < (date_time::second_clock<boost::posix_time::ptime>::universal_time() -
- m_maximumNoActivityPeriod)) {
+ if (m_lastPositiveActivity <
+ (time::steady_clock::now() - m_maximumNoActivityPeriod)) {
bool done = false;
{
- unique_lock<mutex> lock(m_seqNoMutex);
+ std::unique_lock<std::mutex> lock(m_seqNoMutex);
m_inActivePipeline.erase(seqno);
m_activePipeline--;
@@ -267,20 +256,25 @@
if (done) {
{
- unique_lock<mutex> lock(m_seqNoMutex);
+ std::unique_lock<std::mutex> lock(m_seqNoMutex);
_LOG_DEBUG("Telling that fetch failed");
_LOG_DEBUG("Active pipeline size should be zero: " << m_inActivePipeline.size());
}
m_active = false;
- if (!m_onFetchFailed.empty()) {
- m_onFetchFailed(ref(*this));
+ if (m_onFetchFailed != nullptr) {
+ m_onFetchFailed(std::ref(*this));
}
// this is not valid anymore, but we still should be able finish work
}
}
else {
_LOG_DEBUG("Asking to reexpress seqno: " << seqno);
- m_ccnx->sendInterest(name, closure, selectors);
+ m_face.expressInterest(interest,
+ bind(&Fetcher::OnData, this, seqno, _1, _2), // TODO: correct?
+ bind(&Fetcher::OnTimeout, this, seqno, _1)); // TODO: correct?
}
}
+
+} // namespace chronoshare
+} // namespace ndn
diff --git a/src/fetcher.hpp b/src/fetcher.hpp
index c1da2a3..40d5258 100644
--- a/src/fetcher.hpp
+++ b/src/fetcher.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016, Regents of the University of California.
+ * Copyright (c) 2013-2017, Regents of the University of California.
*
* This file is part of ChronoShare, a decentralized file sharing application over NDN.
*
@@ -18,47 +18,46 @@
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
-#ifndef FETCHER_H
-#define FETCHER_H
+#ifndef CHRONOSHARE_SRC_FETCHER_HPP
+#define CHRONOSHARE_SRC_FETCHER_HPP
-#include "ccnx-name.h"
-#include "ccnx-wrapper.h"
+#include "core/chronoshare-common.hpp"
-#include "executor.h"
-#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <ndn-cxx/face.hpp>
+
+#include <set>
+#include <thread>
+#include <mutex>
#include <boost/intrusive/list.hpp>
-#include <set>
-#include <set>
+namespace ndn {
+namespace chronoshare {
class FetchManager;
class Fetcher
{
public:
- typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName, uint64_t seq, Ccnx::PcoPtr pco)>
- SegmentCallback;
- typedef boost::function<void(Ccnx::Name& deviceName, Ccnx::Name& baseName)> FinishCallback;
- typedef boost::function<void(Fetcher&, const Ccnx::Name& deviceName, const Ccnx::Name& baseName)>
- OnFetchCompleteCallback;
- typedef boost::function<void(Fetcher&)> OnFetchFailedCallback;
+ typedef std::function<void(Name& deviceName, Name& baseName, uint64_t seq, shared_ptr<Data> data)> SegmentCallback;
+ typedef std::function<void(Name& deviceName, Name& baseName)> FinishCallback;
+ typedef std::function<void(Fetcher&, const Name& deviceName, const Name& baseName)> OnFetchCompleteCallback;
+ typedef std::function<void(Fetcher&)> OnFetchFailedCallback;
- Fetcher(Ccnx::CcnxWrapperPtr ccnx, ExecutorPtr executor,
+ Fetcher(Face& face,
const SegmentCallback& segmentCallback, // callback passed by caller of FetchManager
const FinishCallback& finishCallback, // callback passed by caller of FetchManager
- OnFetchCompleteCallback onFetchComplete,
- OnFetchFailedCallback onFetchFailed, // callbacks provided by FetchManager
- const Ccnx::Name& deviceName, const Ccnx::Name& name, int64_t minSeqNo, int64_t maxSeqNo,
- boost::posix_time::time_duration timeout =
- boost::posix_time::seconds(30), // this time is not precise, but sets min bound
- // actual time depends on how fast Interests timeout
- const Ccnx::Name& forwardingHint = Ccnx::Name());
+ const OnFetchCompleteCallback& onFetchComplete,
+ const OnFetchFailedCallback& onFetchFailed, // callbacks provided by FetchManager
+ const Name& deviceName, const Name& name, int64_t minSeqNo, int64_t maxSeqNo,
+ time::milliseconds timeout = time::seconds(30), // this time is not precise, but sets min bound
+ // actual time depends on how fast Interests timeout
+ const Name& forwardingHint = Name());
virtual ~Fetcher();
- inline bool
+ bool
IsActive() const;
- inline bool
+ bool
IsTimedWait() const
{
return m_timedwait;
@@ -68,46 +67,46 @@
RestartPipeline();
void
- SetForwardingHint(const Ccnx::Name& forwardingHint);
+ SetForwardingHint(const Name& forwardingHint);
- const Ccnx::Name&
+ const Name&
GetForwardingHint() const
{
return m_forwardingHint;
}
- const Ccnx::Name&
+ const Name&
GetName() const
{
return m_name;
}
- const Ccnx::Name&
+ const Name&
GetDeviceName() const
{
return m_deviceName;
}
- double
+ time::seconds
GetRetryPause() const
{
return m_retryPause;
}
void
- SetRetryPause(double pause)
+ SetRetryPause(time::seconds pause)
{
m_retryPause = pause;
}
- boost::posix_time::ptime
+ const time::steady_clock::TimePoint&
GetNextScheduledRetry() const
{
return m_nextScheduledRetry;
}
void
- SetNextScheduledRetry(boost::posix_time::ptime nextScheduledRetry)
+ SetNextScheduledRetry(const time::steady_clock::TimePoint& nextScheduledRetry)
{
m_nextScheduledRetry = nextScheduledRetry;
}
@@ -117,24 +116,16 @@
FillPipeline();
void
- OnData(uint64_t seqno, const Ccnx::Name& name, Ccnx::PcoPtr data);
+ OnData(uint64_t seqno, const Interest& interest, Data& data);
void
- OnData_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::PcoPtr data);
-
- void
- OnTimeout(uint64_t seqno, const Ccnx::Name& name, const Ccnx::Closure& closure,
- Ccnx::Selectors selectors);
-
- void
- OnTimeout_Execute(uint64_t seqno, Ccnx::Name name, Ccnx::Closure closure,
- Ccnx::Selectors selectors);
+ OnTimeout(uint64_t seqno, const Interest& interest);
public:
boost::intrusive::list_member_hook<> m_managerListHook;
private:
- Ndnx::NdnxWrapperPtr m_ndnx;
+ Face& m_face;
SegmentCallback m_segmentCallback;
OnFetchCompleteCallback m_onFetchComplete;
@@ -145,55 +136,47 @@
bool m_active;
bool m_timedwait;
- Ndnx::Name m_name;
- Ndnx::Name m_deviceName;
- Ndnx::Name m_forwardingHint;
+ Name m_name;
+ Name m_deviceName;
+ Name m_forwardingHint;
- boost::posix_time::time_duration m_maximumNoActivityPeriod;
+ time::milliseconds m_maximumNoActivityPeriod;
int64_t m_minSendSeqNo;
int64_t m_maxInOrderRecvSeqNo;
std::set<int64_t> m_outOfOrderRecvSeqNo;
std::set<int64_t> m_inActivePipeline;
- int64_t m_minSeqNo;
+ // int64_t m_minSeqNo;
int64_t m_maxSeqNo;
uint32_t m_pipeline;
uint32_t m_activePipeline;
- double m_rto;
- double m_maxRto;
+ // double m_rto;
+ // double m_maxRto;
bool m_slowStart;
uint32_t m_threshold;
uint32_t m_roundCount;
- boost::posix_time::ptime m_lastPositiveActivity;
+ time::steady_clock::TimePoint m_lastPositiveActivity;
- double m_retryPause; // pause to stop trying to fetch (for fetch-manager)
- boost::posix_time::ptime m_nextScheduledRetry;
+ time::seconds m_retryPause; // pause to stop trying to fetch(for fetch-manager)
+ time::steady_clock::TimePoint m_nextScheduledRetry;
- ExecutorPtr m_executor; // to serialize FillPipeline events
+ std::mutex m_seqNoMutex;
- boost::mutex m_seqNoMutex;
- boost::mutex m_rtoMutex;
- boost::mutex m_pipelineMutex;
+ boost::asio::io_service& m_ioService;
};
-typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
+typedef shared_ptr<Fetcher> FetcherPtr;
-namespace Error {
-struct Fetcher : virtual boost::exception, virtual std::exception
-{
-};
-}
-
-typedef boost::shared_ptr<Fetcher> FetcherPtr;
-
-bool
+inline bool
Fetcher::IsActive() const
{
return m_active;
}
+} // namespace chronoshare
+} // namespace ndn
-#endif // FETCHER_H
+#endif // CHRONOSHARE_SRC_FETCHER_HPP
diff --git a/wscript b/wscript
index 117e90c..8ac2af3 100644
--- a/wscript
+++ b/wscript
@@ -108,6 +108,7 @@
'src/file-state.cpp',
'src/action-log.cpp',
'src/object-*.cpp',
+ 'src/fetch*.cpp',
]),
use='core-objects adhoc NDN_CXX BOOST TINYXML SQLITE3',
includes="src",