Class: Chromecast::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/chromecast/connection.rb

Constant Summary collapse

DEFAULT_PORT =
8009
DEFAULT_SOURCE =
'source-0'
DEFAULT_DESTINATION =
'receiver-0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
# File 'lib/chromecast/connection.rb', line 15

def initialize(host)
  @host         = host
  @port         = DEFAULT_PORT
  @source       = DEFAULT_SOURCE
  @destination  = DEFAULT_DESTINATION
  @key_file     = 'certificate.key'
  @crt_file     = 'certificate.crt'
end

Instance Attribute Details

#crt_fileObject

Returns the value of attribute crt_file.



13
14
15
# File 'lib/chromecast/connection.rb', line 13

def crt_file
  @crt_file
end

#destinationObject (readonly)

Returns the value of attribute destination.



12
13
14
# File 'lib/chromecast/connection.rb', line 12

def destination
  @destination
end

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/chromecast/connection.rb', line 12

def host
  @host
end

#key_fileObject

Returns the value of attribute key_file.



13
14
15
# File 'lib/chromecast/connection.rb', line 13

def key_file
  @key_file
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/chromecast/connection.rb', line 12

def port
  @port
end

#socketObject (readonly)

Returns the value of attribute socket.



12
13
14
# File 'lib/chromecast/connection.rb', line 12

def socket
  @socket
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/chromecast/connection.rb', line 12

def source
  @source
end

Instance Method Details

#certificate(type) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/chromecast/connection.rb', line 24

def certificate(type)
  case type
    when :crt
      return File.open(crt_file).read
    when :key
      return File.open(key_file).read
  end
  return nil
end

#connectionObject



71
72
73
# File 'lib/chromecast/connection.rb', line 71

def connection
  @connection_channel ||= Channel::Connection.new(self)
end

#has_data?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/chromecast/connection.rb', line 54

def has_data?
  ready = IO.select([@socket], nil, nil, 0)

  return ready != nil
end

#heartbeatObject



79
80
81
# File 'lib/chromecast/connection.rb', line 79

def heartbeat
  @heartbeat_channel ||= Channel::Heartbeat.new(self)
end

#mediaObject



83
84
85
# File 'lib/chromecast/connection.rb', line 83

def media
  @media_channel ||= Channel::Media.new(self)
end

#openObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chromecast/connection.rb', line 34

def open
  tcp = TCPSocket.new(host, port)
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.cert = OpenSSL::X509::Certificate.new(certificate(:crt))
  ctx.key = OpenSSL::PKey::RSA.new(certificate(:key))
  ctx.ssl_version = :SSLv23
  ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)

  @socket = OpenSSL::SSL::SSLSocket.new(tcp, ctx).tap do |socket|
    socket.sync_close = true
    socket.connect
  end

  self
end

#readObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/chromecast/connection.rb', line 60

def read
  return nil unless has_data?

  resp_size = @socket.read(4).unpack('N').first
  resp_data = @socket.read(resp_size)

  resp = Protocol::CastMessage.decode(resp_data)

  JSON.parse(resp.payload_utf8)
end

#receiverObject



75
76
77
# File 'lib/chromecast/connection.rb', line 75

def receiver
  @receiver_channel ||= Channel::Receiver.new(self)
end

#write(data) ⇒ Object



50
51
52
# File 'lib/chromecast/connection.rb', line 50

def write data
  @socket.write data
end