Class: XwotDiscovery::XwotServiceProtocol

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

Defined Under Namespace

Classes: Registry

Instance Method Summary collapse

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 = Message.new({
    method: 'alive',
    urn: resource.urn,
    location: resource.location,
    content_type: 'application/json',
    payload: resource.payload})
  @protocol.send(message)
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 = Message.new({
    method: 'bye',
    urn: resource.urn,
    location: resource.location,
    content_type: 'application/json',
    payload: resource.payload})
  @protocol.send(message)
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(message)
  @listeners.each do |listener|
    case message.method.downcase
    when 'alive'
      @registry.add(message)
      listener.alive(message)
    when 'update'
      @registry.update(message)
      listener.update(message)
    when 'find'
      listener.find(message, self)
    when 'bye'
      @registry.remove(message)
      listener.bye(message)
    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 = Message.new({
    method: 'find',
    urn: urn_str})
  #p message
  @protocol.send(message)
end

#register_listener(listener) ⇒ Object



47
48
49
# File 'lib/xwot_discovery/service_protocol.rb', line 47

def register_listener(listener)
  @listeners << listener
end

#shutdownObject



43
44
45
# File 'lib/xwot_discovery/service_protocol.rb', line 43

def shutdown
  @protocol.close
end

#startObject



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 = Message.new({
    method: 'update',
    urn: resource.urn,
    location: resource.location,
    content_type: 'application/json',
    payload: resource.payload})
  @protocol.send(message)
end