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'.freeze

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



134
135
136
# File 'lib/osctl/client.rb', line 134

def close
  @sock.close
end

#cmd(cmd, **opts) ⇒ Object



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

def cmd(cmd, **opts)
  @sock.puts({ cmd:, opts: }.to_json)
end

#cmd_data!(cmd) ⇒ Object



130
131
132
# File 'lib/osctl/client.rb', line 130

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

#cmd_response(cmd) ⇒ Object



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

def cmd_response(cmd, **, &)
  cmd(cmd, **)
  receive_resp(&)
end

#cmd_response!(cmd) ⇒ Object

Raises:



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

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

  ret
end

#data!Object



126
127
128
# File 'lib/osctl/client.rb', line 126

def data!(&)
  receive_resp!(&).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



138
139
140
# File 'lib/osctl/client.rb', line 138

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 += m
    raise Error, '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
    msgs = if @buffer.any?
             @buffer

           else
             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!Object

Raises:



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

def response!(&)
  ret = receive_resp(&)
  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



142
143
144
# File 'lib/osctl/client.rb', line 142

def socket
  @sock
end