A data interaction method and system

Through the data structure definition file in proto format and the serialization technology of Protobuf, the complexity and efficiency problems of data interaction in the existing technology are solved, and efficient data transmission and simplified development process are achieved.

CN118642873BActive Publication Date: 2024-10-29CHENGDU CELIS TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411117887.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-15
Publication Date
2024-10-29
Estimated Expiration
2044-08-15

Smart Images

  • Figure CN118642873B_ABST
    Figure CN118642873B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of communication technology, and in particular, to a data interaction method and system. The system includes a creation module, which is used to configure the subscribed topic, initialize key variables, and simultaneously initialize the publisher and subscriber through an initialization function; the publisher is used to read a data structure definition file in proto format; serialize the business data according to the data structure definition file; transmit the serialized data to the subscriber through a zero message queue; the subscriber is used to read the data structure definition file; subscribe to the serialized data; and deserialize the serialized data according to the data structure definition file. In the present invention, the publisher and subscriber read the data structure definition file in proto format, and serialize and deserialize the business data according to the data structure definition file, so that there is no need to define the structure of the data in an interactive manner, thereby realizing efficient data interaction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technologies, and in particular to a data interaction method and system. Background Art

[0002] In modern computer networks, data interaction is a basic and important task. With the development of the Internet, there are more and more scenarios for data interaction, such as remote data access, distributed systems, and real-time data processing. In these scenarios, data transmission and reception are key links, and their efficiency and reliability directly affect the performance of the system and user experience. Therefore, how to efficiently and reliably interact with data has always been an important research direction in the field of computer networks. Among them, Zero Message Queue (ZeroMQ, also known as zmq) is an event-driven asynchronous messaging framework that implements data distribution through a publish / subscribe model.

[0003] The existing zero-message queue-based distribution mechanism requires custom interaction data structures in the code. Since the data transmission format needs to be negotiated and determined by both parties, the implementation complexity will increase significantly when there are many parties. Moreover, if the data format customized by both parties is directly used for transmission, the efficiency of data transmission will be affected.

[0004] In view of this, the present invention is proposed. Summary of the invention

[0005] The purpose of the present invention is to provide a data interaction method and system, which simultaneously initializes the publisher and the subscriber, and the publisher and the subscriber read the data structure definition file in the proto format, and serialize and deserialize the business data according to the data structure definition file, without the need to define the data structure in an interactive manner; efficient data interaction is achieved through zero message queue transmission.

[0006] In order to achieve the above object, the present invention adopts the following technical solutions:

[0007] In a first aspect, the present invention provides a data interaction system, comprising:

[0008] Create a module for configuring the subscribed topic, initializing key variables, and simultaneously initializing the publisher and subscriber through an initialization function; wherein the initialization function is used to initialize the publishing and subscription environment, and initialize the data structure;

[0009] The publishing end is used to read the data structure definition file in proto format; and

[0010] Serializing the business data according to the data structure definition file; transmitting the serialized data to the subscriber through the zero message queue;

[0011] The subscriber is used to read the data structure definition file in proto format; and

[0012] Subscribing to the serialized data; deserializing the serialized data according to the data structure definition file to obtain business data.

[0013] In a second aspect, the present invention provides a data interaction method, comprising:

[0014] Configure the subscribed topic by creating a module, initialize key variables, and initialize the publisher and subscriber at the same time through an initialization function; wherein the initialization function is used to initialize the publishing and subscription environment, and initialize the data structure;

[0015] Read the data structure definition file in proto format through the publisher; and

[0016] Serializing the business data according to the data structure definition file; transmitting the serialized data to the subscriber through the zero message queue;

[0017] Reading a data structure definition file in proto format through the subscriber; and,

[0018] Subscribing to the serialized data; deserializing the serialized data according to the data structure definition file to obtain business data.

[0019] Compared with the prior art, the present invention has the following beneficial effects:

[0020] Different from the solution of separately initializing the publisher and subscriber in the prior art, the present invention completes the initialization of the publisher and subscriber at the same time through an initialization function, which can improve the initialization efficiency and avoid the complicated operation of creating the publisher and subscriber separately. Unified initialization makes the code and process more concise and convenient to use. The present invention pre-generates a data structure definition file in proto format, so that the publisher and subscriber can directly read the structure definition file and perform serialization, without the need for the publisher and subscriber to interact to determine the data structure. By using Protobuf, the definition of data can be unified and standardized in the proto file, avoiding the increase in implementation complexity caused by multiple interacting parties. At the same time, the same proto file can be used by all interacting parties, further simplifying the negotiation process of the data format.

