Class: Terminalwire::Client::Handler

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/terminalwire/client/handler.rb

Overview

The handler is the main class that connects to the Terminalwire server and dispatches messages to the appropriate resources.

Constant Summary collapse

VERSION =

The version of the Terminalwire client.

Terminalwire::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, arguments: ARGV, program_name: $0, endpoint:) {|_self| ... } ⇒ Handler

Returns a new instance of Handler.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/terminalwire/client/handler.rb', line 13

def initialize(adapter, arguments: ARGV, program_name: $0, endpoint:)
  @endpoint = endpoint
  @adapter = adapter
  @program_arguments = arguments
  @program_name = program_name
  @entitlement = Entitlement::Policy.resolve(authority: @endpoint.authority)

  yield self if block_given?

  @resources = Resource::Handler.new do |it|
    it << Resource::STDOUT.new("stdout", @adapter, entitlement:)
    it << Resource::STDIN.new("stdin", @adapter, entitlement:)
    it << Resource::STDERR.new("stderr", @adapter, entitlement:)
    it << Resource::Browser.new("browser", @adapter, entitlement:)
    it << Resource::File.new("file", @adapter, entitlement:)
    it << Resource::Directory.new("directory", @adapter, entitlement:)
    it << Resource::EnvironmentVariable.new("environment_variable", @adapter, entitlement:)
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



10
11
12
# File 'lib/terminalwire/client/handler.rb', line 10

def adapter
  @adapter
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/terminalwire/client/handler.rb', line 10

def endpoint
  @endpoint
end

#entitlementObject

Returns the value of attribute entitlement.



11
12
13
# File 'lib/terminalwire/client/handler.rb', line 11

def entitlement
  @entitlement
end

#resourcesObject (readonly)

Returns the value of attribute resources.



10
11
12
# File 'lib/terminalwire/client/handler.rb', line 10

def resources
  @resources
end

Instance Method Details

#connectObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/terminalwire/client/handler.rb', line 41

def connect
  verify_license

  @adapter.write(
    event: "initialization",
    protocol: { version: VERSION },
    entitlement: @entitlement.serialize,
    program: {
      name: @program_name,
      arguments: @program_arguments
    }
  )

  loop do
    handle @adapter.read
  end
end

#handle(message) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/terminalwire/client/handler.rb', line 59

def handle(message)
  case message
  in { event: "resource", action: "command", name:, parameters: }
    @resources.dispatch(**message)
  in { event: "exit", status: }
    exit Integer(status)
  end
end

#verify_licenseObject



33
34
35
36
37
38
39
# File 'lib/terminalwire/client/handler.rb', line 33

def verify_license
  # Connect to the Terminalwire license server to verify the URL endpoint
  # and displays a message to the user, if any are present.
  $stdout.print ServerLicenseVerification.new(url: @endpoint.to_url).message
rescue
  $stderr.puts "Failed to verify server license."
end