blob: 0fe12a6665c775ccd422ab3281128c6b85600abf [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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>
Vince Lehman0a7da612014-10-29 14:39:29 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#include "sync-state.h"
24#include "sync-diff-leaf.h"
25#include "sync-std-name-info.h"
26
27#include <boost/assert.hpp>
28#include <boost/foreach.hpp>
akmhoque66e66182014-02-21 17:56:03 -060029#include <boost/throw_exception.hpp>
30#include <boost/lexical_cast.hpp>
31
Vince Lehman0a7da612014-10-29 14:39:29 -050032typedef boost::error_info<struct tag_errmsg, std::string> info_str;
akmhoque66e66182014-02-21 17:56:03 -060033
34using namespace Sync::Error;
Vince Lehman0a7da612014-10-29 14:39:29 -050035using boost::lexical_cast;
akmhoque66e66182014-02-21 17:56:03 -060036
37namespace Sync {
38
39/*
40std::ostream &
41operator << (std::ostream &os, const State &state)
42{
43 os << "<state>"; DEBUG_ENDL;
Vince Lehman0a7da612014-10-29 14:39:29 -050044
akmhoque66e66182014-02-21 17:56:03 -060045 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
46 {
47 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
48 if (diffLeaf != 0)
49 {
50 os << "<item action=\"" << diffLeaf->getOperation () << "\">"; DEBUG_ENDL;
51 }
52 else
53 {
54 os << "<item>"; DEBUG_ENDL;
55 }
56 os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL;
57 if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
58 {
59 os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
60 }
61 os << "</item>"; DEBUG_ENDL;
62 }
63 os << "</state>";
64}
65*/
66
67SyncStateMsg &
68operator << (SyncStateMsg &ossm, const State &state)
69{
70 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
71 {
72 SyncState *oss = ossm.add_ss();
73 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
74 if (diffLeaf != 0 && diffLeaf->getOperation() != UPDATE)
75 {
76 oss->set_type(SyncState::DELETE);
77 }
78 else
79 {
80 oss->set_type(SyncState::UPDATE);
81 }
82
83 std::ostringstream os;
84 os << *leaf->getInfo();
85 oss->set_name(os.str());
86
87 if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
88 {
89 SyncState::SeqNo *seqNo = oss->mutable_seqno();
90 seqNo->set_session(leaf->getSeq().getSession());
91 seqNo->set_seq(leaf->getSeq().getSeq());
92 }
93 }
94 return ossm;
95}
96
97/*
98std::istream &
99operator >> (std::istream &in, State &state)
100{
101 TiXmlDocument doc;
102 in >> doc;
103
104 if (doc.RootElement() == 0)
105 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML"));
Vince Lehman0a7da612014-10-29 14:39:29 -0500106
akmhoque66e66182014-02-21 17:56:03 -0600107 for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item");
108 iterator != 0;
109 iterator = iterator->NextSiblingElement("item"))
110 {
111 TiXmlElement *name = iterator->FirstChildElement ("name");
112 if (name == 0 || name->GetText() == 0)
113 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing"));
Vince Lehman0a7da612014-10-29 14:39:29 -0500114
akmhoque66e66182014-02-21 17:56:03 -0600115 NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText());
Vince Lehman0a7da612014-10-29 14:39:29 -0500116
akmhoque66e66182014-02-21 17:56:03 -0600117 if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0)
118 {
119 TiXmlElement *seq = iterator->FirstChildElement ("seq");
120 if (seq == 0)
121 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing"));
Vince Lehman0a7da612014-10-29 14:39:29 -0500122
akmhoque66e66182014-02-21 17:56:03 -0600123 TiXmlElement *session = seq->FirstChildElement ("session");
124 TiXmlElement *seqno = seq->FirstChildElement ("seqno");
125
126 if (session == 0 || session->GetText() == 0)
127 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing"));
128 if (seqno == 0 || seqno->GetText() == 0)
129 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing"));
130
131 state.update (info, SeqNo (
132 lexical_cast<uint32_t> (session->GetText()),
133 lexical_cast<uint32_t> (seqno->GetText())
134 ));
135 }
136 else
137 {
138 state.remove (info);
139 }
140 }
141
142 return in;
143}
144*/
145
146SyncStateMsg &
147operator >> (SyncStateMsg &issm, State &state)
148{
149 int n = issm.ss_size();
150 for (int i = 0; i < n; i++)
151 {
152 const SyncState &ss = issm.ss(i);
153 NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name());
154 if (ss.type() == SyncState::UPDATE)
155 {
156 uint64_t session = lexical_cast<uint64_t>(ss.seqno().session());
157 uint64_t seq = lexical_cast<uint64_t>(ss.seqno().seq());
158 SeqNo seqNo(session, seq);
159 state.update(info, seqNo);
160 }
161 else
162 {
163 state.remove(info);
164 }
165 }
166 return issm;
167}
168
169}