blob: ef54a997cf5c9a6cfd3d74574eba94e132d6b24d [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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: Yingdi Yu <yingdi@cs.ucla.edu>
19 */
20
21#ifndef _SYNC_SOCKET_H
22#define _SYNC_SOCKET_H
23
akmhoquec8a10f72014-04-25 18:42:55 -050024#include <ndn-cxx/face.hpp>
25#include <ndn-cxx/security/validator.hpp>
26#include <ndn-cxx/security/key-chain.hpp>
akmhoque66e66182014-02-21 17:56:03 -060027
28#include "sync-logic.h"
29#include "sync-seq-no.h"
30
31#include <utility>
32#include <map>
33#include <vector>
34#include <sstream>
35
36namespace Sync {
37
38/**
39 * \ingroup sync
40 * @brief A simple interface to interact with client code
41 */
42class SyncSocket
43{
44public:
akmhoque157b0a42014-05-13 00:26:37 -050045 typedef ndn::function<void(const std::vector<MissingDataInfo> &, SyncSocket *)> NewDataCallback;
46 typedef ndn::function<void(const std::string &/*prefix*/)> RemoveCallback;
akmhoque66e66182014-02-21 17:56:03 -060047
48 /**
49 * @brief the constructor for SyncAppSocket; the parameter syncPrefix
50 * should be passed to the constructor of m_syncAppWrapper; the other
51 * parameter should be passed to the constructor of m_fetcher; furthermore,
52 * the fetch function of m_fetcher should be a second paramter passed to
53 * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell
54 * m_fetcher to fetch the actual app data after it learns the names
55 *
56 * @param syncPrefix the name prefix for Sync Interest
57 * @param dataCallback the callback to process data
58 */
akmhoque157b0a42014-05-13 00:26:37 -050059 SyncSocket (const ndn::Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060060 ndn::shared_ptr<ndn::Validator> validator,
61 ndn::shared_ptr<ndn::Face> face,
akmhoque157b0a42014-05-13 00:26:37 -050062 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060063 RemoveCallback rmCallback);
64
65 ~SyncSocket ();
66
akmhoque157b0a42014-05-13 00:26:37 -050067 bool
68 publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len,
69 int freshness,uint64_t seq);
akmhoque66e66182014-02-21 17:56:03 -060070
akmhoque157b0a42014-05-13 00:26:37 -050071 void
72 remove (const ndn::Name &prefix)
akmhoque66e66182014-02-21 17:56:03 -060073 { m_syncLogic.remove(prefix); }
74
akmhoque157b0a42014-05-13 00:26:37 -050075 void
76 fetchData(const ndn::Name &prefix, const SeqNo &seq,
77 const ndn::OnDataValidated& onValidated, int retry = 0);
akmhoque66e66182014-02-21 17:56:03 -060078
akmhoque157b0a42014-05-13 00:26:37 -050079 std::string
80 getRootDigest()
akmhoque66e66182014-02-21 17:56:03 -060081 { return m_syncLogic.getRootDigest(); }
82
83 uint64_t
84 getNextSeq (const ndn::Name &prefix, uint64_t session);
85
86 SyncLogic &
akmhoque157b0a42014-05-13 00:26:37 -050087 getLogic ()
akmhoque66e66182014-02-21 17:56:03 -060088 { return m_syncLogic; }
89
90 // make this a static function so we don't have to create socket instance without
91 // knowing the local prefix. it's a wrong place for this function anyway
92 static std::string
akmhoque157b0a42014-05-13 00:26:37 -050093 GetLocalPrefix ();
94
akmhoque66e66182014-02-21 17:56:03 -060095private:
96 void
akmhoque157b0a42014-05-13 00:26:37 -050097 publishDataInternal(ndn::shared_ptr<ndn::Data> data,
98 const ndn::Name &prefix, uint64_t session,uint64_t seq);
akmhoque66e66182014-02-21 17:56:03 -060099
akmhoque157b0a42014-05-13 00:26:37 -0500100 void
101 passCallback(const std::vector<MissingDataInfo> &v)
102 {
103 m_newDataCallback(v, this);
104 }
akmhoque66e66182014-02-21 17:56:03 -0600105
106 void
107 onData(const ndn::Interest& interest, ndn::Data& data,
108 const ndn::OnDataValidated& onValidated,
109 const ndn::OnDataValidationFailed& onValidationFailed);
110
111 void
akmhoque157b0a42014-05-13 00:26:37 -0500112 onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600113 int retry,
114 const ndn::OnDataValidated& onValidated,
115 const ndn::OnDataValidationFailed& onValidationFailed);
116
117 void
118 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
119
120private:
121 typedef std::map<ndn::Name, SeqNo> SequenceLog;
122 NewDataCallback m_newDataCallback;
123 SequenceLog m_sequenceLog;
124 ndn::shared_ptr<ndn::Validator> m_validator;
125 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
126 ndn::shared_ptr<ndn::Face> m_face;
akmhoque66e66182014-02-21 17:56:03 -0600127 SyncLogic m_syncLogic;
128};
129
130} // Sync
131
132#endif // SYNC_SOCKET_H