[0021] By combining the zero message queue distribution mechanism and the protocol buffer's efficient data serialization and deserialization, data in a custom data format can be avoided from being directly used for transmission, which can greatly improve data transmission efficiency. This advantage is more obvious when there are many interacting parties.

[0022] The present invention improves the efficiency of data transmission and reception and reduces the time of data processing, thereby improving the performance of the system and also improving the user experience.

[0023] Furthermore, compared with the prior art, the present invention does not need to manually convert the data structure definition of the protocol buffer into executable function code, but automatically generates header files and source code files; the header files and source code files can be directly added during subsequent serialization. Only the logical part of the data operation needs to be focused on, and there is no need to worry about the serialization and deserialization process, thereby reducing the complexity and workload of development. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0025] Figure 1 is a structural diagram of a data interaction system provided by an embodiment of the present invention;

[0026] Figure 2 is an operation flow chart of the initialization function provided by an embodiment of the present invention;

[0027] Figure 3 is a schematic diagram of a zero message queue agent environment provided by an embodiment of the present invention;

[0028] Figure 4 It is an operation flow chart of the publishing end provided by the present invention when publishing data;

[0029] Figure 5 It is a flow chart of a data interaction method provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0030] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0031] As mentioned in the background technology, the prior art requires the interacting two or more parties to negotiate the data structure, and it is necessary to customize the interactive data structure in the code. Since the format of data transmission needs to be determined by negotiation between the interacting two parties, this will significantly increase the complexity of implementation when there are many interacting parties; moreover, if the data in the data format customized by the interacting two parties is directly used for transmission, the efficiency of data transmission will be affected. The present invention pre-generates a data structure definition file in proto format, and through the distribution mechanism of the zero message queue, performs efficient data serialization and deserialization to solve the problems existing in the background technology. The present invention is further described in detail below in conjunction with the embodiments.

[0032] Example 1

[0033] Figure 1 : is a schematic diagram of the structure of the data interaction system provided by the present invention. This embodiment is applicable to the case where the publisher and the subscriber exchange data according to a certain data format. The publisher and the subscriber can be integrated into one device to exchange data within the device; or they can be distributed and deployed in different devices to exchange data based on the communication network between the devices. Optionally, the data interaction system in this embodiment can be configured in a vehicle.

[0034] Before exchanging data through the publisher and subscriber, the creation module in this system configures the subscribed topic and initializes key variables (that is, the code variables required for subsequent publisher and subscriber execution). Optionally, both the publisher and the subscriber can be created in the same device, so that the device calls the corresponding interface of the publisher or subscriber to implement the publishing or definition function.

[0035] Specifically, R&D personnel configure the subscribed topic based on actual business. The code example is as follows: zmqTopicIndex_e ipcSubTopicId[subTopicNum] = {TOIPC_NAME}.

[0036] The key variables initialized are as follows:

[0037] addr

[16] ; / / Broker connection IP address.

[0038] pubPoint

[32] ; / / PUB connection point.

[0039] subPoint

[32] ; / / SUB connection point.

[0040] attrFlag; / / ZMQ additional attributes.

[0041] subTopicNum; / / Number of subscribed topics.

[0042] subTopicId; / / Subscribed topic.

[0043] lastHeartbeat; / / The time when the last heartbeat was received (system startup time).

[0044] The creation module is also used to initialize the publishing end and the subscribing end simultaneously through an initialization function; wherein the initialization function is used to initialize the publishing and subscribing environment and initialize the data structure.

[0045] First, the initialization function (ZmqPubSubV2) is called on both the publisher and subscriber to initialize the current publishing and subscription environment. For the internal flow chart of ZmqPubSubV2 processing, see Figure 2 shown.

[0046] S100, start.

[0047] S110, determine whether the key variables for publishing and subscribing are set, if yes, cache the topic, that is, execute S111, if not, return.

[0048] S111. Cache topic.

