Skip to content

Commit

Permalink
Merge branch 'develop' into feature/update-celuloid-0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed Mar 2, 2016
2 parents 9184c73 + 2aa8fe9 commit 3b2d18b
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 165 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description

CelluloidPubsub is a simple ruby implementation of publish subscribe design patterns using celluloid actors and websockets, using Celluloid::Reel server

Starting with version 0.4.0, Redis support was added [courtesy of em-hiredis](https://github.com/mloughran/em-hiredis)
Starting with version 0.6.0, Redis support was moved into gem [celluloid_pubsub_redis_adapter](https://github.com/bogdanRada/celluloid_pubsub_redis_adapter)

Requirements
------------
Expand All @@ -20,8 +20,7 @@ Requirements
5. [http >= 1.0.2](https://github.com/httprb/http)
6. [Celluloid-websocket-client = 0.0.1](https://github.com/jeremyd/celluloid-websocket-client)
7. [ActiveSuport >= 4.2.0](https://rubygems.org/gems/activesupport)
8. [em-hiredis >= 0.3.0](https://github.com/mloughran/em-hiredis)
9. [json >= 1.8.3](https://github.com/flori/json)
8. [json >= 1.8.3](https://github.com/flori/json)

Compatibility
-------------
Expand Down Expand Up @@ -51,7 +50,7 @@ Creating a websocket server is simple as doing this. This are all the options av
```ruby
CelluloidPubsub::WebServer.supervise_as(:web_server,
enable_debug: true, # if debug messages should be logged
use_redis: false , # if set to true, will instantiate a RedisReactor class to handle each connection, which requires Redis to be available. Otherwise will use a simple Reactor to handle the connections which has no dependencies .
adapter: nil , # if set to nil, will instantiate a simple Reactor to handle the connections which has no dependencies . Otherwise will try to use that adapter. Please see [celluloid_pubsub_redis_adapter](https://github.com/bogdanRada/celluloid_pubsub_redis_adapter) for more details on using redis adapter
log_file_path: "path/to/log_file.log", # The log file where all debugging information will be printed
hostname: "0.0.0.0", # the hostname of the server.
port: 1234, # the port on which the server will listen for connections
Expand Down
4 changes: 2 additions & 2 deletions celluloid_pubsub.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'reel', '~> 0.6', '>= 0.6.0'
s.add_runtime_dependency 'http', '~> 1.0', '>= 1.0.2'
s.add_runtime_dependency 'celluloid-websocket-client', '~> 0.0', '>= 0.0.1'
s.add_runtime_dependency 'activesupport', '~> 4.1', '>= 4.1.0'
s.add_runtime_dependency 'em-hiredis', '~> 0.3', '>= 0.3.0'
s.add_runtime_dependency 'activesupport', '>= 4.0', '>= 4.0'
s.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3'


s.add_development_dependency 'rspec', '~> 3.4', '>= 3.4'
s.add_development_dependency 'simplecov', '~> 0.10', '>= 0.10'
s.add_development_dependency 'simplecov-summary', '~> 0.0.4', '>= 0.0.4'
Expand Down
2 changes: 0 additions & 2 deletions examples/redis_test.rb

This file was deleted.

72 changes: 0 additions & 72 deletions examples/shared_classes.rb

This file was deleted.

142 changes: 72 additions & 70 deletions examples/simple_test.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
# <<<<<<< HEAD
# require 'bundler/setup'
# require 'celluloid_pubsub'
# require 'logger'
#
# debug_enabled = ENV['DEBUG'].present? && ENV['DEBUG'].to_s == 'true'
#
# if debug_enabled == true
# log_file_path = File.join(File.expand_path(File.dirname(__FILE__)), 'log', 'celluloid_pubsub.log')
# puts log_file_path
# puts CelluloidPubsub::BaseActor.celluloid_version
# puts CelluloidPubsub::BaseActor.version_less_than_sixten?
# FileUtils.rm(log_file_path) if File.exist?(log_file_path)
# FileUtils.mkdir_p(File.dirname(log_file_path))
# log_file = File.open(log_file_path, 'w')
# log_file.sync = true
# $CELLULOID_DEBUG = true
# Celluloid.logger = ::Logger.new(log_file_path)
# end
#
# # actor that subscribes to a channel
# class Subscriber < CelluloidPubsub::BaseActor
#
# def initialize(options = {})
# @client = CelluloidPubsub::Client.connect({ actor: Actor.current, channel: 'test_channel' }.merge(options))
# end
#
# def on_message(message)
# if @client.succesfull_subscription?(message)
# puts "subscriber got successful subscription #{message.inspect}"
# @client.publish('test_channel2', 'data' => ' subscriber got successfull subscription') # the message needs to be a Hash
# else
# puts "subscriber got message #{message.inspect}"
# @client.unsubscribe('test_channel')
# end
# end
#
# def on_close(code, reason)
# puts "websocket connection closed: #{code.inspect}, #{reason.inspect}"
# terminate
# end
# end
#
# # actor that publishes a message in a channel
# class Publisher < CelluloidPubsub::BaseActor
#
# def initialize(options = {})
# @client = CelluloidPubsub::Client.connect({ actor: Actor.current, channel: 'test_channel2' }.merge(options))
# @client.publish('test_channel', 'data' => 'my_message') # the message needs to be a Hash
# end
#
# def on_message(message)
# puts " publisher got #{message.inspect}"
# @client.unsubscribe('test_channel2')
# end
#
# def on_close(code, reason)
# puts "websocket connection closed: #{code.inspect}, #{reason.inspect}"
# terminate
# end
# end
#
# CelluloidPubsub::BaseActor.setup_actor_supervision(CelluloidPubsub::WebServer, actor_name: :web_server, args: {enable_debug: debug_enabled })
# CelluloidPubsub::BaseActor.setup_actor_supervision(Subscriber, actor_name: :subscriber, args: {enable_debug: debug_enabled })
# CelluloidPubsub::BaseActor.setup_actor_supervision(Publisher, actor_name: :publisher, args: {enable_debug: debug_enabled })
#
# sleep
# =======
ENV['USE_REDIS'] = 'false'
require_relative './shared_classes'
require 'bundler/setup'
require 'celluloid_pubsub'
require 'logger'

debug_enabled = ENV['DEBUG'].present? && ENV['DEBUG'].to_s == 'true'
log_file_path = File.join(File.expand_path(File.dirname(__FILE__)), 'log', 'celluloid_pubsub.log')

# actor that subscribes to a channel
class FirstActor
include Celluloid

def initialize(options = {})
@client = CelluloidPubsub::Client.new({ actor: Actor.current, channel: 'test_channel' }.merge(options))
end

def on_message(message)
if @client.succesfull_subscription?(message)
puts "subscriber got successful subscription #{message.inspect}"
@client.publish('test_channel2', 'data' => ' subscriber got successfull subscription') # the message needs to be a Hash
else
puts "subscriber got message #{message.inspect}"
end
end

def on_close(code, reason)
puts "websocket connection closed: #{code.inspect}, #{reason.inspect}"
terminate
end


end

# actor that publishes a message in a channel
class SecondActor
include Celluloid

def initialize(options = {})
@client = CelluloidPubsub::Client.new({ actor: Actor.current, channel: 'test_channel2' }.merge(options))
end

def on_message(message)
if @client.succesfull_subscription?(message)
puts "publisher got successful subscription #{message.inspect}"
@client.publish('test_channel', 'data' => ' my_message') # the message needs to be a Hash
else
puts "publisher got message #{message.inspect}"
end
end

def on_close(code, reason)
puts "websocket connection closed: #{code.inspect}, #{reason.inspect}"
terminate
end

end


# please don't use the BaseActor class to supervise actors. This is subject to change . This class is used only to test backward compatibility.
# For more information on how to supervise actors please see Celluloid wiki.
CelluloidPubsub::BaseActor.setup_actor_supervision(CelluloidPubsub::WebServer, actor_name: :web_server, args: {enable_debug: debug_enabled, adapter: nil,log_file_path: log_file_path })
CelluloidPubsub::BaseActor.setup_actor_supervision(FirstActor, actor_name: :first_actor, args: {enable_debug: debug_enabled })
CelluloidPubsub::BaseActor.setup_actor_supervision(SecondActor, actor_name: :second_actor, args: {enable_debug: debug_enabled })

signal_received = false

Signal.trap('INT') do
puts "\nAn interrupt signal has been triggered!"
signal_received = true
end

puts 'Exited succesfully! =)'
sleep 0.1 until signal_received
1 change: 1 addition & 0 deletions lib/celluloid_pubsub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
require 'active_support/all'
require 'json'
require 'celluloid_pubsub/base_actor'
Gem.find_files('celluloid_pubsub/initializers/**/*.rb').each { |path| require path }
Gem.find_files('celluloid_pubsub/**/*.rb').each { |path| require path }
15 changes: 15 additions & 0 deletions lib/celluloid_pubsub/initializers/reel_colors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'reel/spy'
Reel::Spy::Colors.class_eval do
alias_method :original_colorize, :colorize

def colorize(n, str)
force_utf8_encoding(str)
end

# Returns utf8 encoding of the msg
# @param [String] msg
# @return [String] ReturnsReturns utf8 encoding of the msg
def force_utf8_encoding(msg)
msg.respond_to?(:force_encoding) && msg.encoding.name != 'UTF-8' ? msg.force_encoding('UTF-8') : msg
end
end
2 changes: 1 addition & 1 deletion lib/celluloid_pubsub/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def start_subscriber(channel, message)
return unless channel.present?
add_subscriber_to_channel(channel, message)
log_debug "#{self.class} subscribed to #{channel} with #{message}"
@websocket << message.merge('client_action' => 'successful_subscription', 'channel' => channel).to_json unless @server.redis_enabled?
@websocket << message.merge('client_action' => 'successful_subscription', 'channel' => channel).to_json if @server.adapter == CelluloidPubsub::WebServer::CLASSIC_ADAPTER
end

# this method will return a list of all subscribers to a particular channel or a empty array
Expand Down
2 changes: 1 addition & 1 deletion lib/celluloid_pubsub/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module VERSION
# minor release version
MINOR = 6
# tiny release version
TINY = 0
TINY = 1
# prelease version ( set this only if it is a prelease)
PRE = nil

Expand Down
23 changes: 11 additions & 12 deletions lib/celluloid_pubsub/web_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class WebServer < Reel::Server::HTTP
PORT = 1234
# The request path that the webserver accepts by default
PATH = '/ws'
# The name of the default adapter
CLASSIC_ADAPTER = 'classic'

attr_accessor :server_options, :subscribers
finalizer :shutdown
Expand All @@ -47,14 +49,20 @@ def initialize(options = {})
super(hostname, port, { spy: spy, backlog: backlog }, &method(:on_connection))
end

def run
@spy = Celluloid.logger if spy
loop { async.handle_connection @server.accept }
end

# the method will return true if redis can be used otherwise false
#
#
# @return [Boolean] return true if redis can be used otherwise false
#
# @api public
def use_redis
@use_redis = @server_options.fetch('use_redis', false)
def adapter
@adapter ||= @server_options.fetch('adapter', CelluloidPubsub::WebServer::CLASSIC_ADAPTER)
@adapter.present? ? @adapter : CelluloidPubsub::WebServer::CLASSIC_ADAPTER
end

# the method will return true if debug is enabled otherwise false
Expand Down Expand Up @@ -139,15 +147,6 @@ def backlog
@backlog = @server_options.fetch('backlog', 1024)
end

# the method will return true if redis is enabled otherwise false
#
#
# @return [Boolean] returns true if redis is enabled otherwise false
#
# @api public
def redis_enabled?
use_redis.to_s.downcase == 'true'
end

# callback that will execute when receiving new conections
# If the connections is a websocket will call method {#route_websocket}
Expand Down Expand Up @@ -190,7 +189,7 @@ def on_connection(connection)
#
# @api public
def reactor_class
redis_enabled? ? CelluloidPubsub::RedisReactor : CelluloidPubsub::Reactor
adapter == CelluloidPubsub::WebServer::CLASSIC_ADAPTER ? CelluloidPubsub::Reactor : "CelluloidPubsub::#{adapter.camelize}Reactor".constantize
end

# method will instantiate a new reactor object, will link the reactor to the current actor and will dispatch the request to the reactor
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/celluloid_pubsub/reactor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
server.stubs(:async).returns(server)
server.stubs(:handle_dispatched_message)
server.stubs(:subscribers).returns({})
server.stubs(:redis_enabled?).returns(false)
server.stubs(:adapter).returns(CelluloidPubsub::WebServer::CLASSIC_ADAPTER)
websocket.stubs(:read)
websocket.stubs(:url)
websocket.stubs(:close)
Expand Down

0 comments on commit 3b2d18b

Please sign in to comment.