parkspace_client.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * @Description: 模拟主程序发送车位分配请求,并监听状态信息
  3. * @Author: yct
  4. * @Date: 2020-07-08 15:44:43
  5. * @LastEditTime: 2020-07-08 18:03:59
  6. * @LastEditors: yct
  7. */
  8. #include <iostream>
  9. #include <nnxx/message>
  10. #include <nnxx/socket.h>
  11. #include <nnxx/bus.h>
  12. #include "unistd.h"
  13. #include "parkspace_allocation_message.pb.h"
  14. #include <thread>
  15. int main()
  16. {
  17. nnxx::socket socket{nnxx::SP, nnxx::BUS};
  18. socket.bind("tcp://127.0.0.1:7000");
  19. socket.connect("tcp://127.0.0.1:7001");
  20. int n = 0;
  21. message::Parkspace_allocation_status_msg parkspace_status;
  22. message::Parkspace_allocation_response_msg response;
  23. message::Base_info base_info;
  24. while (1)
  25. {
  26. // 发送车位分配请求
  27. message::Error_manager error_code;
  28. message::Base_info base_msg;
  29. message::Parkspace_allocation_request_msg request;
  30. base_msg.set_msg_type(message::Message_type::eParkspace_allocation_request_msg);
  31. base_msg.set_sender(message::eMain);
  32. base_msg.set_receiver(message::eParkspace_allocator);
  33. base_msg.set_timeout_ms(2000);
  34. request.mutable_base_info()->CopyFrom(base_msg);
  35. request.set_command_id(n);
  36. request.set_car_length(4500);
  37. request.set_car_width(1800);
  38. request.set_car_height(1500);
  39. socket.send(request.SerializeAsString());
  40. // 接收消息
  41. std::string t_receive_string = socket.recv<std::string>(1);
  42. if (t_receive_string.length() > 0)
  43. {
  44. base_info.ParseFromString(t_receive_string);
  45. // 接收并打印车位状态信息
  46. std::cout << "====================================================" << std::endl;
  47. std::cout << "cycle " << n << std::endl;
  48. if(base_info.msg_type() == message::Message_type::eParkspace_allocation_status_msg)
  49. {
  50. parkspace_status.ParseFromString(t_receive_string);
  51. std::cout << parkspace_status.DebugString() << std::endl;
  52. // continue;
  53. }
  54. else
  55. {
  56. response.ParseFromString(t_receive_string);
  57. std::cout<< "----------------- 分配车位结果 -----------------" << std::endl;
  58. std::cout << response.DebugString() << std::endl;
  59. }
  60. }
  61. // std::this_thread::yield();
  62. n++;
  63. usleep(1000*1000);
  64. }
  65. }