Class: TelosLWCP::Engine
- Inherits:
-
Object
- Object
- TelosLWCP::Engine
- Defined in:
- lib/telos_lwcp/engine.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #call_number(line_nr, number) ⇒ Object
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize(host:, port: 20518) ⇒ Engine
constructor
A new instance of Engine.
- #login(user, password) ⇒ Object
- #seize(line_nr) ⇒ Object
- #select_studio(id) ⇒ Object
- #subscribe(command: nil, object: nil, matcher: nil, &block) ⇒ Object
- #wait(expectations, read_timeout: 5, &block) ⇒ Object
-
#write(command, object, arguments = {}) ⇒ Object
Low level.
Constructor Details
#initialize(host:, port: 20518) ⇒ Engine
Returns a new instance of Engine.
9 10 11 12 13 14 15 |
# File 'lib/telos_lwcp/engine.rb', line 9 def initialize host:, port: 20518 self.host = host self.port = port @listeners = Hash.new {|h, k| h[k] = {}} @subscriptions = [] end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
7 8 9 |
# File 'lib/telos_lwcp/engine.rb', line 7 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/telos_lwcp/engine.rb', line 7 def port @port end |
Instance Method Details
#call_number(line_nr, number) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/telos_lwcp/engine.rb', line 57 def call_number(line_nr, number) wait( [:event, "studio.line##{line_nr}"] => proc { true } ) do write :call, "studio.line##{line_nr}", number: number end end |
#connect ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/telos_lwcp/engine.rb', line 17 def connect @server = TCPSocket.new host, port start_reader_thread if block_given? yield self disconnect end end |
#disconnect ⇒ Object
26 27 28 29 |
# File 'lib/telos_lwcp/engine.rb', line 26 def disconnect @reader_thread.terminate if @reader_thread @server.close end |
#login(user, password) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/telos_lwcp/engine.rb', line 31 def login(user, password) wait( [:ack, :cc] => proc {|r| r.arguments[:logged] } ) do write :login, :cc, user: user, password: password end end |
#seize(line_nr) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/telos_lwcp/engine.rb', line 48 def seize(line_nr) wait( [:event, "studio.line##{line_nr}"] => proc { true }, [:ack, "studio.line##{line_nr}"] => proc {|r| Error.new(r.system_items[:msg]) } ) do write :seize, "studio.line##{line_nr}" end end |
#select_studio(id) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/telos_lwcp/engine.rb', line 39 def select_studio(id) wait( [:event, :studio] => proc {|r| r }, [:ack, :studio] => proc {|r| Error.new(r.system_items[:msg]) } ) do write :select, :studio, id: id end end |
#subscribe(command: nil, object: nil, matcher: nil, &block) ⇒ Object
65 66 67 |
# File 'lib/telos_lwcp/engine.rb', line 65 def subscribe(command: nil, object: nil, matcher: nil, &block) @subscriptions << Subscription.new(command: command, object: object, matcher: matcher, block: block) end |
#wait(expectations, read_timeout: 5, &block) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/telos_lwcp/engine.rb', line 75 def wait(expectations, read_timeout: 5, &block) data = nil thread = Thread.current expectations.each do |(cmd, object), proc| @listeners[cmd.to_s][object.to_s] = proc {|d| data = proc.call(d); expectations.each {|(cmd, object), _| @listeners[cmd.to_s][object.to_s] = nil } thread.run } end block.call Timeout.timeout(read_timeout) do Thread.stop end Error === data ? raise(data) : data end |
#write(command, object, arguments = {}) ⇒ Object
Low level
70 71 72 73 |
# File 'lib/telos_lwcp/engine.rb', line 70 def write(command, object, arguments = {}) cmd = Command.outgoing(command, object, arguments) @server.write(cmd.to_s) end |