* websocket gateway allows login to server and to session

This commit is contained in:
Seth Call 2012-08-23 21:46:58 -05:00
parent 11e403deb3
commit 89a4be2321
9 changed files with 86 additions and 41 deletions

1
.pg_migrate Normal file
View File

@ -0,0 +1 @@
up.connopts=dbname:jam

View File

@ -7,7 +7,7 @@ gem 'uuidtools', '2.1.2'
gem 'bcrypt-ruby', '3.0.1'
gem 'jruby-openssl'
gem 'ruby-protocol-buffers', '1.2.2'
gem 'pg_migrate', '0.1.4'
gem 'pg_migrate','0.1.5' #:path => '~/workspace/pg_migrate_ruby' #'0.1.4'
gem 'jam_db', :path => '~/workspace/jam-db/target/ruby_package'
gem 'jam_ruby', :path => '~/workspace/jam-ruby'
gem 'jampb', :path => '~/workspace/jam-pb/target/ruby/jampb'

View File

@ -10,6 +10,7 @@ jamenv = ENV['JAMENV']
jamenv ||= 'development'
config = YAML::load(File.open('config/application.yml'))[jamenv]
db_config = YAML::load(File.open('config/database.yml'))[jamenv]
if config["verbose"]
@ -20,4 +21,5 @@ end
Logging.logger.root.appenders = Logging.appenders.stdout
ActiveRecord::Base.establish_connection(db_config)
Server.new.run :port => config["port"], :debug => false#=> config["verbose"]

View File

@ -1,4 +1,4 @@
#!/bin/sh
# this script is used only in development
bundle exec ruby -Ilib bin/jam_websockets $*
bundle exec ruby -Ilib bin/websocket_gateway $*

View File

@ -8,3 +8,14 @@ test:
password: postgres
timeout: 2000
encoding: unicode
development:
adapter: jdbcpostgresql
database: jam
host: localhost
port: 5432
pool: 3
username: postgres
password: postgres
timeout: 2000
encoding: unicode

View File

@ -1,9 +1,12 @@
require "logging"
require "jam_ruby"
require "jam_websockets/version"
require "jam_websockets/client_context"
require "jam_websockets/router"
require "jam_websockets/server"
include JamRuby
module JamWebsockets
# Your code goes here...
end

View File

