2012-08-17 03:22:31 +00:00
|
|
|
require 'em-websocket'
|
|
|
|
|
|
|
|
|
|
module JamWebsockets
|
2012-10-07 03:38:38 +00:00
|
|
|
|
2012-08-17 03:22:31 +00:00
|
|
|
class Server
|
|
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
|
@log = Logging.logger[self]
|
|
|
|
|
@count=0
|
2012-10-23 03:16:14 +00:00
|
|
|
@router = Router.new
|
2012-08-17 03:22:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def run(options={})
|
|
|
|
|
|
|
|
|
|
host = "0.0.0.0"
|
|
|
|
|
port = options[:port]
|
2012-10-23 11:42:28 +00:00
|
|
|
max_stale_connection_time = options[:max_stale_connection_time]
|
2013-02-06 13:43:26 +00:00
|
|
|
max_reconnect_time = options[:max_reconnect_time]
|
2012-08-17 03:22:31 +00:00
|
|
|
|
2012-10-23 11:42:28 +00:00
|
|
|
@log.info "starting server #{host}:#{port} with staleness_time=#{max_stale_connection_time}"
|
2012-08-24 02:46:58 +00:00
|
|
|
|
2012-10-23 03:16:14 +00:00
|
|
|
EventMachine.run do
|
2012-10-23 11:42:28 +00:00
|
|
|
@router.start(max_stale_connection_time)
|
2012-08-24 02:46:58 +00:00
|
|
|
|
2012-10-23 03:16:14 +00:00
|
|
|
# if you don't do this, the app won't exit unless you kill -9
|
|
|
|
|
at_exit do
|
|
|
|
|
@log.info "cleaning up server"
|
|
|
|
|
@router.cleanup
|
|
|
|
|
end
|
2012-08-24 02:46:58 +00:00
|
|
|
|
2013-02-06 13:43:26 +00:00
|
|
|
start_connection_flagger(max_stale_connection_time)
|
|
|
|
|
start_connection_cleaner(max_reconnect_time)
|
2012-10-23 11:42:28 +00:00
|
|
|
|
|
|
|
|
start_websocket_listener(host, port, options[:emwebsocket_debug])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def start_websocket_listener(listen_ip, port, emwebsocket_debug)
|
|
|
|
|
EventMachine::WebSocket.start(:host => listen_ip, :port => port, :debug => emwebsocket_debug) do |ws|
|
|
|
|
|
@log.info "new client #{ws}"
|
|
|
|
|
@router.new_client(ws)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_connection_cleaner(stale_max_time)
|
|
|
|
|
# one cleanup on startup
|
|
|
|
|
cleanup_stale_connections(stale_max_time)
|
|
|
|
|
|
|
|
|
|
EventMachine::PeriodicTimer.new(15) do
|
|
|
|
|
cleanup_stale_connections(stale_max_time)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def cleanup_stale_connections(stale_max_time)
|
|
|
|
|
ConnectionManager.active_record_transaction do |connection_manager|
|
|
|
|
|
connection_manager.remove_stale_connections(stale_max_time)
|
2012-10-23 03:16:14 +00:00
|
|
|
end
|
2012-08-17 03:22:31 +00:00
|
|
|
end
|
2013-02-06 13:43:26 +00:00
|
|
|
|
|
|
|
|
def start_connection_flagger(flag_max_time)
|
|
|
|
|
# one cleanup on startup
|
|
|
|
|
flag_stale_connections(flag_max_time)
|
|
|
|
|
|
|
|
|
|
EventMachine::PeriodicTimer.new(15) do
|
|
|
|
|
flag_stale_connections(flag_max_time)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def flag_stale_connections(flag_max_time)
|
|
|
|
|
ConnectionManager.active_record_transaction do |connection_manager|
|
|
|
|
|
connection_manager.flag_stale_connections(flag_max_time)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-08-17 03:22:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|