blob: daa6e2fa4ffa1fcfa71663b022565683a2bd3639 [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>
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070048#include <ns3/random-variable.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070049#endif
50
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080051namespace Sync {
52
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070053struct MissingDataInfo {
54 std::string prefix;
55 SeqNo low;
56 SeqNo high;
57};
58
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080059/**
60 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080061 * @brief A wrapper for SyncApp, which handles ccnx related things (process
62 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080063 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080064class SyncLogic
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070065#ifdef NS3_MODULE
66 : public ns3::Application
67#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080068{
69public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070070 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
71 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyev750d1872012-03-12 15:33:56 -070072 typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080073
Alexander Afanasyevc1030192012-03-08 22:21:28 -080074 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080075 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080076 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070077 * @param onUpdate function that will be called when new state is detected
78 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080079 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080080 * the app data when new remote names are learned
81 */
Alexander Afanasyev750d1872012-03-12 15:33:56 -070082 SyncLogic (const std::string &syncPrefix,
83 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070084 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080085
Alexander Afanasyevc1030192012-03-08 22:21:28 -080086 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080087
Alexander Afanasyevc1030192012-03-08 22:21:28 -080088 /**
89 * a wrapper for the same func in SyncApp
90 */
91 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080092
Alexander Afanasyevc1030192012-03-08 22:21:28 -080093 /**
94 * @brief respond to the Sync Interest; a lot of logic needs to go in here
95 * @param interest the Sync Interest in string format
96 */
97 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080098
Alexander Afanasyevc1030192012-03-08 22:21:28 -080099 /**
100 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -0800101 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800102 * @param dataBuffer the sync data
103 */
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700104 void respondSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800105
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700106 /**
107 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700108 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700109 */
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700110 void remove (const std::string &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700111
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700112#ifdef _DEBUG
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700113 Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700114 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700115#endif
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700116
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700117#ifdef NS3_MODULE
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700118public:
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700119 virtual void StartApplication ();
120 virtual void StopApplication ();
121#endif
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700122
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800123private:
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700124 void
125 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700126
127 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700128 processSyncInterest (const std::string &name,
129 DigestConstPtr digest, bool timedProcessing=false);
130
131 void
132 processSyncData (const std::string &name,
133 DigestConstPtr digest, const std::string &dataBuffer);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700134
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700135 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700136 processSyncRecoveryInterest (const std::string &name,
137 DigestConstPtr digest);
138
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700139 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700140 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700141
142 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700143 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800144
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700145 boost::tuple<DigestConstPtr, std::string>
146 convertNameToDigestAndType (const std::string &name);
147
148 void
149 sendSyncInterest ();
150
151 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700152 sendSyncRecoveryInterests (DigestConstPtr digest);
153
154 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700155 sendSyncData (const std::string &name,
156 DigestConstPtr digest, StateConstPtr state);
157
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800158private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700159 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800160 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700161 boost::recursive_mutex m_stateMutex;
162
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700163 std::string m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800164 SyncInterestTable m_syncInterestTable;
165
166 std::string m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700167 LogicUpdateCallback m_onUpdate;
168 LogicRemoveCallback m_onRemove;
Zhenkai Zhuce66e212012-03-12 22:27:19 -0700169 std::auto_ptr<CcnxWrapper> m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800170
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700171 Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800172
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700173#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700174 boost::mt19937 m_randomGenerator;
175 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700176 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700177#else
178 ns3::UniformVariable m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700179 ns3::UniformVariable m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700180#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700181
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700182 static const int m_unknownDigestStoreTime = 10; // seconds
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700183#ifdef NS3_MODULE
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700184 static const int m_syncResponseFreshness = 100; // milliseconds
Alexander Afanasyev55608612012-04-30 13:24:50 -0700185 static const int m_syncInterestReexpress = 10; // seconds
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700186 // don't forget to adjust value in SyncCcnxWrapper
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700187#else
Alexander Afanasyev70f32052012-04-24 14:10:38 -0700188 static const int m_syncResponseFreshness = 2000;
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700189 static const int m_syncInterestReexpress = 4;
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700190#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700191
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700192 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
193 uint32_t m_recoveryRetransmissionInterval; // milliseconds
194
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700195 enum EventLabels
196 {
197 DELAYED_INTEREST_PROCESSING = 1,
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700198 REEXPRESSING_INTEREST = 2,
199 REEXPRESSING_RECOVERY_INTEREST = 3
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700200 };
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800201};
202
203
204} // Sync
205
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800206#endif // SYNC_APP_WRAPPER_H