blob: 06f6687a6459dc5a55b47c0625c53d9bfffa0804 [file] [log] [blame]
Alexander Afanasyev49a18522013-01-18 17:49:04 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-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 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#ifndef FETCH_MANAGER_H
23#define FETCH_MANAGER_H
24
25#include <boost/exception/all.hpp>
26#include <boost/shared_ptr.hpp>
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080027#include <boost/function.hpp>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080028#include <string>
Alexander Afanasyev83531a42013-01-19 16:21:54 -080029#include <list>
Alexander Afanasyev49a18522013-01-18 17:49:04 -080030#include <stdint.h>
31#include "scheduler.h"
Zhenkai Zhuab9215c2013-01-28 23:42:28 -080032#include "executor.h"
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070033#include "ndnx-wrapper.h"
Zhenkai Zhuda686882013-01-29 22:32:24 -080034#include "fetch-task-db.h"
Alexander Afanasyev49a18522013-01-18 17:49:04 -080035
Alexander Afanasyev83531a42013-01-19 16:21:54 -080036#include "fetcher.h"
37
Alexander Afanasyev49a18522013-01-18 17:49:04 -080038class FetchManager
39{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080040public:
Alexander Afanasyev49a18522013-01-18 17:49:04 -080041 enum
42 {
43 PRIORITY_NORMAL,
44 PRIORITY_HIGH
45 };
46
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047 typedef boost::function<Ndnx::Name(const Ndnx::Name &)> Mapping;
48 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName, uint64_t seq, Ndnx::PcoPtr pco)> SegmentCallback;
49 typedef boost::function<void(Ndnx::Name &deviceName, Ndnx::Name &baseName)> FinishCallback;
50 FetchManager (Ndnx::NdnxWrapperPtr ndnx,
Alexander Afanasyev473346f2013-02-07 14:14:45 -080051 const Mapping &mapping,
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070052 const Ndnx::Name &broadcastForwardingHint,
Alexander Afanasyev473346f2013-02-07 14:14:45 -080053 uint32_t parallelFetches = 3,
54 const SegmentCallback &defaultSegmentCallback = SegmentCallback(),
55 const FinishCallback &defaultFinishCallback = FinishCallback(),
56 const FetchTaskDbPtr &taskDb = FetchTaskDbPtr()
Zhenkai Zhua0147382013-01-29 15:57:27 -080057 );
Alexander Afanasyev49a18522013-01-18 17:49:04 -080058 virtual ~FetchManager ();
59
60 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070061 Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName,
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080062 const SegmentCallback &segmentCallback, const FinishCallback &finishCallback,
63 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080064
Zhenkai Zhua0147382013-01-29 15:57:27 -080065 // Enqueue using default callbacks
66 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070067 Enqueue (const Ndnx::Name &deviceName, const Ndnx::Name &baseName,
Zhenkai Zhua0147382013-01-29 15:57:27 -080068 uint64_t minSeqNo, uint64_t maxSeqNo, int priority=PRIORITY_NORMAL);
69
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080070 // only for Fetcher
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070071 inline Ndnx::NdnxWrapperPtr
72 GetNdnx ();
Alexander Afanasyeve41e7d22013-01-19 15:13:47 -080073
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080074private:
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080075 // Fetch Events
76 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077 DidDataSegmentFetched (Fetcher &fetcher, uint64_t seqno, const Ndnx::Name &basename,
78 const Ndnx::Name &name, Ndnx::PcoPtr data);
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080079
Alexander Afanasyev83531a42013-01-19 16:21:54 -080080 void
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080081 DidNoDataTimeout (Fetcher &fetcher);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080082
83 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070084 DidFetchComplete (Fetcher &fetcher, const Ndnx::Name &deviceName, const Ndnx::Name &baseName);
Alexander Afanasyev83531a42013-01-19 16:21:54 -080085
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080086 void
87 ScheduleFetches ();
88
Zhenkai Zhu354d46d2013-02-06 13:49:48 -080089 void
90 TimedWait (Fetcher &fetcher);
91
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080092private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070093 Ndnx::NdnxWrapperPtr m_ndnx;
Zhenkai Zhu3d1beca2013-01-23 14:55:32 -080094 Mapping m_mapping;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080095
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080096 uint32_t m_maxParallelFetches;
97 uint32_t m_currentParallelFetches;
Alexander Afanasyev83531a42013-01-19 16:21:54 -080098 boost::mutex m_parellelFetchMutex;
99
100 // optimized list structure for fetch queue
101 typedef boost::intrusive::member_hook< Fetcher,
102 boost::intrusive::list_member_hook<>, &Fetcher::m_managerListHook> MemberOption;
103 typedef boost::intrusive::list<Fetcher, MemberOption> FetchList;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800104
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800105 FetchList m_fetchList;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800106 SchedulerPtr m_scheduler;
Zhenkai Zhuab9215c2013-01-28 23:42:28 -0800107 ExecutorPtr m_executor;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800108 TaskPtr m_scheduleFetchesTask;
Zhenkai Zhua0147382013-01-29 15:57:27 -0800109 SegmentCallback m_defaultSegmentCallback;
110 FinishCallback m_defaultFinishCallback;
Zhenkai Zhuda686882013-01-29 22:32:24 -0800111 FetchTaskDbPtr m_taskDb;
Alexander Afanasyev473346f2013-02-07 14:14:45 -0800112
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700113 const Ndnx::Name m_broadcastHint;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800114};
115
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700116Ndnx::NdnxWrapperPtr
117FetchManager::GetNdnx ()
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800118{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700119 return m_ndnx;
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800120}
121
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800122typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800123namespace Error {
Alexander Afanasyev83531a42013-01-19 16:21:54 -0800124struct FetchManager : virtual boost::exception, virtual std::exception { };
Alexander Afanasyev49a18522013-01-18 17:49:04 -0800125}
126
127typedef boost::shared_ptr<FetchManager> FetchManagerPtr;
128
129
130#endif // FETCHER_H