blob: 831f588119bb14696e063faa1b1a2230d693750f [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "dispatcher.hpp"
22#include "logging.hpp"
23#include "ccnx-discovery.hpp"
24#include "fetch-task-db.hpp"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080025
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080026#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080027#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080028
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070029using namespace Ndnx;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080030using namespace std;
31using namespace boost;
32
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080035static const string CHRONOSHARE_APP = "chronoshare";
36static const string BROADCAST_DOMAIN = "/ndn/broadcast";
37
Zhenkai Zhu126b21c2013-01-28 17:56:19 -080038static const int CONTENT_FRESHNESS = 1800; // seconds
39const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080040
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080041Dispatcher::Dispatcher(const std::string &localUserName
42 , const std::string &sharedFolder
43 , const filesystem::path &rootDir
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070044 , Ndnx::NdnxWrapperPtr ndnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080045 , bool enablePrefixDiscovery
46 )
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047 : m_ndnx(ndnx)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080048 , m_core(NULL)
49 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080050 , 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 -070051 , m_objectManager(ndnx, rootDir, CHRONOSHARE_APP)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080052 , m_localUserName(localUserName)
53 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080054 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080055 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080056{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080057 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070058 m_actionLog = make_shared<ActionLog>(m_ndnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
60 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
61 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080062 m_fileState = m_actionLog->GetFileState ();
Yingdi Yuadb54eb2013-08-15 10:28:28 -070063 m_fileStateCow = make_shared<FileState> (m_rootDir, true);
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -080064
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080065 Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080066
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070067 // m_server needs a different ndnx face
68 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 -080069 m_server->registerPrefix(Name("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080070 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080071
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070072 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 -080073 // no need to register, right now only listening on localhost prefix
74
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080075 m_core = new SyncCore (m_syncLog, localUserName, Name("/"), syncPrefix,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ndnx, DEFAULT_SYNC_INTEREST_INTERVAL);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080077
Zhenkai Zhuda686882013-01-29 22:32:24 -080078 FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070079 m_actionFetcher = make_shared<FetchManager> (m_ndnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
Alexander Afanasyev473346f2013-02-07 14:14:45 -080080 Name(BROADCAST_DOMAIN), // no appname suffix now
81 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080082 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback(), actionTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080083
Zhenkai Zhuda686882013-01-29 22:32:24 -080084 FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file");
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070085 m_fileFetcher = make_shared<FetchManager> (m_ndnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1),
Alexander Afanasyev473346f2013-02-07 14:14:45 -080086 Name(BROADCAST_DOMAIN), // no appname suffix now
87 3,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080088 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
89 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
90 fileTaskDb);
Zhenkai Zhub8c49af2013-01-29 16:03:56 -080091
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080092
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080093 if (m_enablePrefixDiscovery)
94 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080095 _LOG_DEBUG("registering prefix discovery in Dispatcher");
96 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070097 Ndnx::NdnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080098 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080099
100 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800101}
102
103Dispatcher::~Dispatcher()
104{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800105 _LOG_DEBUG ("Enter destructor of dispatcher");
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800106 m_executor.shutdown ();
107
108 // _LOG_DEBUG (">>");
109
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800110 if (m_enablePrefixDiscovery)
111 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800112 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
113 string tag = "dispatcher" + m_localUserName.toString();
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700114 Ndnx::NdnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800115 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800116
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800117 if (m_core != NULL)
118 {
119 delete m_core;
120 m_core = NULL;
121 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -0800122
123 if (m_server != NULL)
124 {
125 delete m_server;
126 m_server = NULL;
127 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800128
129 if (m_stateServer != NULL)
130 {
131 delete m_stateServer;
132 m_stateServer = NULL;
133 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800134}
135
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800136void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700137Dispatcher::Did_LocalPrefix_Updated (const Ndnx::Name &forwardingHint)
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800138{
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800139 Name effectiveForwardingHint;
140 if (m_localUserName.size () >= forwardingHint.size () &&
141 m_localUserName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
142 {
143 effectiveForwardingHint = Name ("/"); // "directly" accesible
144 }
145 else
146 {
147 effectiveForwardingHint = forwardingHint;
148 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800149
Alexander Afanasyev7aced752013-02-13 09:57:25 -0800150 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
151
152 if (oldLocalPrefix == effectiveForwardingHint)
153 {
154 _LOG_DEBUG ("Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint << ", but effective prefix didn't change");
155 return;
156 }
157
158 if (effectiveForwardingHint == Name ("/") ||
159 effectiveForwardingHint == Name (BROADCAST_DOMAIN))
160 {
161 _LOG_DEBUG ("Basic effective prefix [" << effectiveForwardingHint << "]. Updating local prefix, but don't reregister");
162 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
163 return;
164 }
165
166 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint);
167
168 m_server->registerPrefix(effectiveForwardingHint);
169 m_syncLog->UpdateLocalLocator (effectiveForwardingHint);
170
171 if (oldLocalPrefix == Name ("/") ||
172 oldLocalPrefix == Name (BROADCAST_DOMAIN))
173 {
174 _LOG_DEBUG ("Don't deregister basic prefix: " << oldLocalPrefix);
175 return;
176 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800177 m_server->deregisterPrefix(oldLocalPrefix);
178}
179
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800180// moved to state-server
181// void
182// Dispatcher::Restore_LocalFile (FileItemPtr file)
183// {
184// m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file));
185// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800186
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800187/////////////////////////////////////////////////////////////////////////////////////////////////////
188/////////////////////////////////////////////////////////////////////////////////////////////////////
189/////////////////////////////////////////////////////////////////////////////////////////////////////
190
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800191void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800192Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800193{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800194 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800195}
196
197void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800198Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800199{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800200 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800201 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800202 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800203 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800204 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
205 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
206 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800207 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800208
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700209 FileItemPtr currentFile = m_fileStateCow->LookupFile (relativeFilePath.generic_string ());
210 if(!currentFile)
211 currentFile = m_fileState->LookupFile (relativeFilePath.generic_string ());
212
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800213 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800214 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
215 // The following two are commented out to prevent front end from reporting intermediate files
216 // should enable it if there is other way to prevent this
217 // && last_write_time (absolutePath) == currentFile->mtime ()
218 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
219 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800220 {
221 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
222 return;
223 }
224
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800225
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800226 int seg_num;
227 HashPtr hash;
228 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
229
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800230 try
231 {
232 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
233 *hash,
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700234 last_write_time (absolutePath),
235#if BOOST_VERSION >= 104900
236 status (absolutePath).permissions (),
237#else
238 0,
239#endif
240 seg_num);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800241
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800242 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800243 m_core->localStateChangedDelayed ();
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800244 }
245 catch (filesystem::filesystem_error &error)
246 {
247 _LOG_ERROR ("File operations failed on [" << relativeFilePath << "] (ignoring)");
248 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800249}
250
251void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800252Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
253{
254 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
255}
256
257void
258Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
259{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800260 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevd3310b12013-01-25 17:44:11 -0800261 if (filesystem::exists(absolutePath))
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800262 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800263 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
Alexander Afanasyev38a04732013-01-28 17:40:21 -0800264 _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string());
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800265 return;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800266 }
267
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800268 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
269 // notify SyncCore to propagate the change
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800270 m_core->localStateChangedDelayed();
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800271}
272
273/////////////////////////////////////////////////////////////////////////////////////////////////////
274/////////////////////////////////////////////////////////////////////////////////////////////////////
275/////////////////////////////////////////////////////////////////////////////////////////////////////
276
277/**
278 * Callbacks:
279 *
280 * - from SyncLog: when state changes -> to fetch missing actions
281 *
282 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
283 * -> to add action to the action log
284 *
285 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
286 *
287 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
288 *
289 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
290 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
291 */
292
293void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800294Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
295{
296 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
297}
298
299void
300Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800301{
302 int size = stateMsg->state_size();
303 int index = 0;
304 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800305 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800306 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800307 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800308 if (state.has_old_seq() && state.has_seq())
309 {
310 uint64_t oldSeq = state.old_seq();
311 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800312 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800313
314 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800315 Name actionNameBase = Name ("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800316
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800317 m_actionFetcher->Enqueue (userName, actionNameBase,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800318 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800319 }
320 }
321}
322
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800323
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800324void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700325Dispatcher::Did_FetchManager_ActionFetch (const Ndnx::Name &deviceName, const Ndnx::Name &actionBaseName, uint32_t seqno, Ndnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800326{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800327 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800328 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800329
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800330 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800331 if (!action)
332 {
333 _LOG_ERROR ("AddRemoteAction did not insert action, ignoring");
334 return;
335 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800336 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800337
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800338 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800339 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800340 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
341
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800342 Name fileNameBase = Name ("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800343
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800344 string hashStr = lexical_cast<string> (hash);
345 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, hashStr))
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800346 {
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800347 _LOG_DEBUG ("File already exists in the database. No need to refetch, just directly applying the action");
348 Did_FetchManager_FileFetchComplete (deviceName, fileNameBase);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800349 }
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800350 else
351 {
352 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
353 {
354 _LOG_DEBUG ("create ObjectDb for " << hash);
355 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", hashStr);
356 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800357
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800358 m_fileFetcher->Enqueue (deviceName, fileNameBase,
Alexander Afanasyeve2277212013-01-29 12:46:31 -0800359 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
360 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800361 }
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -0800362 // 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 -0800363}
364
365void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800366Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800367{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800368 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
369}
370
371void
372Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
373{
374 _LOG_DEBUG ("Action to delete " << filename);
375
376 filesystem::path absolutePath = m_rootDir / filename;
Zhenkai Zhudd4359b2013-02-24 11:07:34 -0800377 try
378 {
379 if (filesystem::exists(absolutePath))
380 {
381 // need some protection from local detection of removal
382 remove (absolutePath);
383
384 // hack to remove empty parent dirs
385 filesystem::path parentPath = absolutePath.parent_path();
386 while (parentPath > m_rootDir)
387 {
388 if (filesystem::is_empty(parentPath))
389 {
390 filesystem::remove(parentPath);
391 parentPath = parentPath.parent_path();
392 }
393 else
394 {
395 break;
396 }
397 }
398 }
399 // don't exist
400 }
401 catch (filesystem::filesystem_error &error)
402 {
403 _LOG_ERROR ("File operations failed when removing [" << absolutePath << "] (ignoring)");
404 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800405}
406
407void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700408Dispatcher::Did_FetchManager_FileSegmentFetch (const Ndnx::Name &deviceName, const Ndnx::Name &fileSegmentBaseName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800409{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800410 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800411}
412
413void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700414Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ndnx::Name deviceName, Ndnx::Name fileSegmentBaseName, uint32_t segment, Ndnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800415{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800416 // fileSegmentBaseName: /<device_name>/<appname>/file/<hash>
417
418 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800419 Hash hash (head(hashBytes), hashBytes.size());
420
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800421 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
422
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800423 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800424
425 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
426 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800427 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800428 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800429 }
430 else
431 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800432 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800433 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800434
435 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
436 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800437}
438
439void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700440Dispatcher::Did_FetchManager_FileFetchComplete (const Ndnx::Name &deviceName, const Ndnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800441{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800442 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
443}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800444
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800445void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700446Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ndnx::Name deviceName, Ndnx::Name fileBaseName)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800447{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800448 // fileBaseName: /<device_name>/<appname>/file/<hash>
449
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800450 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800451
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800452 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800453 Hash hash (head (hashBytes), hashBytes.size ());
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800454 _LOG_DEBUG ("Extracted hash: " << hash.shortHash ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800455
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800456 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
457 {
458 // remove the db handle
459 m_objectDbMap.erase (hash); // to commit write
460 }
461 else
462 {
463 _LOG_ERROR ("no db available for this file: " << hash);
464 }
465
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800466 FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800467
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800468 for (FileItems::iterator file = filesToAssemble->begin ();
469 file != filesToAssemble->end ();
470 file++)
471 {
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700472 m_fileStateCow->UpdateFile (file->filename(), file->version(),
473 Hash(file->file_hash ().c_str(), file->file_hash ().size ()),
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700474 NdnxCharbuf (file->device_name().c_str(), file->device_name().size()), file->seq_no(),
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700475 file->mtime(), file->mtime(), file->mtime(),
476 file->mode(), file->seg_num());
477
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800478 boost::filesystem::path filePath = m_rootDir / file->filename ();
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800479
Alexander Afanasyevce925102013-01-31 17:04:05 -0800480 try
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800481 {
Alexander Afanasyevce925102013-01-31 17:04:05 -0800482 if (filesystem::exists (filePath) &&
483 filesystem::last_write_time (filePath) == file->mtime () &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700484#if BOOST_VERSION >= 104900
Alexander Afanasyevce925102013-01-31 17:04:05 -0800485 filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700486#endif
Alexander Afanasyevce925102013-01-31 17:04:05 -0800487 *Hash::FromFileContent (filePath) == hash)
488 {
489 _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
490 continue;
491 }
492 }
493 catch (filesystem::filesystem_error &error)
494 {
495 _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
Alexander Afanasyev9079a2d2013-01-29 15:18:29 -0800496 }
497
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800498 if (ObjectDb::DoesExist (m_rootDir / ".chronoshare", deviceName, boost::lexical_cast<string>(hash)))
499 {
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800500 bool ok = m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
501 if (ok)
502 {
503 last_write_time (filePath, file->mtime ());
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700504#if BOOST_VERSION >= 104900
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800505 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyevf8ff5e12013-07-11 13:57:32 -0700506#endif
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800507
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800508 m_fileState->SetFileComplete (file->filename ());
Yingdi Yuadb54eb2013-08-15 10:28:28 -0700509 m_fileStateCow->DeleteFile(file->filename());
Alexander Afanasyevf3958822013-02-05 11:27:30 -0800510 }
511 else
512 {
513 _LOG_ERROR ("Notified about complete fetch, but file cannot be restored from the database: [" << filePath << "]");
514 }
Zhenkai Zhuf3a9fa62013-01-31 17:23:09 -0800515 }
516 else
517 {
518 _LOG_ERROR (filePath << " supposed to have all segments, but not");
519 // should abort for debugging
520 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800521 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800522}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800523
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800524// moved to state-server
525// void
526// Dispatcher::Restore_LocalFile_Execute (FileItemPtr file)
527// {
528// _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]");
529// // the rest will gracefully fail if object-db is missing or incomplete
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800530
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800531// boost::filesystem::path filePath = m_rootDir / file->filename ();
532// Name deviceName (file->device_name ().c_str (), file->device_name ().size ());
533// Hash hash (file->file_hash ().c_str (), file->file_hash ().size ());
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800534
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800535// try
536// {
537// if (filesystem::exists (filePath) &&
538// filesystem::last_write_time (filePath) == file->mtime () &&
539// filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) &&
540// *Hash::FromFileContent (filePath) == hash)
541// {
542// _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem");
543// return;
544// }
545// }
546// catch (filesystem::filesystem_error &error)
547// {
548// _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)");
549// }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800550
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800551// m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800552
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800553// last_write_time (filePath, file->mtime ());
554// permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800555
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800556// }