Class: Citrus::Common::Service::ChannelService::Channel

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/citrus/common/service/channel_service.rb

Overview

Channel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, service) ⇒ Channel

Create a new channel

Parameters:

  • name (String)
  • service (Object)


403
404
405
406
407
408
409
410
# File 'lib/citrus/common/service/channel_service.rb', line 403

def initialize name, service
  @name = name
  @groups = {}
  @records = {}
  @channel_service = service
  @state = :state_inited
  @user_amount = 0
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



397
398
399
# File 'lib/citrus/common/service/channel_service.rb', line 397

def groups
  @groups
end

#user_amountObject (readonly)

Returns the value of attribute user_amount.



397
398
399
# File 'lib/citrus/common/service/channel_service.rb', line 397

def user_amount
  @user_amount
end

Instance Method Details

#add(uid, sid) ⇒ Object

Add user to channel

Parameters:

  • uid (String)
  • sid (String)


416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/citrus/common/service/channel_service.rb', line 416

def add uid, sid
  return false unless @state == :state_inited

  res = add uid, sid, @groups
  if res
    @records[uid] = { :sid => sid, :uid => uid }
    @user_amount += 1
  end

  add_to_store @channel_service, gen_key(@channel_service, @name), gen_value(sid, uid)
  res
end

#destroyObject

Destroy channel



464
465
466
467
# File 'lib/citrus/common/service/channel_service.rb', line 464

def destroy
  @state = :state_destroyed
  @channel_service.destroy_channel @name
end

#get_member(uid) ⇒ Object

Get member info

Parameters:

  • uid (String)


459
460
461
# File 'lib/citrus/common/service/channel_service.rb', line 459

def get_member uid
  @records[uid]
end

#get_membersObject

Get channel members



450
451
452
453
454
# File 'lib/citrus/common/service/channel_service.rb', line 450

def get_members
  res = []
  @groups.each { |group| group.each { |e| res << e } }
  res
end

#leave(uid, sid) ⇒ Object

Remove user from channel

Parameters:

  • uid (String)
  • sid (String)


433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/citrus/common/service/channel_service.rb', line 433

def leave uid, sid
  return unless uid && sid

  @records.delete uid
  @user_amount -= 1
  @user_amout = 0 if @user_amount < 0

  remove_from_store @channel_service, gen_key(@channel_service, @name), gen_value(sid, uid)

  res = delete_from uid, sid, @groups[sid]
  if @groups[sid] && @groups[sid].length == 0
    @groups.delete sid
  end
  res
end

#push_message(route, msg, args, &block) ⇒ Object

Push message to all the members in the channel

Parameters:

  • route (String)
  • msg (Hash)
  • args (Hash)


474
475
476
477
478
479
480
# File 'lib/citrus/common/service/channel_service.rb', line 474

def push_message route, msg, args, &block
  unless @state == :state_inited
    block_given? and yield Exception.new 'channel is not running now'
    return
  end
  send_message_by_group @channel_service, route, msg, @groups, args, &block
end