Class: OsCtl::Client
- Inherits:
-
Object
- Object
- OsCtl::Client
- Defined in:
- lib/osctl/client.rb
Defined Under Namespace
Constant Summary collapse
- SOCKET =
'/run/osctl/osctld.sock'.freeze
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #close ⇒ Object
- #cmd(cmd, **opts) ⇒ Object
- #cmd_data!(cmd) ⇒ Object
- #cmd_response(cmd) ⇒ Object
- #cmd_response!(cmd) ⇒ Object
- #data! ⇒ Object
-
#initialize(sock = SOCKET) ⇒ Client
constructor
A new instance of Client.
- #open ⇒ Object
- #parse(raw) ⇒ Object
- #receive ⇒ Object
- #receive_resp(&block) ⇒ Object
- #receive_version ⇒ Object
- #response! ⇒ Object
- #send_io(io) ⇒ Object
- #socket ⇒ Object
Constructor Details
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
44 45 46 |
# File 'lib/osctl/client.rb', line 44 def version @version end |
Instance Method Details
#close ⇒ Object
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
119 120 121 122 123 124 |
# File 'lib/osctl/client.rb', line 119 def cmd_response!(cmd, **, &) ret = cmd_response(cmd, **, &) raise Error, ret. if ret.error? ret end |
#data! ⇒ Object
126 127 128 |
# File 'lib/osctl/client.rb', line 126 def data!(&) receive_resp!(&).data end |
#open ⇒ Object
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 |
#receive ⇒ Object
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.) if block end end end |
#receive_version ⇒ Object
77 78 79 |
# File 'lib/osctl/client.rb', line 77 def receive_version parse(receive[0]) end |
#response! ⇒ Object
107 108 109 110 111 112 |
# File 'lib/osctl/client.rb', line 107 def response!(&) ret = receive_resp(&) raise Error, ret. 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 |
#socket ⇒ Object
142 143 144 |
# File 'lib/osctl/client.rb', line 142 def socket @sock end |