Class: XwotDiscovery::XwotService

Inherits:
Object
  • Object
show all
Defined in:
lib/xwot_discovery/service.rb

Defined Under Namespace

Classes: ServiceProtocolListener

Constant Summary collapse

ALIVE_INTERVAL_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_protocol) ⇒ XwotService

Returns a new instance of XwotService.



39
40
41
42
43
44
45
46
# File 'lib/xwot_discovery/service.rb', line 39

def initialize(service_protocol)
  @service_protocol = service_protocol
  @resources = []
  @rand = rand * 2 * 60
  @find_callbacks = []
  listener = ServiceProtocolListener.new(self, @resources)
  @service_protocol.register_listener(listener)
end

Instance Attribute Details

#find_callbacksObject

Returns the value of attribute find_callbacks.



37
38
39
# File 'lib/xwot_discovery/service.rb', line 37

def find_callbacks
  @find_callbacks
end

Instance Method Details

#find(urn = '*', &block) ⇒ Object



60
61
62
63
64
65
# File 'lib/xwot_discovery/service.rb', line 60

def find(urn = '*', &block)
  if block_given?
    @find_callbacks << [urn, block]
  end
  @service_protocol.find(urn)
end

#register_listener(listener) ⇒ Object



77
78
79
# File 'lib/xwot_discovery/service.rb', line 77

def register_listener(listener)
  @service_protocol.register_listener(listener)
end

#register_resource(resource) ⇒ Object



67
68
69
70
# File 'lib/xwot_discovery/service.rb', line 67

def register_resource(resource)
  @resources << resource
  send_alive(resource)
end

#send_alive(resource) ⇒ Object



86
87
88
89
90
91
# File 'lib/xwot_discovery/service.rb', line 86

def send_alive(resource)
  2.times do
    @service_protocol.alive(resource)
    sleep 0.5
  end
end

#send_bye(resource) ⇒ Object



93
94
95
96
97
98
# File 'lib/xwot_discovery/service.rb', line 93

def send_bye(resource)
  2.times do
    @service_protocol.bye(resource)
    sleep 0.5
  end
end

#startObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xwot_discovery/service.rb', line 48

def start
  @service_protocol.start
  #p @rand
  Thread.new do
    sleep @rand
    loop do
      @resources.each { |resource| send_alive(resource) }
      sleep ALIVE_INTERVAL_SECONDS
    end
  end
end

#unregister_listener(listener) ⇒ Object



81
82
83
# File 'lib/xwot_discovery/service.rb', line 81

def unregister_listener(listener)
  @service_protocol.unregister_listener(listener)
end

#unregister_resource(resource) ⇒ Object



72
73
74
75
# File 'lib/xwot_discovery/service.rb', line 72

def unregister_resource(resource)
  @resources.delete(resource)
  send_bye(resource)
end