Communication models
====================

The network communication in the Modelverse builds on top of the raw socket implementation of Python.
Whereas we use normal HTTP requests, we explicitly opted not to use these libraries.
Many of these libraries hide important implementation details, such as how successive requests are handled (sequential, threaded, ...), whether or not to keep connections open and reuse them, which HTTP version to implement, and so on.
As the HTTP protocol is a simple protocol, we reimplemented it completely in SCCD (StateCharts and Class Diagrams).

There are two network components: a server in the MvS, and a server and client in the MvK.

MvS server
----------

.. image:: img/mvs_server.svg

The MvS server mainly just waits for incoming HTTP requests and presents the message to the MvS interface itself.
Each part of the processing happens in a seperate orthogonal component, but in all cases, processing happens sequentially, and buffers are implemented in between each layer.
This way, we can be certain that invocations on the MvS will always happen sequentially.

The statechart consists of two parts.

The topmost part is the HTTP server itself, which waits from messages from the sockets.
When it starts up, a socket is created that listens to incoming connections.
Messages from the socket are then processed.
Apart from setting up the sockets, this component serializes all requests that get sent to the MvS: events are placed in a queue, and are only executed on the MvS when it has finished processing the previous request.

The bottommost part is the socket server, which wraps around the socket and keeps fetching from the socket until it gets a complete HTTP request.
The HTTP request is then split from the other data on the socket, and forwarded as a single request to the HTTP server.
We explicitly need to check and split HTTP requests, as our protocol allows users to bundle multiple HTTP requests simultaneously.
These requests will just come one after the other, so the socket buffer might contain two messages at the same time, or even fragments of a second request.
A message is forwarded as soon as it is completely reconstructed.

MvK server
----------

.. image:: img/mvk_server.svg

.. note::
    This figure shows the intended model, not the optimized model.
    For performance reasons, many of these parts are merged together in the *run_local_modelverse.py* script.
    As a result, it only implements the *MvKController*, *Server*, and *Socket* classes.
    A more elaborate statechart implementation also exists, which does implements this whole figure.
