blob: 51470b7b1e700fc8066658b66eba73e3c42b86e6 [file] [log] [blame]
Yingdi Yu274a2272014-08-26 19:25:16 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2014 University of California, Los Angeles
4 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <yingdi@cs.ucla.edu>
23 */
24
25#ifndef CHRONOSYNC_LEAF_CONTAINER_HPP
26#define CHRONOSYNC_LEAF_CONTAINER_HPP
27
28#include "mi-tag.hpp"
29#include "leaf.hpp"
30
31#include <ndn-cxx/util/crypto.hpp>
32
33#include <boost/multi_index_container.hpp>
34#include <boost/multi_index/ordered_index.hpp>
35#include <boost/multi_index/hashed_index.hpp>
36#include <boost/multi_index/mem_fun.hpp>
37
38
39
40namespace chronosync {
41
42namespace mi = boost::multi_index;
43
44struct SessionNameHash
45{
46 std::size_t
47 operator()(const Name& prefix) const
48 {
49 ndn::ConstBufferPtr buffer =
50 ndn::crypto::sha256(prefix.wireEncode().wire(), prefix.wireEncode().size());
51
52 BOOST_ASSERT(buffer->size() > sizeof(std::size_t));
53
54 return *reinterpret_cast<const std::size_t*>(buffer->buf());
55 }
56};
57
58struct SessionNameEqual
59{
60 bool
61 operator()(const Name& prefix1, const Name& prefix2) const
62 {
63 return prefix1 == prefix2;
64 }
65};
66
67struct SessionNameCompare : public std::less<Name>
68{
69 bool
70 operator()(const Name& prefix1, const Name& prefix2) const
71 {
72 return prefix1 < prefix2;
73 }
74};
75
76/**
77 * @brief Container for chronosync leaves
78 */
79struct LeafContainer : public mi::multi_index_container<
80 LeafPtr,
81 mi::indexed_by<
82 // For fast access to elements using SessionName
83 mi::hashed_unique<
84 mi::tag<hashed>,
85 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
86 SessionNameHash,
87 SessionNameEqual
88 >,
89
90 mi::ordered_unique<
91 mi::tag<ordered>,
92 mi::const_mem_fun<Leaf, const Name&, &Leaf::getSessionName>,
93 SessionNameCompare
94 >
95 >
96 >
97{
98};
99
100} // namespace chronosync
101
102#endif // CHRONOSYNC_LEAF_CONTAINER_HPP