class NchanTools::Subscriber

Attributes

client[RW]
client_class[RW]
concurrency[RW]
errors[RW]
finished[RW]
log[RW]
max_round_trips[RW]
messages[RW]
quit_message[RW]
url[RW]
waiting[RW]

Public Class Methods

new(url, concurrency=1, opt={}) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1522
def initialize(url, concurrency=1, opt={})
  @empty_block = Proc.new {}
  @on={}
  @care_about_message_ids=opt[:use_message_id].nil? ? true : opt[:use_message_id]
  @url=url
  @quit_message = opt[:quit_message]
  opt[:timeout] ||= 30
  opt[:connect_timeout] ||= 5
  #puts "Starting subscriber on #{url}"
  @Client_Class = Client.lookup(opt[:client] || :longpoll)
  if @Client_Class.nil?
    raise SubscriberError, "unknown client type #{opt[:client]}"
  end
  
  if !opt[:nostore] && opt[:nomsg]
    opt[:nomsg] = nil
    puts "nomsg reverted to false because nostore is false"
  end
  opt[:concurrency]=concurrency
  @concurrency = opt[:concurrency]
  @opt=opt
  if opt[:log]
    @log = Subscriber::Logger.new
    opt[:logger]=@log
  end
  new_client
  reset
end

Public Instance Methods

abort() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1564
def abort
  @client.terminate
end
errors?() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1567
def errors?
  not no_errors?
end
make_error(client, what, code, msg, failword=" failed") click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1641
def make_error(client, what, code, msg, failword=" failed")
  "#{client.class.name.split('::').last} #{what}#{failword}: #{msg} (code #{code})"
end
match_errors(regex) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1573
def match_errors(regex)
  return false if no_errors?
  @errors.each do |err|
    return false unless err =~ regex
  end
  true
end
new_client() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1550
def new_client
  @client=@Client_Class.new self, @opt
end
no_errors?() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1570
def no_errors?
  @errors.empty?
end
on(evt_name = nil, &block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1619
def on(evt_name = nil, &block)
  if block_given?
    @on[evt_name.to_sym] = block
  else
    @on[evt_name.to_sym] or @empty_block
  end
end
on_failure(err=nil, nostore=false, &block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1645
def on_failure(err=nil, nostore=false, &block)
  if block_given?
    @on_failure=block
  else
    @errors << err.to_s unless nostore
    @on_failure.call(err.to_s, err.bundle) if @on_failure.respond_to? :call
  end
end
on_message(msg=nil, bundle=nil, &block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1627
def on_message(msg=nil, bundle=nil, &block)
  #puts "received message #{msg && msg.to_s[0..15]}"
  if block_given?
    @on_message=block
  else
    @messages << msg if @messages
    if @quit_message == msg.to_s
      @on_message.call(msg, bundle) if @on_message
      return false 
    end
    @on_message.call(msg, bundle) if @on_message
  end
end
reset() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1553
def reset
  @errors=[]
  unless @nostore
    @messages=MessageStore.new :noid => !(client.provides_msgid? && @care_about_message_ids)
    @messages.name="sub"
  end
  @waiting=0
  @finished=0
  new_client if terminated?
  self
end
run() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1582
def run
  begin
    client.current_actor
  rescue Celluloid::DeadActorError
    return false
  end
  @client.async.run
  self
end
stop() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1591
def stop
  begin
    @client.stop
  rescue Celluloid::DeadActorError
    return false
  end
  true
end
terminate() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1599
def terminate
  begin
    @client.terminate
  rescue Celluloid::DeadActorError
    return false
  end
  true
end
terminated?() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1607
def terminated?
  begin
    client.current_actor unless client == nil
  rescue Celluloid::DeadActorError
    return true
  end
  false
end
wait(until_what=nil, timeout = nil) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1615
def wait(until_what=nil, timeout = nil)
  @client.poke until_what, timeout
end