2024-02-23 08:55:32 +00:00
|
|
|
.. only:: comment
|
|
|
|
|
|
|
|
|
|
© Crown-owned copyright 2023, Defence Science and Technology Laboratory UK
|
|
|
|
|
|
2024-02-23 16:49:01 +00:00
|
|
|
.. _DatabaseService:
|
|
|
|
|
|
2024-02-23 08:55:32 +00:00
|
|
|
DatabaseService
|
2024-02-23 16:49:01 +00:00
|
|
|
###############
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
The ``DatabaseService`` provides a SQL database server simulation by extending the base Service class.
|
|
|
|
|
|
|
|
|
|
Key capabilities
|
2024-02-23 16:49:01 +00:00
|
|
|
================
|
2024-02-23 08:55:32 +00:00
|
|
|
|
2024-02-28 15:08:00 +00:00
|
|
|
- Creates a database file in the ``FileSystem`` of the ``Node`` (which the ``DatabaseService`` is installed on) upon creation.
|
2024-02-23 08:55:32 +00:00
|
|
|
- Handles connecting clients by maintaining a dictionary of connections mapped to session IDs.
|
|
|
|
|
- Authenticates connections using a configurable password.
|
|
|
|
|
- Simulates ``SELECT``, ``DELETE`` and ``INSERT`` SQL queries.
|
|
|
|
|
- Returns query results and status codes back to clients.
|
|
|
|
|
- Leverages the Service base class for install/uninstall, status tracking, etc.
|
|
|
|
|
|
|
|
|
|
Usage
|
2024-02-23 16:49:01 +00:00
|
|
|
=====
|
2024-02-23 08:55:32 +00:00
|
|
|
- Install on a Node via the ``SoftwareManager`` to start the database service.
|
|
|
|
|
- Clients connect, execute queries, and disconnect.
|
|
|
|
|
- Service runs on TCP port 5432 by default.
|
|
|
|
|
|
|
|
|
|
Implementation
|
2024-02-23 16:49:01 +00:00
|
|
|
==============
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
- Creates the database file within the node's file system.
|
|
|
|
|
- Manages client connections in a dictionary by session ID.
|
|
|
|
|
- Processes SQL queries.
|
|
|
|
|
- Returns results and status codes in a standard dictionary format.
|
|
|
|
|
- Extends Service class for integration with ``SoftwareManager``.
|
2024-02-23 16:49:01 +00:00
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
========
|
|
|
|
|
|
|
|
|
|
Python
|
|
|
|
|
""""""
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
from ipaddress import IPv4Address
|
|
|
|
|
|
|
|
|
|
from primaite.simulator.network.hardware.nodes.host.server import Server
|
|
|
|
|
from primaite.simulator.system.services.database.database_service import DatabaseService
|
|
|
|
|
|
|
|
|
|
# Create Server
|
|
|
|
|
server = Server(
|
|
|
|
|
hostname="server",
|
|
|
|
|
ip_address="192.168.2.2",
|
|
|
|
|
subnet_mask="255.255.255.0",
|
|
|
|
|
default_gateway="192.168.1.1",
|
|
|
|
|
start_up_duration=0,
|
|
|
|
|
)
|
|
|
|
|
server.power_on()
|
|
|
|
|
|
|
|
|
|
# Install DatabaseService on server
|
|
|
|
|
server.software_manager.install(DatabaseService)
|
|
|
|
|
db_service: DatabaseService = server.software_manager.software.get("DatabaseService")
|
|
|
|
|
db_service.start()
|
|
|
|
|
|
|
|
|
|
# configure DatabaseService
|
|
|
|
|
db_service.configure_backup(IPv4Address("192.168.0.10"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Via Configuration
|
|
|
|
|
"""""""""""""""""
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
simulation:
|
|
|
|
|
network:
|
|
|
|
|
nodes:
|
|
|
|
|
- ref: example_server
|
|
|
|
|
hostname: example_server
|
|
|
|
|
type: server
|
|
|
|
|
...
|
|
|
|
|
services:
|
|
|
|
|
- ref: database_service
|
|
|
|
|
type: DatabaseService
|
|
|
|
|
options:
|
|
|
|
|
backup_server_ip: 192.168.0.10
|
|
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
|
=============
|
|
|
|
|
|
|
|
|
|
.. include:: ../common/common_configuration.rst
|
|
|
|
|
|
|
|
|
|
.. |SOFTWARE_NAME| replace:: DatabaseService
|
|
|
|
|
.. |SOFTWARE_NAME_BACKTICK| replace:: ``DatabaseService``
|
|
|
|
|
|
|
|
|
|
``backup_server_ip``
|
|
|
|
|
""""""""""""""""""""
|
|
|
|
|
|
|
|
|
|
Optional. Default value is ``None``.
|
|
|
|
|
|
|
|
|
|
The IP Address of the backup server that the ``DatabaseService`` will use to create backups of the database.
|
|
|
|
|
|
|
|
|
|
This must be a valid octet i.e. in the range of ``0.0.0.0`` and ``255.255.255.255``.
|
|
|
|
|
|
|
|
|
|
``password``
|
|
|
|
|
""""""""""""
|
|
|
|
|
|
|
|
|
|
Optional. Default value is ``None``.
|
|
|
|
|
|
|
|
|
|
The password that needs to be provided by connecting clients in order to create a successful connection.
|