[0049] 120. Determine whether the callback function is empty. If it is not empty, initialize the internal publishing end and receiving end, that is, execute S121 and S122. If it is empty, return.

[0050] It should be noted that the purpose of the callback function here is that after the subscriber receives the subscribed message, the callback function will be called to receive the message. The specific business logic can be implemented in the callback function. Here, only the callback function is judged, and the callback function is not executed.

[0051] S121. Initialize the internal publishing end.

[0052] S122. Initialize the internal receiving end.

[0053] 130. Determine whether the initialization status of the internal publisher and receiver is complete. If complete, initialize the publish-subscribe data structure. Otherwise, reset the internal publisher and subscriber and return.

[0054] S131, reset the internal publisher and subscriber, and execute S140.

[0055] S132: Initialize the publish-subscribe data structure. Execute S140.

[0056] S140, end.

[0057] At this point, both the publisher and the subscriber have been initialized. After the initialization is completed, the corresponding publishing and subscription functions can be implemented during the data transmission process.

[0058] Before the publisher and subscriber interact, a data structure definition file in proto format needs to be generated in advance. Protocol Buffers (also known as Protobuf) is a lightweight data description language. Protobuf is efficient in serializing data. Compared with XML, Protobuf is smaller, faster and simpler. Once the data structure of the data to be processed is defined, the protoc tool can be used to generate the relevant code. The data structure can also be updated without the need to redeploy the application. Just use Protobuf to describe the data structure once, and you can use various languages ​​or various data streams to conveniently read and write your structured data. Protobuf is very suitable for data storage or RPC data exchange format. It can be used in communication protocols, data storage and other fields. It is a language-independent, platform-independent, and extensible serialized structure data format. Among them, the protoc tool does not limit the compilation type, such as java, C++ or C.

[0059] The data structure definition file specifies the format of the interactive data, such as the location and meaning of the fields. This embodiment does not limit the data structure definition file generation subject, which can be generated by the publisher and subscriber, or generated by other terminals / other devices and distributed to the publisher and subscriber. Specifically, it includes the following two implementation methods:

[0060] In a first implementation mode, the system further includes a definition module for defining a data format for publishing and subscribing; a header file and a source code file are generated according to the data format by a protoc tool, and the header file and the source code file constitute a data structure definition file. The header file includes a serialization interface and a deserialization interface. After the header file and the source code file are generated, they are distributed to the publisher and the subscriber.

[0061] Specifically, define the data format for publishing and subscription, such as LogonReqMessage.proto, the file name is XXXX.proto, where XXXX is a custom file name, and the data definition format is, for example, the Protocol Buffer file format definition:

[0062] message LogonReqMessage {

[0063] int32 requestID = 1; #Request service ID field, 1 indicates location, the first field

[0064] int32 actionID = 2; #Execution ID field, 2 indicates the location, the second field

[0065] }

[0066] Optionally, this embodiment generates header files and source code files written in C language by using the protoc-c tool written in C language, as follows:

[0067] protoc-c --c_out=.LogonReqMessage.proto

[0068] LogonReqMessage.pb-cc#Source code file

[0069] LogonReqMessage.pb-ch#header file

[0070] The second implementation method: the definition of the data format is stored in the code repository, and the publisher and subscriber monitor the data format in real time; if the data format is monitored to change, the protoc tool (such as the protoc-c tool) is used to generate header files and source code files according to the data format, and replace the local header files and source code files. The process of generating header files and source code files is described in the first implementation method and will not be repeated here. By monitoring whether the data format has changed by the publisher and the receiver, the internal data format definition file can be updated at the first time to ensure that the format of data interaction is correct.

[0071] The above two implementation methods do not need to manually convert the definition of the protocol buffer into executable function code. You only need to focus on the logical part of the data operation without worrying about the parsing part. In other words, the process of converting data from binary stream to specific data structure does not need to be parsed. By calling the automatically generated interface, the binary stream can be directly converted into the corresponding data structure, thereby reducing the complexity and workload of development.

[0072] Compared with the manual writing of data format code in the prior art, this embodiment automatically generates code (i.e., header file and source code file). If the data format changes, it is only necessary to update the data structure LogonReqMessage.proto and regenerate the corresponding header file and source code. The interacting parties can call the corresponding serialization interface and deserialization interface according to the new header file, without the need to manually write data format code.

