Class: DTAS::UNIXClient
Overview
a socket connection used by dtas-player clients (e.g. dtas-ctl)
Instance Attribute Summary collapse
-
#to_io ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path = self.class.default_path) ⇒ UNIXClient
constructor
A new instance of UNIXClient.
- #req(args, timeout = nil) ⇒ Object
- #req_ok(args, timeout = nil) ⇒ Object
- #req_start(args) ⇒ Object
- #res_wait(timeout = nil) ⇒ Object
Methods included from XS
Constructor Details
#initialize(path = self.class.default_path) ⇒ UNIXClient
Returns a new instance of UNIXClient.
20 21 22 23 |
# File 'lib/dtas/unix_client.rb', line 20 def initialize(path = self.class.default_path) @to_io = Socket.new(:UNIX, :SEQPACKET, 0) @to_io.connect(Socket.pack_sockaddr_un(path)) end |
Instance Attribute Details
#to_io ⇒ Object (readonly)
:nodoc:
12 13 14 |
# File 'lib/dtas/unix_client.rb', line 12 def to_io @to_io end |
Class Method Details
.default_path ⇒ Object
16 17 18 |
# File 'lib/dtas/unix_client.rb', line 16 def self.default_path (ENV["DTAS_PLAYER_SOCK"] || File.("~/.dtas/player.sock")) end |
Instance Method Details
#req(args, timeout = nil) ⇒ Object
36 37 38 39 |
# File 'lib/dtas/unix_client.rb', line 36 def req(args, timeout = nil) req_start(args) res_wait(timeout) end |
#req_ok(args, timeout = nil) ⇒ Object
30 31 32 33 34 |
# File 'lib/dtas/unix_client.rb', line 30 def req_ok(args, timeout = nil) res = req(args, timeout) res == "OK" or raise "Unexpected response: #{res}" res end |
#req_start(args) ⇒ Object
25 26 27 28 |
# File 'lib/dtas/unix_client.rb', line 25 def req_start(args) args = xs(args) if Array === args @to_io.send(args, Socket::MSG_EOR) end |
#res_wait(timeout = nil) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/dtas/unix_client.rb', line 41 def res_wait(timeout = nil) IO.select([@to_io], nil, nil, timeout) nr = @to_io.nread nr > 0 or raise EOFError, "unexpected EOF from server" @to_io.recv(nr) end |