瀏覽代碼

增加车位分配通信模块

youchen 4 年之前
父節點
當前提交
91b8805cc7

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.vscode/*
+build/*

+ 31 - 0
CMakeLists.txt

@@ -97,6 +97,37 @@ add_executable(locate_test
         ${COMMUNICATION_SRC}
         )
 target_link_libraries(locate_test
+        nnxx
+        nanomsg
+        ${PROTOBUF_LIBRARIES}
+        /usr/local/lib/libglog.a
+        /usr/local/lib/libgflags.a
+        )
+
+# 车位分配测试
+add_executable(parkspace_test
+        ./test/test_parkspace_sample.cpp
+        ./parkspace_allocation/parkspace_allocation_communicator.cpp
+        ./error_code/error_code.cpp
+        ${message_src}
+        ${TOOL_SRC}
+        ${COMMUNICATION_SRC}
+        )
+target_link_libraries(parkspace_test
+        nnxx
+        nanomsg
+        ${PROTOBUF_LIBRARIES}
+        /usr/local/lib/libglog.a
+        /usr/local/lib/libgflags.a
+        )
+add_executable(parkspace_client
+        ./test/parkspace_client.cpp
+        ./error_code/error_code.cpp
+        ${message_src}
+        ${TOOL_SRC}
+        ${COMMUNICATION_SRC}
+        )
+target_link_libraries(parkspace_client
         nnxx
         nanomsg
         ${PROTOBUF_LIBRARIES}

+ 14 - 10
communication/communication_message.h

@@ -22,25 +22,29 @@ public:
 	//消息类型定义,每个在网络上传输的消息必须含有这个属性
 	enum Message_type
 	{
-		eBase_msg=0x00,
-		eCommand_msg=0x01,                      //指令消息
+		eBase_msg = 0x00,
+		eCommand_msg = 0x01, //指令消息
 
-		eLocate_status_msg=0x11,                //定位模块状态消息
-		eLocate_request_msg=0x12,               //定位请求消息
-		eLocate_response_msg=0x13,              //定位反馈消息
+		eLocate_status_msg = 0x11,	 //定位模块状态消息
+		eLocate_request_msg = 0x12,	 //定位请求消息
+		eLocate_response_msg = 0x13, //定位反馈消息
 
-		eHarware_statu_msg=0x21,                //调度模块硬件状态消息
-		eExecute_request_msg=0x22,              //请求调度消息
-		eExecute_response_msg=0x23,             //调度结果反馈消息
+		eHarware_statu_msg = 0x21,	  //调度模块硬件状态消息
+		eExecute_request_msg = 0x22,  //请求调度消息
+		eExecute_response_msg = 0x23, //调度结果反馈消息
+
+		eParkspace_allocation_status_msg = 0x31,   //车位分配模块状态消息,包括车位信息
+		eParkspace_allocation_request_msg = 0x32,  //请求分配车位消息
+		eParkspace_allocation_response_msg = 0x33, //分配车位结果反馈消息
 	};
 
-//通讯单元
+	//通讯单元
 	enum Communicator
 	{
 		eEmpty=0x0000,		//空
 		eMain=0x0001,    	//主流程
 		eTerminor=0x0100,	//终端
-		eTable=0x0200,		//数据表
+		eParkspace_allocator=0x0200,		//数据表
 		eMeasurer=0x0300,	//测量单元
 		eProcess=0x0400,	//调度机构
 		//...

+ 1 - 1
communication/communication_socket_base.cpp

@@ -393,7 +393,7 @@ Error_manager Communication_socket_base::analysis_receive_list()
 					iter = m_receive_data_list.m_data_list.erase(iter);
 					//注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
 
-					//注:消息删除之后, 不需要发送答复消息, 发送方也会有超时处理的,  只有 execute_msg 里面可以答复消息
+					//注:消息删除之后, 不需要发送答复消息, 发送方也会有超时处理的, 只有 execute_msg 里面可以答复消息
 				}
 			}
 		}

+ 4 - 0
error_code/error_code.h

@@ -216,6 +216,10 @@ enum Error_code
 	COMMUNICATION_CONNECT_ERROR,
 	COMMUNICATION_ANALYSIS_TIME_OUT,									//解析超时,
 	COMMUNICATION_EXCUTER_IS_BUSY,										//处理器正忙, 请稍等
+
+    //parkspace allocator,车位分配模块
+    PARKSPACE_ALLOCATOR_ERROR_BASE                  = 0x12010000,      
+    PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR,    //反馈车位消息类型错误
 };
 
 //错误等级,用来做故障处理

+ 14 - 8
message/message_base.pb.cc

@@ -236,20 +236,23 @@ void AddDescriptorsImpl() {
       "\030\004 \001(\002\022\024\n\014locate_width\030\005 \001(\002\022\025\n\rlocate_h"
       "eight\030\006 \001(\002\022\031\n\021locate_wheel_base\030\007 \001(\002\022\032"
       "\n\022locate_wheel_width\030\010 \001(\002\022\026\n\016locate_cor"
-      "rect\030\t \001(\010*\307\001\n\014Message_type\022\r\n\teBase_msg"
+      "rect\030\t \001(\010*\274\002\n\014Message_type\022\r\n\teBase_msg"
       "\020\000\022\020\n\014eCommand_msg\020\001\022\026\n\022eLocate_status_m"
       "sg\020\021\022\027\n\023eLocate_request_msg\020\022\022\030\n\024eLocate"
       "_response_msg\020\023\022\026\n\022eHarware_statu_msg\020!\022"
       "\030\n\024eExecute_request_msg\020\"\022\031\n\025eExecute_re"
-      "sponse_msg\020#*a\n\014Communicator\022\n\n\006eEmpty\020\000"
-      "\022\t\n\005eMain\020\001\022\016\n\teTerminor\020\200\002\022\013\n\006eTable\020\200\004"
-      "\022\016\n\teMeasurer\020\200\006\022\r\n\010eProcess\020\200\010*e\n\013Error"
-      "_level\022\n\n\006NORMAL\020\000\022\024\n\020NEGLIGIBLE_ERROR\020\001"
-      "\022\017\n\013MINOR_ERROR\020\002\022\017\n\013MAJOR_ERROR\020\003\022\022\n\016CR"
-      "ITICAL_ERROR\020\004"
+      "sponse_msg\020#\022$\n eParkspace_allocation_st"
+      "atus_msg\0201\022%\n!eParkspace_allocation_requ"
+      "est_msg\0202\022&\n\"eParkspace_allocation_respo"
+      "nse_msg\0203*o\n\014Communicator\022\n\n\006eEmpty\020\000\022\t\n"
+      "\005eMain\020\001\022\016\n\teTerminor\020\200\002\022\031\n\024eParkspace_a"
+      "llocator\020\200\004\022\016\n\teMeasurer\020\200\006\022\r\n\010eProcess\020"
+      "\200\010*e\n\013Error_level\022\n\n\006NORMAL\020\000\022\024\n\020NEGLIGI"
+      "BLE_ERROR\020\001\022\017\n\013MINOR_ERROR\020\002\022\017\n\013MAJOR_ER"
+      "ROR\020\003\022\022\n\016CRITICAL_ERROR\020\004"
   };
   ::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
-      descriptor, 974);
+      descriptor, 1105);
   ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
     "message_base.proto", &protobuf_RegisterTypes);
 }
@@ -280,6 +283,9 @@ bool Message_type_IsValid(int value) {
     case 33:
     case 34:
     case 35:
+    case 49:
+    case 50:
+    case 51:
       return true;
     default:
       return false;

+ 6 - 3
message/message_base.pb.h

@@ -82,11 +82,14 @@ enum Message_type {
   eLocate_response_msg = 19,
   eHarware_statu_msg = 33,
   eExecute_request_msg = 34,
-  eExecute_response_msg = 35
+  eExecute_response_msg = 35,
+  eParkspace_allocation_status_msg = 49,
+  eParkspace_allocation_request_msg = 50,
+  eParkspace_allocation_response_msg = 51
 };
 bool Message_type_IsValid(int value);
 const Message_type Message_type_MIN = eBase_msg;
-const Message_type Message_type_MAX = eExecute_response_msg;
+const Message_type Message_type_MAX = eParkspace_allocation_response_msg;
 const int Message_type_ARRAYSIZE = Message_type_MAX + 1;
 
 const ::google::protobuf::EnumDescriptor* Message_type_descriptor();
@@ -103,7 +106,7 @@ enum Communicator {
   eEmpty = 0,
   eMain = 1,
   eTerminor = 256,
-  eTable = 512,
+  eParkspace_allocator = 512,
   eMeasurer = 768,
   eProcess = 1024
 };

+ 5 - 3
message/message_base.proto

@@ -17,6 +17,9 @@ enum Message_type
     eExecute_request_msg=0x22;              //请求调度消息
     eExecute_response_msg=0x23;             //调度结果反馈消息
 
+    eParkspace_allocation_status_msg=0x31;  //车位分配模块状态消息,包括车位信息
+    eParkspace_allocation_request_msg=0x32; //请求分配车位消息
+    eParkspace_allocation_response_msg=0x33;//分配车位结果反馈消息
 }
 
 //通讯单元
@@ -26,15 +29,14 @@ enum Communicator
     eMain=0x0001;    //主流程
 
     eTerminor=0x0100;
-    //数据表
-    eTable=0x0200;
+    //车位数据表
+    eParkspace_allocator=0x0200;
     //测量单元
     eMeasurer=0x0300;
     //调度机构
     eProcess=0x0400;
     //...
 
-
 }
 ////base message 用于解析未知类型的消息
 message Base_info

File diff suppressed because it is too large
+ 2321 - 0
message/parkspace_allocation_message.pb.cc


File diff suppressed because it is too large
+ 1588 - 0
message/parkspace_allocation_message.pb.h


+ 55 - 0
message/parkspace_allocation_message.proto

@@ -0,0 +1,55 @@
+syntax = "proto2";
+package message;
+import "message_base.proto";
+
+//车位分配模块请求消息
+message Parkspace_allocation_request_msg
+{
+    required Base_info                  base_info=1;            //消息类型
+    required int32                      command_id=2;           //指令唯一标识符id
+    required int32                      car_length=3;           //车长
+    required int32                      car_width=4;            //车宽
+    required int32                      car_height=5;           //车高
+}
+
+//车位分配模块反馈消息
+message Parkspace_allocation_response_msg
+{
+    required Base_info                  base_info=1;            //消息类型
+    required int32                      command_id=2;           //指令唯一标识符id
+    required Error_manager              error_manager=3;        //分配成功与否标志
+    required int32                      allocated_space_id=4;   //分配车位id
+}
+
+//车位分配模块状态消息
+message Parkspace_allocation_status_msg
+{
+    required Base_info                  base_info=1;            //消息类型
+    required Error_manager              error_manager=2;
+    repeated Parkspace_info             parkspace_info=3;       //车位状态
+}
+
+//车位状态枚举
+enum Parkspace_status
+{
+    eParkspace_empty            = 0;         //空闲,可分配
+    eParkspace_occupied         = 1;         //被占用,不可分配
+    eParkspace_reserverd        = 2;         //被预约,预约车辆可分配
+    eParkspace_error            = 3;         //车位机械结构或硬件故障   
+}
+
+//单个车位基本信息与状态信息
+message Parkspace_info
+{
+    required int32              parkspace_id=1;         //车位编号
+    required int32              x_coordinate=2;         //x坐标
+    required int32              y_coordinate=3;         //y坐标
+    required int32              z_coordinate=4;         //z坐标
+    required int32              length=5;               //车位长
+    required int32              width=6;                //车位宽
+    required int32              height=7;               //车位高
+    required Parkspace_status   parkspace_status=8;     //车位当前状态
+    optional int32              license=9;              //当前车位存入车辆的凭证号
+    optional string             entry_time=10;          //入场时间
+    optional string             leave_time=11;          //离场时间
+}

+ 193 - 0
parkspace_allocation/parkspace_allocation_communicator.cpp

@@ -0,0 +1,193 @@
+//
+// Created by zx on 2020/6/18.
+//
+
+#include "parkspace_allocation_communicator.h"
+
+Parkspace_allocation_communicator::Parkspace_allocation_communicator()
+{
+}
+
+Parkspace_allocation_communicator::~Parkspace_allocation_communicator()
+{
+}
+
+//************************************* 公有函数 ****************************************
+//外部调用发送分配车位结果
+Error_manager Parkspace_allocation_communicator::send_allocation_result(Communication_message *message)
+{
+    return encapsulate_msg(message);
+}
+
+//外部调用更新所有车位信息/初始化所有车位
+Error_manager Parkspace_allocation_communicator::update_parkspace_status(message::Parkspace_allocation_status_msg status_msg)
+{
+    std::lock_guard<std::mutex> lck(m_status_mutex);
+    m_parkspace_status_msg.CopyFrom(status_msg);
+    return SUCCESS;
+}
+
+//外部调用更新单一车位信息
+Error_manager Parkspace_allocation_communicator::update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info)
+{
+    std::lock_guard<std::mutex> lck(m_status_mutex);
+    if(m_parkspace_status_msg.parkspace_info_size() >= parkspace_id)
+    {
+        m_parkspace_status_msg.mutable_parkspace_info(parkspace_id-1)->CopyFrom(parkspace_info);
+    }
+    return SUCCESS;
+}
+
+//外部调用获取当前车位状态消息
+message::Parkspace_allocation_status_msg Parkspace_allocation_communicator::get_status()
+{
+    std::lock_guard<std::mutex> lck(m_status_mutex);
+    message::Parkspace_allocation_status_msg parkspace_status_msg;
+    parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
+    return parkspace_status_msg;
+}
+
+//************************************* 内部函数 ****************************************
+//封装反馈消息自动发送
+Error_manager Parkspace_allocation_communicator::encapsulate_msg(Communication_message *message)
+{
+    if (message == nullptr)
+    {
+        return Error_manager(POINTER_IS_NULL, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message null pointer");
+    }
+    //记录请求
+    if (message->get_message_type() == Communication_message::eParkspace_allocation_response_msg)
+    {
+        message::Parkspace_allocation_response_msg response;
+        bool result = response.ParseFromString(message->get_message_buf());
+        //可增加待发送分配车位反馈消息校核
+        
+    }
+    else
+    {
+        return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "Parkspace_allocation_communicator::message response type error");
+    }
+    //发送请求
+    return Communication_socket_base::encapsulate_msg(message);
+}
+
+//重载执行消息,父类线程通过check_msg接收到车位分配请求后调用,解析内容并进行相应处理
+Error_manager Parkspace_allocation_communicator::execute_msg(Communication_message *p_msg)
+{
+    if (p_msg == nullptr)
+        return Error_manager(POINTER_IS_NULL, CRITICAL_ERROR, "parkspace allocation response msg pointer is null");
+    //车位分配request消息
+    switch (p_msg->get_message_type())
+    {
+        ///测量结果反馈消息
+        case Communication_message::eParkspace_allocation_request_msg:
+        {
+            message::Parkspace_allocation_request_msg request;
+            request.ParseFromString(p_msg->get_message_buf());
+            //根据请求的信息反馈分配的车位,并封装发送
+            //此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试
+            Communication_message* response=new Communication_message();
+            message::Parkspace_allocation_response_msg response_msg;
+            message::Base_info t_response_header;
+            message::Error_manager t_error;
+            t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
+            t_response_header.set_timeout_ms(1000);
+            t_response_header.set_sender(message::Communicator::eParkspace_allocator);
+            t_response_header.set_receiver(message::Communicator::eMain);
+            t_error.set_error_code(0);
+            t_error.set_error_level(message::Error_level::NORMAL);
+            response_msg.mutable_base_info()->CopyFrom(t_response_header);
+            response_msg.mutable_error_manager()->CopyFrom(t_error);
+            response_msg.set_command_id(request.command_id());
+            response_msg.set_allocated_space_id(33);
+            response->reset(t_response_header, response_msg.SerializeAsString());
+            return send_allocation_result(response);
+            // return SUCCESS;
+        }
+    }
+    return Error_manager(PARKSPACE_ALLOCATOR_MSG_RESPONSE_TYPE_ERROR, NEGLIGIBLE_ERROR, "parkspace allocation wrong request type");
+}
+
+//检查消息是否应由本模块接收
+Error_manager Parkspace_allocation_communicator::check_msg(Communication_message* p_msg)
+{
+    //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是车位请求消息.
+	if ( p_msg->get_message_type() == Communication_message::Message_type::eParkspace_allocation_request_msg
+		 && p_msg->get_receiver() == Communication_message::Communicator::eParkspace_allocator)
+	{
+		return Error_code::SUCCESS;
+	}
+	else
+	{
+		//认为接受人
+		return Error_code::INVALID_MESSAGE;
+	}
+}
+
+Error_manager Parkspace_allocation_communicator::check_executer(Communication_message*  p_msg)
+{
+    //检查对应模块的状态, 判断是否可以处理这条消息
+	//同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
+	//如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
+
+	if ( p_msg->is_over_time() )
+	{
+		std::cout << "Communication_socket_base::check_msg  p_buf =  " << p_msg->get_message_buf() << std::endl;
+		std::cout << "Communication_socket_base::check_msg   size =  " << p_msg->get_message_buf().size() << std::endl;
+		std::cout << "COMMUNICATION_ANALYSIS_TIME_OUT , " << std::endl;
+		return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
+	}
+	else
+	{
+		bool executer_is_ready = true;
+		//通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
+		//这里子类重载时, 增加判断逻辑, 以后再写.
+		// std::cout << "Communication_socket_base::check_msg  p_buf =  " << p_msg->get_message_buf() << std::endl;
+		// std::cout << "Communication_socket_base::check_msg   size =  " << p_msg->get_message_buf().size() << std::endl;
+
+		if ( executer_is_ready )
+		{
+			std::cout << "executer_is_ready , " << std::endl;
+			return Error_code::SUCCESS;
+		}
+		else
+		{
+			std::cout << "executer_is_busy , " << std::endl;
+			return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
+		}
+	}
+
+	return Error_code::SUCCESS;
+}
+
+//重载心跳与车位状态发送函数
+Error_manager Parkspace_allocation_communicator::encapsulate_send_data()
+{
+    return Error_code::SUCCESS;
+    std::lock_guard<std::mutex> lck(m_status_mutex);
+    //车位信息消息赋值
+	message::Parkspace_allocation_status_msg t_parkspace_status_msg;
+    t_parkspace_status_msg.CopyFrom(m_parkspace_status_msg);
+    message::Base_info t_base_info;
+    message::Error_manager t_error;
+	t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
+	t_base_info.set_timeout_ms(5000);
+	t_base_info.set_sender(message::Communicator::eParkspace_allocator);
+	t_base_info.set_receiver(message::Communicator::eMain);
+    t_error.set_error_code(0);
+    t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
+    t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
+
+	Communication_message* tp_msg = new Communication_message(t_parkspace_status_msg.SerializeAsString());
+    //重置消息,填入头信息
+    tp_msg->reset(t_base_info, tp_msg->get_message_buf());
+	bool is_push = m_send_data_list.push(tp_msg);
+	if ( is_push == false )
+	{
+		delete(tp_msg);
+		tp_msg = NULL;
+		return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
+							 " Parkspace_allocation_communicator::send status error ");
+	}
+	return Error_code::SUCCESS;
+}

+ 50 - 0
parkspace_allocation/parkspace_allocation_communicator.h

@@ -0,0 +1,50 @@
+//
+// Created by zx on 2020/6/18.
+//
+
+#ifndef PARKSPACE_ALLOCATION_COMMUNICATOR_H
+#define PARKSPACE_ALLOCATION_COMMUNICATOR_H
+
+#include <mutex>
+#include "communication_socket_base.h"
+#include "singleton.h"
+
+#include "parkspace_allocation_message.pb.h"
+#include "error_code.h"
+
+#include "thread_safe_map.h"
+
+class Parkspace_allocation_communicator: public Singleton<Parkspace_allocation_communicator>, public Communication_socket_base
+{
+    friend Singleton<Parkspace_allocation_communicator>;
+public:
+    virtual ~Parkspace_allocation_communicator();
+    //外部调用,反馈结果消息
+    Error_manager send_allocation_result(Communication_message* message);
+    //更新所有车位状态,现有状态将被全部覆盖,车位数量可能变动
+    Error_manager update_parkspace_status(message::Parkspace_allocation_status_msg message);
+    //更新某个车位状态,不修改车位数量,车位按编号从1开始
+    Error_manager update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info);
+    //获取所有车位状态
+    message::Parkspace_allocation_status_msg get_status();
+
+protected:
+    Parkspace_allocation_communicator();
+    //封装反馈消息,加入队列父类自行发送
+    virtual Error_manager encapsulate_msg(Communication_message* message);
+    //解析车位分配请求消息具体内容,激活车位分配单例处理函数(不可阻塞),获得结果后由车位分配单例封装消息发送
+    virtual Error_manager execute_msg(Communication_message* p_msg);
+    //检查是否接收到本模块需要的消息,重载后父类自行调用判断
+    virtual Error_manager check_msg(Communication_message* p_msg); 
+    //重载检查执行者函数,判断执行者状态
+    virtual Error_manager check_executer(Communication_message*  p_msg);
+    //心跳与车位实时信息
+    virtual Error_manager encapsulate_send_data();
+
+protected:
+    message::Parkspace_allocation_status_msg                                m_parkspace_status_msg;
+    std::mutex                                                              m_status_mutex;
+};
+
+
+#endif //PARKSPACE_ALLOCATION_COMMUNICATOR_H

+ 2 - 1
proto.sh

@@ -1,3 +1,4 @@
 protoc -I=./message message_base.proto --cpp_out=./message
 protoc -I=./message measure_message.proto --cpp_out=./message
-protoc -I=./message hardware_message.proto --cpp_out=./message
+protoc -I=./message hardware_message.proto --cpp_out=./message
+protoc -I=./message parkspace_allocation_message.proto --cpp_out=./message

+ 71 - 0
test/parkspace_client.cpp

@@ -0,0 +1,71 @@
+/*
+ * @Description: 模拟主程序发送车位分配请求,并监听状态信息
+ * @Author: yct
+ * @Date: 2020-07-08 15:44:43
+ * @LastEditTime: 2020-07-08 18:03:59
+ * @LastEditors: yct
+ */
+
+#include <iostream>
+#include <nnxx/message>
+#include <nnxx/socket.h>
+#include <nnxx/bus.h>
+#include "unistd.h"
+
+#include "parkspace_allocation_message.pb.h"
+#include <thread>
+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<std::string>(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);
+    }
+}