[0073] The publishing end is used to read the data structure definition file in proto format and serialize the business data according to the data structure definition file. Specifically, add the header file and source code file in the business source code; compile and introduce the Protobuf library dependency on the application end, and add the header file and source code file to the business source code. Call the serialization interface in the header file to serialize the business data. Optionally, when Protobuf is implemented in computer C language, it is necessary to compile and introduce the Protobuf-C library dependency on the application end.

[0074] The serialization process includes: using a data structure definition file (.proto) in a protocol buffer (Protobuf) to define a message structure, wherein the message structure includes field identifiers, field types, and field names. Using the protoc tool to compile the data structure definition file into code in the target language; encoding the business data to be serialized according to the message structure, and generating a binary data stream. Optionally, using a data structure definition file to define a message structure in a protocol buffer Protobuf-C implemented in C language.

[0075] The steps for serialization code implementation are as follows: Create a data pointer to be sent, which includes the memory size, request service ID, execution ID, size of the packaged data, and the Protobuf space for serialization (for serialized transmission). Then, serialize the packaged data to the requested space. The code example is as follows:

[0076] void *buf = NULL; / / / Data pointer to be sent.

[0077] unsigned int len ​​= 0; / / Memory size.

[0078] logonVal.requestid = 0x01; / / Request service ID.

[0079] logonVal.actionid = 0xF1; / / Execution ID.

[0080] len = logon_req_message__get_packed_size(&logonVal); / / The size of the packed data.

[0081] buf = (uint8_t *)malloc(len); / / Apply for serialized Protobuf space, mainly used for serialized transmission.

[0082] ret = logon_req_message__pack(&logonVal, buf); / / Serialize and pack data into buf.

[0083] Next, the publisher transmits the serialized data to the subscriber through the zero message queue. The zero message queue is an event-driven asynchronous messaging framework that implements data distribution through the publish / subscribe model. Using the ZMQ publish-subscribe model, efficient data distribution and retrieval can be achieved. This model can effectively reduce the complexity of data transmission and improve the efficiency of data transmission.

[0084] The subscriber is used to read the data structure definition file in proto format; and subscribe to the serialized data; deserialize the serialized data according to the data structure definition file to obtain business data.

[0085] Specifically, the subscriber generates or receives the header file and source code file of the data structure sent by the definition module. Compile and introduce the protobuf library dependency on the application side, such as the protobuf-c library dependency, and add the header file and source code file to the business source code. Call the deserialization interface in the header file to deserialize the subscribed data, that is, decode the received binary data stream into original data.

[0086] The steps to implement deserialization are as follows: create a pointer to store the deserialized data, receive the serialized data, deserialize the received data, and release the space in the protocol buffer. The code example is as follows:

[0087] LogonReqMessage *unpackMsgData = NULL; / / Pointer to store deserialized data.

[0088] uint8_t *buf = NULL;

[0089] buf = (uint8_t *)msg; / / The original serialized data received.

[0090] unpackMsgData = logon_req_message__unpack(NULL,msgLen, buf); / / Deserialize the received data.

[0091] logon_req_message__free_unpacked(unpackMsgData, NULL); / / Release the space of the protocol buffer.

[0092] In this embodiment, based on the distribution mechanism of zero message queue, combined with Protobuf to define the format of interactive data, and using Protobuf to perform efficient data serialization and deserialization on the protocol buffer, efficient data interaction can be achieved. Specifically, the two parties in the interaction can determine the format of data transmission through negotiation, define the data definition file in proto format, and use the protoc tool to generate the corresponding serialization and deserialization code. Then use Protobuf to serialize the data, compress and encode it, and transmit it through the zero message queue. The recipient then deserializes the received data to achieve data exchange. After the data format changes in the later stage, it only needs to modify the data definition file in proto format, and use the protoc tool to generate the corresponding serialization source code, without a large amount of modification of the application source code, to achieve the change of communication data.

[0093] Compared with the prior art, this technical solution mainly solves the following problems:

[0094] 1) When there are many interacting parties, the format of data transmission needs to be negotiated and determined by both parties. As the number of interacting parties increases and the scale of interaction expands, the implementation complexity increases.

