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