/* * @Description: 模拟主程序发送车位分配请求,并监听状态信息 * @Author: yct * @Date: 2020-07-08 15:44:43 * @LastEditTime: 2020-07-08 18:03:59 * @LastEditors: yct */ #include #include #include #include #include "unistd.h" #include "parkspace_allocation_message.pb.h" #include int main() { nnxx::socket socket{nnxx::SP, nnxx::BUS}; socket.bind("tcp://127.0.0.1:7000"); socket.connect("tcp://127.0.0.1:7001"); int n = 0; message::Parkspace_allocation_status_msg parkspace_status; message::Parkspace_allocation_response_msg response; message::Base_info base_info; while (1) { // 发送车位分配请求 message::Error_manager error_code; message::Base_info base_msg; message::Parkspace_allocation_request_msg request; base_msg.set_msg_type(message::Message_type::eParkspace_allocation_request_msg); base_msg.set_sender(message::eMain); base_msg.set_receiver(message::eParkspace_allocator); base_msg.set_timeout_ms(2000); request.mutable_base_info()->CopyFrom(base_msg); request.set_command_id(n); request.set_car_length(4500); request.set_car_width(1800); request.set_car_height(1500); socket.send(request.SerializeAsString()); // 接收消息 std::string t_receive_string = socket.recv(1); if (t_receive_string.length() > 0) { base_info.ParseFromString(t_receive_string); // 接收并打印车位状态信息 std::cout << "====================================================" << std::endl; std::cout << "cycle " << n << std::endl; if(base_info.msg_type() == message::Message_type::eParkspace_allocation_status_msg) { parkspace_status.ParseFromString(t_receive_string); std::cout << parkspace_status.DebugString() << std::endl; // continue; } else { response.ParseFromString(t_receive_string); std::cout<< "----------------- 分配车位结果 -----------------" << std::endl; std::cout << response.DebugString() << std::endl; } } // std::this_thread::yield(); n++; usleep(1000*1000); } }