Class: XwotDiscovery::XwotServiceProtocol
- Inherits:
-
Object
- Object
- XwotDiscovery::XwotServiceProtocol
- Defined in:
- lib/xwot_discovery/service_protocol.rb
Defined Under Namespace
Classes: Registry
Instance Method Summary collapse
- #alive(resource) ⇒ Object
- #bye(resource) ⇒ Object
- #dispatch(message) ⇒ Object
- #find(urn = '') ⇒ Object
-
#initialize(protocol) ⇒ XwotServiceProtocol
constructor
A new instance of XwotServiceProtocol.
- #register_listener(listener) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #unregister_listener(listener) ⇒ Object
- #update(resource) ⇒ Object
Constructor Details
#initialize(protocol) ⇒ XwotServiceProtocol
Returns a new instance of XwotServiceProtocol.
31 32 33 34 35 36 37 |
# File 'lib/xwot_discovery/service_protocol.rb', line 31 def initialize(protocol) @protocol = protocol @listeners = [] @find_callbacks = [] @registry = Registry.new @protocol.notify_me(self) end |
Instance Method Details
#alive(resource) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/xwot_discovery/service_protocol.rb', line 92 def alive(resource) = Message.new({ method: 'alive', urn: resource.urn, location: resource.location, content_type: 'application/json', payload: resource.payload}) @protocol.send() end |
#bye(resource) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/xwot_discovery/service_protocol.rb', line 102 def bye(resource) = Message.new({ method: 'bye', urn: resource.urn, location: resource.location, content_type: 'application/json', payload: resource.payload}) @protocol.send() end |
#dispatch(message) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/xwot_discovery/service_protocol.rb', line 55 def dispatch() @listeners.each do |listener| case .method.downcase when 'alive' @registry.add() listener.alive() when 'update' @registry.update() listener.update() when 'find' listener.find(, self) when 'bye' @registry.remove() listener.bye() else # do nothing end end end |
#find(urn = '') ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xwot_discovery/service_protocol.rb', line 75 def find(urn = '') urn_str = case urn.to_s when '*' '*' when '' '*' else urn end # TODO: registry lookup = Message.new({ method: 'find', urn: urn_str}) #p message @protocol.send() end |
#register_listener(listener) ⇒ Object
47 48 49 |
# File 'lib/xwot_discovery/service_protocol.rb', line 47 def register_listener(listener) @listeners << listener end |
#shutdown ⇒ Object
43 44 45 |
# File 'lib/xwot_discovery/service_protocol.rb', line 43 def shutdown @protocol.close end |
#start ⇒ Object
39 40 41 |
# File 'lib/xwot_discovery/service_protocol.rb', line 39 def start @protocol.listen end |
#unregister_listener(listener) ⇒ Object
51 52 53 |
# File 'lib/xwot_discovery/service_protocol.rb', line 51 def unregister_listener(listener) @listeners.delete(listener) end |
#update(resource) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/xwot_discovery/service_protocol.rb', line 112 def update(resource) = Message.new({ method: 'update', urn: resource.urn, location: resource.location, content_type: 'application/json', payload: resource.payload}) @protocol.send() end |