module Citrus::Common::Service::ChannelService::Util
Private Instance Methods
Add uid and server id into group
@param [String] uid @param [String] sid @param [Hash] groups
@private
# File lib/citrus/common/service/channel_service.rb, line 34 def add uid, sid, groups unless sid return false end group = groups[sid] group = []; groups[sid] = group unless group group << uid; true end
Add to store
@param [Object] service @param [String] key @param [Hash] value
@private
# File lib/citrus/common/service/channel_service.rb, line 175 def add_to_store service, key, value if service.store service.store.add(key, value) { |err| if err end } end end
Delete element from array
@param [String] uid @param [String] sid @param [Array] group
@private
# File lib/citrus/common/service/channel_service.rb, line 52 def delete_from uid, sid, group return true unless group group.each { |e| group.delete e; return true if e == uid } return false end
Generate key
@param [Object] service @param [String] name
@private
# File lib/citrus/common/service/channel_service.rb, line 239 def gen_key service, name='' unless name.empty? service.prefix + ':' + service.app.server_id + ':' + name else service.prefix + ':' + service.app.server_id end end
Generate value
@param [String] sid @param [String] uid
@private
# File lib/citrus/common/service/channel_service.rb, line 253 def gen_value sid, uid sid + ':' + uid end
Load all from store
@param [Object] service @param [String] key
@private
# File lib/citrus/common/service/channel_service.rb, line 206 def load_all_from_store service, key, &block if service.store service.store.load(key) { |err, list| if err block_given? and yield err else block_given? and yield nil, list end } end end
Remove all from store
@param [Object] service @param [String] key
@private
# File lib/citrus/common/service/channel_service.rb, line 224 def remove_all_from_store service, key if service.store service.store.remove_all(key) { |err| if err end } end end
Remove from store
@param [Object] service @param [String] key @param [Hash] value
@private
# File lib/citrus/common/service/channel_service.rb, line 191 def remove_from_store service, key, value if service.store service.store.remove(key, value) { |err| if err end } end end
Restore channel
@param [Object] service
@private
# File lib/citrus/common/service/channel_service.rb, line 130 def restore_channel service, &block if service.store block_given? and yield return end load_all_from_store(service, gen_key(service)) { |err, list| if err block_given? and yield err return end unless (list.instance_of? Array) && list.lenth > 0 block_given? and yield return end load_p = Proc.new { |key| load_all_from_store(service, key) { |err, items| items.each { |item| sid, uid = item.split ':' channel = service.channels[name] if add uid, sid, channel.groups channel.records[uid] = { :sid => sid, :uid => uid } end } } } list.each_index { |index| name = list[index][gen_key(service).length+1..-1] service.channels[name] = Channel.new name, service load_p.call list[index] } block_given? and yield } end
Push message by group
@param [Object] service @param [String] route @param [Hash] msg @param [Hash] groups @param [Hash] args
@private
# File lib/citrus/common/service/channel_service.rb, line 70 def send_message_by_group service, route, msg, groups, args, &block app = service.app namespace = 'sys' service = 'channelRemote' method = 'pushMessage' count = groups.length success_flag = false fail_ids = [] block_given? and yield if count == 0 latch = Utils::CountDownLatch.new(count) { unless success_flag block_given? and yield Exception.new 'all uids push message fail' return end block_given? and yield nil, fail_ids } rpc_cb = Proc.new { |server_id| Proc.new { |err, fails| if err latch.done return end fail_ids += fails if fails success_flag = true latch.done } } args = { :type => 'push', :user_args => args || {} } send_message = Proc.new { |sid| if sid == app.server_id service.channelRemote.send method, route, msg, groups[sid], args, &rpc_cb.call else app.rpc_invoke(sid, { :namespace => namespace, :service => service, :method => method, :args => [route, msg, groups[sid], args] }, &rpc_cb.call(sid)) end } groups.each_with_index { |group, sid| if group && group.length > 0 send_message sid else EM.next_tick { rpc_cb.call.call } end } end