blob: 1dc684d4b822f572d1b9df16a91937660467ad19 [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#ifndef NDN_DISCOVERY_H
23#define NDN_DISCOVERY_H
24
25#include "ndn.cxx/wrapper.h"
26#include "ndn.cxx/common.h"
27#include "ndn.cxx/name.h"
28
29#include <boost/shared_ptr.hpp>
30#include <boost/function.hpp>
31#include <boost/random/random_device.hpp>
32#include <boost/random/uniform_int_distribution.hpp>
33#include <boost/thread/mutex.hpp>
34#include <boost/thread/locks.hpp>
35#include <list>
36
37class Scheduler;
38typedef boost::shared_ptr<Scheduler> SchedulerPtr;
39
40namespace ndn
41{
42
43class Discovery;
44typedef boost::shared_ptr<Discovery> DiscoveryPtr;
45
46namespace discovery
47{
48
49class TaggedFunction
50{
51public:
52 typedef boost::function<void (const Name &)> Callback;
53 TaggedFunction(const Callback &callback, const std::string &tag = GetRandomTag());
54 ~TaggedFunction(){};
55
56 bool
57 operator==(const TaggedFunction &other) { return m_tag == other.m_tag; }
58
59 void
60 operator()(const Name &name);
61
62private:
63 static const std::string CHAR_SET;
64 static const int DEFAULT_TAG_SIZE = 32;
65
66 static std::string
67 GetRandomTag();
68
69private:
70 Callback m_callback;
71 std::string m_tag;
72};
73
74}
75
76class Discovery
77{
78public:
79 const static double INTERVAL;
80 // Add a callback to be invoked when local prefix changes
81 // you must remember to deregister the callback
82 // otherwise you may have undefined behavior if the callback is
83 // bind to a member function of an object and the object is deleted
84 static void
85 registerCallback(const discovery::TaggedFunction &callback);
86
87 // remember to call this before you quit
88 static void
89 deregisterCallback(const discovery::TaggedFunction &callback);
90
91private:
92 Discovery();
93 ~Discovery();
94
95 void
96 poll();
97
98 void
99 addCallback(const discovery::TaggedFunction &callback);
100
101 int
102 deleteCallback(const discovery::TaggedFunction &callback);
103
104private:
105 typedef boost::mutex Mutex;
106 typedef boost::unique_lock<Mutex> Lock;
107 typedef std::list<discovery::TaggedFunction> List;
108
109 static Discovery *instance;
110 static Mutex mutex;
111 List m_callbacks;
112 SchedulerPtr m_scheduler;
113 Name m_localPrefix;
114};
115
116} // ndn
117
118#endif // NDN_DISCOVERY_H