Class: DTAS::UNIXClient

Inherits:
Object
  • Object
show all
Includes:
XS
Defined in:
lib/dtas/unix_client.rb

Overview

a socket connection used by dtas-player clients (e.g. dtas-ctl)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XS

#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_ioObject (readonly)

:nodoc:


12
13
14
# File 'lib/dtas/unix_client.rb', line 12

def to_io
  @to_io
end

Class Method Details

.default_pathObject


16
17
18
# File 'lib/dtas/unix_client.rb', line 16

def self.default_path
  (ENV["DTAS_PLAYER_SOCK"] || File.expand_path("~/.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