|
2 | 2 | Python simulator of distributed system
|
3 | 3 |
|
4 | 4 | [](https://mybinder.org/v2/gh/xneg/distributed-game/main?labpath=%2Fsrc%2Fsandbox_notebook.ipynb)
|
| 5 | + |
| 6 | +# Web-version |
| 7 | +https://xneg.github.io/distributed-game/ |
| 8 | + |
| 9 | +# How to |
| 10 | +You need to implement your own algorithm inheriting [class Node](https://github.com/xneg/distributed-game/blob/main/src/engine/node.py). |
| 11 | +There are two abstract methods processing read and write requests: |
| 12 | +```python |
| 13 | +@WebServer.endpoint(message_type=ClientReadRequest) |
| 14 | +@abc.abstractmethod |
| 15 | +def process_read_request(self, request): |
| 16 | + pass |
| 17 | + |
| 18 | +@WebServer.endpoint(message_type=ClientWriteRequest) |
| 19 | +@abc.abstractmethod |
| 20 | +def process_write_request(self, request): |
| 21 | + pass |
| 22 | +``` |
| 23 | +You can see examples [here](https://github.com/xneg/distributed-game/tree/main/src/algorithms). |
| 24 | + |
| 25 | +# Engine |
| 26 | + |
| 27 | +This diagram shows communication between two `WebServers`. |
| 28 | +WebServer contains three main methods: |
| 29 | + |
| 30 | +* send_message(receiver_id, message) |
| 31 | + |
| 32 | +* add_message(message) |
| 33 | + |
| 34 | +* process() |
| 35 | + |
| 36 | +The `send_message(receiver_id, message)` method creates a `Signal` (1) object that contains the "message packet" itself and the ID of the receiving server. |
| 37 | +In fact, `Signal` after a certain number of steps will call the `add_message(message)` (2) callback already on the recipient side. |
| 38 | + |
| 39 | +The `add_message(message)` method sends a message to one of the server endpoints (3) with the help of typing, decorators and a bit of "pythonic" magic. |
| 40 | +Web servers use signals instead of calling each other's methods directly to mimic the asynchronous nature of network. |
| 41 | +The signal can have an arbitrary execution time, moreover, it is possible to make the signal "lost" by simulating network breaks. |
| 42 | + |
| 43 | + |
| 44 | +This diagram shows implemented system. The client only knows about the gateway. |
| 45 | +Gateway acts as a proxy between the client and the nodes of the system, it knows about anything. The gateway forwards client requests to nodes via round-robin (the first request comes to the first node, the second - to the second). |
| 46 | +Then the nodes exchange messages with each other and send the response back to the gateway, which returns it to the client. |
| 47 | +All the arrows on the diagram are, in fact, signals. |
0 commit comments