Class: Remotes::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/remotes/protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, input, output, capabilities = []) ⇒ Protocol

Returns a new instance of Protocol.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/remotes/protocol.rb', line 6

def initialize(command, input, output, capabilities = [])
  @command = command
  @input   = input
  @output  = output

  @input.sync = @output.sync = true

  @caps_local  = capabilities
  @caps_remote = nil
  @caps_sent   = false
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/remotes/protocol.rb', line 4

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/remotes/protocol.rb', line 4

def output
  @output
end

Instance Method Details

#capable?(ability) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/remotes/protocol.rb', line 18

def capable?(ability)
  @caps_remote&.include?(ability)
end

#recv_packetObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/remotes/protocol.rb', line 33

def recv_packet
  head = @input.read(4)
  return head unless /[0-9a-f]{4}/ =~ head

  size = head.to_i(16)
  return nil if size == 0

  line = @input.read(size - 4).sub(/\n$/, "")
  detect_caps(line)
end

#recv_until(terminator) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/remotes/protocol.rb', line 44

def recv_until(terminator)
  loop do
    line = recv_packet
    break if line == terminator
    yield line
  end
end

#send_packet(line) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/remotes/protocol.rb', line 22

def send_packet(line)
  return @output.write("0000") if line == nil

  line = append_caps(line)
  size = line.bytesize + 5

  @output.write(size.to_s(16).rjust(4, "0"))
  @output.write(line)
  @output.write("\n")
end