Disclosure of Invention
Aiming at the defects in the prior art, the invention provides a multi-level low-coupling communication system, which reduces the coupling of the system.
A multi-tier, low coupling communication system comprising:
a local service layer: the system is loaded on an external computer and used for generating service request data; the system is also used for sending or receiving WebSocket data; the external computer is used for interacting with a user;
an access layer: the system comprises a local service layer, a network layer and a network layer, wherein the local service layer is used for receiving WebSocket data and sending the WebSocket data to the local service layer; the local business layer is also used for receiving the business request data of the local business layer and processing the abnormal business request data;
a protocol analysis layer: the boxing and unpacking device is used for completing the WebSocket protocol; the access layer is also used for encrypting and decrypting the service request data of the access layer;
WebSocket transmission layer: the system comprises an access layer, a remote server and a network interface, wherein the access layer is used for accessing a WebSocket data file;
the remote server: the method is used for realizing the transmission of the WebSocket data between the WebSocket transmission layer and the network interface.
Preferably, the access stratum is specifically configured to:
the access layer receives the WebSocket data and judges whether the received WebSocket data is received data or not;
if so, discarding the WebSocket data;
if not, when the issuing mode of the local service layer is subscription, the access layer issues the WebSocket data to the local service layer in a subscription mode.
Preferably, the abnormal service request data includes service request data of request timeout, cancel request, request data persistence and request failure.
Preferably, the access stratum is specifically configured to:
the access layer starts a daemon thread and a queue to be overtime;
if the overtime time is set in the service request data, assembling a request path, a request ID, the overtime time and the time for successfully sending the request in the service request data into an object, and adding the object into the queue to be overtime;
the access layer wakes up the overtime thread, and circularly searches the object which is most quickly overtime from the queue to be overtime in sequence to perform overtime processing until the queue to be overtime is empty;
the timeout processing specifically includes: and when the waiting time of the object exceeds the overtime, calling a Timeout method to send a request path and a request ID in the object to the local service layer, and deleting the object from the queue to be overtime.
Preferably, the access stratum is specifically configured to:
the access layer stores the received service request data into a request object pool;
when the access layer receives a cancel request from the local service layer, searching whether the request object pool has a request ID of the service request data;
and if so, intercepting the service request data corresponding to the request ID.
Preferably, the access stratum is specifically configured to:
and after the access layer is successfully connected with the WebSocket transmission layer, the access layer searches whether an object needing to be retransmitted exists in the request object pool or not, and if so, retransmits the object through the WebSocket transmission layer.
Preferably, the access stratum is specifically configured to:
when receiving a data persistence request from the local service layer, the access layer acquires data to be persisted in the data persistence request and persists the data to be persisted;
the access layer generates a request for retransmitting the persistent data and sends the request to the local service layer;
the local service layer acquires data to be retransmitted in the retransmission persistent data request, generates a data persistence request according to the data to be retransmitted, and sends the data persistence request to the access layer;
and the access layer generates a request for deleting the persistent data, sends the request to the local service layer and deletes the request for deleting the persistent data when receiving a deletion response returned by the local service layer.
Preferably, the protocol resolution layer is further configured to:
when the service request data cannot be analyzed, generating an error data packet and sending the error data packet to the WebSocket transmission layer;
and when the WebSocket transmission layer receives the error data packet, acquiring service request data which cannot be analyzed in the error data packet, and forbidding to send the service request data to the local service layer.
Preferably, the WebSocket transport layer is further configured to:
when an input network automatic disconnection instruction is received, the WebSocket network is reconnected;
if the reconnection times of the WebSocket network are less than the preset maximum trial reconnection times, the reconnection time interval of the WebSocket network is calculated according to the following formula:
a time interval of min (1, (trial reconnection number × trial reconnection number) × ((maximum trial reconnection number × maximum trial reconnection number)/maximum time interval));
wherein the trial reconnection times refer to the WebSocket network reconnection times; min is a minimum function;
if the number of times of the WebSocket network reconnection is larger than the preset maximum trial reconnection number, the time interval of the WebSocket network reconnection is equal to the preset maximum interval time.
According to the technical scheme, the multi-level low-coupling communication system provided by the invention adopts a layered architecture design, the layers cannot be called in a cross-layer mode, the coupling performance between the layers is reduced, the layers below the access layer do not carry out serialization and deserialization on data, all serialization and deserialization are floated up to the local service realization, and the coupling between the system and specific service logic is reduced.
Detailed Description
Embodiments of the present invention will be described in detail below with reference to the accompanying drawings. The following examples are only for illustrating the technical solutions of the present invention more clearly, and therefore are only examples, and the protection scope of the present invention is not limited thereby. It is to be noted that, unless otherwise specified, technical or scientific terms used herein shall have the ordinary meaning as understood by those skilled in the art to which the invention pertains.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the invention herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in the specification of the present invention and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
As used in this specification and the appended claims, the term "if" may be interpreted contextually as "when", "upon" or "in response to a determination" or "in response to a detection". Similarly, the phrase "if it is determined" or "if a [ described condition or event ] is detected" may be interpreted contextually to mean "upon determining" or "in response to determining" or "upon detecting [ described condition or event ]" or "in response to detecting [ described condition or event ]".
The first embodiment is as follows:
a multi-level low coupling communication system, as shown in fig. 1 and 2, comprising:
a local service layer: the system is loaded on an external computer and used for generating service request data; the system is also used for sending or receiving WebSocket data;
specifically, the external computer is used for interacting with a user, and is a computer device, such as a computer, a smart phone, an intelligent chat robot, a mechanical arm, and the like, for obtaining user requirements and instructions. The system can realize the communication between the client and the remote server, and can also be expanded to the communication mode of the server 1, namely the client-remote server, which is indirect from the server to the server. The local service layer is mainly used for being responsible for specific service logic and processing serialization and deserialization of all service data. The local service layer also realizes the transmission and reception of WebSocket data between the local service layer and the access layer.
An access layer: the system comprises a local service layer, a network layer and a network layer, wherein the local service layer is used for receiving WebSocket data and sending the WebSocket data to the local service layer; the local business layer is also used for receiving the business request data of the local business layer and processing the abnormal business request data;
specifically, the access layer is responsible for processing the service data of the local service layer and processing the abnormal service request data, so as to ensure that the service data of the local service layer is processed timely and accurately.
A protocol analysis layer: the boxing and unpacking device is used for completing the WebSocket protocol; the access layer is also used for encrypting and decrypting the service request data of the access layer;
specifically, the protocol analysis layer is used for ensuring the safety of WebSocket communication.
WebSocket transmission layer: the system comprises an access layer, a remote server and a network interface, wherein the access layer is used for accessing a WebSocket data file;
specifically, the WebSocket transport layer is mainly responsible for WebSocket connection and disconnection, TCP data packet transmission and reception, and network disconnection and reconnection.
The remote server: the method is used for realizing the transmission of the WebSocket data between the WebSocket transmission layer and the network interface.
The system adopts a layered architecture design, cross-layer calling cannot be performed between layers, coupling between layers is reduced, the layers below an access layer do not perform serialization and deserialization on data, all serialization and deserialization float up to the implementation of a local service, and coupling between the system and specific service logic is reduced.
Example two:
the second embodiment is added with the following contents on the basis of the first embodiment:
the access stratum is specifically configured to:
the access layer receives the WebSocket data and judges whether the received WebSocket data is received data or not;
if so, discarding the WebSocket data;
if not, when the issuing mode of the local service layer is subscription, the access layer issues the WebSocket data to the local service layer in a subscription mode.
Specifically, the local service layer may receive WebSocket data in a subscription manner, and the access layer directly discards data that the local service layer has received. For example, after the access layer sends the WebSocket data to the local service layer for a period of time, the same WebSocket data is received again, but the data is repeatedly received, so that the newly received WebSocket data is directly discarded.
Preferably, the abnormal service request data includes service request data of request timeout, cancel request, request data persistence and request failure.
The access layer processes the business request data which is requested to be overtime.
The access layer starts a daemon thread and a queue to be overtime;
specifically, the daemon thread of this embodiment is responsible for monitoring the processing of the access stratum queue to be overtime on the message, and when no message needs to be processed on the access stratum, that is, the queue to be overtime is empty, the daemon thread is automatically closed. When the queue to be timed out is empty and a new message is received, it is automatically started.
If the overtime time is set in the service request data, assembling a request path, a request ID, the overtime time and the time for successfully sending the request in the service request data into an object, and adding the object into the queue to be overtime;
specifically, the timeout time is used to indicate the effective time of the service request data, and when the time for which the service request data a waits to be processed exceeds the timeout time, the service request data is considered to be timeout. The request Path may be denoted by Path, indicating the task/content of the current service request data. The request ID is represented by series number and may be an identification code or a serial number. When the timeout time is set in the service request data, the system records the task/content of the current request, the identification code, the timeout time, the time for starting to calculate the timeout waiting time (i.e. the time for the request to be sent successfully), wherein the task/content and the identification code of the current request are used for marking what the request of the information is, for example, how/where to implement the request of the information. The timeout time for the current request and the time the request was sent successfully are to calculate how long the information will time out.
The access layer wakes up the overtime thread, and circularly searches the object which is most quickly overtime from the queue to be overtime in sequence to perform overtime processing until the queue to be overtime is empty;
specifically, the access layer needs to sequentially take out objects in the queue to be overtime for performing the overtime processing until the queue to be overtime is empty. The access layer needs to find out the object which needs to be processed most urgently, namely the object which needs to be timed out most urgently, at each search.
The timeout processing specifically includes: and when the waiting time of the object exceeds the overtime, calling a Timeout method to send a request path and a request ID in the object to the local service layer, and deleting the object from the queue to be overtime.
Specifically, the object is considered to be timed out when the waiting time of the object exceeds the timeout time. The latency of an object refers to the time difference between the issuance of this piece of information and the timeout.
And the access layer processes the service request data for canceling the request.
The access layer stores the received service request data into a request object pool;
specifically, the access layer is further configured to maintain a request object pool, where the request object pool stores the successfully received service request data. Each object has a survival duration and is longer than a timeout duration.
When the access layer receives a cancel request from the local service layer, searching whether the request object pool has a request ID of the service request data;
specifically, the cancellation request may be with a request ID in the service request data. Since each service request data corresponds to a request ID, the request ID can uniquely identify which service request data.
And if so, intercepting the service request data corresponding to the request ID.
Specifically, when a request ID in a cancellation request exists in a request object pool, service request data corresponding to the request ID is intercepted. When the request ID in the request object pool exists, the cancel attribute of the request may also be set to true, indicating that the request needs to be canceled.
And thirdly, the access layer processes the service request data which fails to be requested.
And after the access layer is successfully connected with the WebSocket transmission layer, the access layer searches whether an object needing to be retransmitted exists in the request object pool or not, and if so, retransmits the object through the WebSocket transmission layer.
And fourthly, the access layer processes the service request data which requests data persistence.
When receiving a data persistence request from the local service layer, the access layer acquires data to be persisted in the data persistence request and persists the data to be persisted;
specifically, the data persistence request includes data to be persisted that needs to be persisted. Data persistence refers to the long-term storage of information. The persistence process may employ existing methods for persistent storage.
The access layer generates a request for retransmitting the persistent data and sends the request to the local service layer;
the local service layer acquires data to be retransmitted in the retransmission persistent data request, generates a data persistence request according to the data to be retransmitted, and sends the data persistence request to the access layer;
in particular, the access stratum requires the local service stratum to retransmit persistently stored data.
And the access layer generates a request for deleting the persistent data, sends the request to the local service layer and deletes the request for deleting the persistent data when receiving a deletion response returned by the local service layer.
Specifically, all the objects in the access layer that are requested to be overtime and cancelled are deleted from the request object pool after the request response is returned and the request is overtime. The system also needs to clear the request that does not need to be retransmitted in the request object pool after the network is disconnected. The system may send messages using a single thread, with the pool of request objects being a first-in-first-out ordered queue.
The system ensures that the service data of the local service layer is processed timely and accurately.
For the sake of brief description, the embodiments of the present invention do not refer to the corresponding contents in the foregoing embodiments of the present invention.
Example three:
embodiment three adds the following on the basis of other embodiments:
the protocol resolution layer is further configured to:
when the service request data cannot be analyzed, generating an error data packet and sending the error data packet to the WebSocket transmission layer;
and when the WebSocket transmission layer receives the error data packet, acquiring service request data which cannot be analyzed in the error data packet, and forbidding to send the service request data to the local service layer.
In particular, data transmitted by a computer or a remote server external to the system is always transmitted in the form of protocol packets. The service request data may be registered in a mapping table according to a mapping relationship of path — object class, and the specific mapping relationship may be determined according to a system specifically used by an external computer. And if the protocol packet cannot be analyzed, directly sending an error data packet to the WebSocket transmission layer, and not sending the error data packet to the local service layer.
For the sake of brief description, the embodiments of the present invention do not refer to the corresponding contents in the foregoing embodiments of the present invention.
Example four:
example four on the basis of other examples, the following are added:
the WebSocket transport layer is further configured to:
when an input network automatic disconnection instruction is received, the WebSocket network is reconnected;
specifically, after the WebSocket network is disconnected, it is necessary to determine whether the current WebSocket connection disconnection is manual disconnection, and if the current WebSocket connection disconnection is manual disconnection, it indicates that the current WebSocket connection disconnection is manually and actively disconnected, and the following reconnection operation is not triggered. If the automatic disconnection is performed, the system or the network is in failure, and the WebSocket network needs to be reconnected.
If the reconnection times of the WebSocket network are less than the preset maximum trial reconnection times, the reconnection time interval of the WebSocket network is calculated according to the following formula:
a time interval of min (1, (trial reconnection number × trial reconnection number) × ((maximum trial reconnection number × maximum trial reconnection number)/maximum time interval));
wherein the trial reconnection times refer to the WebSocket network reconnection times; min is a minimum function;
specifically, the trial reconnection number refers to the number of attempts to reconnect the WebSocket network after the WebSocket network is disconnected.
If the number of times of the WebSocket network reconnection is larger than the preset maximum trial reconnection number, the time interval of the WebSocket network reconnection is equal to the preset maximum interval time.
For the sake of brief description, the embodiments of the present invention do not refer to the corresponding contents in the foregoing embodiments of the present invention.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; such modifications and substitutions do not depart from the spirit and scope of the present invention, and they should be construed as being included in the following claims and description.