@ -14,7 +14,7 @@ module JamWebsockets
@connection = nil
@channel = nil
@endpoints = nil
@message_factory = MessageFactory.new
@message_factory = JamRuby::MessageFactory.new
end
def start(options = {})
@ -36,18 +36,9 @@ module JamWebsockets
end
def cleanup()
@clients.each do |key, context|
begin
# todo delegate to client cleanup method
if context.subscription.active?
context.subscription.shutdown!
end
rescue => e
@log.debug "unable to cancel subscription on cleanup: #{e}"
end
#context.user_queue.unbind(@endpoints)
@clients.each do |client, context|
cleanup_client(client)
#context.user_queue.unbind(@endpoints)
end
if !@channel.nil?
@ -56,33 +47,33 @@ module JamWebsockets
if !@connection.nil?
@connection.close
end
end
end
end
def stop
@log.debug "shutdown"
cleanup
end
def stop
@log.debug "shutdown"
cleanup
end
def new_client(client)
def new_client(client)
@pending_clients.add(client)
@pending_clients.add(client)
client.onopen {
@log.debug "client connected #{client}"
}
client.onopen {
@log.debug "client connected #{client}"
}
client.onclose {
@log.debug "Connection closed"
client.onclose {
@log.debug "Connection closed"
cleanup_client(client)
cleanup_client(client)
}
client.onerror { |error|
if error.kind_of?(EM::WebSocket::WebSocketError)
@log.error "websockets error: #{error}"
else
@log.error "generic error #{error}"
@log.error "generic error: #{error} #{error.backtrace}"
end
cleanup_client(client)
@ -94,11 +85,12 @@ module JamWebsockets
# TODO: set a max message size before we put it through PB?
# TODO: rate limit?
pb_msg = Jampb::ClientMessage.parse(msg.to_s)
begin
pb_msg = Jampb::ClientMessage.parse(msg.to_s)
self.route(pb_msg, client)
rescue => e
@log.debug "ending client session due to error: #{e.to_s} #{e.backtrace}"
@log.debug "ending client session due to error: #{e.to_s}"
@log.debug e
begin
# wrap the message up and send it down
@ -116,16 +108,31 @@ module JamWebsockets
def cleanup_client(client)
@log.debug "cleaning up client #{client}"
begin
@pending_clients.delete(client)
@pending_clients.delete(client)
context = @clients.delete(client)
if !context.nil?
begin
if context.subscription.active?
@log.debug "cleaning up user subscription"
context.subscription.shutdown!
end
if !context.session_topic.nil? && context.session_topic.active?
@log.debug "cleaning up session subscription"
context.session_topic.shutdown!
end
rescue => e
@log.debug "unable to cancel subscription on cleanup: #{e}"
end
if !context.nil?
mark_context_for_deletion(context)
end
ensure
client.close
client.close_websocket
end
end
@ -135,7 +142,7 @@ module JamWebsockets
end
def extract_inner_message(client_msg)
msg = client_msg.fields[client_msg.type]
msg = client_msg.value_for_tag(client_msg.type)
if msg.nil?
raise "inner message is null. type: #{client_msg.type}, target: #{client_msg.target}"
@ -183,9 +190,13 @@ module JamWebsockets
handle_login(client_msg.login, client)
elsif client_msg.type == ClientMessage::Type::HEARTBEAT
handle_heartbeat(client_msg.heartbeat, client)
elsif client_msg.type == ClientMessage::Type::LOGIN_JAM_SESSION
handle_join_jam_session(client_msg.join_jam_session, client)
handle_join_jam_session(client_msg.login_jam_session, client)
elsif client_msg.type == ClientMessage::Type::LEAVE_JAM_SESSION
@ -221,7 +232,7 @@ module JamWebsockets
end
# respond with LOGIN_ACK to let client know it was successful
client.send(@message_factory.login_ack(client.ip).to_s)
client.send(@message_factory.login_ack(client.request["origin"]).to_s)
# remove from pending_queue
@pending_clients.delete(client)
@ -234,6 +245,10 @@ module JamWebsockets
end
end
def handle_heartbeat(heartbeat, client)
# todo
end
def handle_join_jam_session(join_jam_session, client)
# verify that the current user has the rights to actually join the jam session
context = @clients[client]
@ -241,9 +256,10 @@ module JamWebsockets
session_id = join_jam_session.jam_session;
begin
access_jam_session?(session_id)
access_jam_session?(session_id, context.user)
rescue => e
# send back a failure ack and bail
@log.debug "client requested non-existent session. client:#{client.request['origin']} user:#{context.user.email}"
client.send(@message_factory.login_jam_session_ack(true, e.to_s).to_s)
return
end
@ -314,7 +330,7 @@ module JamWebsockets
end
def access_jam_session?(jam_session_id, user)
jam_session = JamSession.find(jam_session_id)
jam_session = JamSession.find_by_id(jam_session_id)
if jam_session.nil?
raise 'specified session not found'

View File

@ -15,9 +15,18 @@ module JamWebsockets
port = options[:port]
@log.debug "starting server #{host}:#{port}"
@router.start
# if you don't do this, the app won't exit unless you kill -9
at_exit do
@log.debug "cleaning up server"
@router.cleanup
end
EventMachine.run {
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => options[:port], :debug => options[:debug]) do |ws|
@log.debug "new client #{ws}"
@router.new_client(ws)
end
}

3
migrate.sh Executable file
View File

@ -0,0 +1,3 @@
bundle exec jam_db up --connopts=dbname:jam host:localhost user:postgres password:postgres --verbose