Ping/Ack

@startuml
autonumber

participant ApplicationManager as AM

participant RialtoServerManager as RSM
participant RialtoApplicationSessionServer as RASS
participant RialtoClient as RC

== Periodic health check ==

loop Every [5] seconds
  loop For each Application Session Server

    RSM  --/  RASS:  ping(unique_id)
    RSM  ->   RSM:   SetTimer(healthcheck_frequency[seconds], app_server_id)
    RASS --/  RC:    ping(unique_id)

    note left
      This step and next step should be skipped if
      Rialto Client hasn't established an IPC 
      connection - the app session server can ack
      the ping immediately
    end note

    alt Happy case, ping is acknowledged
      RC   --/  RASS:  ack(unique_id)
      RASS --/  RSM:   ack(unique_id)
      opt unique_id in ack matches unique id in ping
        RSM  ->   RSM:   CancelTimer(app_server_id)
      end
    else Timer fires (Ack not received in time)
      alt App session server is not assigned to an app yet (preloaded)
        RSM  ->   RSM:   Kill app session server & spawn a new one
      else App session server is assigned to an app
        RSM  --/  AM:    appFailed(application_id)
      end
    end
    note across
        App Manager should process the error accordingly, e.g. display an error, restart the app etc.
        Rialto will take no further action until App Manager requests the moving app to the not running state
    end note
  end
end

== Unexpected socket disconnect ==

RASS --/  RSM:   Socket connection lost
note right
  Rialto Server Manager detects connection
  to app session server lost
end note
alt App session server is not assigned to an app yet (preloaded)
  RSM  ->   RSM:   Kill app session server & spawn a new one
else App session server is assigned to an app
  RSM  --/  AM:    appFailed(application_id)
end


@enduml