+ 60 - 0
test/test_parkspace_sample.cpp

@@ -0,0 +1,60 @@
+/*
+ * @Description: 测试车位分配模块通信情况
+ * @Author: yct
+ * @Date: 2020-07-08 15:51:46
+ * @LastEditTime: 2020-07-08 17:38:21
+ * @LastEditors: yct
+ */ 
+#include "../parkspace_allocation/parkspace_allocation_communicator.h"
+
+int main()
+{
+    Parkspace_allocation_communicator *p_parkspace_allocator = Parkspace_allocation_communicator::get_instance_pointer();
+    
+    p_parkspace_allocator->communication_bind("tcp://127.0.0.1:7001");
+    p_parkspace_allocator->communication_connect("tcp://127.0.0.1:7000");
+    p_parkspace_allocator->communication_run();
+
+    message::Parkspace_allocation_status_msg parkspace_status;
+    for (size_t i = 1; i < 4; i++)
+    {
+        message::Parkspace_info* space = parkspace_status.add_parkspace_info();
+        space->set_parkspace_id(i);
+        space->set_x_coordinate(i*100);
+        space->set_y_coordinate(i*200);
+        space->set_z_coordinate(i*300);
+        space->set_length(5500);
+        space->set_width(2200);
+        space->set_height(1750);
+        space->set_parkspace_status(message::Parkspace_status::eParkspace_empty);
+    }
+    message::Base_info base_msg;
+    message::Error_manager error;
+    base_msg.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
+    base_msg.set_timeout_ms(1000);
+    base_msg.set_sender(message::Communicator::eParkspace_allocator);
+    base_msg.set_receiver(message::Communicator::eMain);
+    error.set_error_code(0);
+    parkspace_status.mutable_base_info()->CopyFrom(base_msg);
+    parkspace_status.mutable_error_manager()->CopyFrom(error);
+    p_parkspace_allocator->update_parkspace_status(parkspace_status);
+
+    int k=1;
+
+    while(1) {
+        message::Parkspace_info space;
+        space.set_parkspace_id(3);
+        space.set_x_coordinate(k*100);
+        space.set_y_coordinate(k*200);
+        space.set_z_coordinate(k*300);
+        space.set_length(5500);
+        space.set_width(2200);
+        space.set_height(k*400);
+        space.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
+        p_parkspace_allocator->update_parkspace_status(3, space);
+        usleep(1000*1000);
+        k++;
+    }
+
+    return 0;
+}