blob: 9d91b0f2eecd33fee64626f74a098313b50bf0d3 [file] [log] [blame]
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 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 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "dispatcher.hpp"
23#include "logging.hpp"
24#include "ccnx-discovery.hpp"
25#include "fetch-task-db.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080026
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080027#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080028#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080029
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070030using namespace Ndnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080031using namespace std;
32using namespace boost;
33
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080034INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080035
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080036static const string CHRONOSHARE_APP = "chronoshare";
37static const string BROADCAST_DOMAIN = "/ndn/broadcast";
38
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080039static const int CONTENT_FRESHNESS = 1800; // seconds
40const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080041
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080042Dispatcher::Dispatcher(const std::string &localUserName
43 , const std::string &sharedFolder
44 , const filesystem::path &rootDir
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070045 , Ndnx::NdnxWrapperPtr ndnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080046 , bool enablePrefixDiscovery
47 )
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070048 : m_ndnx(ndnx)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080049 , m_core(NULL)
50 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080051 , m_executor(1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070052 , m_objectManager(ndnx, rootDir, CHRONOSHARE_APP)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080053 , m_localUserName(localUserName)
54 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080055 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080056 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080057{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080058 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070059 m_actionLog = make_shared<ActionLog>(m_ndnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080060 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
61 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
62 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080063 m_fileState = m_actionLog->GetFileState ();
Yingdi Yuadb54eb2013-08-15 10:28:28 -070064 m_fileStateCow = make_shared<FileState> (m_rootDir, true);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080065
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080066 Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080067
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070068 // m_server needs a different ndnx face
69 m_server = new ContentServer(make_shared<NdnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CHRONOSHARE_APP, CONTENT_FRESHNESS);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080070 m_server->registerPrefix(Name("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080071 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080072
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 m_stateServer = new StateServer (make_shared<NdnxWrapper>(), m_actionLog, rootDir, m_localUserName, m_sharedFolder, CHRONOSHARE_APP, m_objectManager, CONTENT_FRESHNESS);
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080074 // no need to register, right now only listening on localhost prefix
75
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080076 m_core = new SyncCore (m_syncLog, localUserName, Name("/"), syncPrefix,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ndnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080078
Zhenkai Zhuda686882013-01-29 22:32:24 -080079 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070080 m_actionFetcher = make_shared<FetchManager> (m_ndnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
Alexander Afanasyev473346f2013-02-07 14:14:45 -080081 Name(BROADCAST_DOMAIN), // no appname suffix now
82 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080083 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080084
Zhenkai Zhuda686882013-01-29 22:32:24 -080085 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070086 m_fileFetcher = make_shared<FetchManager> (m_ndnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
Alexander Afanasyev473346f2013-02-07 14:14:45 -080087 Name(BROADCAST_DOMAIN), // no appname suffix now
88 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080089 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
90 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
91 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080092
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080093
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080094 if (m_enablePrefixDiscovery)
95 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080096 _LOG_DEBUG("registering prefix discovery in Dispatcher");
97 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070098 Ndnx::NdnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080099 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800100
101 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800102}
103
104Dispatcher::~Dispatcher()
105{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800106 _LOG_DEBUG ("Enter destructor of dispatcher");
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800107 m_executor.shutdown ();
108
109 // _LOG_DEBUG (">>");
110
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800111 if (m_enablePrefixDiscovery)
112 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800113 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
114 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700115 Ndnx::NdnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800116 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800117
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800118 if (m_core != NULL)
119 {
120 delete m_core;
121 m_core = NULL;
122 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800123
124 if (m_server != NULL)
125 {
126 delete m_server;
127 m_server = NULL;
128 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800129
130 if (m_stateServer != NULL)
131 {
132 delete m_stateServer;
133 m_stateServer = NULL;
134 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800135}
136
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800137void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700138Dispatcher::Did_LocalPrefix_Updated (const Ndnx::Name &forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800139{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800140 Name effectiveForwardingHint;
141 if (m_localUserName.size () >= forwardingHint.size () &&
142 m_localUserName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
143 {
144 effectiveForwardingHint = Name ("/"); // "directly" accesible
145 }
146 else
147 {
148 effectiveForwardingHint = forwardingHint;
149 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800150
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800151 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
152
153 if (oldLocalPrefix == effectiveForwardingHint)
154 {
155 _LOG_DEBUG ("Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change");
156 return;
157 }
158
159 if (effectiveForwardingHint == Name ("/") ||
160 effectiveForwardingHint == Name (BROADCAST_DOMAIN))
161 {
162 _LOG_DEBUG ("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister");
163 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
164 return;
165 }
166
167 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
168
169 m_server->registerPrefix(effectiveForwardingHint);
170 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
171
172 if (oldLocalPrefix == Name ("/") ||
173 oldLocalPrefix == Name (BROADCAST_DOMAIN))
174 {
175 _LOG_DEBUG ("Don't deregister basic prefix: " << oldLocalPrefix);
176 return;
177 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800178 m_server->deregisterPrefix(oldLocalPrefix);
179}
180
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800181// moved to state-server
182// void
183// Dispatcher::Restore_LocalFile (FileItemPtr file)
184// {
185// m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
186// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800187
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800188/////////////////////////////////////////////////////////////////////////////////////////////////////
189/////////////////////////////////////////////////////////////////////////////////////////////////////
190/////////////////////////////////////////////////////////////////////////////////////////////////////
191
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800192void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800193Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800194{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800195 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800196}
197
198void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800199Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800200{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800201 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800202 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800203 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800204 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800205 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
206 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
207 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800208 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800209
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700210 FileItemPtr currentFile = m_fileStateCow->LookupFile (relativeFilePath.generic_string ());
211 if(!currentFile)
212 currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
213
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800214 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800215 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
216 // The following two are commented out to prevent front end from reporting intermediate files
217 // should enable it if there is other way to prevent this
218 // && last_write_time (absolutePath) == currentFile->mtime ()
219 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
220 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800221 {
222 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
223 return;
224 }
225
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800226
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800227 int seg_num;
228 HashPtr hash;
229 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
230
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800231 try
232 {
233 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
234 *hash,
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700235 last_write_time (absolutePath),
236#if BOOST_VERSION >= 104900
237 status (absolutePath).permissions (),
238#else
239 0,
240#endif
241 seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800242
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800243 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800244 m_core->localStateChangedDelayed ();
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800245 }
246 catch (filesystem::filesystem_error &error)
247 {
248 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
249 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800250}
251
252void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800253Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
254{
255 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
256}
257
258void
259Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
260{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800261 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800262 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800263 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800264 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800265 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800266 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800267 }
268
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800269 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
270 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800271 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800272}
273
274/////////////////////////////////////////////////////////////////////////////////////////////////////
275/////////////////////////////////////////////////////////////////////////////////////////////////////
276/////////////////////////////////////////////////////////////////////////////////////////////////////
277
278/**
279 * Callbacks:
280 *
281 * - from SyncLog: when state changes -> to fetch missing actions
282 *
283 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
284 * -> to add action to the action log
285 *
286 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
287 *
288 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
289 *
290 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
291 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
292 */
293
294void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800295Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
296{
297 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
298}
299
300void
301Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800302{
303 int size = stateMsg->state_size();
304 int index = 0;
305 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800306 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800307 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800308 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800309 if (state.has_old_seq() && state.has_seq())
310 {
311 uint64_t oldSeq = state.old_seq();
312 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800313 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800314
315 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800316 Name actionNameBase = Name ("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800317
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800318 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800319 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800320 }
321 }
322}
323
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800324
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800325void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700326Dispatcher::Did_FetchManager_ActionFetch (const Ndnx::Name &deviceName, const Ndnx::Name &actionBaseName, uint32_t seqno, Ndnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800327{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800328 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800329 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800330
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800331 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800332 if (!action)
333 {
334 _LOG_ERROR ("AddRemoteAction did not insert action, ignoring");
335 return;
336 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800337 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800338
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800339 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800340 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800341 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
342
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800343 Name fileNameBase = Name ("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800344
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800345 string hashStr = lexical_cast<string> (hash);
346 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800347 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800348 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
349 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800350 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800351 else
352 {
353 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
354 {
355 _LOG_DEBUG ("create ObjectDb for " << hash);
356 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
357 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800358
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800359 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800360 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
361 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800362 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800363 // if necessary (when version number is the highest) delete will be applied through the trigger in m_actionLog->AddRemoteAction call
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800364}
365
366void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800367Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800368{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800369 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
370}
371
372void
373Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
374{
375 _LOG_DEBUG ("Action to delete " << filename);
376
377 filesystem::path absolutePath = m_rootDir / filename;
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800378 try
379 {
380 if (filesystem::exists(absolutePath))
381 {
382 // need some protection from local detection of removal
383 remove (absolutePath);
384
385 // hack to remove empty parent dirs
386 filesystem::path parentPath = absolutePath.parent_path();
387 while (parentPath > m_rootDir)
388 {
389 if (filesystem::is_empty(parentPath))
390 {
391 filesystem::remove(parentPath);
392 parentPath = parentPath.parent_path();
393 }
394 else
395 {
396 break;
397 }
398 }
399 }
400 // don't exist
401 }
402 catch (filesystem::filesystem_error &error)
403 {
404 _LOG_ERROR ("File operations failed when removing [" << absolutePath << "] (ignoring)");
405 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800406}
407
408void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700409Dispatcher::Did_FetchManager_FileSegmentFetch (const Ndnx::Name &deviceName, const Ndnx::Name &fileSegmentBaseName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800410{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800411 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800412}
413
414void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700415Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ndnx::Name deviceName, Ndnx::Name fileSegmentBaseName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800416{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800417 // fileSegmentBaseName: /<device_name>/<appname>/file/<hash>
418
419 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800420 Hash hash (head(hashBytes), hashBytes.size());
421
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800422 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
423
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800424 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800425
426 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
427 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800428 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800429 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800430 }
431 else
432 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800433 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800434 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800435
436 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
437 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800438}
439
440void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700441Dispatcher::Did_FetchManager_FileFetchComplete (const Ndnx::Name &deviceName, const Ndnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800442{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800443 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
444}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800445
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800446void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700447Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ndnx::Name deviceName, Ndnx::Name fileBaseName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800448{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800449 // fileBaseName: /<device_name>/<appname>/file/<hash>
450
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800451 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800452
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800453 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800454 Hash hash (head (hashBytes), hashBytes.size ());
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800455 _LOG_DEBUG ("Extracted hash: " << hash.shortHash ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800456
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800457 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
458 {
459 // remove the db handle
460 m_objectDbMap.erase (hash); // to commit write
461 }
462 else
463 {
464 _LOG_ERROR ("no db available for this file: " << hash);
465 }
466
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800467 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800468
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800469 for (FileItems::iterator file = filesToAssemble->begin ();
470 file != filesToAssemble->end ();
471 file++)
472 {
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700473 m_fileStateCow->UpdateFile (file->filename(), file->version(),
474 Hash(file->file_hash ().c_str(), file->file_hash ().size ()),
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700475 NdnxCharbuf (file->device_name().c_str(), file->device_name().size()), file->seq_no(),
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700476 file->mtime(), file->mtime(), file->mtime(),
477 file->mode(), file->seg_num());
478
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800479 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800480
Alexander Afanasyevce925102013-01-31 17:04:05 -0800481 try
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800482 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800483 if (filesystem::exists (filePath) &&
484 filesystem::last_write_time (filePath) == file->mtime () &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700485#if BOOST_VERSION >= 104900
Alexander Afanasyevce925102013-01-31 17:04:05 -0800486 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700487#endif
Alexander Afanasyevce925102013-01-31 17:04:05 -0800488 *Hash::FromFileContent (filePath) == hash)
489 {
490 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
491 continue;
492 }
493 }
494 catch (filesystem::filesystem_error &error)
495 {
496 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800497 }
498
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800499 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash)))
500 {
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800501 bool ok = m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
502 if (ok)
503 {
504 last_write_time (filePath, file->mtime ());
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700505#if BOOST_VERSION >= 104900
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800506 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700507#endif
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800508
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800509 m_fileState->SetFileComplete (file->filename ());
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700510 m_fileStateCow->DeleteFile(file->filename());
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800511 }
512 else
513 {
514 _LOG_ERROR ("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]");
515 }
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800516 }
517 else
518 {
519 _LOG_ERROR (filePath << " supposed to have all segments, but not");
520 // should abort for debugging
521 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800522 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800523}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800524
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800525// moved to state-server
526// void
527// Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
528// {
529// _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
530// // the rest will gracefully fail if object-db is missing or incomplete
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800531
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800532// boost::filesystem::path filePath = m_rootDir / file->filename ();
533// Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
534// Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800535
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800536// try
537// {
538// if (filesystem::exists (filePath) &&
539// filesystem::last_write_time (filePath) == file->mtime () &&
540// filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
541// *Hash::FromFileContent (filePath) == hash)
542// {
543// _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
544// return;
545// }
546// }
547// catch (filesystem::filesystem_error &error)
548// {
549// _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
550// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800551
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800552// m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800553
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800554// last_write_time (filePath, file->mtime ());
555// permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800556
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800557// }