[0095] 2) If the data format customized by the interacting parties is directly used for transmission, if it involves complex data, as well as the transmission and interaction of large amounts of data, there will be problems affecting the efficiency of data transmission;

[0096] 3) Although protocol buffers can achieve efficient serialization and deserialization of data, in actual applications, it is necessary to manually convert the definition of protocol buffers into executable function code, which increases the complexity and workload of development.

[0097] Compared with the existing technology, the beneficial effects of this technical solution are as follows:

[0098] 1. Simplify data format negotiation: By using the protocol buffer (Protobuf), the definition of data can be unified and standardized in the proto file, avoiding the increase in implementation complexity caused by multiple interacting parties. At the same time, the same proto file can be used by all interacting parties, further simplifying the data format negotiation process.

[0099] 2. Improve data transmission efficiency: By combining the distribution mechanism of zero message queues and the efficient data serialization and deserialization of protocol buffers, the data transmission efficiency can be greatly improved. This advantage is more obvious when there are many interacting parties.

[0100] 3. Reduce development complexity and workload: Compared with the existing technology, this technical solution does not need to manually convert the definition of the protocol buffer into executable function code. It only needs to focus on the logical part of the data operation without worrying about the parsing part, thereby reducing the complexity and workload of development.

[0101] 4. Improve system performance and user experience: By improving the efficiency of data transmission and reception and reducing the time of data processing, the system performance is improved, and the user experience is also improved.

[0102] 5. Protobuf is an efficient structured data storage format that can achieve efficient serialization and deserialization of data. In this way, it can avoid the custom data format being used directly for transmission, thereby improving the efficiency of data transmission.

[0103] 6. This embodiment uses one initialization function to complete the initialization of the publisher and the subscriber at the same time, which can improve the initialization efficiency.

[0104] 7. Compared with the serialization and deserialization schemes implemented in languages ​​such as C++, C#, and Java, Protobuf-C has the following characteristics: (1) Compact data format: The binary data generated by Protobuf-C is more compact than other data formats, which can save storage space and network bandwidth. (2) Fast data access: Because the generated data uses binary encoding, the parsing speed is faster. (3) Strong portability: Protobuf-C supports multiple platforms and operating systems and can be easily ported to different devices. Based on this, the present invention can unify and standardize the data format definition in the proto file by using the protocol buffer (Protobuf-C) implemented in the computer C language, avoiding the increase in implementation complexity caused by the large number of interacting parties. At the same time, the same proto file can be used by all interacting parties, further simplifying the data format negotiation process. By combining the distribution mechanism of the zero message queue (ZMQ) and the efficient data serialization and deserialization of Protobuf-C, the data transmission efficiency can be greatly improved. Especially when the program runs on an embedded platform with many interacting parties, the efficiency advantage of Protobuf-C implemented in C language is more obvious.

[0105] Example 2

[0106] Based on the above embodiments, this embodiment configures the operating environment in the data interaction system before the publisher and the subscriber interact with each other to ensure the safe execution of publishing and subscribing. Specifically, it includes setting the agent environment. Before configuring the subscribed topic and initializing key variables, the creation module is also used to create a zero message queue agent; wherein the zero message queue agent connects at least one publisher and at least one subscriber.

[0107] Optionally, use ZMQ's XPUB-XSUB (multiple publishers PUB and multiple subscribers SUB) to construct proxy communication. The overall communication environment is as follows Figure 3 shown.

[0108] The steps to configure and deploy the ZMQ agent in the system are as follows:

[0109] Step 1: Create a zmq socket, including the context, published zmqsocket and subscribed zmqsocket. The code used is as follows:

[0110] context = zmq_ctx_new(); / / The context environment required by zmqsocket.

[0111] pubProxy = zmq_socket(context, ZMQ_XSUB); / / Create a published zmqsocket.

[0112] subProxy = zmq_socket(context, ZMQ_XPUB); / / Create a subscribed zmqsocket.

