forked from ToolDAQ/libDAQInterface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Win_Mac_translation.cpp
173 lines (119 loc) · 4.25 KB
/
Win_Mac_translation.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
#include <iostream>
#include <zmq.hpp>
#include <ServiceDiscovery.h>
#include <boost/uuid/uuid.hpp> // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp> // streaming operators etc.
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace ToolFramework;
int main(){
zmq::context_t context(1);
ServiceDiscovery SD(false,true, 55555 , "239.192.1.1", 5000, &context, boost::uuids::random_generator()(), "Win_Mac_translation", 5, 60);
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::socket_t publish_sock (context, ZMQ_PUB);
publish_sock.connect("tcp://127.0.0.1:666");
boost::posix_time::time_duration period(0,0,1,0);
boost::posix_time::ptime last= boost::posix_time::microsec_clock::universal_time();
///////////////////////////// MM ///////////////////
zmq::socket_t publish_sock_MM (context, ZMQ_PUB);
publish_sock.connect("tcp://127.0.0.1:667");
struct sockaddr_in addr;
int addrlen, sock, cnt;
struct ip_mreq mreq;
char message[512];
// set up socket //
sock = socket(AF_INET, SOCK_DGRAM, 0);
int a =1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &a, sizeof(int));
//fcntl(sock, F_SETFL, O_NONBLOCK);
if (sock < 0) {
perror("socket");
exit(1);
}
bzero((char *)&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(5554U);
addrlen = sizeof(addr);
// receive //
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("bind");
printf("Failed to bind to multicast listen socket");
exit(1);
}
mreq.imr_multiaddr.s_addr = inet_addr("239.192.1.1");
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq)) < 0) {
perror("setsockopt mreq");
printf("Failed to goin multicast group listen thread");
exit(1);
}
zmq::pollitem_t items [] = {
{ NULL, sock, ZMQ_POLLIN, 0 }
};
///////////////////////////////////////////////////
while(true){
zmq::poll (&items [0], 1, 100);
if ((items [0].revents & ZMQ_POLLIN)) {
cnt = recvfrom(sock, message, sizeof(message), 0, (struct sockaddr *) &addr, (socklen_t*) &addrlen);
if ((cnt > 0) ) {
zmq::message_t MM_message(sizeof(message));
memcpy( MM_message.data(), message, sizeof(message));
// snprintf ((char *) MM_message.data(), sizeof(message) , "%d" , message) ;
publish_sock_MM.send(MM_message);
}
}
boost::posix_time::time_duration lapse(period-(boost::posix_time::microsec_clock::universal_time() - last));
if(lapse.is_negative()){
last= boost::posix_time::microsec_clock::universal_time();
zmq::message_t send(4);
snprintf ((char *) send.data(), 4 , "%s" ,"All") ;
Ireceive.send(send);
zmq::message_t receive;
Ireceive.recv(&receive);
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);
publish_sock.send(servicem);
// std::istringstream ss(static_cast<char*>(servicem.data()));
//std::cout<<ss.str()<<std::endl;
/* service->JsonParser(ss.str());
std::string type;
std::string uuid;
std::string ip;
std::string remote_port;
service->Get("msg_value",type);
service->Get("uuid",uuid);
service->Get("ip",ip);
if(port=="") service->Get("remote_port",remote_port);
else remote_port=port;
std::string tmp=ip + ":" + remote_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();
*/
}
}
}
return 0;
}