blob: 03d80b91b0365fee0d04568bc1d377b67dcba411 [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>
19 * 卞超轶 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
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080025#include <boost/shared_ptr.hpp>
26#include <boost/function.hpp>
27#include "sync-ccnx-wrapper.h"
28#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080029#include "sync-diff-state.h"
30#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080031#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080032
Alexander Afanasyevc1030192012-03-08 22:21:28 -080033#include "sync-diff-state-container.h"
34
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080035namespace Sync {
36
37/**
38 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080039 * @brief A wrapper for SyncApp, which handles ccnx related things (process
40 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080041 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080042class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080043{
44public:
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080045 typedef boost::function<void (const std::string &, uint32_t, uint32_t)> LogicCallback;
46
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080048 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080049 * @param syncPrefix the name prefix to use for the Sync Interest
50 * @param fetch the fetch function, which will be called to actually fetch
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080051 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080052 * the app data when new remote names are learned
53 */
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080054 SyncLogic (const std::string &syncPrefix, LogicCallback fetch, CcnxWrapperPtr ccnxHandle);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080055
Alexander Afanasyevc1030192012-03-08 22:21:28 -080056 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080057
Alexander Afanasyevc1030192012-03-08 22:21:28 -080058 /**
59 * a wrapper for the same func in SyncApp
60 */
61 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080062
Alexander Afanasyevc1030192012-03-08 22:21:28 -080063 /**
64 * @brief respond to the Sync Interest; a lot of logic needs to go in here
65 * @param interest the Sync Interest in string format
66 */
67 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080068
Alexander Afanasyevc1030192012-03-08 22:21:28 -080069 /**
70 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -080071 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -080072 * @param dataBuffer the sync data
73 */
74 void processSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080075
76private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -080077 void sendSyncInterest ();
Chaoyi Bian69955ca2012-03-09 22:40:22 -080078 void checkAgain (const std::string &interest, DigestPtr digest);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080079
80private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -080081 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080082 DiffStateContainer m_log;
Alexander Afanasyevc1030192012-03-08 22:21:28 -080083 SyncInterestTable m_syncInterestTable;
84
85 std::string m_syncPrefix;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086 LogicCallback m_fetch;
Alexander Afanasyevc1030192012-03-08 22:21:28 -080087 CcnxWrapperPtr m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080088
Chaoyi Bian633255f2012-03-09 21:25:38 -080089 boost::thread m_thread;
90
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080091 static const int m_syncResponseFreshness = 2;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080092};
93
94
95} // Sync
96
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080097#endif // SYNC_APP_WRAPPER_H