class RIMS::ServerResponseChannel

Public Class Methods

new() click to toggle source
# File lib/rims/channel.rb, line 18
def initialize
  @mutex = Thread::Mutex.new
  @channel = {}
end

Public Instance Methods

detach(sub) click to toggle source

do not call this method directly, call the following method instead.

- ServerResponsePublisher#detach
- ServerResponseSubscriber#detach
# File lib/rims/channel.rb, line 46
def detach(sub)
  @mutex.synchronize{
    unless ((@channel.key? sub.mbox_id) && (@channel[sub.mbox_id].key? sub.pub_sub_pair_key)) then
      raise ServerResponseChannelDetachError.new('unregistered pub-sub pair.', channel: self, subscriber: sub)
    end

    unless (@channel[sub.mbox_id][sub.pub_sub_pair_key] == sub) then
      raise ServerResponseChannelDetachError.new('internal error: mismatched subscriber.', channel: self, subscribe: sub)
    end

    @channel[sub.mbox_id].delete(sub.pub_sub_pair_key)
    if (@channel[sub.mbox_id].empty?) then
      @channel.delete(sub.mbox_id)
    end
  }

  nil
end
make_pub_sub_pair(mbox_id) click to toggle source
# File lib/rims/channel.rb, line 23
def make_pub_sub_pair(mbox_id)
  pub = ServerResponsePublisher.new(self, mbox_id)
  sub = ServerResponseSubscriber.new(self, pub)
  return pub, attach(sub)
end
publish(mbox_id, pub_sub_pair_key, response_message) click to toggle source

do not call this method directly, call the following method instead.

- ServerResponsePublisher#publish
# File lib/rims/channel.rb, line 68
def publish(mbox_id, pub_sub_pair_key, response_message)
  @mutex.synchronize{
    @channel[mbox_id].each_value do |sub|
      if (sub.pub_sub_pair_key != pub_sub_pair_key) then
        sub.publish(response_message)
      end
    end
  }

  nil
end

Private Instance Methods

attach(sub) click to toggle source
# File lib/rims/channel.rb, line 29
def attach(sub)
  @mutex.synchronize{
    @channel[sub.mbox_id] ||= {}
    if (@channel[sub.mbox_id].key? sub.pub_sub_pair_key) then
      raise ServerResponseChannelAttachError.new('conflicted subscriber.', channel: self, subscriber: sub)
    end
    @channel[sub.mbox_id][sub.pub_sub_pair_key] = sub
  }

  sub
end