Network messaging - Part II
So, in the previous example we figured out how to send data from one module to another. And in principle we could have finished here, if not for one nuance - our example will not work if the WOWCube is half-rotated and only half of the modules are connected to each other. But this happens quite often - the player spins the cube, the contacts between the modules are constantly disconnect and connect back again... and this means that there is a fairly high chance of losing data, because if the contact between the modules is broken, the data simply will not reach the recipient!
So it's safe to say the method we used in the previous example, although it works, is not optimal in the case where data delivery to modules is required regardless of the current state of the cube.
What should we do?
It's all quite simple!
Instead of making a network message to be sent on a specific event (such as a pat), the application should send data periodically. Because in this case, even if the actual event that all modules should "know" about happened at the moment the connection was broken, the information will not be lost, it will simply be delayed for a short time. As soon as the connection between the modules is restored, the data will be received.
Aggregating the application state on one of the modules (the master module) and then periodically sending this state to the other modules is a good practice. The frequency of sending depends on the application logic and the expected user actions.
Please note that sending data over the network too frequently may result in increased battery drain.
The example
So, let's move on to an example. As was said above, if we want to ensure reliable data exchange between modules, we need to send this data periodically, and not just once. But what exactly are we going to send?
Ok, let's imagine that one module has a heart, and it beats like a human's. And the other modules also want to know whether this module is alive, whether the heart is beating?
It turns out that all we need to do is send the heartbeat to all modules over the network periodically!
In fact, we will be sending out the information needed to animate the heartbeat graph
So...
First of all, let's define some enum values for the graphic resources and for the timer, which will be used to send messages periodically:
Next we initialize the graphics and the timer
and create a function called by the timer:
As you can see, this function performs two tasks at once: it calculates coordinates for animation and sends data to other modules if a certain condition is met.
Note that sending data, although tied to the logic of this application, is performed periodically, regardless of the state of neighboring modules.
Now let's write the code that receives the data. It's quite simple: if cubepp is running on a non-zero module, then we just have to read out the network message data when it received it and assign the values to variables.
And the last thing: now that all modules "know" about the heartbeat state of the sending module, the only thing left is to draw a nice graph.
I'm sure there shouldn't be any problems with this part.
It's all very simple - the background is drawn, then the graph line is drawn on top, and finally - an animated dot. While the coordinates of this dot are transmitted via a message over the network.
What did we get in the end?
One module sends out a heartbeat, the other modules listen to it and show it synchronously. But only if the cube is fully assembled. If the connection is broken, only one source module will show the heartbeat.