Class: INatGet::App::Server
- Inherits:
-
Object
- Object
- INatGet::App::Server
show all
- Defined in:
- lib/inat-get/app/core/server.rb
Defined Under Namespace
Classes: API, Console, Proxy
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(socket_path, **params) ⇒ Server
Returns a new instance of Server.
15
16
17
18
19
|
# File 'lib/inat-get/app/core/server.rb', line 15
def initialize socket_path, **params
@socket_path = socket_path
@params = params
@config = ::INatGet::App::Setup::config
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
13
14
15
|
# File 'lib/inat-get/app/core/server.rb', line 13
def config
@config
end
|
Class Method Details
.create(socket_path, **params) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/inat-get/app/core/server.rb', line 115
def create socket_path, **params
@proxies ||= {}
raise ArgumentError, "Server already created: #{ socket_path }", caller_locations if @proxies.has_key?(socket_path)
FileUtils.mkdir_p File.dirname(socket_path)
pid = fork do
server = new(socket_path, **params)
server.run
end
detacher = Process::detach pid
@proxies[socket_path] = INatGet::App::Server::Proxy::new(detacher, socket_path, wait_answer?)
@proxies[socket_path]
end
|
.used?(socket_path) ⇒ Boolean
128
129
130
131
132
133
|
# File 'lib/inat-get/app/core/server.rb', line 128
def used? socket_path
proxy = INatGet::App::Server::Proxy::new nil, socket_path, true
proxy.ping == :pong
rescue
return false
end
|
.wait_answer? ⇒ Boolean
139
140
141
|
# File 'lib/inat-get/app/core/server.rb', line 139
def wait_answer?
true
end
|
Instance Method Details
#after_loop ⇒ Object
64
65
66
|
# File 'lib/inat-get/app/core/server.rb', line 64
def after_loop
end
|
#before_loop ⇒ Object
60
61
62
|
# File 'lib/inat-get/app/core/server.rb', line 60
def before_loop
end
|
#on_error(exception) ⇒ Object
68
69
70
71
|
# File 'lib/inat-get/app/core/server.rb', line 68
def on_error exception
warn exception
warn exception.backtrace
end
|
#process_msg(msg) ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/inat-get/app/core/server.rb', line 47
def process_msg msg
method = msg[:method]
return :quit if method == :quit
return :pong if method == :ping
args = msg[:args] || []
kwargs = msg[:kwargs] || {}
self.send method, *args, **kwargs
rescue => e
on_error e
end
|
#run ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/inat-get/app/core/server.rb', line 21
def run
@server = ::UNIXServer::new @socket_path
@server.listen 1024
before_loop
loop do
client = @server.accept
msg = ::Marshal.load client
result = process_msg msg
case result
when nil
when :quit
::Marshal.dump result, client
break
else
::Marshal.dump result, client
end
client.close
end
after_loop
@server.close
::File.delete @socket_path
end
|