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