forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_request_server.cpp
219 lines (163 loc) · 5.48 KB
/
ipc_request_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
#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 "ipc_request_messages.hpp"
#include <string>
#include <deque>
#include <iostream>
#include <regex>
using namespace solid;
using namespace std;
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;
};
//-----------------------------------------------------------------------------
struct Date{
uint8_t day;
uint8_t month;
uint16_t year;
};
struct AccountData{
string userid;
string full_name;
string email;
string country;
string city;
Date birth_date;
};
using AccountDataDequeT = std::deque<AccountData>;
AccountDataDequeT account_dq = {
{"user1", "Super Man", "[email protected]", "RO", "bucharest", {11, 1, 2001}},
{"user2", "Spider Man", "[email protected]", "RO", "bucharest", {12, 2, 2002}},
{"user3", "Ant Man", "[email protected]", "RO", "bucharest", {13, 3, 2003}},
{"iron_man", "Iron Man", "[email protected]", "UK", "london", {11,4,2004}},
{"dragon_man", "Dragon Man", "[email protected]", "FR", "paris", {12,5,2005}},
{"frog_man", "Frog Man", "[email protected]", "PL", "warsaw", {13,6,2006}},
};
ipc_request::UserData make_user_data(const AccountData &_rad){
ipc_request::UserData ud;
ud.full_name = _rad.full_name;
ud.email = _rad.email;
ud.country = _rad.country;
ud.city = _rad.city;
ud.birth_date.day = _rad.birth_date.day;
ud.birth_date.month = _rad.birth_date.month;
ud.birth_date.year = _rad.birth_date.year;
return ud;
}
}//namespace
namespace ipc_request_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_request::Request>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_request::Request> &_rsent_msg_ptr,
std::shared_ptr<ipc_request::Request> &_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_request::Response>(*_rrecv_msg_ptr);
std::regex userid_regex(_rrecv_msg_ptr->userid_regex);
for(const auto &ad: account_dq){
if(std::regex_match(ad.userid, userid_regex)){
msgptr->user_data_map.insert(std::move(ipc_request::Response::UserDataMapT::value_type(ad.userid, std::move(make_user_data(ad)))));
}
}
ErrorConditionT err = _rctx.service().sendMessage(_rctx.recipientId(), std::move(msgptr));
SOLID_CHECK(not err);
}
template <>
void complete_message<ipc_request::Response>(
frame::ipc::ConnectionContext &_rctx,
std::shared_ptr<ipc_request::Response> &_rsent_msg_ptr,
std::shared_ptr<ipc_request::Response> &_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_request::ProtoSpecT::setup<ipc_request_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;
}