blob: 6fa791348ca25297652883f023e0983a369ce5e2 [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>
29#include <ndn-cpp/security/identity/identity-manager.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 Yu46c9f1a2013-12-18 15:15:46 +080058 ndn::ptr_lib::shared_ptr<SyncPolicyManager> syncPolicyManager,
Yingdi Yu6e235db2013-12-27 08:40:53 +080059 ndn::ptr_lib::shared_ptr<ndn::Face> face,
60 ndn::ptr_lib::shared_ptr<ndn::Transport> transport,
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:
Yingdi Yu6e235db2013-12-27 08:40:53 +080093 // void
94 // connectToDaemon();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080095
Yingdi Yu6e235db2013-12-27 08:40:53 +080096 // void
97 // onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
98 // const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080099
Yingdi Yu6e235db2013-12-27 08:40:53 +0800100 // void
101 // onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800102
Yingdi Yu43e71612013-10-30 22:19:31 -0700103 void
104 passCallback(const std::vector<MissingDataInfo> &v)
105 { m_newDataCallback(v, this); }
106
107 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108 onChatCert(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
109 const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
110 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep);
Yingdi Yu43e71612013-10-30 22:19:31 -0700111
112 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800113 onChatCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
114 const ndn::OnVerifyFailed& onVerifyFailed,
115 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
116 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep);
117
118 void
119 onChatData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
120 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
121 const ndn::OnVerified& onVerified,
122 const ndn::OnVerifyFailed& onVerifyFailed);
123
124 void
125 onChatDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
126 int retry,
127 const ndn::OnVerified& onVerified,
128 const ndn::OnVerifyFailed& onVerifyFailed);
129
130 void
131 onChatDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu43e71612013-10-30 22:19:31 -0700132
133private:
134 typedef boost::unordered_map<std::string, SeqNo> SequenceLog;
135 NewDataCallback m_newDataCallback;
136 SequenceLog m_sequenceLog;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800137 ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
138 ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800139 ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;
Yingdi Yu6e235db2013-12-27 08:40:53 +0800140 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu43e71612013-10-30 22:19:31 -0700141 SyncLogic m_syncLogic;
142};
143
144} // Sync
145
146#endif // SYNC_SOCKET_H