[0113] Step 2. Bind the zmqsocket to the port number and URL. Specifically, construct the address port number and communication protocol for publishing the pubProxy binding. TCP is used here, but udp can also be used. 0.0.0.0 represents any address; bind the aforementioned constructed information. Code example: snprintf(zmqSockAddr, sizeof(zmqSockAddr), "tcp: / / 0.0.0.0:%d", ZMQ_PUB_LISTEN_PORT); zmq_bind(pubProxy, zmqSockAddr). Construct the address port number and communication protocol for subscribing to the subProxy binding. TCP is used here, but udp can also be used. 0.0.0.0 represents any address; bind the aforementioned constructed information. Code example: snprintf(zmqSockAddr, sizeof(zmqSockAddr),"tcp: / / 0.0.0.0:%d", ZMQ_SUB_LISTEN_PORT);zmq_bind(subProxy,zmqSockAddr); / / Bind the information constructed in the previous step.

[0114] Step 3: Generate and run an XMQ broker locally to handle the communication link between the two parties. Code example for generating a ZMQ broker: zmq_proxy(pubProxy, subProxy, NULL).

[0115] After setting up the broker environment, you can create message publishers and subscribers.

[0116] Optionally, when the publisher transmits the serialized data to the subscriber through the zero message queue, it is specifically used to: determine whether the key variables of the publication are set, and determine the size of the publication buffer space and the preset space for storing the publication data; if the key variables of the publication have been set and the preset space is greater than or equal to the publication buffer space, the serialized data is transmitted to the subscriber through the zero message queue. Figure 4 , the publishing end operates as follows when publishing data:

[0117] S200, start.

[0118] S210: Check whether the key variables of the release are set. If yes, initialize the release cache space, that is, execute S211; if not, return.

[0119] S211, initialize the publishing cache space.

[0120] S220, determine the size of the preset space and the publishing cache space. If the preset space is smaller than the publishing cache space, that is, the preset space is insufficient, allocate the preset space according to the space size required for publishing, that is, execute S221; if the preset space is greater than or equal to the publishing cache space, that is, the preset space is sufficient, publish the data in the preset space.

[0121] The cache space is determined according to the size of the data actually published, and the preset space is the space reserved for storing the published data.

[0122] S221. Allocate preset space according to the space size required for publishing.

[0123] S230, package and publish data.

[0124] S240, end.

[0125] At the code level, you only need to call the ZmqMsgPub function to publish data after initialization is completed. The ZmqMsgPub function is defined as follows:

[0126] @brief: ZMQ / / Publish a message.

[0127] @prama: [elem] / / pubsub instance.

[0128] [topicId] / / Publish message topic ID.

[0129] [msg] / / Published message.

[0130] [msgLen] / / Message length.

[0131] @retrn: ZMQ_PS_RET_OK / / Published successfully.

[0132] Other / / Publish failed.

[0133] The publishing function is as follows: int ZmqMsgPub(zmqPubSubElem_s *elem, zmqTopicIndex_etopicId, void *msg, int msgLen). This publishing function can run the publishing function ZmqMsgPub in a separate thread to do publishing logic processing and implement Figure 4 The release process described.

[0134] Optionally, when subscribing to the serialized data, the subscriber is specifically used to: call a callback function and subscribe to the serialized data according to the parameters described in the callback function; wherein the parameters include the topic number, the type of subscription message received, and the length of the subscription message.

[0135] At the code level, the function definition of ZmqPubSubV2 is as follows:

[0136] @brief: / / Create a new ZMQ subscription.

[0137] @prama: [elem] / / pubsub instance.

[0138] [subTopicId] / / Subscription topic ID. Supports subscribing to multiple topics at the same time.

[0139] [subTopicNum] / / Number of subscribed topics.

[0140] [subMsgProc] / / Callback processing function after receiving the subscription message.

[0141] @retrn: ZMQ_PS_RET_OK / / Subscription successful.

[0142] Other / / Subscription failed.

[0143] ZmqPubSubV2(&g_rpcZmqPubSub, ipcSubTopicId, subTopicNum,RpcZmqMsgRecv); / / ZmqPubSubV2 function structure.

[0144] RpcZmqMsgRecv is a callback function. After the subscriber receives the subscribed message, the callback function will be called to receive the message. The specific business logic can be implemented in the callback function. The function definition and parameter description of the callback function are as follows:

[0145] Subscription message processing function type definition:

[0146] topic_id / / Topic ID.

[0147] msg / / Received subscription message.

[0148] Msglen / / Subscription message length.

