blob: 8d8590b0859d17e6f0dead36d67edda71c2d7b28 [file] [log] [blame]
Mengjin Yanaec70742014-08-25 10:37:45 -07001/* -*- 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#include "chatroom-discovery-logic.hpp"
13
14
15namespace chronos {
16
17const size_t ChatroomDiscoveryLogic::OFFSET_CHATROOM_NAME = 4;
18const size_t ChatroomDiscoveryLogic::DISCOVERY_INTEREST_NAME_SIZE = 4;
19const size_t ChatroomDiscoveryLogic::REFRESHING_INTEREST_NAME_SIZE = 5;
20const time::milliseconds ChatroomDiscoveryLogic::DEFAULT_REFRESHING_TIMER(10000);
21const Name ChatroomDiscoveryLogic::DISCOVERY_PREFIX("/ndn/broadcast/chronochat/chatroom-list");
22const time::seconds ChatroomDiscoveryLogic::m_discoveryInterval(600);
23
24ChatroomDiscoveryLogic::ChatroomDiscoveryLogic(shared_ptr<ndn::Face> face,
25 const UpdateCallback& updateCallback)
26 : m_face(face)
27 , m_scheduler(face->getIoService())
28 , m_onUpdate(updateCallback)
29{
30
31 m_face->setInterestFilter(DISCOVERY_PREFIX,
32 bind(&ChatroomDiscoveryLogic::onDiscoveryInterest,
33 this, _1, _2),
34 bind(&ChatroomDiscoveryLogic::onPrefixRegistrationFailed,
35 this, _1));
36}
37
38void
39ChatroomDiscoveryLogic::addLocalChatroom(const ChatroomInfo& chatroom)
40{
41 m_localChatrooms[chatroom.getName()] = chatroom;
42 m_chatrooms[chatroom.getName()] = chatroom; // no need to discover local chatroom
43}
44
45void
46ChatroomDiscoveryLogic::removeLocalChatroom(const Name::Component& chatroomName)
47{
48 m_localChatrooms.erase(chatroomName);
49 m_chatrooms.erase(chatroomName);
50}
51
52void
53ChatroomDiscoveryLogic::sendDiscoveryInterest()
54{
55 Interest discovery(DISCOVERY_PREFIX);
56 discovery.setMustBeFresh(true);
57 discovery.setInterestLifetime(time::milliseconds(10000));
58
59 Exclude exclude;
60 Chatrooms::iterator it;
61 for (it = m_chatrooms.begin(); it != m_chatrooms.end(); ++it) {
62 exclude.excludeOne(it->first);
63 }
64 discovery.setExclude(exclude);
65
66 m_face->expressInterest(discovery,
67 bind(&ChatroomDiscoveryLogic::onReceiveData, this,
68 _1, _2, false),
69 bind(&ChatroomDiscoveryLogic::onDiscoveryInterestTimeout, this,
70 _1));
71
72}
73
74void
75ChatroomDiscoveryLogic::onDiscoveryInterest(const Name& name, const Interest& interest)
76{
77 if (m_localChatrooms.empty())
78 return;
79
80 if (interest.getName() == DISCOVERY_PREFIX) {
81 // discovery
82 for (Chatrooms::const_iterator it = m_localChatrooms.begin();
83 it != m_localChatrooms.end(); ++it) {
84
85 if (!interest.getExclude().empty() &&
86 interest.getExclude().isExcluded(it->first))
87 continue;
88
89
90 Name dataName(interest.getName());
91 dataName.append(it->first);
92
93 shared_ptr<Data> chatroomInfo = make_shared<Data>(dataName);
94 chatroomInfo->setFreshnessPeriod(time::seconds(10));
95 chatroomInfo->setContent(it->second.wireEncode());
96
97 m_keyChain.sign(*chatroomInfo);
98 m_face->put(*chatroomInfo);
99 break;
100 }
101 return;
102 }
103
104 if (DISCOVERY_PREFIX.isPrefixOf(interest.getName()) &&
105 interest.getName().size() == REFRESHING_INTEREST_NAME_SIZE) {
106 // refreshing
107 Chatrooms::const_iterator it =
108 m_localChatrooms.find(interest.getName().get(OFFSET_CHATROOM_NAME));
109
110 if (it == m_localChatrooms.end())
111 return;
112
113 shared_ptr<Data> chatroomInfo = make_shared<Data>(interest.getName());
114 chatroomInfo->setFreshnessPeriod(time::seconds(10));
115 chatroomInfo->setContent(it->second.wireEncode());
116
117 m_keyChain.sign(*chatroomInfo);
118 m_face->put(*chatroomInfo);
119
120 return;
121 }
122}
123
124void
125ChatroomDiscoveryLogic::onPrefixRegistrationFailed(const Name& name)
126{
127}
128
129void
130ChatroomDiscoveryLogic::refreshChatroom(const Name::Component& chatroomName)
131{
132 Name name = DISCOVERY_PREFIX;
133 name.append(chatroomName);
134
135 BOOST_ASSERT(name.size() == REFRESHING_INTEREST_NAME_SIZE);
136
137 Interest discovery(name);
138 discovery.setMustBeFresh(true);
139 discovery.setInterestLifetime(time::milliseconds(10000));
140
141 m_face->expressInterest(discovery,
142 bind(&ChatroomDiscoveryLogic::onReceiveData, this,
143 _1, _2, true),
144 bind(&ChatroomDiscoveryLogic::onRefreshingInterestTimeout, this,
145 _1));
146
147}
148
149void
150ChatroomDiscoveryLogic::onReceiveData(const ndn::Interest& interest,
151 const ndn::Data& data,
152 const bool isRefreshing)
153{
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700154 // Name::Component chatroomName = data.getName().get(OFFSET_CHATROOM_NAME);
Mengjin Yanaec70742014-08-25 10:37:45 -0700155
156 ChatroomInfo chatroom;
157 chatroom.wireDecode(data.getContent().blockFromValue());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700158 // chatroom.setName(chatroomName);
Mengjin Yanaec70742014-08-25 10:37:45 -0700159
160 // Tmp Disabled
161 // if (chatroom.getTrustModel() == ChatroomInfo::TRUST_MODEL_WEBOFTRUST)
162 // addContacts(chatroom);
163
164
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700165 m_chatrooms[chatroom.getName()] = chatroom;
Mengjin Yanaec70742014-08-25 10:37:45 -0700166 m_onUpdate(chatroom, true); //add
167
168 time::milliseconds refreshingTime;
169 if (data.getFreshnessPeriod() > DEFAULT_REFRESHING_TIMER)
170 refreshingTime = data.getFreshnessPeriod();
171 else
172 refreshingTime = DEFAULT_REFRESHING_TIMER;
173
174 m_scheduler.scheduleEvent(refreshingTime,
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700175 bind(&ChatroomDiscoveryLogic::refreshChatroom, this,
176 chatroom.getName()));
Mengjin Yanaec70742014-08-25 10:37:45 -0700177
178 if (!isRefreshing)
179 sendDiscoveryInterest();
180}
181
182void
183ChatroomDiscoveryLogic::onRefreshingInterestTimeout(const ndn::Interest& interest)
184{
185 Name::Component chatroomName = interest.getName().get(OFFSET_CHATROOM_NAME);
186
187 Chatrooms::iterator it = m_chatrooms.find(chatroomName);
188 if (it != m_chatrooms.end()) {
189 m_onUpdate(it->second, false); //delete
190 m_chatrooms.erase(it);
191 }
192}
193
194
195void
196ChatroomDiscoveryLogic::onDiscoveryInterestTimeout(const Interest& interest)
197{
198 m_scheduler.scheduleEvent(m_discoveryInterval,
199 bind(&ChatroomDiscoveryLogic::sendDiscoveryInterest, this));
200}
201
202void
203ChatroomDiscoveryLogic::addContacts(ChatroomInfo& chatroom)
204{
205 // Tmp Disabled
206
207 // std::vector<Name>::const_iterator nameIt;
208 // std::vector<shared_ptr<Contact> >::iterator contactIt;
209 // ContactList contactList;
210 // m_contactManager->getContactList(contactList);
211
212 // for (contactIt = contactList.begin();
213 // contactIt != contactList.end(); ++contactIt) {
214 // nameIt = std::find(chatroom.getParticipants().begin(),
215 // chatroom.getParticipants().end(), (*contactIt)->getNameSpace());
216 // if (nameIt != chatroom.getParticipants().end())
217 // chatroom.addContact(*nameIt);
218 // }
219}
220
221
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700222} // namespace chronos