============
Architecture
============

The API is designed in a way that the turret functions as a server and a client for different purposes.
The server accepts requests for actions which it then tries to execute on the turret, while the client is responsible for sending out notifications about the changes induced either by the user or through the requests sent through the API.

.. uml::

    frame Turret {
        [TurretClient] as tclient
        [TurretServer] as tserver
    }

    [ApplicationClient] as aclient
    [ApplicationServer] as aserver

    interface TurretRequestService as trs
    interface TurretNotificationService as tns

    tserver -down- trs
    tclient -down-( tns

    aserver -up- tns
    aclient -up-( trs



----------------
Turret as Server
----------------

If the Thrift API is enabled for the client in the System Manager, the turret starts a Thrift Server and accepts connections and requests on port *9007*.
Thrift generates the required class which realizes the :thrift:service:`TurretApi.TurretRequestService` interface to send the requests to the server.

See :ref:`Logging in/out example <ex-loginlogout>`

.. uml::

    autoactivate on
    hide footbox

    participant AppClient as aclient

    participant TurretServer as tserver
    participant Turret as turret

    turret -> tserver ** : create

    aclient -> tserver : connect
    tserver --> aclient

    aclient -> tserver : action
    opt action valid
        tserver -> turret : action
        turret --> : act
        tserver --> aclient
    else action invalid
        tserver --> aclient : error
    end


.. _turretclient:

----------------
Turret as Client
----------------

You can request the turret for notifications of the different events that happen.
For this you first have to connect to the turret, and :thrift:service_method:`TurretApi.TurretRequestService.requestNotifications` supplying it with the address of your server.
Your server will need to implement the :thrift:service:`TurretApi.TurretNotificationService` interface.

See :ref:`Monitoring example <ex-monitoring>`

.. uml::

    participant AppClient as aclient
    participant AppServer as aserver

    participant TurretClient as tclient
    participant Turret as turret
    participant TurretServer as tserver

    == Connecting ==

    turret -> tserver ** : create

    aclient -> tserver : connect
    tserver --> aclient

    aclient -> tserver : requestNotifications(serverInfo)
    tserver --> aclient
    tserver -> turret : notifications requested
    turret -> tclient ** : create
    tclient -> aserver : connect
    aserver --> tclient
    tclient --> turret
    turret --> tserver

    == Notification ==

    turret <-- : action
    turret -> tclient : notifyAction
    loop each subscriber
        tclient -> aserver : notifyAction
        aserver --> tclient
        tclient --> turret
        aserver -> aserver : handleAction
    end
