|
| 1 | +// Copyright (c) 2020 by Chrono |
| 2 | +// |
| 3 | +// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -o c.out;./c.out |
| 4 | +// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o c.out |
| 5 | +// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o c.out;./c.out |
| 6 | + |
| 7 | +//#include <iostream> |
| 8 | + |
| 9 | +#include "cpplang.hpp" |
| 10 | +#include "SalesData.hpp" |
| 11 | +#include "SpinLock.hpp" |
| 12 | +#include "Zmq.hpp" |
| 13 | + |
| 14 | +// you should put json.hpp in ../common |
| 15 | +#include "json.hpp" |
| 16 | + |
| 17 | +static |
| 18 | +auto debug_print = [](auto& b) |
| 19 | +{ |
| 20 | + using json_t = nlohmann::json; |
| 21 | + |
| 22 | + json_t j; |
| 23 | + |
| 24 | + j["id"] = b.id(); |
| 25 | + j["sold"] = b.sold(); |
| 26 | + j["revenue"] = b.revenue(); |
| 27 | + //j["average"] = b.average(); |
| 28 | + |
| 29 | + std::cout << j.dump(2) << std::endl; |
| 30 | +}; |
| 31 | + |
| 32 | +int main() |
| 33 | +try |
| 34 | +{ |
| 35 | + USING_NAMESPACE(std); |
| 36 | + USING_NAMESPACE(cpp_study); |
| 37 | + |
| 38 | + cout << "hello cpp_study" << endl; |
| 39 | + |
| 40 | + // sales data |
| 41 | + |
| 42 | + SalesData book1("isbn001"); |
| 43 | + |
| 44 | + book1.inc_sold(10); |
| 45 | + book1.inc_revenue(100); |
| 46 | + |
| 47 | + debug_print(book1); |
| 48 | + |
| 49 | + auto buf = book1.pack(); |
| 50 | + cout << buf.size() << endl; |
| 51 | + |
| 52 | + // zmq send |
| 53 | + |
| 54 | + using zmq_ctx = ZmqContext<1>; |
| 55 | + |
| 56 | + auto sock = zmq_ctx::send_sock(); |
| 57 | + |
| 58 | + sock.connect("tcp://127.0.0.1:5555"); |
| 59 | + assert(sock.connected()); |
| 60 | + |
| 61 | + auto len = sock.send(buf.data(), buf.size()); |
| 62 | + assert(len == buf.size()); |
| 63 | + |
| 64 | + cout << "send len = " << len << endl; |
| 65 | +} |
| 66 | +catch(std::exception& e) |
| 67 | +{ |
| 68 | + std::cerr << e.what() << std::endl; |
| 69 | +} |
0 commit comments