From c520fb71a71d09a9a8d9672aa1b5cb7735c1cac2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 16 Sep 2010 21:34:15 -0700 Subject: [PATCH] Moved socket out to separate process. Multiple thins should now be possible --- Gemfile | 5 ++++- Gemfile.lock | 10 ++++++++++ app/controllers/sockets_controller.rb | 2 +- app/models/post.rb | 1 + config/app_config.yml | 1 + config/initializers/socket.rb | 26 -------------------------- lib/diaspora/websocket.rb | 6 +++++- 7 files changed, 22 insertions(+), 29 deletions(-) delete mode 100644 config/initializers/socket.rb diff --git a/Gemfile b/Gemfile index 77627808d..cc92bec3d 100644 --- a/Gemfile +++ b/Gemfile @@ -28,9 +28,12 @@ gem 'redfinger', :git => 'git://github.com/rsofaer/redfinger.git' #EventMachine gem 'em-http-request',:git => 'git://github.com/igrigorik/em-http-request.git', :require => 'em-http' -gem 'em-websocket' gem 'thin' +#Websocket +gem 'em-websocket' +gem 'magent', :git => 'http://github.com/dcu/magent.git' + #File uploading gem 'carrierwave', :git => 'git://github.com/rsofaer/carrierwave.git' , :branch => 'master' #Untested mongomapper branch gem 'mini_magick' diff --git a/Gemfile.lock b/Gemfile.lock index 007810850..dd181b320 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,14 @@ GIT bcrypt-ruby (~> 2.1.2) warden (~> 0.10.7) +GIT + remote: http://github.com/dcu/magent.git + revision: 06513f3dac812469a55f2e365c349af4d2abc92a + specs: + magent (0.4.2) + mongo (>= 0.1.0) + uuidtools (>= 2.0.0) + GIT remote: http://github.com/jnunemaker/mongomapper.git revision: 931dab779011aa7acf60c1a4c7ad19e1ba838345 @@ -213,6 +221,7 @@ GEM treetop (1.4.8) polyglot (>= 0.3.1) tzinfo (0.3.23) + uuidtools (2.1.1) warden (0.10.7) rack (>= 1.0.0) webmock (1.3.5) @@ -242,6 +251,7 @@ DEPENDENCIES haml jnunemaker-validatable (= 1.8.4)! json + magent! mini_magick mocha mongo_mapper (= 0.8.4)! diff --git a/app/controllers/sockets_controller.rb b/app/controllers/sockets_controller.rb index 2852a7160..9b90ea831 100644 --- a/app/controllers/sockets_controller.rb +++ b/app/controllers/sockets_controller.rb @@ -14,7 +14,7 @@ class SocketsController < ApplicationController def outgoing(uid,object,opts={}) @_request = ActionDispatch::Request.new({}) - Diaspora::WebSocket.push_to_user(uid, action_hash(uid, object, opts)) + Diaspora::WebSocket.queue_to_user(uid, action_hash(uid, object, opts)) end end diff --git a/app/models/post.rb b/app/models/post.rb index e2dd53d3d..9f7c3ff27 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -4,6 +4,7 @@ class Post + require 'lib/diaspora/websocket' require 'lib/encryptable' include MongoMapper::Document include ApplicationHelper diff --git a/config/app_config.yml b/config/app_config.yml index 0a206c366..a138424ee 100644 --- a/config/app_config.yml +++ b/config/app_config.yml @@ -8,6 +8,7 @@ development: debug: false socket_debug : false socket_port: 8080 + socket_collection_name: 'websocket' pubsub_server: 'https://pubsubhubbub.appspot.com/' test: diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb deleted file mode 100644 index 0b1035001..000000000 --- a/config/initializers/socket.rb +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3. See -# the COPYRIGHT file. - - -require 'em-websocket' -require 'eventmachine' -require 'lib/diaspora/websocket' - EM.next_tick { - Diaspora::WebSocket.initialize_channels - - EventMachine::WebSocket.start( - :host => "0.0.0.0", - :port => APP_CONFIG[:socket_port], - :debug =>APP_CONFIG[:socket_debug]) do |ws| - ws.onopen { - - sid = Diaspora::WebSocket.subscribe(ws.request['Path'].gsub('/',''), ws) - - ws.onmessage { |msg| SocketsController.new.incoming(msg) } - - ws.onclose { Diaspora::WebSocket.unsubscribe(ws.request['Path'].gsub('/',''), sid) } - } - end - } - diff --git a/lib/diaspora/websocket.rb b/lib/diaspora/websocket.rb index fc79559f6..687bd2b7c 100644 --- a/lib/diaspora/websocket.rb +++ b/lib/diaspora/websocket.rb @@ -6,6 +6,11 @@ module Diaspora module WebSocket + def self.queue_to_user(uid, data) + channel = Magent::GenericChannel.new('websocket') + channel.enqueue({:uid => uid, :data => data}) + end + def self.initialize_channels @channels = {} end @@ -44,6 +49,5 @@ module Diaspora def unsocket_from_uid(id, opts={}) SocketsController.new.outgoing(id, Retraction.for(self), opts) end - end end