rkconnect 1.0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +1 -0
  3. data/lib/rkconnect.rb +85 -0
  4. data/lib/thread_pool.rb +33 -0
  5. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ce1b427fd0cc984b4cfdea1ee4235a02e3ef9b9a
4
+ data.tar.gz: bdf9c065ef2c8e3215e04d6105dd14318ad125b7
5
+ SHA512:
6
+ metadata.gz: 0e064a938c118ad9075edbd711cb076ea0115ec0e0454b538e8e35de8757c6dbc9b7f16266698173177ff71f81ad52f49c3c830c589ee2f1e37c2ba8a31f203a
7
+ data.tar.gz: 533e06e99b8fa5a0bc947a9edaf54b881d910f7bfe584f8de42ffa03de4caed51793ebce96812a427ccc752f8cef7674b3d68edc58a0d4305705cfc01b95fb3d
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # RKConnect (Roku Connect)
data/lib/rkconnect.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "net/http"
2
+ require 'net/telnet'
3
+ require "uri"
4
+
5
+ require_relative 'thread_pool'
6
+
7
+ class RKConnect
8
+ # include Observer
9
+ DEBUGGER_PORT = '8085'
10
+ EXTERNAL_C_PORT = '8060'
11
+
12
+ def initialize(ip_address, &block)
13
+ #Check to make sure that the ip address used to initialize is valid
14
+ raise 'Invalid ip_address type. Need a string arg' if !ip_address.is_a? String
15
+ raise 'Invalid ip_address.' if ip_address.split('.').size != 4
16
+ raise 'No callback block was passed in. Make sure a block is passed in as an argument.' if block == nil
17
+
18
+ @model_call = false
19
+
20
+ #Set two different ip addresses. One is just the ip address and the other 'http' is used to send commands to the roku
21
+ @ip_address = ip_address
22
+ @http_address = 'http://' << ip_address << ':'\
23
+
24
+ #Single threads could be used, though using a thread pool could provide future possibilites
25
+ @thread_pool = ThreadPool.new(2)
26
+ @callback = block
27
+
28
+ @roku_debugger = Net::Telnet::new('Host' => @ip_address, 'Port' => DEBUGGER_PORT, 'Telnetmode' => false, 'Waittime' => 1)
29
+ end
30
+
31
+ def post_key(key)
32
+ raise 'Invalid key type. Need a string arg' if !key.is_a? String
33
+
34
+ Net::HTTP.post_form(URI(@http_address + EXTERNAL_C_PORT + "/keypress/#{key}"), {})
35
+ end
36
+
37
+ def post_channel(app_id)
38
+ #Launch a channel based on the application id. Should be only be used in the home screen.
39
+ raise 'Invalid app_id type. Need a string arg' if !app_id.is_a? String
40
+
41
+ Net::HTTP.post_form(URI(@http_address + EXTERNAL_C_PORT + "/launch/#{app_id}"), {})
42
+ end
43
+
44
+ def request_channel_listing()
45
+ #Returns a http response in xml format
46
+ return Net::HTTP.get(URI(@http_address + EXTERNAL_C_PORT + "/query/apps"))
47
+ end
48
+
49
+ def request_debug(request)
50
+ raise 'Invalid request type. Need a string arg' if !request.is_a? String
51
+
52
+ callback_string = ' '
53
+ #Breaks the telnet connection and enters the roku debugger.
54
+ ignore_exceptions{@roku_debugger.cmd("String" => "\03", "Timeout" => 1)}
55
+
56
+ @thread_pool.schedule_jobs do
57
+ @roku_debugger.cmd(request) do | telnet_callback |
58
+ callback_string << telnet_callback
59
+ end
60
+ end
61
+ #There needs to be a sleep here in order to allow the callback for callback_string to return
62
+ sleep 1
63
+
64
+ callback_string.sub!("BrightScript Debugger>", "")
65
+ array = callback_string.split(/\n/)
66
+
67
+ @callback.call(callback_string)
68
+
69
+ ignore_exceptions{@roku_debugger.cmd("String" => "cont", "Timeout" => 1)}
70
+ sleep 1
71
+ end
72
+
73
+ def close_connection
74
+ @roku_debugger.close
75
+ end
76
+
77
+ private
78
+ def ignore_exceptions
79
+ begin
80
+ yield
81
+ rescue Exception => e
82
+ # puts(e.message)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,33 @@
1
+ class ThreadPool
2
+ attr_reader :pool
3
+
4
+ def initialize(size)
5
+ @size = size
6
+ @job_queue = Queue.new
7
+
8
+ @pool = Array.new(@size) do |i|
9
+ Thread.new do
10
+ Thread.current[:id] = i
11
+
12
+ catch(:exit) do
13
+ loop do
14
+ job, args = @job_queue.pop
15
+ job.call(*args)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def schedule_jobs(*args, &block)
23
+ @job_queue << [block, args]
24
+ end
25
+
26
+ def kill()
27
+ @size.times do
28
+ schedule_jobs{throw :exit}
29
+ end
30
+
31
+ @pool.map(&:join)
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rkconnect
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Isaiah Soung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An interface that connects to the roku.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/rkconnect.rb
21
+ - lib/thread_pool.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Provides an interface to connect to a roku, send commands to the roku, and
46
+ receive roku debugger messages.
47
+ test_files: []