[0149] int (*RpcZmqMsgRecv) (int topicId, void *msg, int msgLen).

[0150] The function defined according to the parameters and return value type described in the callback function definition can be passed into the [subMsgProc] in the ZmqPubSubV2 function as a callback function to implement customized receive message processing.

[0151] This embodiment describes in detail the configuration process of the agent environment to facilitate connecting multiple publishers and subscribers in the system, and creating an XMQ agent between the publisher and the receiver to process the communication link between the two interacting parties.

[0152] When publishing data, this embodiment again determines whether the key variables are set and whether the cache space required for publishing the data is sufficient to ensure smooth data publishing and avoid lost operations.

[0153] This embodiment calls the callback function and subscribes to the serialized data according to the parameters described in the callback function, thereby improving the repeatability and flexibility of the calling code.

[0154] Example 3

[0155] Figure 5 is a flow chart of a data interaction method provided by an embodiment of the present invention. This embodiment provides a data interaction method, which is executed by the data interaction system provided by the above embodiment. Figure 5 As shown, the method includes the following operations:

[0156] S300, configuring the subscribed topic by creating a module, initializing key variables, and simultaneously initializing the publisher and subscriber through an initialization function; wherein the initialization function is used to initialize the publishing and subscription environment, and initialize the data structure.

[0157] S310. Read the data structure definition file in proto format through the publishing end.

[0158] S320, serializing the business data according to the data structure definition file through the publishing end; transmitting the serialized data to the subscription end through the zero message queue.

[0159] S330. Read the data structure definition file in proto format through the subscriber.

[0160] S340, subscribing to the serialized data through the subscriber; deserializing the serialized data according to the data structure definition file to obtain business data.

[0161] Optionally, before S310, the process also includes: defining a data format for publishing and subscribing by defining a module; generating a header file and a source code file according to the data format by using a protoc tool, wherein the header file and the source code file constitute a data structure definition file.

[0162] Optionally, serializing the business data according to the data structure definition file includes: adding the header file and the source code file to the business source code; and calling the serialization interface in the header file to serialize the business data.

[0163] Optionally, calling the serialization interface in the header file to serialize the business data includes: using a data structure definition file to define a message structure in a protocol buffer, the message structure including a field identifier, a field type, and a field name; using a protoc tool to compile the data structure definition file into a target language code; encoding the business data to be serialized according to the message structure, and generating a binary data stream.

[0164] Optionally, a header file and a source code file are generated according to the data format by a protoc-c tool, and the header file and the source code file constitute a data structure definition file written in C language; and defining the message structure using the data structure definition file in the protocol buffer comprises: defining the message structure using the data structure definition file in the protocol buffer Protobuf-C implemented in C language.

[0165] Optionally, before S300, it also includes: creating a zero message queue proxy through a creation module; the zero message queue proxy connects at least one publisher and at least one subscriber. Optionally, before S310, it also includes: configuring the subscribed topic through a creation module, initializing key variables, and initializing the publisher and subscriber through an initialization function; wherein the initialization function is used to initialize the publishing and subscription environment, and initialize the data structure.

[0166] Optionally, when the publisher transmits the serialized data to the subscriber through the zero message queue, it is specifically used to: determine whether the key variables for publication are set, and whether the cache space required for publishing the data is sufficient; if the key variables for publication have been set and the cache space is sufficient, the serialized data is transmitted to the subscriber through the zero message queue.

[0167] Optionally, when subscribing to the serialized data, the subscriber is specifically used to: call a callback function and subscribe to the serialized data according to the parameters described in the callback function; wherein the parameters include the topic number, the type of subscription message received, and the length of the subscription message.

[0168] Optionally, before reading the data structure definition file in proto format, the publisher and subscriber are also used to: monitor the data format in real time; if it is monitored that the data format has changed, generate a header file and a source code file according to the data format through the protoc tool, and replace the local header file and source code file.

[0169] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this application can be executed in parallel, sequentially or in different orders, as long as the expected results of the technical solution disclosed in this application can be achieved, and this document is not limited here.

[0170] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.

Claims

