Class: OsCtl::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/client.rb

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

SOCKET =
'/run/osctl/osctld.sock'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock = SOCKET) ⇒ Client

Returns a new instance of Client



46
47
48
49
# File 'lib/osctl/client.rb', line 46

def initialize(sock = SOCKET)
  @sock_path = sock
  @buffer = []
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version



44
45
46
# File 'lib/osctl/client.rb', line 44

def version
  @version
end

Instance Method Details

#closeObject



132
133
134
# File 'lib/osctl/client.rb', line 132

def close
  @sock.close
end

#cmd(cmd, opts = {}) ⇒ Object



57
58
59
# File 'lib/osctl/client.rb', line 57

def cmd(cmd, opts = {})
  @sock.send({:cmd => cmd, :opts => opts}.to_json + "\n", 0)
end

#cmd_data!(*args, &block) ⇒ Object



128
129
130
# File 'lib/osctl/client.rb', line 128

def cmd_data!(*args, &block)
  cmd_response!(*args, &block).data
end

#cmd_response(cmd, opts = {}, &block) ⇒ Object



113
114
115
116
# File 'lib/osctl/client.rb', line 113

def cmd_response(cmd, opts = {}, &block)
  cmd(cmd, opts)
  receive_resp(&block)
end

#cmd_response!(*args, &block) ⇒ Object

Raises:



118
119
120
121
122
# File 'lib/osctl/client.rb', line 118

def cmd_response!(*args, &block)
  ret = cmd_response(*args, &block)
  raise Error, ret.message if ret.error?
  ret
end

#data!(&block) ⇒ Object



124
125
126
# File 'lib/osctl/client.rb', line 124

def data!(&block)
  receive_resp!(&block).data
end

#openObject



51
52
53
54
55
# File 'lib/osctl/client.rb', line 51

def open
  @sock = UNIXSocket.new(@sock_path)
  greetings = receive_version
  @version = greetings[:version]
end

#parse(raw) ⇒ Object



136
137
138
# File 'lib/osctl/client.rb', line 136

def parse(raw)
  JSON.parse(raw, symbolize_names: true)
end

#receiveObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/osctl/client.rb', line 65

def receive
  buf = ""

  while m = @sock.recv(1024)
    buf = buf + m
    fail 'osctld closed connection' if m.nil? || m.empty?
    break if m[-1].chr == "\n"
  end

  buf.split("\n")
end

#receive_resp(&block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/osctl/client.rb', line 81

def receive_resp(&block)
  loop do
    if @buffer.any?
      msgs = @buffer

    else
      msgs = receive
    end

    while msgs.any?
      msg = msgs.shift

      resp = Response.new(parse(msg))

      # Proper response
      unless resp.update?
        @buffer = msgs
        return resp
      end

      # Update
      block.call(resp.message) if block
    end
  end
end

#receive_versionObject



77
78
79
# File 'lib/osctl/client.rb', line 77

def receive_version
  parse(receive[0])
end

#response!(&block) ⇒ Object

Raises:



107
108
109
110
111
# File 'lib/osctl/client.rb', line 107

def response!(&block)
  ret = receive_resp(&block)
  raise Error, ret.message if ret.error?
  ret
end

#send_io(io) ⇒ Object



61
62
63
# File 'lib/osctl/client.rb', line 61

def send_io(io)
  @sock.send_io(io)
end

#socketObject



140
141
142
# File 'lib/osctl/client.rb', line 140

def socket
  @sock
end