class SystemBrowser::Session

This class glues {SystemBrowser::Server} and {SystemBrowser::Client} providing the support for interaction between them.

Attributes

connection[R]

@return [TCPSocket] the connection between the server and the client

Public Class Methods

init() click to toggle source

Initialises a new session.

# File lib/system_browser/session.rb, line 10
def self.init
  if @@running_session
    SLogger.debug("can't init a new session! Kill the old one first")
    return
  else
    @@running_session = true
  end

  self.new(Server.new, Client.new).init
end
new(server, client) click to toggle source
# File lib/system_browser/session.rb, line 24
def initialize(server, client)
  @server = server
  @client = client
  [@server, @client].each { |o| o.session = self }

  @previous_sigint_callback = nil

  self.register_sigint_hook
end

Public Instance Methods

connection=(connection) click to toggle source

@param connection [TCPSocket] the connection between the server and the client @return [void]

# File lib/system_browser/session.rb, line 75
def connection=(connection)
  @connection = connection
  self.initialize_connection
end
destroy() click to toggle source
# File lib/system_browser/session.rb, line 45
def destroy
  self.restore_previous_sigint

  @client.close
  @server.shutdown

  @@running_session = false

  SLogger.debug('[SESSION] the session was destroyed')
end
init() click to toggle source

Runs {SystemBrowser::Server} in background. Invokes {SystemBrowser::Client} and suspends the calling thread for the duration of the client. @return [void]

# File lib/system_browser/session.rb, line 38
def init
  Thread.new { @server.start }
  Thread.new { @client.start }.join

  true
end
send(response) click to toggle source

Sends a response to the client.

@param response [SystemBrowser::Response] the data to be sent to the client @return [void]

# File lib/system_browser/session.rb, line 67
def send(response)
  self.connection.puts(response.to_json)
end
set_client_pid(pid) click to toggle source

Sets the client's window pid (real pid).

# File lib/system_browser/session.rb, line 58
def set_client_pid(pid)
  @client.window_pid = pid
end

Protected Instance Methods

initialize_connection() click to toggle source

This method bootstraps the connection between the server and the client. @return [void]

# File lib/system_browser/session.rb, line 85
def initialize_connection
  self.send(Response.new(action: 'init'))
end
register_sigint_hook() click to toggle source
# File lib/system_browser/session.rb, line 93
def register_sigint_hook
  @previous_sigint_callback = Signal.trap(:INT, '')

  Signal.trap(:INT) do
    SLogger.debug('[SESSION] received Ctrl-C, killing myself softly')

    self.destroy

    if @previous_sigint_callback.instance_of?(Proc)
      self.restore_previous_sigint
      @previous_sigint_callback.call
    end
  end
end
restore_previous_sigint() click to toggle source
# File lib/system_browser/session.rb, line 89
def restore_previous_sigint
  Signal.trap(:INT, @previous_sigint_callback)
end