blob: b558ff96a829d4681447f8c9823135d4d595620f [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- 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
24#include "sync-logic.h"
25#include <boost/function.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070026#include "sync-seq-no.h"
Yingdi Yue8154712014-01-21 10:20:14 -080027#include <ndn-cpp-dev/face.hpp>
28#include <ndn-cpp-dev/security/verifier.hpp>
29#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070030#include <utility>
31#include <map>
32#include <vector>
33#include <sstream>
34
35namespace Sync {
36
37/**
38 * \ingroup sync
39 * @brief A simple interface to interact with client code
40 */
41class SyncSocket
42{
43public:
44 typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
45 typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback;
46 /**
47 * @brief the constructor for SyncAppSocket; the parameter syncPrefix
48 * should be passed to the constructor of m_syncAppWrapper; the other
49 * parameter should be passed to the constructor of m_fetcher; furthermore,
50 * the fetch function of m_fetcher should be a second paramter passed to
51 * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell
52 * m_fetcher to fetch the actual app data after it learns the names
53 *
54 * @param syncPrefix the name prefix for Sync Interest
55 * @param dataCallback the callback to process data
56 */
57 SyncSocket (const std::string &syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080058 ndn::ptr_lib::shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080059 ndn::ptr_lib::shared_ptr<ndn::Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070060 NewDataCallback dataCallback,
61 RemoveCallback rmCallback);
62
63 ~SyncSocket ();
64
65 bool
Yingdi Yu6d638f02014-01-24 11:01:21 -080066 publishData(const ndn::Name &prefix, uint32_t session, const char *buf, size_t len, int freshness);
Yingdi Yu43e71612013-10-30 22:19:31 -070067
68 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080069 remove (const ndn::Name &prefix)
Yingdi Yu43e71612013-10-30 22:19:31 -070070 { m_syncLogic.remove(prefix); }
71
72 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080073 fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070074
75 std::string
76 getRootDigest()
77 { return m_syncLogic.getRootDigest(); }
78
79 uint32_t
Yingdi Yu6d638f02014-01-24 11:01:21 -080080 getNextSeq (const ndn::Name &prefix, uint32_t session);
Yingdi Yu43e71612013-10-30 22:19:31 -070081
82 SyncLogic &
83 getLogic ()
84 { return m_syncLogic; }
85
86 // make this a static function so we don't have to create socket instance without
87 // knowing the local prefix. it's a wrong place for this function anyway
88 static std::string
89 GetLocalPrefix ();
90
91private:
92 void
93 passCallback(const std::vector<MissingDataInfo> &v)
94 { m_newDataCallback(v, this); }
95
96 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080097 onData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080098 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
99 const ndn::OnVerified& onVerified,
100 const ndn::OnVerifyFailed& onVerifyFailed);
101
102 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800103 onDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800104 int retry,
105 const ndn::OnVerified& onVerified,
106 const ndn::OnVerifyFailed& onVerifyFailed);
107
108 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800109 onDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu43e71612013-10-30 22:19:31 -0700110
111private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800112 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu43e71612013-10-30 22:19:31 -0700113 NewDataCallback m_newDataCallback;
114 SequenceLog m_sequenceLog;
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800115 ndn::ptr_lib::shared_ptr<SecPolicySync> m_policy;
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800116 ndn::ptr_lib::shared_ptr<ndn::Verifier> m_verifier;
117 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu6e235db2013-12-27 08:40:53 +0800118 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu43e71612013-10-30 22:19:31 -0700119 SyncLogic m_syncLogic;
120};
121
122} // Sync
123
124#endif // SYNC_SOCKET_H