blob: 5e0965573fadcb63ab8adb0421afc24a79bba02e [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -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>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080022
23#include "sync-state.h"
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080024#include "sync-diff-leaf.h"
25#include "sync-std-name-info.h"
26
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080027#include <boost/assert.hpp>
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080028#include <boost/foreach.hpp>
29#include <boost/shared_ptr.hpp>
30#include <boost/throw_exception.hpp>
31#include <boost/lexical_cast.hpp>
32
33#define TIXML_USE_STL
34#include <tinyxml.h>
35
36using namespace std;
37using namespace boost;
38
39typedef error_info<struct tag_errmsg, string> info_str;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080040
41namespace Sync {
42
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080043#ifdef _DEBUG
44#define DEBUG_ENDL os << "\n";
45#else
46#define DEBUG_ENDL
47#endif
48
49std::ostream &
50operator << (std::ostream &os, const State &state)
51{
52 os << "<state>"; DEBUG_ENDL;
53
54 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
55 {
56 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
57 if (diffLeaf != 0)
58 {
59 os << "<item action=\"" << diffLeaf->getOperation () << "\">"; DEBUG_ENDL;
60 }
61 else
62 {
63 os << "<item>"; DEBUG_ENDL;
64 }
65 os << "<name>" << leaf->getInfo () << "</name>"; DEBUG_ENDL;
66 if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
67 {
68 os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
69 }
70 os << "</item>"; DEBUG_ENDL;
71 }
72 os << "</state>";
73}
74
75std::istream &
76operator >> (std::istream &in, State &state)
77{
78 TiXmlDocument doc;
79 in >> doc;
80
81 if (doc.RootElement() == 0)
82 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML"));
83
84 for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item");
85 iterator != 0;
86 iterator = iterator->NextSiblingElement("item"))
87 {
88 TiXmlElement *name = iterator->FirstChildElement ("name");
89 if (name == 0 || name->GetText() == 0)
90 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing"));
91
92 NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText());
93
94 if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0)
95 {
96 TiXmlElement *seq = iterator->FirstChildElement ("seq");
97 if (seq == 0)
98 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing"));
99
100 TiXmlElement *session = seq->FirstChildElement ("session");
101 TiXmlElement *seqno = seq->FirstChildElement ("seqno");
102
103 if (session == 0 || session->GetText() == 0)
104 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing"));
105 if (seqno == 0 || seqno->GetText() == 0)
106 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing"));
107
108 state.update (info, SeqNo (
109 lexical_cast<uint32_t> (session->GetText()),
110 lexical_cast<uint32_t> (seqno->GetText())
111 ));
112 }
113 else
114 {
115 state.remove (info);
116 }
117 }
118
119 return in;
120}
121
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800122}