blob: cb014fc49024a329dad1f66aee307d237f42e9b0 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, The University of Memphis
4 *
5 * This file is part of PSync.
6 * See AUTHORS.md for complete list of PSync authors and contributors.
7 *
8 * PSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#ifndef PSYNC_PARTIAL_PRODUCER_HPP
21#define PSYNC_PARTIAL_PRODUCER_HPP
22
23#include "detail/bloom-filter.hpp"
24#include "producer-base.hpp"
25
26#include <map>
27#include <unordered_set>
28
29#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/util/scheduler.hpp>
31#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
32#include <ndn-cxx/util/time.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
34
35namespace psync {
36
37struct PendingEntryInfo
38{
39 BloomFilter bf;
40 IBLT iblt;
41 ndn::util::scheduler::ScopedEventId expirationEvent;
42};
43
44/**
45 * @brief Partial sync logic to publish data names
46 *
47 * Application should call publishName whenever it wants to
48 * let consumers know that new data is available.
49 * Additional userPrefix should be added via addUserNode before calling publishName
50 * Currently, publishing of data needs to be handled by the application.
51 */
52class PartialProducer : public ProducerBase
53{
54public:
55 /**
56 * @brief constructor
57 *
58 * Registers syncPrefix in NFD and sets internal filters for
59 * "sync" and "hello" under syncPrefix
60 *
61 * @param expectedNumEntries expected entries in IBF
62 * @param face application's face
63 * @param syncPrefix The prefix of the sync group
64 * @param userPrefix The prefix of the first user in the group
65 * @param syncReplyFreshness freshness of sync data
66 * @param helloReplyFreshness freshness of hello data
67 */
68 PartialProducer(size_t expectedNumEntries,
69 ndn::Face& face,
70 const ndn::Name& syncPrefix,
71 const ndn::Name& userPrefix,
72 ndn::time::milliseconds helloReplyFreshness = HELLO_REPLY_FRESHNESS,
73 ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS);
74
75 ~PartialProducer();
76
77 /**
78 * @brief Publish name to let subscribed consumers know
79 *
80 * If seq is null then the seq of prefix is incremented by 1 else
81 * the supplied sequence is set in the IBF.
82 * Upon updating the sequence in the IBF satisfyPendingSyncInterests
83 * is called to let subscribed consumers know.
84 *
85 * @param prefix the prefix to be updated
86 * @param seq the sequence number of the prefix
87 */
88 void
89 publishName(const ndn::Name& prefix, ndn::optional<uint64_t> seq = ndn::nullopt);
90
91private:
92 /**
93 * @brief Satisfy any pending interest that have subscription for prefix
94 *
95 * @param prefix the prefix that was updated in publishName
96 */
97 void
98 satisfyPendingSyncInterests(const ndn::Name& prefix);
99
100 /**
101 * @brief Receive hello interest from consumer and respond with hello data
102 *
103 * Hello data's name format is: /\<sync-prefix\>/hello/\<current-IBF\>
104 */
105 void
106 onHelloInterest(const ndn::Name& prefix, const ndn::Interest& interest);
107
108PUBLIC_WITH_TESTS_ELSE_PRIVATE:
109 /**
110 * @brief Receive sync interest from consumer
111 *
112 * Either respond with sync data if consumer is behind or
113 * store sync interest in m_pendingEntries
114 *
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500115 * Sync data's name format is: /\<syncPrefix\>/sync/\<BF\>/\<old-IBF\>/\<current-IBF\>
116 * (BF has 3 components).
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500117 */
118 void
119 onSyncInterest(const ndn::Name& prefix, const ndn::Interest& interest);
120
121PUBLIC_WITH_TESTS_ELSE_PRIVATE:
122 std::map <ndn::Name, PendingEntryInfo> m_pendingEntries;
123
124 const ndn::RegisteredPrefixId* m_registerPrefixId;
125};
126
127} // namespace psync
128
129#endif // PSYNC_PARTIAL_PRODUCER_HPP