-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-pub-sub-basic-client.cpp
41 lines (32 loc) · 1.22 KB
/
04-pub-sub-basic-client.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
#include <zmq.hpp>
#include <iostream>
#include <sstream>
#include <unistd.h>
int main (int argc, char *argv[])
{
zmq::context_t context (1);
// socket
std::cout << "Collecting updates from weather server...\n" << std::endl;
zmq::socket_t subscriber (context, zmq::socket_type::sub);
subscriber.connect("tcp://localhost:5556");
// Subscribe to zipcode, default is NYC, 10001
const char *filter = (argc > 1)? argv [1]: "10001 ";
subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 20; update_nbr++) { // 20번 반복
zmq::message_t update;
int zipcode, temperature, relhumidity;
subscriber.recv(update, zmq::recv_flags::none);
std::istringstream iss(static_cast<char*>(update.data()));
iss >> zipcode >> temperature >> relhumidity ;
total_temp += temperature;
std::cout << "Receive temperature for zipcode '" << filter << "' was " << temperature << "F" << std::endl;
sleep(1);
}
// 평균 temp 출력
std::cout << "Average temperature for zipcode '"<< filter
<<"' was "<< ((float)total_temp / update_nbr) <<"F"
<< std::endl;
return 0;
}