class Citrus::Common::Service::ChannelService
Attributes
app[R]
channel_remote[R]
channels[R]
prefix[R]
store[R]
Public Class Methods
new(app, args={})
click to toggle source
Initialize the service
@param [Object] app @param [Hash] args
# File lib/citrus/common/service/channel_service.rb, line 266 def initialize app, args={} @app = app @channels = {} @prefix = args[:prefix] @store = args[:store] @broadcast_filter = args[:broadcast_filter] @channel_remote = Remote::Frontend::ChannelRemote.new app end
Public Instance Methods
broadcast(server_type, route, msg, args) { || ... }
click to toggle source
Broadcast message to all the connected clients
@param [String] server_type @param [String] route @param [Hash] message @param [Hash] args
# File lib/citrus/common/service/channel_service.rb, line 338 def broadcast server_type, route, msg, args, &block namespace = 'sys' service = 'channelRemote' method = 'broadcast' servers = @app.get_servers_by_type server_type unless servers && servers.length != 0 # server list is empty block_given? and yield end count = servers.length success_flag = false latch = Utils::CountDownLatch.new(count) { unless success_flag block_given? and yield Exception.new 'broadcast failed' return end block_given? and yield nil } gen_cb = Proc.new { |server_id| Proc.new { |err| if err latch.done return end success_flag = true latch.done } } send_message = Proc.new { |server_id| if server_id == @app.server_id @channel_remote.send method, route, msg, args, &gen_cb.call else @app.rpc_invoke(server_id, { :namespace => namespace, :service => service, :method => method, :args => [route, msg, args] }, &gen_cb.call(server_id)) end } args = { :type => 'broadcast', :user_args => args || {} } servers.each { |server| send_message server[:server_id] } end
create_channel(name)
click to toggle source
Create channel with name
@param [String] name
# File lib/citrus/common/service/channel_service.rb, line 283 def create_channel name return @channels[name] if @channels[name] c = Channel.new name add_to_store self, gen_key(self), gen_key(self, name) @channels[name] = c c end
destroy_channel(name)
click to toggle source
Destroy channel by name
@param [String] name
# File lib/citrus/common/service/channel_service.rb, line 308 def destroy_channel name @channels.delete name remove_from_store self, gen_key(self), gen_key(self, name) remove_all_from_store self, gen_key(self, name) end
get_channel(name, create=false)
click to toggle source
Get channel by name
@param [String] name @param [Boolean] create
# File lib/citrus/common/service/channel_service.rb, line 296 def get_channel name, create=false channel = @channels[name] if !channel && create channel = @channels[name] = Channel.new name add_to_store self, gen_key(self), gen_key(self, name) end channel end
push_message_by_uids(route, msg, uids, args) { |exception 'uids should not be empty'| ... }
click to toggle source
Push message by uids
@param [String] route @param [Hash] msg @param [Array] uids @param [Hash] args
# File lib/citrus/common/service/channel_service.rb, line 320 def push_message_by_uids route, msg, uids, args, &block unless uids && uids.length != 0 block_given? and yield Exception.new 'uids should not be empty' return end groups = {} uids.each { |record| add record[:uid], record[:sid], groups } send_message_by_group self, route, msg, groups, args, &block end
start(&block)
click to toggle source
Start the service
# File lib/citrus/common/service/channel_service.rb, line 276 def start &block restore_channel self, &block end