-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtilities.cpp
More file actions
268 lines (170 loc) · 5.7 KB
/
Utilities.cpp
File metadata and controls
268 lines (170 loc) · 5.7 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <Utilities.h>
Utilities::Utilities(zmq::context_t* zmqcontext){
context=zmqcontext;
Threads.clear();
}
bool Utilities::AddService(std::string ServiceName, unsigned int port, bool StatusQuery){
zmq::socket_t Ireceive (*context, ZMQ_PUSH);
Ireceive.connect("inproc://ServicePublish");
boost::uuids::uuid m_UUID;
m_UUID = boost::uuids::random_generator()();
std::stringstream test;
test<<"Add "<< ServiceName <<" "<<m_UUID<<" "<<port<<" "<<((int)StatusQuery) ;
zmq::message_t send(test.str().length()+1);
snprintf ((char *) send.data(), test.str().length()+1 , "%s" ,test.str().c_str()) ;
return Ireceive.send(send);
}
bool Utilities::RemoveService(std::string ServiceName){
zmq::socket_t Ireceive (*context, ZMQ_PUSH);
Ireceive.connect("inproc://ServicePublish");
std::stringstream test;
test<<"Delete "<< ServiceName << " ";
zmq::message_t send(test.str().length()+1);
snprintf ((char *) send.data(), test.str().length()+1 , "%s" ,test.str().c_str()) ;
return Ireceive.send(send);
}
int Utilities::UpdateConnections(std::string ServiceName, zmq::socket_t* sock, std::map<std::string,Store*> &connections, std::string port){
boost::uuids::uuid m_UUID=boost::uuids::random_generator()();
long msg_id=0;
zmq::socket_t Ireceive (*context, ZMQ_DEALER);
Ireceive.connect("inproc://ServiceDiscovery");
zmq::message_t send(4);
snprintf ((char *) send.data(), 4 , "%s" ,"All") ;
if(!Ireceive.send(send)){
std::cerr<<"Failed to send 'ALL' query to ServiceDiscovery!"<<std::endl;
};
zmq::message_t receive;
if(!Ireceive.recv(&receive)){
std::cerr<<"Failed to receive 'ALL' query from ServiceDiscovery!"<<std::endl;
};
std::istringstream iss(static_cast<char*>(receive.data()));
int size;
iss>>size;
for(int i=0;i<size;i++){
Store *service = new Store;
zmq::message_t servicem;
Ireceive.recv(&servicem);
std::istringstream ss(static_cast<char*>(servicem.data()));
service->JsonParser(ss.str());
std::string type;
std::string uuid;
std::string ip;
std::string store_port="";
service->Get("msg_value",type);
service->Get("uuid",uuid);
service->Get("ip",ip);
service->Get("remote_port",store_port);
std::string tmp;
if(port==""){
tmp=ip + ":" + store_port;
} else {
tmp=ip + ":" + port;
}
//if(type == ServiceName && connections.count(uuid)==0){
if(type == ServiceName && connections.count(tmp)==0){
connections[tmp]=service;
//std::string ip;
//std::string port;
//service->Get("ip",ip);
//service->Get("remote_port",port);
tmp="tcp://"+ tmp;
sock->connect(tmp.c_str());
}
else{
delete service;
service=0;
}
}
return connections.size();
}
Thread_args* Utilities::CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args){
if(Threads.count(ThreadName)==0){
if(args==0) args = new Thread_args();
args->context=context;
args->ThreadName=ThreadName;
args->func=func;
args->running=true;
pthread_create(&(args->thread), NULL, Utilities::Thread, args);
args->sock=0;
Threads[ThreadName]=args;
}
else args=0;
return args;
}
Thread_args* Utilities::CreateThread(std::string ThreadName, void (*func)(std::string)){
Thread_args *args =0;
if(Threads.count(ThreadName)==0){
args = new Thread_args(context, ThreadName, func);
pthread_create(&(args->thread), NULL, Utilities::String_Thread, args);
args->sock=0;
args->running=true;
Threads[ThreadName]=args;
}
return args;
}
void *Utilities::String_Thread(void *arg){
Thread_args *args = static_cast<Thread_args *>(arg);
zmq::socket_t IThread(*(args->context), ZMQ_PAIR);
/// need to subscribe
std::stringstream tmp;
tmp<<"inproc://"<<args->ThreadName;
IThread.bind(tmp.str().c_str());
zmq::pollitem_t initems[] = {
{IThread, 0, ZMQ_POLLIN, 0}};
args->running = true;
while(!args->kill){
if(args->running){
std::string command="";
zmq::poll(&initems[0], 1, 0);
if ((initems[0].revents & ZMQ_POLLIN)){
zmq::message_t message;
IThread.recv(&message);
command=std::string(static_cast<char *>(message.data()));
}
args->func_with_string(command);
}
else usleep(100);
}
pthread_exit(NULL);
}
void *Utilities::Thread(void *arg){
Thread_args *args = static_cast<Thread_args *>(arg);
while (!args->kill){
if(args->running) args->func(args );
else usleep(100);
}
pthread_exit(NULL);
}
bool Utilities::MessageThread(Thread_args* args, std::string Message, bool block){
bool ret=false;
if(args){
if(!args->sock){
args->sock = new zmq::socket_t(*(args->context), ZMQ_PAIR);
std::stringstream tmp;
tmp<<"inproc://"<<args->ThreadName;
args->sock->connect(tmp.str().c_str());
}
zmq::message_t msg(Message.length()+1);
snprintf((char *)msg.data(), Message.length()+1, "%s", Message.c_str());
if(block) ret=args->sock->send(msg);
else ret=args->sock->send(msg, ZMQ_NOBLOCK);
}
return ret;
}
bool Utilities::MessageThread(std::string ThreadName, std::string Message, bool block){
return MessageThread(Threads[ThreadName],Message,block);
}
bool Utilities::KillThread(Thread_args* &args){
bool ret=false;
if(args){
args->running=false;
args->kill=true;
pthread_join(args->thread, NULL);
//delete args;
//args=0;
}
return ret;
}
bool Utilities::KillThread(std::string ThreadName){
return KillThread(Threads[ThreadName]);
}