core: use ndn-cxx scheduler::ScopedEventId

refs #4698

Change-Id: I5ab56855fb22824e129fd7e3ccfb53174cf34f82
This commit is contained in:
Junxiao Shi
2019-01-17 19:06:00 +00:00
parent 9120cee21d
commit feddc3c054
10 changed files with 43 additions and 246 deletions
+2 -50
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2017, Regents of the University of California,
/*
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -49,59 +49,11 @@ schedule(time::nanoseconds after, const EventCallback& event)
return getGlobalScheduler().scheduleEvent(after, event);
}
void
cancel(const EventId& eventId)
{
getGlobalScheduler().cancelEvent(eventId);
}
void
resetGlobalScheduler()
{
g_scheduler.reset();
}
ScopedEventId::ScopedEventId()
{
}
ScopedEventId::ScopedEventId(const EventId& event)
: m_event(event)
{
}
ScopedEventId::ScopedEventId(ScopedEventId&& other)
: m_event(other.m_event)
{
other.release();
}
ScopedEventId&
ScopedEventId::operator=(const EventId& event)
{
if (m_event != event) {
scheduler::cancel(m_event);
m_event = event;
}
return *this;
}
ScopedEventId::~ScopedEventId()
{
scheduler::cancel(m_event);
}
void
ScopedEventId::cancel()
{
scheduler::cancel(m_event);
}
void
ScopedEventId::release()
{
m_event.reset();
}
} // namespace scheduler
} // namespace nfd
+8 -51
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,13 +33,9 @@
namespace nfd {
namespace scheduler {
using ndn::Scheduler;
/** \class nfd::scheduler::EventId
* \brief Opaque handle for a scheduled event.
*/
using ndn::EventId;
using ndn::util::scheduler::Scheduler;
using ndn::util::scheduler::EventId;
using ndn::util::scheduler::ScopedEventId;
using ndn::util::scheduler::EventCallback;
/** \brief Schedule an event.
@@ -49,50 +45,11 @@ schedule(time::nanoseconds after, const EventCallback& event);
/** \brief Cancel a scheduled event.
*/
void
cancel(const EventId& eventId);
/** \brief Cancels an event automatically upon destruction.
*/
class ScopedEventId : noncopyable
inline void
cancel(EventId eventId)
{
public:
ScopedEventId();
/** \brief Implicit constructor from EventId.
* \param event the event to be cancelled upon destruction
*/
ScopedEventId(const EventId& event);
/** \brief Move constructor.
*/
ScopedEventId(ScopedEventId&& other);
/** \brief Assigns an event.
*
* If a different event has been assigned to this instance previously,
* that event will be cancelled immediately.
*/
ScopedEventId&
operator=(const EventId& event);
/** \brief Cancels the event.
*/
~ScopedEventId();
/** \brief Cancels the event manually.
*/
void
cancel();
/** \brief Releases the event so that it won't be cancelled when this ScopedEventId is destructed.
*/
void
release();
private:
EventId m_event;
};
eventId.cancel();
}
} // namespace scheduler
} // namespace nfd
+2 -2
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -75,7 +75,7 @@ Readvertise::afterAddRoute(const RibRouteRef& ribRoute)
ReadvertisedRouteContainer::iterator rrIt;
bool isNew = false;
std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix, m_scheduler);
std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix);
if (!isNew && rrIt->signer != action->signer) {
NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+4 -4
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -29,7 +29,7 @@
#include "core/common.hpp"
#include <ndn-cxx/security/signing-info.hpp>
#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
#include <ndn-cxx/util/scheduler.hpp>
namespace nfd {
namespace rib {
@@ -39,11 +39,11 @@ namespace rib {
class ReadvertisedRoute : noncopyable
{
public:
ReadvertisedRoute(const Name& prefix, ndn::util::Scheduler& scheduler)
explicit
ReadvertisedRoute(const Name& prefix)
: prefix(prefix)
, nRibRoutes(0)
, retryDelay(0)
, retryEvt(scheduler)
{
}
+2 -3
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -59,7 +59,6 @@ RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
, m_localhostValidator(face)
, m_localhopValidator(face)
, m_isLocalhopEnabled(false)
, m_activeFaceFetchEvent(m_scheduler)
{
registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
@@ -139,7 +138,7 @@ RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseco
if (expires) {
auto event = m_scheduler.scheduleEvent(*expires, [=] { m_rib.onRouteExpiration(name, route); });
route.setExpirationEvent(event, m_scheduler);
route.setExpirationEvent(event);
NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
}
+2 -2
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -36,7 +36,7 @@
#include <ndn-cxx/mgmt/nfd/controller.hpp>
#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
#include <ndn-cxx/util/scheduler.hpp>
namespace nfd {
namespace rib {
+4 -7
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -60,17 +60,15 @@ public:
}
void
setExpirationEvent(const ndn::util::scheduler::EventId& eid, ndn::util::Scheduler& scheduler)
setExpirationEvent(const ndn::util::scheduler::EventId& eid)
{
m_expirationEvent = eid;
m_scheduler = &scheduler;
}
void
cancelExpirationEvent() const
cancelExpirationEvent()
{
if (m_scheduler)
m_scheduler->cancelEvent(m_expirationEvent);
m_expirationEvent.cancel();
}
std::underlying_type<ndn::nfd::RouteFlags>::type
@@ -104,7 +102,6 @@ public:
private:
ndn::util::scheduler::EventId m_expirationEvent;
ndn::util::Scheduler* m_scheduler;
};
bool
+14 -121
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2016, Regents of the University of California,
/*
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -30,7 +30,6 @@
#include <boost/thread.hpp>
namespace nfd {
namespace scheduler {
// defined in scheduler.cpp
Scheduler&
@@ -39,58 +38,28 @@ getGlobalScheduler();
namespace tests {
using scheduler::EventId;
using scheduler::ScopedEventId;
BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture)
class SchedulerFixture : protected BaseFixture
BOOST_AUTO_TEST_CASE(ScheduleCancel)
{
public:
SchedulerFixture()
: count1(0)
, count2(0)
, count3(0)
{
}
int count1 = 0, count2 = 0, count3 = 0;
void
event1()
{
scheduler::schedule(500_ms, [&] {
BOOST_CHECK_EQUAL(count3, 1);
++count1;
}
});
void
event2()
{
++count2;
}
scheduler::EventId eid = scheduler::schedule(1_s, [&] { ++count2; });
scheduler::cancel(eid);
void
event3()
{
scheduler::schedule(250_ms, [&] {
BOOST_CHECK_EQUAL(count1, 0);
++count3;
}
});
public:
int count1;
int count2;
int count3;
};
BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
{
scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this));
scheduler::cancel(i);
scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
scheduler::cancel(i);
{
scheduler::ScopedEventId se = scheduler::schedule(50_ms, [&] { ++count2; });
} // se goes out of scope, canceling the event
g_io.run();
@@ -99,87 +68,11 @@ BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
BOOST_CHECK_EQUAL(count3, 1);
}
BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
{
EventId i;
scheduler::cancel(i);
// Trivial check to avoid "test case did not check any assertions" message from Boost.Test
BOOST_CHECK(true);
}
class SelfCancelFixture : protected BaseFixture
{
public:
void
cancelSelf() const
{
scheduler::cancel(m_selfEventId);
}
public:
EventId m_selfEventId;
};
BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
{
m_selfEventId = scheduler::schedule(time::milliseconds(100),
bind(&SelfCancelFixture::cancelSelf, this));
BOOST_REQUIRE_NO_THROW(g_io.run());
}
BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture)
{
int hit = 0;
{
ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
} // se goes out of scope
this->advanceClocks(time::milliseconds(1), 15);
BOOST_CHECK_EQUAL(hit, 0);
}
BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture)
{
int hit1 = 0, hit2 = 0;
ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; });
se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; });
this->advanceClocks(time::milliseconds(1), 15);
BOOST_CHECK_EQUAL(hit1, 0);
BOOST_CHECK_EQUAL(hit2, 1);
}
BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture)
{
int hit = 0;
{
ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
se.release();
} // se goes out of scope
this->advanceClocks(time::milliseconds(1), 15);
BOOST_CHECK_EQUAL(hit, 1);
}
BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture)
{
int hit = 0;
unique_ptr<scheduler::ScopedEventId> se2;
{
ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; });
se2.reset(new ScopedEventId(std::move(se)));
} // se goes out of scope
this->advanceClocks(time::milliseconds(1), 15);
BOOST_CHECK_EQUAL(hit, 1);
}
BOOST_AUTO_TEST_CASE(ThreadLocalScheduler)
{
scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler();
scheduler::Scheduler* s2 = nullptr;
boost::thread t([&s2] {
s2 = &scheduler::getGlobalScheduler();
});
boost::thread t([&s2] { s2 = &scheduler::getGlobalScheduler(); });
t.join();
BOOST_CHECK(s1 != nullptr);
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
{
Scenario scenario;
Name prefix("/ndn/memphis/test");
ReadvertisedRoute rr(prefix, scheduler);
ReadvertisedRoute rr(prefix);
const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
dest.advertise(rr, successCallback, failureCallback);
@@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
{
Scenario scenario;
Name prefix("/ndn/memphis/test");
ReadvertisedRoute rr(prefix, scheduler);
ReadvertisedRoute rr(prefix);
const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
dest.withdraw(rr, successCallback, failureCallback);
+2 -3
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2017, Regents of the University of California,
* Copyright (c) 2014-2019, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -35,7 +35,6 @@
#include <boost/program_options/variables_map.hpp>
#include <ndn-cxx/net/network-monitor.hpp>
#include <ndn-cxx/util/scheduler.hpp>
#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
#include <ndn-cxx/util/time.hpp>
// suppress warning caused by boost::program_options::parse_config_file
@@ -88,7 +87,7 @@ runDaemon(Procedure& proc)
});
util::Scheduler sched(proc.getIoService());
util::scheduler::ScopedEventId runEvt(sched);
util::scheduler::ScopedEventId runEvt;
auto scheduleRerun = [&] (time::nanoseconds delay) {
runEvt = sched.scheduleEvent(delay, [&] { proc.runOnce(); });
};