Class: MsgPackRPCServer

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack_rpc_server.rb

Overview

This class is a thin layer over MessagePack::RPC::Server, which implements privilege dropping and daemonizing.

Signal handlers call a close method, which the handler class must implement, and in which e.g. database connections are closed.

Server example:

 require 'msgpack_rpc_server'

 msgpack_config = {:uid => 'nobody',
                   :gid => 'nobody',
                   :listen_ip => '127.0.0.1',
                   :port => 12345,
                   :dir => '.',
                   :app_name => 'test_server',
                   :ontop => false,
                   :backtrace => true,
                   :dir_mode => :script,
                   :log_output => true }

class Handler
    def ping(arg); "pong received #{arg}"; end
    def close; nil; end
end

MsgPackRPCServer.new(msgpack_config, Handler.new).run

Client example:

require 'msgpack/rpc'

c = MessagePack::RPC::Client.new('127.0.0.1', 12345)
puts c.call(:ping, "hello")

MessagePack documentation: msgpack.org/

Daemons documentation: daemons.rubyforge.org/

Instance Method Summary collapse

Constructor Details

#initialize(config, handler) ⇒ MsgPackRPCServer

Returns a new instance. The first parameter is a hash containing configuration parameters. Refer to the example and the Daemons library documentation for the format and meanings of configuration parameters. The other parameter is an instance of the handler class.



51
52
53
54
# File 'lib/msgpack_rpc_server.rb', line 51

def initialize(config, handler)
	@config = config
	@handler = handler
end

Instance Method Details

#runObject

Starts the server after daemonizing and privilege dropping. Sets signal handlers for INT and TERM.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/msgpack_rpc_server.rb', line 85

def run
	daemonize
	drop_privileges

	Signal.trap('INT') { log "received INT signal; exiting."; close; exit }
	Signal.trap('TERM') { log "received TERM signal; exiting."; close; exit }

	svr = MessagePack::RPC::Server.new
	svr.listen @config[:listen_ip], @config[:port], @handler
	log "starting."
	svr.run
end