Class: DTAS::UNIXClient
- Inherits:
-
Object
- Object
- DTAS::UNIXClient
- Defined in:
- lib/dtas/unix_client.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#to_io ⇒ Object
readonly
Returns the value of attribute to_io.
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
Constructor Details
#initialize(path = self.class.default_path) ⇒ UNIXClient
Returns a new instance of UNIXClient.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dtas/unix_client.rb', line 16 def initialize(path = self.class.default_path) @to_io = begin raise if ENV["_DTAS_NOSEQPACKET"] Socket.new(:AF_UNIX, :SOCK_SEQPACKET, 0) rescue warn("get your operating system developers to support " \ "SOCK_SEQPACKET for AF_UNIX sockets") warn("falling back to SOCK_DGRAM, reliability possibly compromised") Socket.new(:AF_UNIX, :SOCK_DGRAM, 0) end @to_io.connect(Socket.pack_sockaddr_un(path)) end |
Instance Attribute Details
#to_io ⇒ Object (readonly)
Returns the value of attribute to_io.
10 11 12 |
# File 'lib/dtas/unix_client.rb', line 10 def to_io @to_io end |
Class Method Details
.default_path ⇒ Object
12 13 14 |
# File 'lib/dtas/unix_client.rb', line 12 def self.default_path (ENV["DTAS_PLAYER_SOCK"] || File.("~/.dtas/player.sock")).b end |
Instance Method Details
#req(args, timeout = nil) ⇒ Object
40 41 42 43 |
# File 'lib/dtas/unix_client.rb', line 40 def req(args, timeout = nil) req_start(args) res_wait(timeout) end |
#req_ok(args, timeout = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/dtas/unix_client.rb', line 34 def req_ok(args, timeout = nil) res = req(args, timeout) res == "OK" or raise "Unexpected response: #{res}" res end |
#req_start(args) ⇒ Object
29 30 31 32 |
# File 'lib/dtas/unix_client.rb', line 29 def req_start(args) args = Shellwords.join(args) if Array === args @to_io.send(args, Socket::MSG_EOR) end |
#res_wait(timeout = nil) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/dtas/unix_client.rb', line 45 def res_wait(timeout = nil) @to_io.wait(timeout) nr = @to_io.nread nr > 0 or raise EOFError, "unexpected EOF from server" @to_io.recvmsg[0] end |