Zhenkai Zhu | e4dc4d8 | 2013-01-23 13:17:08 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 31 | #include <boost/random.hpp> |
Zhenkai Zhu | e4dc4d8 | 2013-01-23 13:17:08 -0800 | [diff] [blame] | 32 | #include <boost/thread/mutex.hpp> |
| 33 | #include <boost/thread/locks.hpp> |
| 34 | #include <list> |
| 35 | |
| 36 | namespace Ccnx |
| 37 | { |
| 38 | |
| 39 | class CcnxDiscovery; |
| 40 | typedef boost::shared_ptr<CcnxDiscovery> CcnxDiscoveryPtr; |
| 41 | |
| 42 | class TaggedFunction |
| 43 | { |
| 44 | public: |
| 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 | |
| 55 | private: |
| 56 | static const std::string CHAR_SET; |
| 57 | static const int DEFAULT_TAG_SIZE = 32; |
| 58 | |
| 59 | static std::string |
| 60 | GetRandomTag(); |
| 61 | |
| 62 | private: |
| 63 | Callback m_callback; |
| 64 | std::string m_tag; |
| 65 | }; |
| 66 | |
| 67 | class CcnxDiscovery |
| 68 | { |
| 69 | public: |
| 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 | |
| 82 | private: |
| 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 | |
| 95 | private: |
| 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 |