1. A data interaction system, characterized in that: include: Create a module to configure the subscription topic, initialize key variables, and initialize the publisher and subscriber at the same time through the initialization function; The publishing end is used to read the data structure definition file in proto format; as well as, Serializing the business data according to the data structure definition file; The serialized data is transmitted to the subscriber through the zero message queue; The subscriber is used to read the data structure definition file in proto format; as well as, Subscribe to the serialized data; Deserialize the serialized data according to the data structure definition file to obtain business data; The creation module is also used to create a zero message queue agent, and the zero message queue agent is connected to at least one publishing end and at least one subscribing end; The initialization function is used to initialize the publishing and subscription environment and the data structure; the internal process of the initialization function includes: Determine whether the key variables for publishing and subscription are set, and cache the topic if they are set; Then determine whether the callback function is empty. If not, initialize the internal publisher and receiver. Finally, determine whether the initialization status of the publisher and receiver is complete. If complete, initialize the publish-subscribe data structure. Otherwise, reset the internal publisher and subscriber.

2. The system according to claim 1, characterized in that Also includes: Definition module, used to define the data format for publishing and subscription; A header file and a source code file are generated according to the data format by using the protoc tool, and the header file and the source code file constitute a data structure definition file.

3. The system according to claim 2, characterized in that The serializing the business data according to the data structure definition file includes: Add the header file and source code file to the business source code; The serialization interface in the header file is called to serialize the business data.

4. The system according to claim 3, characterized in that The calling of the serialization interface in the header file to serialize the business data includes: Using a data structure definition file to define a message structure in a protocol buffer, the message structure includes a field identifier, a field type, and a field name; Use the protoc tool to compile the data structure definition file into code in the target language; The business data to be serialized is encoded according to the message structure, and a binary data stream is generated.

5. The system according to claim 4, characterized in that The definition module is used to generate a header file and a source code file according to the data format through the protoc-c tool, and the header file and the source code file constitute a data structure definition file written in C language; Defining the message structure using a data structure definition file in a protocol buffer includes: defining the message structure using a data structure definition file in a protocol buffer Protobuf-C implemented using C language.

6. The system according to claim 1, characterized in that When the publisher transmits the serialized data to the subscriber through the zero message queue, it is specifically used to: Determine whether the key variables for publishing are set, and determine the size of the publishing cache space and the preset space for storing publishing data; If the key variables for publishing have been set and the preset space is greater than or equal to the publishing buffer space, the serialized data is transmitted to the subscriber through the zero message queue.

7. The system according to claim 1, characterized in that When the subscriber subscribes to the serialized data, the subscriber is specifically used to: Calling a callback function to subscribe to the serialized data according to the parameters described in the callback function; The parameters include the topic number, the type of subscription message received, and the length of the subscription message.

8. The system according to claim 1, characterized in that Before reading the data structure definition file in proto format, the publisher and subscriber are also used to: Real-time monitoring of data formats; If the data format is monitored to have changed, the protoc tool is used to generate header files and source code files according to the data format, and replace the local header files and source code files.

9. A data interaction method, characterized in that: include: Configure the subscribed topics by creating modules, initialize key variables, and initialize both the publisher and subscriber through initialization functions; Read the data structure definition file in proto format through the publisher; as well as, Serializing the business data according to the data structure definition file; transmitting the serialized data to the subscriber through the zero message queue; Read the data structure definition file in proto format through the subscriber; as well as, Subscribe to the serialized data; Deserialize the serialized data according to the data structure definition file to obtain business data; The method further comprises: Creating a zero message queue agent through the creation module, wherein the zero message queue agent is connected to at least one publishing end and at least one subscribing end; The initialization function is used to initialize the publishing and subscription environment and the data structure; the internal process of the initialization function includes: Determine whether the key variables for publishing and subscription are set, and cache the topic if they are set; Then determine whether the callback function is empty. If not, initialize the internal publisher and receiver. Finally, determine whether the initialization status of the publisher and receiver is complete. If complete, initialize the publish-subscribe data structure. Otherwise, reset the internal publisher and subscriber.

Citation Information

Patent Citations

  • Communication system, information interaction method, information interaction equipment and medium

    CN113849449A

  • Vehicle data acquisition method and device, vehicle and storage medium

    CN116524621A

  • Software testing method and device based on ZeroMQ

    CN118312422A

  • Method and a system for protecting path and data of a mobile agent within a network system

    US20080253571A1