Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Mengjin Yan <jane.yan0129@gmail.com> |
| 9 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 10 | */ |
| 11 | |
| 12 | #ifndef CHRONOCHAT_CHATROOM_DISCOVERY_LOGIC_HPP |
| 13 | #define CHRONOCHAT_CHATROOM_DISCOVERY_LOGIC_HPP |
| 14 | |
| 15 | #include "chatroom-info.hpp" |
| 16 | #include <ndn-cxx/util/scheduler.hpp> |
| 17 | |
| 18 | namespace chronos { |
| 19 | |
| 20 | class ChatroomDiscoveryLogic : noncopyable |
| 21 | { |
| 22 | public: |
| 23 | typedef function<void(const ChatroomInfo& chatroomName, bool isAdd)> UpdateCallback; |
| 24 | |
| 25 | typedef std::map<Name::Component, chronos::ChatroomInfo> Chatrooms; |
| 26 | static const size_t OFFSET_CHATROOM_NAME; |
| 27 | static const size_t DISCOVERY_INTEREST_NAME_SIZE; |
| 28 | static const size_t REFRESHING_INTEREST_NAME_SIZE; |
| 29 | static const time::milliseconds DEFAULT_REFRESHING_TIMER; |
| 30 | static const Name DISCOVERY_PREFIX; |
| 31 | |
| 32 | public: |
| 33 | explicit |
| 34 | ChatroomDiscoveryLogic(shared_ptr<ndn::Face> face, |
| 35 | const UpdateCallback& updateCallback); |
| 36 | |
| 37 | |
| 38 | /** \brief obtain the ongoing chatroom list |
| 39 | */ |
| 40 | const Chatrooms& |
| 41 | getChatrooms() const; |
| 42 | |
| 43 | /** \brief add a local chatroom in the ongoing chatroom list |
| 44 | */ |
| 45 | void |
| 46 | addLocalChatroom(const ChatroomInfo& chatroom); |
| 47 | |
| 48 | void |
| 49 | removeLocalChatroom(const Name::Component& chatroomName); |
| 50 | |
| 51 | /** \brief send discovery interest |
| 52 | */ |
| 53 | void |
| 54 | sendDiscoveryInterest(); |
| 55 | |
Yingdi Yu | 6a61444 | 2014-10-31 17:42:43 -0700 | [diff] [blame^] | 56 | CHRONOCHAT_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 57 | /** |
| 58 | */ |
| 59 | void |
| 60 | onDiscoveryInterest(const Name& name, const Interest& interest); |
| 61 | |
| 62 | /** |
| 63 | */ |
| 64 | void |
| 65 | onPrefixRegistrationFailed(const Name& name); |
| 66 | |
| 67 | /** \brief schedule another discovery |
| 68 | */ |
| 69 | void |
| 70 | onDiscoveryInterestTimeout(const Interest& interest); |
| 71 | |
| 72 | /** \brief send interest to find if the certain chatroom exists |
| 73 | */ |
| 74 | void |
| 75 | refreshChatroom(const Name::Component& chatroomName); |
| 76 | |
| 77 | /** \brief copy the contact from the participants of the chatroom to contacts of the chatroom |
| 78 | */ |
| 79 | void |
| 80 | addContacts(ChatroomInfo& chatroom); |
| 81 | |
Yingdi Yu | 6a61444 | 2014-10-31 17:42:43 -0700 | [diff] [blame^] | 82 | CHRONOCHAT_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 83 | |
| 84 | /** \brief erase the chatroom from the ongoing chatroom list |
| 85 | */ |
| 86 | void |
| 87 | onRefreshingInterestTimeout(const Interest& interest); |
| 88 | |
| 89 | /** \brief operate the chatroom data and schedule discovery and refreshing process |
| 90 | */ |
| 91 | void |
| 92 | onReceiveData(const ndn::Interest& interest, const ndn::Data& data, const bool isRefreshing); |
| 93 | |
| 94 | private: |
| 95 | static const time::seconds m_discoveryInterval; |
| 96 | |
| 97 | shared_ptr<ndn::Face> m_face; |
| 98 | |
| 99 | ndn::KeyChain m_keyChain; |
| 100 | |
| 101 | Chatrooms m_chatrooms; |
| 102 | Chatrooms m_localChatrooms; |
| 103 | |
| 104 | ndn::Scheduler m_scheduler; |
| 105 | |
| 106 | UpdateCallback m_onUpdate; |
| 107 | |
| 108 | }; |
| 109 | |
| 110 | inline const ChatroomDiscoveryLogic::Chatrooms& |
| 111 | ChatroomDiscoveryLogic::getChatrooms() const |
| 112 | { |
| 113 | return m_chatrooms; |
| 114 | } |
| 115 | |
| 116 | } // namespace chronos |
| 117 | |
| 118 | #endif // CHRONOCHAT_CHATROOM_DISCOVERY_LOGIC_HPP |