hyunjin

add test/

1 +cmake_minimum_required(VERSION 3.0)
2 +project(zmqrpc)
3 +find_package(Git REQUIRED)
4 +if(CMAKE_TOOLCHAIN_FILE)
5 +set(TOOLCHAIN "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
6 +else()
7 +set(TOOLCHAIN "")
8 +endif()
9 +
10 +
11 +project(zmqrpc)
12 +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
13 + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.2)
14 + message(FATAL_ERROR "GCC version must be at least 7.2!")
15 + endif()
16 +endif()
17 +
18 +add_compile_options(-std=c++17)
19 +add_compile_options(-O4)
20 +
21 +include(ExternalProject)
22 +
23 +ExternalProject_Add (
24 + cppzmq
25 + GIT_REPOSITORY https://github.com/zeromq/cppzmq.git
26 + GIT_TAG v4.3.0
27 + UPDATE_COMMAND ""
28 + INSTALL_COMMAND ""
29 + CONFIGURE_COMMAND ""
30 + BUILD_COMMAND ""
31 + CMAKE_ARGS "${TOOLCHAIN}"
32 + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/cppzmq"
33 +)
34 +
35 +
36 +ExternalProject_Add (
37 + zmq
38 + BUILD_IN_SOURCE 1
39 + URL https://github.com/zeromq/libzmq/releases/download/v4.3.1/zeromq-4.3.1.tar.gz
40 + UPDATE_COMMAND ""
41 + PATCH_COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/patches/ip_resolver.cpp ${CMAKE_CURRENT_BINARY_DIR}/external/libzmq/src/zmq/src/
42 + INSTALL_COMMAND make install
43 + CONFIGURE_COMMAND ./configure --without-docs --enable-static --prefix=${CMAKE_CURRENT_BINARY_DIR}/external/libzmq CXX=${CMAKE_CXX_COMPILER} CC=${CMAKE_C_COMPILER} ${CONFIG_HOST}
44 + PREFIX "${CMAKE_CURRENT_BINARY_DIR}/external/libzmq"
45 + BUILD_COMMAND make -j8
46 +)
47 +
48 +
49 +add_library(libzmq STATIC IMPORTED)
50 +set_target_properties(libzmq PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/external/libzmq/lib/libzmq.a)
51 +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
52 +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/variant/include)
53 +include_directories(${CMAKE_CURRENT_BINARY_DIR}/external/libzmq/include)
54 +include_directories(${CMAKE_CURRENT_BINARY_DIR}/external/cppzmq/src/cppzmq)
55 +
56 +add_executable(server test/server.cpp)
57 +target_link_libraries(server libzmq pthread rt)
58 +add_dependencies(server zmq cppzmq)
59 +
60 +add_executable(client test/client.cpp)
61 +target_link_libraries(client libzmq pthread rt)
62 +add_dependencies(client zmq cppzmq)
63 +
64 +
This diff is collapsed. Click to expand it.
1 +set(CMAKE_C_COMPILER "/usr/bin/cc")
2 +set(CMAKE_C_COMPILER_ARG1 "")
3 +set(CMAKE_C_COMPILER_ID "GNU")
4 +set(CMAKE_C_COMPILER_VERSION "5.4.0")
5 +set(CMAKE_C_COMPILER_VERSION_INTERNAL "")
6 +set(CMAKE_C_COMPILER_WRAPPER "")
7 +set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "11")
8 +set(CMAKE_C_EXTENSIONS_COMPUTED_DEFAULT "ON")
9 +set(CMAKE_C_COMPILE_FEATURES "c_std_90;c_function_prototypes;c_std_99;c_restrict;c_variadic_macros;c_std_11;c_static_assert")
10 +set(CMAKE_C90_COMPILE_FEATURES "c_std_90;c_function_prototypes")
11 +set(CMAKE_C99_COMPILE_FEATURES "c_std_99;c_restrict;c_variadic_macros")
12 +set(CMAKE_C11_COMPILE_FEATURES "c_std_11;c_static_assert")
13 +set(CMAKE_C17_COMPILE_FEATURES "")
14 +set(CMAKE_C23_COMPILE_FEATURES "")
15 +
16 +set(CMAKE_C_PLATFORM_ID "Linux")
17 +set(CMAKE_C_SIMULATE_ID "")
18 +set(CMAKE_C_COMPILER_FRONTEND_VARIANT "")
19 +set(CMAKE_C_SIMULATE_VERSION "")
20 +
21 +
22 +
23 +
24 +set(CMAKE_AR "/usr/bin/ar")
25 +set(CMAKE_C_COMPILER_AR "/usr/bin/gcc-ar-5")
26 +set(CMAKE_RANLIB "/usr/bin/ranlib")
27 +set(CMAKE_C_COMPILER_RANLIB "/usr/bin/gcc-ranlib-5")
28 +set(CMAKE_LINKER "/usr/bin/ld")
29 +set(CMAKE_MT "")
30 +set(CMAKE_COMPILER_IS_GNUCC 1)
31 +set(CMAKE_C_COMPILER_LOADED 1)
32 +set(CMAKE_C_COMPILER_WORKS TRUE)
33 +set(CMAKE_C_ABI_COMPILED TRUE)
34 +
35 +set(CMAKE_C_COMPILER_ENV_VAR "CC")
36 +
37 +set(CMAKE_C_COMPILER_ID_RUN 1)
38 +set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)
39 +set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC)
40 +set(CMAKE_C_LINKER_PREFERENCE 10)
41 +
42 +# Save compiler ABI information.
43 +set(CMAKE_C_SIZEOF_DATA_PTR "8")
44 +set(CMAKE_C_COMPILER_ABI "ELF")
45 +set(CMAKE_C_BYTE_ORDER "LITTLE_ENDIAN")
46 +set(CMAKE_C_LIBRARY_ARCHITECTURE "x86_64-linux-gnu")
47 +
48 +if(CMAKE_C_SIZEOF_DATA_PTR)
49 + set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}")
50 +endif()
51 +
52 +if(CMAKE_C_COMPILER_ABI)
53 + set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}")
54 +endif()
55 +
56 +if(CMAKE_C_LIBRARY_ARCHITECTURE)
57 + set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu")
58 +endif()
59 +
60 +set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "")
61 +if(CMAKE_C_CL_SHOWINCLUDES_PREFIX)
62 + set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}")
63 +endif()
64 +
65 +
66 +
67 +
68 +
69 +set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/5/include;/usr/local/include;/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed;/usr/include/x86_64-linux-gnu;/usr/include")
70 +set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "gcc;gcc_s;c;gcc;gcc_s")
71 +set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/5;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib")
72 +set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
1 +set(CMAKE_CXX_COMPILER "/usr/bin/c++")
2 +set(CMAKE_CXX_COMPILER_ARG1 "")
3 +set(CMAKE_CXX_COMPILER_ID "GNU")
4 +set(CMAKE_CXX_COMPILER_VERSION "5.4.0")
5 +set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "")
6 +set(CMAKE_CXX_COMPILER_WRAPPER "")
7 +set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "98")
8 +set(CMAKE_CXX_EXTENSIONS_COMPUTED_DEFAULT "ON")
9 +set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17")
10 +set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters")
11 +set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates")
12 +set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates")
13 +set(CMAKE_CXX17_COMPILE_FEATURES "cxx_std_17")
14 +set(CMAKE_CXX20_COMPILE_FEATURES "")
15 +set(CMAKE_CXX23_COMPILE_FEATURES "")
16 +
17 +set(CMAKE_CXX_PLATFORM_ID "Linux")
18 +set(CMAKE_CXX_SIMULATE_ID "")
19 +set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "")
20 +set(CMAKE_CXX_SIMULATE_VERSION "")
21 +
22 +
23 +
24 +
25 +set(CMAKE_AR "/usr/bin/ar")
26 +set(CMAKE_CXX_COMPILER_AR "/usr/bin/gcc-ar-5")
27 +set(CMAKE_RANLIB "/usr/bin/ranlib")
28 +set(CMAKE_CXX_COMPILER_RANLIB "/usr/bin/gcc-ranlib-5")
29 +set(CMAKE_LINKER "/usr/bin/ld")
30 +set(CMAKE_MT "")
31 +set(CMAKE_COMPILER_IS_GNUCXX 1)
32 +set(CMAKE_CXX_COMPILER_LOADED 1)
33 +set(CMAKE_CXX_COMPILER_WORKS TRUE)
34 +set(CMAKE_CXX_ABI_COMPILED TRUE)
35 +
36 +set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")
37 +
38 +set(CMAKE_CXX_COMPILER_ID_RUN 1)
39 +set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;m;mm;mpp;CPP;ixx;cppm)
40 +set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
41 +
42 +foreach (lang C OBJC OBJCXX)
43 + if (CMAKE_${lang}_COMPILER_ID_RUN)
44 + foreach(extension IN LISTS CMAKE_${lang}_SOURCE_FILE_EXTENSIONS)
45 + list(REMOVE_ITEM CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${extension})
46 + endforeach()
47 + endif()
48 +endforeach()
49 +
50 +set(CMAKE_CXX_LINKER_PREFERENCE 30)
51 +set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1)
52 +
53 +# Save compiler ABI information.
54 +set(CMAKE_CXX_SIZEOF_DATA_PTR "8")
55 +set(CMAKE_CXX_COMPILER_ABI "ELF")
56 +set(CMAKE_CXX_BYTE_ORDER "LITTLE_ENDIAN")
57 +set(CMAKE_CXX_LIBRARY_ARCHITECTURE "x86_64-linux-gnu")
58 +
59 +if(CMAKE_CXX_SIZEOF_DATA_PTR)
60 + set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}")
61 +endif()
62 +
63 +if(CMAKE_CXX_COMPILER_ABI)
64 + set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}")
65 +endif()
66 +
67 +if(CMAKE_CXX_LIBRARY_ARCHITECTURE)
68 + set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu")
69 +endif()
70 +
71 +set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "")
72 +if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX)
73 + set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}")
74 +endif()
75 +
76 +
77 +
78 +
79 +
80 +set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "/usr/include/c++/5;/usr/include/x86_64-linux-gnu/c++/5;/usr/include/c++/5/backward;/usr/lib/gcc/x86_64-linux-gnu/5/include;/usr/local/include;/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed;/usr/include/x86_64-linux-gnu;/usr/include")
81 +set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "stdc++;m;gcc_s;gcc;c;gcc_s;gcc")
82 +set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/5;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib")
83 +set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
1 +set(CMAKE_HOST_SYSTEM "Linux-4.15.0-112-generic")
2 +set(CMAKE_HOST_SYSTEM_NAME "Linux")
3 +set(CMAKE_HOST_SYSTEM_VERSION "4.15.0-112-generic")
4 +set(CMAKE_HOST_SYSTEM_PROCESSOR "x86_64")
5 +
6 +
7 +
8 +set(CMAKE_SYSTEM "Linux-4.15.0-112-generic")
9 +set(CMAKE_SYSTEM_NAME "Linux")
10 +set(CMAKE_SYSTEM_VERSION "4.15.0-112-generic")
11 +set(CMAKE_SYSTEM_PROCESSOR "x86_64")
12 +
13 +set(CMAKE_CROSSCOMPILING "FALSE")
14 +
15 +set(CMAKE_SYSTEM_LOADED 1)
This diff is collapsed. Click to expand it.
No preview for this file type
This diff is collapsed. Click to expand it.
1 +# This file is generated by cmake for dependency checking of the CMakeCache.txt file
No preview for this file type
No preview for this file type
1 +#include <vector>
2 +#include <thread>
3 +#include <memory>
4 +#include <functional>
5 +
6 +#include <zmq.hpp>
7 +#include <zhelpers.hpp>
8 +
9 +class client_task {
10 +public:
11 + client_task()
12 + : ctx_(1),
13 + client_socket_(ctx_, ZMQ_DEALER)
14 + {}
15 +
16 + void start() {
17 + char identity[10] = {};
18 + sprintf(identity, "%04X-%04X", within(0x10000), within(0x10000));
19 + printf("%s\n", identity);
20 + client_socket_.setsockopt(ZMQ_IDENTITY, identity, strlen(identity));
21 + client_socket_.connect("tcp://localhost:5570");
22 +
23 + zmq::pollitem_t items[] = {
24 + { client_socket_, 0, ZMQ_POLLIN, 0 } };
25 + int request_nbr = 0;
26 + try {
27 + while (true) {
28 + for (int i = 0; i < 100; ++i) {
29 + zmq::poll(items, 1, 10);
30 + if (items[0].revents & ZMQ_POLLIN) {
31 + printf("\n%s ", identity);
32 + s_dump(client_socket_);
33 + }
34 + }
35 + char request_string[16] = {};
36 + sprintf(request_string, "request #%d", ++request_nbr);
37 + client_socket_.send(request_string, strlen(request_string));
38 + }
39 + }
40 + catch (std::exception &e) {}
41 + }
42 +
43 +private:
44 + zmq::context_t ctx_;
45 + zmq::socket_t client_socket_;
46 +};
47 +
48 +int main(void){
49 + client_task ct1;
50 + client_task ct2;
51 + client_task ct3;
52 +}
53 +
1 +#include <vector>
2 +#include <thread>
3 +#include <memory>
4 +#include <functional>
5 +
6 +#include <zmq.hpp>
7 +#include <zhelpers.hpp>
8 +
9 +
10 +class server_worker {
11 +public:
12 + server_worker(zmq::context_t &ctx, int sock_type)
13 + : ctx_(ctx),
14 + worker_(ctx_, sock_type)
15 + {}
16 +
17 + void work() {
18 + worker_.connect("inproc://backend");
19 +
20 + try {
21 + while (true) {
22 + zmq::message_t identity;
23 + zmq::message_t msg;
24 + zmq::message_t copied_id;
25 + zmq::message_t copied_msg;
26 + worker_.recv(&identity);
27 + worker_.recv(&msg);
28 +
29 + int replies = within(5);
30 + for (int reply = 0; reply < replies; ++reply) {
31 + s_sleep(within(1000) + 1);
32 + copied_id.copy(&identity);
33 + copied_msg.copy(&msg);
34 + worker_.send(copied_id, ZMQ_SNDMORE);
35 + worker_.send(copied_msg);
36 + }
37 + }
38 + }
39 + catch (std::exception &e) {}
40 + }
41 +
42 +
43 +
44 +private:
45 + zmq::context_t &ctx_;
46 + zmq::socket_t worker_;
47 +};
48 +
49 +
50 +class server_task {
51 +public:
52 + server_task()
53 + : ctx_(1),
54 + frontend_(ctx_, ZMQ_ROUTER),
55 + backend_(ctx_, ZMQ_DEALER)
56 + {}
57 +
58 + enum { kMaxThread = 5 };
59 +
60 + void run() {
61 + frontend_.bind("tcp://*:5570");
62 + backend_.bind("inproc://backend");
63 +
64 + std::vector<server_worker *> worker;
65 + std::vector<std::thread *> worker_thread;
66 + for (int i = 0; i < kMaxThread; ++i) {
67 + worker.push_back(new server_worker(ctx_, ZMQ_DEALER));
68 +
69 + worker_thread.push_back(new std::thread(std::bind(&server_worker::work, worker[i])));
70 + worker_thread[i]->detach();
71 + }
72 +
73 +
74 + try {
75 + zmq::proxy(static_cast<void*>(frontend_),
76 + static_cast<void*>(backend_),
77 + nullptr);
78 + }
79 + catch (std::exception &e) {}
80 +
81 + for (int i = 0; i < kMaxThread; ++i) {
82 + delete worker[i];
83 + delete worker_thread[i];
84 + }
85 + }
86 +
87 +
88 +private:
89 + zmq::context_t ctx_;
90 + zmq::socket_t frontend_;
91 + zmq::socket_t backend_;
92 +};
93 +
94 +
95 +int main(void){
96 + server_task st;
97 +
98 + /*server = ServerTask(3)
99 + server.start()
100 + server.join()
101 +
102 + std::thread t1(std::bind(&client_task::start, &ct1));
103 + std::thread t2(std::bind(&client_task::start, &ct2));
104 + std::thread t3(std::bind(&client_task::start, &ct3));
105 + std::thread t4(std::bind(&server_task::run, &st));
106 +
107 + t1.detach();
108 + t2.detach();
109 + t3.detach();
110 + t4.detach();*/
111 +
112 +}
113 +
No preview for this file type
1 +#include <zmq.hpp>
2 +#include <iostream>
3 +#include <sstream>
4 +
5 +int main (int argc, char *argv[])
6 +{
7 + zmq::context_t context (1);
8 +
9 + std::cout << "Collecting updates from weather server..." << std::endl;
10 + zmq::socket_t subscriber (context, zmq::socket_type::sub);
11 + subscriber.connect("tcp://localhost:5556");
12 +
13 + const char *filter = (argc > 1)? argv [1]: "10001 ";
14 + subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen(filter));
15 +
16 + int update_nbr;
17 + long total_temp = 0;
18 + for (update_nbr = 0; update_nbr < 20; update_nbr++) {
19 +
20 + zmq::message_t update;
21 + int zipcode, temperature, relhumidity;
22 +
23 + subscriber.recv(update, zmq::recv_flags::none);
24 +
25 + std::istringstream iss(static_cast<char*>(update.data()));
26 + iss >> zipcode >> temperature >> relhumidity ;
27 +
28 + total_temp += temperature;
29 +
30 + std::cout << "Receive temperature for zipcode '"<< filter <<"' was "<<(int) (temperature) <<"F"<< std::endl;
31 + }
32 +
33 + std::cout << "Average temperature for zipcode '"<< filter
34 + <<"' was "<<(int) (total_temp / update_nbr) <<"F"
35 + << std::endl;
36 + return 0;
37 +}
No preview for this file type
1 +#include <zmq.hpp>
2 +#include <stdio.h>
3 +#include <stdlib.h>
4 +#include <iostream>
5 +#include <time.h>
6 +#include <unistd.h>
7 +#if (defined (WIN32))
8 +#include <zhelpers.hpp>
9 +#endif
10 +
11 +#define within(num) (int) ((float) num * random () / (RAND_MAX + 1.0))
12 +
13 +int main () {
14 +
15 + std::cout<<"Publishing updates at weather server..."<<std::endl;
16 +
17 + zmq::context_t context (1);
18 + zmq::socket_t publisher (context, zmq::socket_type::pub);
19 + publisher.bind("tcp://*:5556");
20 + publisher.bind("ipc://weather.ipc");
21 +
22 + srandom ((unsigned) time (NULL));
23 + while (1) {
24 +
25 + int zipcode, temperature, relhumidity;
26 +
27 + zipcode = within (100000);
28 + temperature = within (215) - 80;
29 + relhumidity = within (50) + 10;
30 +
31 + zmq::message_t message(20);
32 + snprintf ((char *) message.data(), 20 ,
33 + "%05d %d %d", zipcode, temperature, relhumidity);
34 + publisher.send(message, zmq::send_flags::none);
35 +
36 +
37 + }
38 + return 0;
39 +}
No preview for this file type
No preview for this file type
1 +#include <zmq.hpp>
2 +#include <string>
3 +#include <iostream>
4 +
5 +int main ()
6 +{
7 + zmq::context_t context (1);
8 + zmq::socket_t socket (context, zmq::socket_type::req);
9 +
10 + std::cout << "Connecting to hello world server..." << std::endl;
11 + socket.connect ("tcp://localhost:5555");
12 +
13 + for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
14 + zmq::message_t request (5);
15 + memcpy (request.data (), "Hello", 5);
16 + std::cout << "Sending request " << request_nbr << "..." << std::endl;
17 + socket.send (request, zmq::send_flags::none);
18 +
19 + zmq::message_t reply;
20 + socket.recv (reply, zmq::recv_flags::none);
21 + std::cout << "Received reply " << request_nbr << std::endl;
22 + }
23 + return 0;
24 +}
No preview for this file type
1 +#include <zmq.hpp>
2 +#include <string>
3 +#include <iostream>
4 +#include <unistd.h>
5 +
6 +int main () {
7 + zmq::context_t context (1);
8 + zmq::socket_t socket (context, zmq::socket_type::rep);
9 + socket.bind ("tcp://*:5555");
10 +
11 + while (true) {
12 + zmq::message_t request;
13 + socket.recv (request, zmq::recv_flags::none);
14 + std::cout << "Received request" << std::endl;
15 +
16 + sleep(1);
17 +
18 + zmq::message_t reply (5);
19 + memcpy (reply.data (), "World", 5);
20 + socket.send (reply, zmq::send_flags::none);
21 + }
22 + return 0;
23 +}
This file is too large to display.