' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
' Unless required by  applicable law or agreed to in writing, software distributed under the License is distributed
' on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and limitations under the License.

@startuml

state "LocalManager\nruns in traffic_manager main event loop" as traffic_manager {
    state "processEventQueue" as tm_process_events {
        state "dequeue msg" as tm_dequeue : from ""mgmt_event_queue""
        state "send msg to processes" as tm_send_events : ""sendMgmtMsgToProcesses""
        tm_dequeue -d-> [*] : empty
        tm_dequeue -d-> tm_send_events : based on msg_id
        tm_send_events -u-> tm_dequeue
    }

    state "pollMgmtProcessServer" as tm_poll_ts {
        state "poll socket" as tm_poll_sock : ""mgmt_select""
        state "new connection" as tm_new_process : register new client socket fd\nto start getting msgs from\nclient
        state "new message" as tm_new_message : new message from process
        state "handle msg" as tm_handle_msg : ""handleMgmtMsgFromProcesses"" will\noften ""signalAlarm"" with received\nmsg_id

        tm_poll_sock -d-> tm_new_process : new process trying to connect
        tm_poll_sock -d-> tm_new_message : msg from ""traffic_server""
        tm_poll_sock -d-> [*] : timed out
        tm_new_message -d-> tm_handle_msg : based on msg_id
        tm_handle_msg -u-> tm_poll_sock
        tm_new_process -u-> tm_poll_sock

        state "External Event" as tm_ext_event : mechanism to stop polling
        tm_ext_event -u-> [*] : ""eventfd"" trigger
    }

    [*] -d-> tm_process_events
    tm_process_events -d-> tm_poll_ts
    tm_poll_ts -d-> [*] : other tasks ""traffic_manager"" is\nresponsible for including ""rotateLogs"",\nmanaging metrics and monitoring\n""traffic_server""
}

state "ProcessManager\nruns in processManagerThread from traffic_server" as traffic_server {
    state "pollLMConnection" as ts_poll_tm {
        state "poll socket" as ts_poll_sock : ""mgmt_read_timeout""
        state "handle event" as ts_handle_event : ""executeMgmtCallback""
        state "send response" as ts_response : enqueue response to ""mgmt_signal_queue""
        ts_poll_sock -down-> ts_handle_event : handle based on ""msg_id""
        ts_handle_event -d-> ts_response : callback enqueues response
        ts_response -u-> ts_handle_event
        ts_handle_event -u-> ts_poll_sock

        ts_poll_sock -down-> [*] : timed out
        state "External Event" as ts_ext_event : mechanism to stop polling
        ts_ext_event -u-> [*] : ""eventfd"" trigger
    }

    state "processSignalQueue" as ts_send_signals {
        state "dequeue signal" as ts_dequeue_signal : dequeue from ""mgmt_signal_queue""
        state "send out msg" as ts_outgoing_msg : ""mgmt_write_pipe""
        ts_dequeue_signal -d-> [*] : empty
        ts_dequeue_signal -d-> ts_outgoing_msg : send out msg
        ts_outgoing_msg -u-> ts_dequeue_signal

    }
    ts_send_signals : send outgoing messages via processSignalQueue
    ts_poll_tm -down-> ts_send_signals
    ts_send_signals -up-> ts_poll_tm
}

ts_send_signals -r-> tm_poll_ts : msg from ""traffic_server"" to ""traffic_manager""
tm_process_events -l-> ts_poll_tm : msg from ""traffic_manager"" to ""traffic_server""


@enduml
