diff --git a/Gemfile b/Gemfile index 5396d4423..5bc617a9c 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem 'will_paginate' gem 'actionmailer' gem 'sendgrid' gem 'rb-readline' +gem 'aasm', '3.0.16' group :development do gem 'pry' diff --git a/bin/websocket_gateway b/bin/websocket_gateway index f49239554..8260d500b 100755 --- a/bin/websocket_gateway +++ b/bin/websocket_gateway @@ -31,4 +31,7 @@ else end ActiveRecord::Base.establish_connection(db_config) -Server.new.run :port => config["port"], :emwebsocket_debug => config["emwebsocket_debug"], :max_stale_connection_time => config["max_stale_connection_time"] +Server.new.run(:port => config["port"], + :emwebsocket_debug => config["emwebsocket_debug"], + :max_stale_connection_time => config["max_stale_connection_time"], + :max_reconnect_time => config["max_reconnect_time"]) diff --git a/config/application.yml b/config/application.yml index df89cddc9..ef2c4e214 100644 --- a/config/application.yml +++ b/config/application.yml @@ -1,15 +1,19 @@ +Defaults: &defaults + max_stale_connection_time: 30 + max_reconnect_time: 180 + development: port: 6767 verbose: true emwebsocket_debug: false - max_stale_connection_time: 30 + <<: *defaults test: port: 6769 verbose: true - max_stale_connection_time: 30 + <<: *defaults production: port: 6767 verbose: false - max_stale_connection_time: 30 + <<: *defaults diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index c8bda4bea..b83e513e1 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -433,9 +433,15 @@ module JamWebsockets # respond with LOGIN_ACK to let client know it was successful #binding.pry + + connection = JamRuby::Connection.find_by_client_id(client_id) remote_ip = extract_ip(client) - login_ack = @message_factory.login_ack(remote_ip, client_id, user.remember_token, @heartbeat_interval) + login_ack = @message_factory.login_ack(remote_ip, + client_id, + user.remember_token, + @heartbeat_interval, + connection.try(:music_session_id)) send_to_client(client, login_ack) @semaphore.synchronize do @@ -448,9 +454,13 @@ module JamWebsockets add_user(context) add_client(client_id, client) # TODO - # log this connection in the database - ConnectionManager.active_record_transaction do |connection_manager| - connection_manager.create_connection(user.id, client.client_id, extract_ip(client)) + if connection + connection.connect! + else + # log this connection in the database + ConnectionManager.active_record_transaction do |connection_manager| + connection_manager.create_connection(user.id, client.client_id, extract_ip(client)) + end end end else diff --git a/lib/jam_websockets/server.rb b/lib/jam_websockets/server.rb index d4eca90f8..be738bba1 100644 --- a/lib/jam_websockets/server.rb +++ b/lib/jam_websockets/server.rb @@ -15,6 +15,7 @@ module JamWebsockets host = "0.0.0.0" port = options[:port] max_stale_connection_time = options[:max_stale_connection_time] + max_reconnect_time = options[:max_reconnect_time] @log.info "starting server #{host}:#{port} with staleness_time=#{max_stale_connection_time}" @@ -27,7 +28,8 @@ module JamWebsockets @router.cleanup end - start_connection_cleaner(max_stale_connection_time) + start_connection_flagger(max_stale_connection_time) + start_connection_cleaner(max_reconnect_time) start_websocket_listener(host, port, options[:emwebsocket_debug]) end @@ -56,6 +58,22 @@ module JamWebsockets connection_manager.remove_stale_connections(stale_max_time) end end + + 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 + end end diff --git a/spec/jam_websockets/router_spec.rb b/spec/jam_websockets/router_spec.rb index 4141e6974..919d39f87 100644 --- a/spec/jam_websockets/router_spec.rb +++ b/spec/jam_websockets/router_spec.rb @@ -42,7 +42,7 @@ def login(router, user, password, client_id) message_factory = MessageFactory.new client = LoginClient.new - login_ack = message_factory.login_ack("127.0.0.1", client_id, user.remember_token, 15) + login_ack = message_factory.login_ack("127.0.0.1", client_id, user.remember_token, 15, nil) router.should_receive(:send_to_client).with(client, login_ack) router.should_receive(:extract_ip).at_least(:once).with(client).and_return("127.0.0.1") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2706e081b..e4c25691f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,8 +8,12 @@ require 'evented-spec' jamenv = ENV['JAMENV'] jamenv ||= 'development' -config = YAML::load(File.open('config/application.yml'))[jamenv] +fn = "#{File.dirname(__FILE__)}/../config/application.yml" +puts "*** spec_helper.rb: fn=#{fn}; #{File.exists?(fn)}; #{jamenv}" +ff = File.open("#{File.dirname(__FILE__)}/../config/application.yml",'r') +config = YAML::load(ff)[jamenv] +puts "*** spec_helper.rb: jamenv=#{jamenv}; config = #{config}" if config["verbose"] Logging.logger.root.level = :debug