-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFCPResult.h
162 lines (136 loc) · 3.6 KB
/
FCPResult.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef FCPRESULT_H__
#define FCPRESULT_H__
#include <vector>
#include <string>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "JobTicket.h"
#include "Base64.h"
namespace FCPLib {
class TestDDAReplyResponse {
friend struct TestDDAReplyConverter;
Message::Ptr message;
TestDDAReplyResponse(Message::Ptr message_) : message(message_) {}
const std::string& getField(std::string field) const {
const std::string& ret = message->getField(field);
if (ret == "")
throw std::logic_error("TestDDAReply does not contain " + field + " field.");
return ret;
}
public:
typedef boost::shared_ptr<TestDDAReplyResponse> Ptr;
const std::string getDirectory() const throw(std::logic_error){
return message->getField("Directory");
}
const std::string getReadFilename() const throw(std::logic_error){
return getField("ReadFilename");
}
const std::string getWriteFilename() const throw(std::logic_error){
return getField("WriteFilename");
}
const std::string getContent () const throw(std::logic_error){
return getField("ContentToWrite");
}
};
class TestDDAResponse {
public:
TestDDAResponse(std::string dir, bool read, bool write)
: directory(dir),
readDirectory(read),
writeDirectory(write)
{}
std::string directory;
bool readDirectory;
bool writeDirectory;
};
class PeerNote {
public:
PeerNote( Message::Ptr m )
: nodeIdentifier( m->getField("NodeIdentifier") ),
noteText( Base64::base64Decode( m->getField("NoteText") ) ),
peerNoteType( boost::lexical_cast<int>(m->getField("PeerNoteType")) )
{}
std::string nodeIdentifier;
std::string noteText;
int peerNoteType;
std::string toString() const {
return "NodeIdentifier="+nodeIdentifier+"\n"+
"NodeText="+noteText+"\n"+
"PeerNoteType="+boost::lexical_cast<std::string>(peerNoteType)+"\n";
}
};
typedef std::vector<PeerNote> PeerNoteContainer;
///////////
struct GetMessage {
Message::Ptr
operator()( ServerMessage::Ptr sm ){
return sm->getMessage();
}
};
struct MessageConverter {
Message::Ptr
operator()( Response &resp )
{
return Message::Ptr( resp.front()->getMessage() );
}
};
struct LastMessageConverter {
ServerMessage::Ptr
operator()( Response &resp )
{
return ServerMessage::Ptr( resp.back() );
}
};
struct VectorConverter {
MessagePtrContainer
operator()( Response &resp )
{
MessagePtrContainer ret( resp.size() ) ;
transform( resp.begin(), resp.end(), ret.begin(), GetMessage() );
return ret;
}
};
struct VectorWithoutLastConverter {
MessagePtrContainer
operator()( Response &resp )
{
int numToCopy = resp.size() - 1;
MessagePtrContainer ret = MessagePtrContainer( numToCopy );
transform( resp.begin(), resp.begin() + numToCopy, ret.begin(), GetMessage() );
return ret;
}
};
struct TestDDAReplyConverter {
TestDDAReplyResponse::Ptr
operator()( Response &resp )
{
return TestDDAReplyResponse::Ptr( new TestDDAReplyResponse(resp.front()->getMessage()) );
}
};
struct PeerNotesConverter {
PeerNoteContainer
operator()( Response &resp )
{
PeerNoteContainer ret;
int size = resp.size() - 1;
Response::iterator end = resp.begin() + size;
for (Response::iterator it = resp.begin(); it != end; ++it)
ret.push_back( PeerNote( (*it)->getMessage() ) );
return ret;
}
};
struct PeerNoteConverter {
PeerNote
operator()( Response &resp )
{
return PeerNote( resp.front()->getMessage() );
}
};
//////////
template<typename ReturnT, typename ConverterT>
ReturnT createResult( Response &resp )
{
return ConverterT()( resp );
}
}
#endif