forked from hyphanet/lib-CppFCPLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerMessage.cpp
251 lines (215 loc) · 6.29 KB
/
ServerMessage.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "Log.h"
#include "ServerMessage.h"
#include "JobTicket.h"
#include <boost/lexical_cast.hpp>
using namespace FCPLib;
ServerMessage::Ptr
ServerMessage::factory(boost::shared_ptr<Server> s){
ServerMessage::Ptr m;
std::string header = s->readln();
log().log(DETAIL, "NODE: " + header);
if (header == "NodeHello"){
m = Ptr( new NodeHelloMessage() );
} else
if (header == "CloseConnectionDuplicateName") {
m = Ptr( new CloseConnectionDuplicateNameMessage() );
} else
if (header == "Peer") {
m = Ptr( new PeerMessage() );
} else
if (header == "EndListPeers") {
m = Ptr( new EndMessage() );
} else
if (header == "PeerNote") {
m = Ptr( new PeerNoteMessage() );
} else
if (header == "EndListPeerNotes") {
m = Ptr( new EndMessage() );
} else
if (header == "PeerRemoved") {
m = Ptr( new PeerRemovedMessage() );
} else
if (header == "NodeData") {
m = Ptr( new NodeDataMessage() );
} else
if (header == "ConfigData") {
m = Ptr( new ConfigDataMessage() );
} else
if (header == "TestDDAReply") {
m = Ptr( new TestDDAReplyMessage() );
} else
if (header == "TestDDAComplete") {
m = Ptr( new TestDDACompleteMessage() );
} else
if (header == "SSKKeypair") {
m = Ptr( new SSKKeypairMessage() );
} else
if (header == "PersistentGet") {
m = Ptr( new PersistentGetMessage() );
} else
if (header == "PersistentPut") {
m = Ptr( new PersistentPutMessage() );
} else
if (header == "PersistentPutDir") {
m = Ptr( new PersistentPutDirMessage() );
} else
if (header == "URIGenerated") {
m = Ptr( new URIGeneratedMessage() );
} else
if (header == "PutSuccessful") {
m = Ptr( new PutSuccessfulMessage() );
} else
if (header == "PutFetchable") {
m = Ptr( new PutFetchableMessage() );
} else
if (header == "DataFound") {
m = Ptr( new DataFoundMessage() );
} else
if (header == "AllData") {
m = Ptr( new AllDataMessage() );
} else
if (header == "StartedCompression") {
m = Ptr( new StartedCompressionMessage() );
} else
if (header == "FinishedCompression") {
m = Ptr( new FinishedCompressionMessage() );
} else
if (header == "SimpleProgress") {
m = Ptr( new SimpleProgressMessage() );
} else
if (header == "EndListPersistentRequest") {
m = Ptr( new EndMessage() );
} else
if (header == "PersistentRequestRemoved") {
m = Ptr( new PersistentRequestRemovedMessage() );
} else
if (header == "PersistentRequestModified") {
m = Ptr( new PersistentRequestModifiedMessage() );
} else
if (header == "PutFailed") {
m = Ptr( new PutFailedMessage() );
} else
if (header == "GetFailed") {
m = Ptr( new GetFailedMessage() );
} else
if (header == "ProtocolError") {
m = Ptr( new ProtocolErrorMessage() );
} else
if (header == "IdentifierCollision") {
m = Ptr( new IdentifierCollisionMessage() );
} else
if (header == "UnknownNodeIdentifier") {
m = Ptr( new UnknownNodeIdentifierMessage() );
} else
if (header == "UnknownPeerNoteType") {
m = Ptr( new UnknownPeerNoteTypeMessage() );
} else
if (header == "SubscribedUSKUpdate") {
m = Ptr( new SubscribedUSKUpdate() );
} else
throw NotImplemented("Message " + header + " not implemented");
m->message = Message::factory(header);
m->read(s);
return m;
}
void
ServerMessage::read(boost::shared_ptr<Server> s)
{
std::string line;
for (;;) {
line = s->readln();
log().log(DETAIL, "NODE: " + line);
if ( line == "End" || line == "EndMessage" )
break;
int pos = line.find_first_of('=');
message->setField(std::string(line.begin(), line.begin() + pos),
std::string(line.begin() + pos + 1, line.end()));
}
}
const std::string&
ServerMessage::toString() const {
return message->toString();
}
bool
IsLastPeer::operator()(const JobTicket::Ptr job) const
{
std::string cmd( job->getCommandName() );
if (cmd == "ListPeers")
return false;
else if (cmd == "ModifyPeer")
return true;
else if (cmd == "AddPeer")
return true;
else if (cmd == "ListPeer")
return true;
throw NotImplemented("Unknown command");
}
bool
IsLastPeerNote::operator()(const JobTicket::Ptr job) const
{
std::string cmd( job->getCommandName() );
if (cmd == "ListPeerNotes")
return false;
else if (cmd == "ModifyPeerNote")
return true;
throw NotImplemented("Unknown command");
}
bool
IsLastPutFailed::operator()(const JobTicketPtr job) const
{
if ( boost::lexical_cast<int>( message->getField("Code") ) == 10 )
return false;
return true;
}
bool
IsLastGetFailed::operator()(const JobTicketPtr job) const
{
if ( boost::lexical_cast<int>( message->getField("Code") ) == 25 )
return false;
return true;
}
bool
IsLastDataFound::operator()(const JobTicketPtr job) const
{
GetJob::Ptr job_ = boost::dynamic_pointer_cast<GetJob, JobTicket>( job );
if ( job_->getReturnType() != GetJob::Direct )
return true;
// non persistent job needs to ask for AllData
return job_->isPersistent();
}
void
AllDataMessage::read(boost::shared_ptr<Server> s)
{
server = s;
std::string line;
for (;;) {
line = s->readln();
log().log(DETAIL, "NODE: " + line);
if ( line == "Data" )
break;
int pos = line.find_first_of('=');
message->setField(std::string(line.begin(), line.begin() + pos),
std::string(line.begin() + pos + 1, line.end()));
}
bytesToRead = boost::lexical_cast<int>( message->getField("DataLength") );
log().log(DETAIL, " ... " + message->getField("DataLength") + " bytes of data ...");
}
bool
AllDataMessage::isLast(const JobTicketPtr job) const
{
log().log(NOISY, "AllDataMessage::isLast: top");
GetJob::Ptr job_ = boost::dynamic_pointer_cast<GetJob, JobTicket>( job );
std::ostream& stream = job_->getStream();
char buf[1024];
std::size_t bytes_available = bytesToRead;
log().log(NOISY, "bytesToRead = " + boost::lexical_cast<std::string>( bytes_available ));
while (bytes_available > 0) {
std::size_t m = std::min<std::size_t>(bytes_available, 1024);
server->read(buf, m);
stream.write(buf, m);
bytes_available -= m;
log().log(DEBUG, "NODE: read "+ boost::lexical_cast<std::string>( m ) + " bytes of data, " + boost::lexical_cast<std::string>( bytes_available ) + " still left");
}
stream.flush();
return true;
}