blob: 273402abe0d252e71d9b7803ba3daf14d082029d [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "discovery.h"
23
24#include "scheduler/scheduler.h"
25#include "scheduler/simple-interval-generator.h"
26#include "scheduler/task.h"
27#include "scheduler/periodic-task.h"
28
29#include <sstream>
30#include <boost/make_shared.hpp>
31#include <boost/bind.hpp>
32
33using namespace std;
34
35namespace ndn
36{
37
38namespace discovery
39{
40
41const string
42TaggedFunction::CHAR_SET = string("abcdefghijklmnopqtrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
43
44TaggedFunction::TaggedFunction(const Callback &callback, const string &tag)
45 : m_callback(callback)
46 , m_tag(tag)
47{
48}
49
50string
51TaggedFunction::GetRandomTag()
52{
53 //boost::random::random_device rng;
54 boost::random::uniform_int_distribution<> dist(0, CHAR_SET.size() - 1);
55 ostringstream oss;
56 for (int i = 0; i < DEFAULT_TAG_SIZE; i++)
57 {
58 //oss << CHAR_SET[dist(rng)];
59 }
60 return oss.str();
61}
62
63void
64TaggedFunction::operator()(const Name &name)
65{
66 if (!m_callback.empty())
67 {
68 m_callback(name);
69 }
70}
71
72} // namespace discovery
73
74const double
75Discovery::INTERVAL = 15.0;
76
77Discovery *
78Discovery::instance = NULL;
79
80boost::mutex
81Discovery::mutex;
82
83Discovery::Discovery()
84 : m_scheduler(new Scheduler())
85 , m_localPrefix("/")
86{
87 m_scheduler->start();
88
89 Scheduler::scheduleOneTimeTask (m_scheduler, 0,
90 boost::bind(&Discovery::poll, this), "Initial-Local-Prefix-Check");
91 Scheduler::schedulePeriodicTask (m_scheduler,
92 boost::make_shared<SimpleIntervalGenerator>(INTERVAL),
93 boost::bind(&Discovery::poll, this), "Local-Prefix-Check");
94}
95
96Discovery::~Discovery()
97{
98 m_scheduler->shutdown();
99}
100
101void
102Discovery::addCallback(const discovery::TaggedFunction &callback)
103{
104 m_callbacks.push_back(callback);
105}
106
107int
108Discovery::deleteCallback(const discovery::TaggedFunction &callback)
109{
110 List::iterator it = m_callbacks.begin();
111 while (it != m_callbacks.end())
112 {
113 if ((*it) == callback)
114 {
115 it = m_callbacks.erase(it);
116 }
117 else
118 {
119 ++it;
120 }
121 }
122 return m_callbacks.size();
123}
124
125void
126Discovery::poll()
127{
128 Name localPrefix = Wrapper::getLocalPrefix();
129 if (localPrefix != m_localPrefix)
130 {
131 Lock lock(mutex);
132 for (List::iterator it = m_callbacks.begin(); it != m_callbacks.end(); ++it)
133 {
134 (*it)(localPrefix);
135 }
136 m_localPrefix = localPrefix;
137 }
138}
139
140void
141Discovery::registerCallback(const discovery::TaggedFunction &callback)
142{
143 Lock lock(mutex);
144 if (instance == NULL)
145 {
146 instance = new Discovery();
147 }
148
149 instance->addCallback(callback);
150}
151
152void
153Discovery::deregisterCallback(const discovery::TaggedFunction &callback)
154{
155 Lock lock(mutex);
156 if (instance == NULL)
157 {
158 cerr << "Discovery::deregisterCallback called without instance" << endl;
159 }
160 else
161 {
162 int size = instance->deleteCallback(callback);
163 if (size == 0)
164 {
165 delete instance;
166 instance = NULL;
167 }
168 }
169}
170
171}
172