Class: INatGet::App::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/inat-get/app/core/server.rb

Direct Known Subclasses

API, Console

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

#configObject (readonly)

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

Raises:

  • (ArgumentError)


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

Returns:

  • (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 (private)

Returns:

  • (Boolean)


139
140
141
# File 'lib/inat-get/app/core/server.rb', line 139

def wait_answer?
  true
end

Instance Method Details

#after_loopObject (protected)



64
65
66
# File 'lib/inat-get/app/core/server.rb', line 64

def after_loop
  # do nothing by default
end

#before_loopObject (protected)



60
61
62
# File 'lib/inat-get/app/core/server.rb', line 60

def before_loop
  # do nothing by default
end

#on_error(exception) ⇒ Object (protected)



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 (private)



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

#runObject



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
      # do nothing
    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