forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_file_server.cpp
235 lines (170 loc) · 5.67 KB
/
ipc_file_server.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
#include "solid/frame/manager.hpp"
#include "solid/frame/scheduler.hpp"
#include "solid/frame/service.hpp"
#include "solid/frame/aio/aioresolver.hpp"
#include "solid/frame/ipc/ipcservice.hpp"
#include "solid/frame/ipc/ipcconfiguration.hpp"
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/utility.hpp>
#include "ipc_file_messages.hpp"
#include <string>
#include <deque>
#include <iostream>
#include <regex>
using namespace solid;
using namespace std;
namespace fs = boost::filesystem;
namespace{
using AioSchedulerT = frame::Scheduler<frame::aio::Reactor>;
//-----------------------------------------------------------------------------
// Parameters
//-----------------------------------------------------------------------------
struct Parameters{
Parameters():listener_port("0"), listener_addr("0.0.0.0"){}
string listener_port;
string listener_addr;
};
//-----------------------------------------------------------------------------
}//namespace
namespace ipc_file_server{
template <class M>
void complete_message(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<M> &_rsent_msg_ptr,
std::shared_ptr<M> &_rrecv_msg_ptr,
ErrorConditionT const &_rerror
);
template <>
void complete_message<ipc_file::ListRequest>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_file::ListRequest> &_rsent_msg_ptr,
std::shared_ptr<ipc_file::ListRequest> &_rrecv_msg_ptr,
ErrorConditionT const &_rerror
){
SOLID_CHECK(not _rerror);
SOLID_CHECK(_rrecv_msg_ptr);
SOLID_CHECK(not _rsent_msg_ptr);
auto msgptr = std::make_shared<ipc_file::ListResponse>(*_rrecv_msg_ptr);
fs::path fs_path(msgptr->path.c_str()/*, fs::native*/);
if(fs::exists( fs_path ) and fs::is_directory(fs_path)){
fs::directory_iterator it,end;
try{
it = fs::directory_iterator(fs_path);
}catch ( const std::exception & ex ){
it = end;
}
while(it != end){
msgptr->node_dq.emplace_back(std::string(it->path().c_str()), static_cast<uint8_t>(is_directory(*it)));
++it;
}
}
ErrorConditionT err = _rctx.service().sendMessage(_rctx.recipientId(), std::move(msgptr));
SOLID_CHECK(not err);
}
template <>
void complete_message<ipc_file::ListResponse>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_file::ListResponse> &_rsent_msg_ptr,
std::shared_ptr<ipc_file::ListResponse> &_rrecv_msg_ptr,
ErrorConditionT const &_rerror
){
SOLID_CHECK(not _rerror);
SOLID_CHECK(not _rrecv_msg_ptr);
SOLID_CHECK(_rsent_msg_ptr);
}
template <>
void complete_message<ipc_file::FileRequest>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_file::FileRequest> &_rsent_msg_ptr,
std::shared_ptr<ipc_file::FileRequest> &_rrecv_msg_ptr,
ErrorConditionT const &_rerror
){
SOLID_CHECK(not _rerror);
SOLID_CHECK(_rrecv_msg_ptr);
SOLID_CHECK(not _rsent_msg_ptr);
auto msgptr = std::make_shared<ipc_file::FileResponse>(*_rrecv_msg_ptr);
if(0){
boost::system::error_code error;
msgptr->remote_file_size = fs::file_size(fs::path(_rrecv_msg_ptr->remote_path), error);
}
ErrorConditionT err = _rctx.service().sendMessage(_rctx.recipientId(), std::move(msgptr));
SOLID_CHECK(not err);
}
template <>
void complete_message<ipc_file::FileResponse>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_file::FileResponse> &_rsent_msg_ptr,
std::shared_ptr<ipc_file::FileResponse> &_rrecv_msg_ptr,
ErrorConditionT const &_rerror
){
SOLID_CHECK(not _rerror);
SOLID_CHECK(not _rrecv_msg_ptr);
SOLID_CHECK(_rsent_msg_ptr);
}
template <typename T>
struct MessageSetup{
void operator()(frame::ipc::serialization_v1::Protocol &_rprotocol, const size_t _protocol_idx, const size_t _message_idx){
_rprotocol.registerType<T>(complete_message<T>, _protocol_idx, _message_idx);
}
};
}//namespace
//-----------------------------------------------------------------------------
bool parseArguments(Parameters &_par, int argc, char *argv[]);
//-----------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------
int main(int argc, char *argv[]){
Parameters p;
if(!parseArguments(p, argc, argv)) return 0;
{
AioSchedulerT scheduler;
frame::Manager manager;
frame::ipc::ServiceT ipcsvc(manager);
ErrorConditionT err;
err = scheduler.start(1);
if(err){
cout<<"Error starting aio scheduler: "<<err.message()<<endl;
return 1;
}
{
frame::ipc::serialization_v1::Protocol *proto = new frame::ipc::serialization_v1::Protocol;
frame::ipc::Configuration cfg(scheduler, proto);
ipc_file::ProtoSpecT::setup<ipc_file_server::MessageSetup>(*proto);
cfg.listener_address_str = p.listener_addr;
cfg.listener_address_str += ':';
cfg.listener_address_str += p.listener_port;
cfg.connection_start_state = frame::ipc::ConnectionState::Active;
err = ipcsvc.reconfigure(std::move(cfg));
if(err){
cout<<"Error starting ipcservice: "<<err.message()<<endl;
manager.stop();
return 1;
}
{
std::ostringstream oss;
oss<<ipcsvc.configuration().listenerPort();
cout<<"server listens on port: "<<oss.str()<<endl;
}
}
cout<<"Press any char and ENTER to stop: ";
char c;
cin>>c;
manager.stop();
}
return 0;
}
//-----------------------------------------------------------------------------
bool parseArguments(Parameters &_par, int argc, char *argv[]){
if(argc == 2){
size_t pos;
_par.listener_addr = argv[1];
pos = _par.listener_addr.rfind(':');
if(pos != string::npos){
_par.listener_addr[pos] = '\0';
_par.listener_port.assign(_par.listener_addr.c_str() + pos + 1);
_par.listener_addr.resize(pos);
}
}
return true;
}