// Copyright 2022 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source // License, and you may not use this file except in compliance with the Business Source License. // // As of the Change Date specified in that file, in accordance with // the Business Source License, use of this software will be governed // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. //#include #include #include #include #include "io/v3/simulator.hpp" #include "utils/terminate_handler.hpp" struct RequestMsg { std::string data; std::vector Serialize() { return std::vector(); } static RequestMsg Deserialize(uint8_t *ptr, size_t len) { return RequestMsg{}; } }; struct ResponseMsg { std::string data; std::vector Serialize() { return std::vector(); } static ResponseMsg Deserialize(uint8_t *ptr, size_t len) { return ResponseMsg{}; } }; struct CounterState { uint64_t highest_seen_; }; struct CounterRequest { uint64_t proposal_; }; struct CounterResponse { uint64_t highest_seen_; }; void run_server(Io srv_io) { CounterState state{}; while (!srv_io.ShouldShutDown()) { auto request_result = srv_io.Receive(); if (request_result.HasError()) { continue; } auto request_envelope = request_result.GetValue(); auto req = std::get(request_envelope.message); state.highest_seen_ = std::max(state.highest_seen_, req.proposal_); auto srv_res = CounterResponse{state.highest_seen_}; request_envelope.Reply(srv_res, srv_io); } } int main() { auto simulator = Simulator(); auto cli_addr = Address::TestAddress(1); auto srv_addr = Address::TestAddress(2); Io cli_io = simulator.Register(cli_addr, false); Io srv_io = simulator.Register(srv_addr, true); auto srv_thread = std::jthread(run_server, std::move(srv_io)); // send request CounterRequest cli_req; cli_req.proposal_ = 1; auto response_future = cli_io.Request(srv_addr, cli_req); // receive response auto response_result = response_future.Wait(); auto response_envelope = response_result.GetValue(); MG_ASSERT(response_envelope.message.highest_seen_ == 1); std::cout << "IT WORKED :)" << std::endl; simulator.ShutDown(); std::cout << "joining" << std::endl; srv_thread.join(); std::cout << "exiting" << std::endl; return 0; }