Class: EasyUpnp::SubscriptionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_upnp/events/subscription_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(event_client, callback_url, options = {}, &block) ⇒ SubscriptionManager

Returns a new instance of SubscriptionManager.



32
33
34
35
36
37
38
39
# File 'lib/easy_upnp/events/subscription_manager.rb', line 32

def initialize(event_client, callback_url, options = {}, &block)
  @options = Options.new(options, &block)
  @event_client = event_client
  @callback_url = callback_url
  @sid = @options.existing_sid

  logger.level = @options.log_level
end

Instance Method Details

#callback_urlObject



45
46
47
48
49
50
51
# File 'lib/easy_upnp/events/subscription_manager.rb', line 45

def callback_url
  if @callback_url.is_a? Proc
    @callback_url.call
  else
    @callback_url
  end
end

#subscribeObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/easy_upnp/events/subscription_manager.rb', line 53

def subscribe
  @subscription_thread ||= Thread.new do
    logger.info "Starting subscription thread..."

    resubscribe_time = start_or_renew_subscription

    begin
      while true
        if Time.now >= resubscribe_time
          resubscribe_time = renew_subscription
        else
          sleep 1
        end
      end
    rescue Exception => e
      logger.error "Caught error: #{e}"
      raise e
    end

    logger.info "Ending subscription"
  end

  true
end

#subscription_idObject



41
42
43
# File 'lib/easy_upnp/events/subscription_manager.rb', line 41

def subscription_id
  @sid
end

#unsubscribeObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/easy_upnp/events/subscription_manager.rb', line 78

def unsubscribe
  if @subscription_thread.nil?
    raise RuntimeError, "Illegal state: no active subscription"
  end
  @subscription_thread.kill
  @subscription_thread = nil

  begin
    if @sid
      @event_client.unsubscribe(@sid)
      @sid = nil
    end
  rescue Exception => e
    logger.error "Error unsubscribing with SID #{@sid}: #{e}"
  end

  @options.on_shutdown.call
end