ChessConverter.cpp

V. Losing, 12/18/2012 11:29 AM

Download (2.46 KB)

 
1
#include "ChessConverter.h"
2
#include <rsb/converter/SerializationException.h>
3
#include <rst/sandbox/rst/Chess/chess.pb.h>
4
#include <rsb/converter/ProtocolBufferConverter.h>
5

    
6
using namespace std;
7

    
8
namespace rst {
9
namespace converter {
10
                ChessConverter::ChessConverter() :
11
        Converter<string>(rsc::runtime::typeName<chess::Chess>(), RSB_TYPE_TAG(Chess)), converter(
12
                new rsb::converter::ProtocolBufferConverter<chess::Chess>) {
13
        }
14

    
15

    
16

    
17
        ChessConverter::~ChessConverter() {
18
        }
19

    
20
        string ChessConverter::getWireSchema() const {
21
                return converter->getWireSchema();
22
        }
23

    
24
        string ChessConverter::getClassName() const {
25
                return "ChessConverter";
26
        }
27

    
28

    
29
        string ChessConverter::serialize(const rsb::AnnotatedData& data, string& wire) {
30

    
31
                if (data.first != getDataType()) {
32
                        throw rsb::converter::SerializationException(
33
                                "Called with unsupported data type " + data.first);
34
                }
35

    
36
                boost::shared_ptr<Chess> chess = boost::static_pointer_cast<
37
                        Chess>(data.second);
38

    
39
                boost::shared_ptr<chess::Chess> protoChess(
40
                        new chess::Chess);
41

    
42
                protoChess->set_turn(rst::chess::Player(chess->getTurn()));
43
                for (vector<Figure>::iterator it = chess->getFigures().begin();
44
                        it != chess->getFigures().end(); ++it) {
45

    
46
                                // convert event to notification
47
                                Figure& figure = *it;
48

    
49
                                chess::Figure* protoFigure = protoChess->add_figures();
50
                                FigureConverter::figureToProto(figure, *protoFigure); 
51
                }
52

    
53
                return converter->serialize(
54
                        make_pair(
55
                        rsc::runtime::typeName<
56
                        chess::Chess>(),
57
                        protoChess), wire);
58
        }
59

    
60
        rsb::AnnotatedData ChessConverter::deserialize(const std::string& wireSchema,
61
                const string& wire) {
62

    
63
                        if (wireSchema != getWireSchema()) {
64
                                throw rsb::converter::SerializationException("Unexpected wire schema " + wireSchema);
65
                        }
66

    
67

    
68
                        boost::shared_ptr<chess::Chess> protoChess =
69
                                boost::static_pointer_cast<chess::Chess>(
70
                                converter->deserialize(wireSchema, wire).second);
71

    
72
                        boost::shared_ptr<Chess> chess(new Chess);
73
                        chess->getFigures().clear();
74
                        chess->setTurn(Player(protoChess->turn()));
75

    
76

    
77
                        // iterate over all notifications in each scope set
78
                        for (int i = 0; i < protoChess->figures_size(); ++i) {
79

    
80
                                // convert the notification back to an event
81
                                const chess::Figure& protoFigure = protoChess->figures(i);
82
                                Figure figure;
83
                                FigureConverter::protoToFigure(figure, protoFigure);
84
                                chess->getFigures().push_back(figure);
85
                        }
86
                        return make_pair(getDataType(), chess);
87

    
88
        }
89

    
90
        }
91

    
92
}