2024-02-23 08:55:32 +00:00
|
|
|
.. only:: comment
|
|
|
|
|
|
2025-01-02 15:05:06 +00:00
|
|
|
© Crown-owned copyright 2025, Defence Science and Technology Laboratory UK
|
2024-02-23 08:55:32 +00:00
|
|
|
|
2024-02-23 16:49:01 +00:00
|
|
|
.. _DatabaseClient:
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
DatabaseClient
|
2024-02-23 16:49:01 +00:00
|
|
|
##############
|
2024-02-23 08:55:32 +00:00
|
|
|
|
2024-02-23 16:49:01 +00:00
|
|
|
The ``DatabaseClient`` provides a client interface for connecting to the :ref:`DatabaseService`.
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
Key features
|
2024-02-23 16:49:01 +00:00
|
|
|
============
|
2024-02-23 08:55:32 +00:00
|
|
|
|
2024-02-23 16:49:01 +00:00
|
|
|
- Connects to the :ref:`DatabaseService` via the ``SoftwareManager``.
|
2024-02-23 08:55:32 +00:00
|
|
|
- Handles connecting and disconnecting.
|
2024-04-26 14:52:21 +00:00
|
|
|
- Handles multiple connections using a dictionary, mapped to connection UIDs
|
2024-02-23 08:55:32 +00:00
|
|
|
- Executes SQL queries and retrieves result sets.
|
|
|
|
|
|
|
|
|
|
Usage
|
2024-02-23 16:49:01 +00:00
|
|
|
=====
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
- Initialise with server IP address and optional password.
|
2024-04-26 14:52:21 +00:00
|
|
|
- Connect to the :ref:`DatabaseService` with ``get_new_connection``.
|
2024-02-23 08:55:32 +00:00
|
|
|
- Retrieve results in a dictionary.
|
|
|
|
|
- Disconnect when finished.
|
|
|
|
|
|
|
|
|
|
Implementation
|
2024-02-23 16:49:01 +00:00
|
|
|
==============
|
2024-02-23 08:55:32 +00:00
|
|
|
|
|
|
|
|
- Leverages ``SoftwareManager`` for sending payloads over the network.
|
2024-04-26 14:52:21 +00:00
|
|
|
- Active sessions are held as ``DatabaseClientConnection`` objects in a dictionary.
|
2024-02-23 08:55:32 +00:00
|
|
|
- Connect and disconnect methods manage sessions.
|
|
|
|
|
- Payloads serialised as dictionaries for transmission.
|
|
|
|
|
- Extends base Application class.
|
2024-02-23 16:49:01 +00:00
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
========
|
|
|
|
|
|
|
|
|
|
Python
|
|
|
|
|
""""""
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
from ipaddress import IPv4Address
|
|
|
|
|
|
|
|
|
|
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
|
|
|
|
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
|
|
|
|
from primaite.simulator.system.applications.database_client import DatabaseClient
|
|
|
|
|
|
|
|
|
|
client = Computer(
|
|
|
|
|
hostname="client",
|
|
|
|
|
ip_address="192.168.10.21",
|
|
|
|
|
subnet_mask="255.255.255.0",
|
|
|
|
|
default_gateway="192.168.10.1",
|
|
|
|
|
operating_state=NodeOperatingState.ON # initialise the computer in an ON state
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# install DatabaseClient
|
|
|
|
|
client.software_manager.install(DatabaseClient)
|
|
|
|
|
|
|
|
|
|
database_client: DatabaseClient = client.software_manager.software.get("DatabaseClient")
|
|
|
|
|
|
|
|
|
|
# Configure the DatabaseClient
|
|
|
|
|
database_client.configure(server_ip_address=IPv4Address("192.168.0.1")) # address of the DatabaseService
|
|
|
|
|
database_client.run()
|
|
|
|
|
|
2024-04-26 14:52:21 +00:00
|
|
|
# Establish a new connection
|
|
|
|
|
database_client.get_new_connection()
|
|
|
|
|
|
2024-02-23 16:49:01 +00:00
|
|
|
|
|
|
|
|
Via Configuration
|
|
|
|
|
"""""""""""""""""
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
simulation:
|
|
|
|
|
network:
|
|
|
|
|
nodes:
|
|
|
|
|
- ref: example_computer
|
|
|
|
|
hostname: example_computer
|
|
|
|
|
type: computer
|
|
|
|
|
...
|
|
|
|
|
applications:
|
|
|
|
|
- ref: database_client
|
|
|
|
|
type: DatabaseClient
|
|
|
|
|
options:
|
|
|
|
|
db_server_ip: 192.168.0.1
|
|
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
|
=============
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``db_server_ip``
|
|
|
|
|
""""""""""""""""
|
|
|
|
|
|
|
|
|
|
IP address of the :ref:`DatabaseService` that the ``DatabaseClient`` will connect to
|
|
|
|
|
|
|
|
|
|
This must be a valid octet i.e. in the range of ``0.0.0.0`` and ``255.255.255.255``.
|
|
|
|
|
|
|
|
|
|
``server_password``
|
|
|
|
|
"""""""""""""""""""
|
|
|
|
|
|
|
|
|
|
Optional. Default value is ``None``.
|
|
|
|
|
|
|
|
|
|
The password that the ``DatabaseClient`` will use to access the :ref:`DatabaseService`.
|
2024-09-05 08:41:04 +01:00
|
|
|
|
|
|
|
|
``Common Attributes``
|
2024-09-05 11:23:52 +01:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^
|
2024-09-05 08:41:04 +01:00
|
|
|
|
|
|
|
|
See :ref:`Common Configuration`
|