blob: c3232bb42e03996090bdc3805661cfdb606f3db9 [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>
26#include <boost/unordered_map.hpp>
27#include "sync-seq-no.h"
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080028#include <ndn-cpp/face.hpp>
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080029#include <ndn-cpp/security/verifier.hpp>
30#include <ndn-cpp/security/key-chain.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070031#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:
45 typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
46 typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback;
47 /**
48 * @brief the constructor for SyncAppSocket; the parameter syncPrefix
49 * should be passed to the constructor of m_syncAppWrapper; the other
50 * parameter should be passed to the constructor of m_fetcher; furthermore,
51 * the fetch function of m_fetcher should be a second paramter passed to
52 * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell
53 * m_fetcher to fetch the actual app data after it learns the names
54 *
55 * @param syncPrefix the name prefix for Sync Interest
56 * @param dataCallback the callback to process data
57 */
58 SyncSocket (const std::string &syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080059 ndn::ptr_lib::shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080060 ndn::ptr_lib::shared_ptr<ndn::Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070061 NewDataCallback dataCallback,
62 RemoveCallback rmCallback);
63
64 ~SyncSocket ();
65
66 bool
67 publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness);
68
69 void
70 remove (const std::string &prefix)
71 { m_syncLogic.remove(prefix); }
72
73 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080074 fetchData(const std::string &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070075
76 std::string
77 getRootDigest()
78 { return m_syncLogic.getRootDigest(); }
79
80 uint32_t
81 getNextSeq (const std::string &prefix, uint32_t session);
82
83 SyncLogic &
84 getLogic ()
85 { return m_syncLogic; }
86
87 // make this a static function so we don't have to create socket instance without
88 // knowing the local prefix. it's a wrong place for this function anyway
89 static std::string
90 GetLocalPrefix ();
91
92private:
93 void
94 passCallback(const std::vector<MissingDataInfo> &v)
95 { m_newDataCallback(v, this); }
96
97 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080098 onChatData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
99 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
100 const ndn::OnVerified& onVerified,
101 const ndn::OnVerifyFailed& onVerifyFailed);
102
103 void
104 onChatDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
105 int retry,
106 const ndn::OnVerified& onVerified,
107 const ndn::OnVerifyFailed& onVerifyFailed);
108
109 void
110 onChatDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu43e71612013-10-30 22:19:31 -0700111
112private:
113 typedef boost::unordered_map<std::string, SeqNo> SequenceLog;
114 NewDataCallback m_newDataCallback;
115 SequenceLog m_sequenceLog;
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800116 ndn::ptr_lib::shared_ptr<SecPolicySync> m_policy;
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800117 ndn::ptr_lib::shared_ptr<ndn::Verifier> m_verifier;
118 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu6e235db2013-12-27 08:40:53 +0800119 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu43e71612013-10-30 22:19:31 -0700120 SyncLogic m_syncLogic;
121};
122
123} // Sync
124
125#endif // SYNC_SOCKET_H