blob: fd8df7e85642159a19e2391c9039718e0ad804cf [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.
Alexander Afanasyev49a18522013-01-18 17:49:04 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev49a18522013-01-18 17:49:04 -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.
Alexander Afanasyev49a18522013-01-18 17:49:04 -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.
Alexander Afanasyev49a18522013-01-18 17:49:04 -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.
Alexander Afanasyev49a18522013-01-18 17:49:04 -080019 */
20
21#ifndef FETCH_MANAGER_H
22#define FETCH_MANAGER_H
23
24#include <boost/exception/all.hpp>
25#include <boost/shared_ptr.hpp>
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080026#include <boost/function.hpp>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080027#include <string>
Alexander Afanasyev83531a42013-01-19 16:21:54 -080028#include <list>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080029#include <stdint.h>
30#include "scheduler.h"
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080031#include "executor.h"
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070032#include "ndnx-wrapper.h"
Zhenkai Zhuda686882013-01-29 22:32:24 -080033#include "fetch-task-db.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080034
Alexander Afanasyev83531a42013-01-19 16:21:54 -080035#include "fetcher.h"
36
Alexander Afanasyev49a18522013-01-18 17:49:04 -080037class FetchManager
38{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080039public:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080040 enum
41 {
42 PRIORITY_NORMAL,
43 PRIORITY_HIGH
44 };
45
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070046 typedef boost::function<Ndnx::Name(const Ndnx::Name &)> Mapping;
47 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName, uint64_t seq, Ndnx::PcoPtr pco)> SegmentCallback;
48 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName)> FinishCallback;
49 FetchManager (Ndnx::NdnxWrapperPtr ndnx,
Alexander Afanasyev473346f2013-02-07 14:14:45 -080050 const Mapping &mapping,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070051 const Ndnx::Name &broadcastForwardingHint,
Alexander Afanasyev473346f2013-02-07 14:14:45 -080052 uint32_t parallelFetches = 3,
53 const SegmentCallback &defaultSegmentCallback = SegmentCallback(),
54 const FinishCallback &defaultFinishCallback = FinishCallback(),
55 const FetchTaskDbPtr &taskDb = FetchTaskDbPtr()
Zhenkai Zhua0147382013-01-29 15:57:27 -080056 );
Alexander Afanasyev49a18522013-01-18 17:49:04 -080057 virtual ~FetchManager ();
58
59 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070060 Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080061 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
62 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080063
Zhenkai Zhua0147382013-01-29 15:57:27 -080064 // Enqueue using default callbacks
65 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070066 Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName,
Zhenkai Zhua0147382013-01-29 15:57:27 -080067 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
68
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080069 // only for Fetcher
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070070 inline Ndnx::NdnxWrapperPtr
71 GetNdnx ();
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080072
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080073private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080074 // Fetch Events
75 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 DidDataSegmentFetched (Fetcher &fetcher, uint64_t seqno, const Ndnx::Name &basename,
77 const Ndnx::Name &name, Ndnx::PcoPtr data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080078
Alexander Afanasyev83531a42013-01-19 16:21:54 -080079 void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080080 DidNoDataTimeout (Fetcher &fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080081
82 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070083 DidFetchComplete (Fetcher &fetcher, const Ndnx::Name &deviceName, const Ndnx::Name &baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080084
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080085 void
86 ScheduleFetches ();
87
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080088 void
89 TimedWait (Fetcher &fetcher);
90
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080091private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070092 Ndnx::NdnxWrapperPtr m_ndnx;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080093 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080094
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080095 uint32_t m_maxParallelFetches;
96 uint32_t m_currentParallelFetches;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080097 boost::mutex m_parellelFetchMutex;
98
99 // optimized list structure for fetch queue
100 typedef boost::intrusive::member_hook< Fetcher,
101 boost::intrusive::list_member_hook<>, &Fetcher::m_managerListHook> MemberOption;
102 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800103
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800104 FetchList m_fetchList;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800105 SchedulerPtr m_scheduler;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800106 ExecutorPtr m_executor;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800107 TaskPtr m_scheduleFetchesTask;
Zhenkai Zhua0147382013-01-29 15:57:27 -0800108 SegmentCallback m_defaultSegmentCallback;
109 FinishCallback m_defaultFinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800110 FetchTaskDbPtr m_taskDb;
Alexander Afanasyev473346f2013-02-07 14:14:45 -0800111
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700112 const Ndnx::Name m_broadcastHint;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800113};
114
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700115Ndnx::NdnxWrapperPtr
116FetchManager::GetNdnx ()
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800117{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700118 return m_ndnx;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800119}
120
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800121typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800122namespace Error {
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800123struct FetchManager : virtual boost::exception, virtual std::exception { };
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800124}
125
126typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
127
128
129#endif // FETCHER_H