Class: Neutron::Controller
- Inherits:
-
UNIXServer
- Object
- UNIXServer
- Neutron::Controller
- Defined in:
- lib/neutron/controller.rb
Constant Summary collapse
- PARSE_ERROR =
-32700
- INVALID_REQUEST =
-32600
- METHOD_NOT_FOUND =
-32601
- INTERNAL_ERROR =
-32603
- SERVER_ERROR =
-32000
Instance Method Summary collapse
-
#initialize(path = '/tmp/neutron.sock') ⇒ Controller
constructor
A new instance of Controller.
- #notify(event, result = nil) ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(path = '/tmp/neutron.sock') ⇒ Controller
Returns a new instance of Controller.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/neutron/controller.rb', line 10 def initialize(path = '/tmp/neutron.sock') File.delete(path) if File.exists?(path) super(path) @path = path @run = true trap('INT') do STDOUT.puts 'Closing socket...' self.stop end end |
Instance Method Details
#notify(event, result = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/neutron/controller.rb', line 54 def notify(event, result = nil) if result.is_a?(Hash) && result[:error].is_a?(Hash) result[:error][:code] ||= SERVER_ERROR result[:error][:message] ||= 'Server error' response = {jsonrpc: '2.0', error: result[:error], event: event, id: -1} else response = {jsonrpc: '2.0', result: result, event: event, id: -1} end sock = Thread.current[:sock] if sock send_string(sock, response.to_json + "\n") end end |
#run ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/neutron/controller.rb', line 29 def run while @run do begin Thread.fork(self.accept) do |sock| # begin Thread.current[:threads] = {} while (data = sock.gets) break if data.empty? data.chop! # STDOUT.puts data Thread.fork(sock, data, Thread.current[:threads]) do |sock, data, threads| Thread.current[:sock] = sock Thread.current[:threads] = threads response = dispatch(data) if response send_string(sock, response.to_json + "\n") end end end end rescue Errno::EBADF # socket has been closed and server is waiting on socket.accept end end end |
#stop ⇒ Object
23 24 25 26 27 |
# File 'lib/neutron/controller.rb', line 23 def stop @run = false self.close File.delete(@path) if File.exists?(@path) end |