blob: 64d551b2720ae85158052204f8f3925a6eaac267 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- 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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevc1030192012-03-08 22:21:28 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080021 */
22
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#ifndef SYNC_LOGIC_H
24#define SYNC_LOGIC_H
Alexander Afanasyev45fba082012-03-12 18:05:24 -070025
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080026#include <boost/shared_ptr.hpp>
Alexander Afanasyev750d1872012-03-12 15:33:56 -070027#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070028#include <boost/random.hpp>
Zhenkai Zhuce66e212012-03-12 22:27:19 -070029#include <memory>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080031#include "sync-ccnx-wrapper.h"
32#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080033#include "sync-diff-state.h"
34#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080035#include "sync-std-name-info.h"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070036#include "sync-scheduler.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080037
Alexander Afanasyevc1030192012-03-08 22:21:28 -080038#include "sync-diff-state-container.h"
39
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070040#ifdef _DEBUG
41#ifdef HAVE_LOG4CXX
42#include <log4cxx/logger.h>
43#endif
44#endif
45
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070046#ifdef NS3_MODULE
47#include <ns3/application.h>
48#endif
49
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080050namespace Sync {
51
52/**
53 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080054 * @brief A wrapper for SyncApp, which handles ccnx related things (process
55 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080056 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080057class SyncLogic
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070058#ifdef NS3_MODULE
59 : public ns3::Application
60#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080061{
62public:
Alexander Afanasyev750d1872012-03-12 15:33:56 -070063 typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
64 typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080065
Alexander Afanasyevc1030192012-03-08 22:21:28 -080066 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080067 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080068 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070069 * @param onUpdate function that will be called when new state is detected
70 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080071 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080072 * the app data when new remote names are learned
73 */
Alexander Afanasyev750d1872012-03-12 15:33:56 -070074 SyncLogic (const std::string &syncPrefix,
75 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070076 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080077
Alexander Afanasyevc1030192012-03-08 22:21:28 -080078 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080079
Alexander Afanasyevc1030192012-03-08 22:21:28 -080080 /**
81 * a wrapper for the same func in SyncApp
82 */
83 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084
Alexander Afanasyevc1030192012-03-08 22:21:28 -080085 /**
86 * @brief respond to the Sync Interest; a lot of logic needs to go in here
87 * @param interest the Sync Interest in string format
88 */
89 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080090
Alexander Afanasyevc1030192012-03-08 22:21:28 -080091 /**
92 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -080093 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -080094 * @param dataBuffer the sync data
95 */
96 void processSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080097
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070098 /**
99 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700100 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700101 */
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700102 void remove (const std::string &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700103
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700104#ifdef _DEBUG
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700105 Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700106 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700107#endif
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700108
109protected:
110#ifdef NS3_MODULE
111 virtual void StartApplication ();
112 virtual void StopApplication ();
113#endif
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700114
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800115private:
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700116 void
117 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700118
119 void
120 processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
121
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700122 void
123 sendSyncInterest ();
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700124
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700125 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700126 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700127
128 void
129 satisfyPendingSyncInterests (DiffStatePtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800130
131private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800132 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800133 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700134 boost::recursive_mutex m_stateMutex;
135
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700136 std::string m_outstandingInterest;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800137 SyncInterestTable m_syncInterestTable;
138
139 std::string m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700140 LogicUpdateCallback m_onUpdate;
141 LogicRemoveCallback m_onRemove;
Zhenkai Zhuce66e212012-03-12 22:27:19 -0700142 std::auto_ptr<CcnxWrapper> m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800143
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700144 Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800145
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700146 boost::mt19937 m_randomGenerator;
147 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
148
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800149 static const int m_syncResponseFreshness = 2;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700150
151 enum EventLabels
152 {
153 DELAYED_INTEREST_PROCESSING = 1,
154 REEXPRESSING_INTEREST = 2
155 };
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700156
157#ifdef _DEBUG
158#ifdef HAVE_LOG4CXX
159 log4cxx::LoggerPtr staticModuleLogger;
160#endif
161#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800162};
163
164
165} // Sync
166
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800167#endif // SYNC_APP_WRAPPER_H