Class: RUPNP::CP::EventServer

Inherits:
EM::HttpServer::Server
  • Object
show all
Includes:
LogMixin
Defined in:
lib/rupnp/cp/event_server.rb

Overview

Event server to receive events from services.

Author:

  • Sylvain Daubert

Constant Summary collapse

@@add_event =
EM::Channel.new
@@events =
[]

Constants included from LogMixin

LogMixin::LOG_LEVEL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LogMixin

#log

Instance Attribute Details

#eventsEM::Channel (readonly)

Channel to return received updated variables from events

Returns:

  • (EM::Channel)


38
39
40
# File 'lib/rupnp/cp/event_server.rb', line 38

def events
  @events
end

Class Method Details

.add_event(event) ⇒ void

This method returns an undefined value.

Channel to add url for listening to

Parameters:



23
24
25
# File 'lib/rupnp/cp/event_server.rb', line 23

def add_event(event)
  @@add_event << event
end

.remove_event(event) ⇒ void

This method returns an undefined value.

Remove an event

Parameters:



30
31
32
# File 'lib/rupnp/cp/event_server.rb', line 30

def remove_event(event)
  @@events.delete_if { |e| e == event }
end

Instance Method Details

#process_http_requestObject

Process a HTTP request received from a service/device



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rupnp/cp/event_server.rb', line 42

def process_http_request
  log :debug, "EventServer: receive request: #@http"

  response = EM::DelegatedHttpResponse.new(self)

  if @http_request_method != 'NOTIFY'
    log :info, "EventServer: method #@http_request_method not allowed"
    response.status = 405
    response.headers['Allow'] = 'NOTIFY'
    response.send_response
    return
  end

  event = @@events.find { |e| e.callback_url == @http_request_uri }

  if event.nil?
    log :info, "EventServer: Requested URI #@http_request_uri unknown"
    response.status = 404
    response.send_response
    return
  end

  if !event.is_a? EM::Channel
    log :error, "EventServer: internal error!"
    response.status = 500
    response.send_response
    return
  end

  if !@http.has_key?(:nt) or !@http.has_key?(:nts)
    log :warn, 'EventServer: malformed NOTIFY event message'
    log :debug, "#@http_headers\n#@http_content"
    response.status = 400
    response.send_response
    return
  end

  if @http[:nt] != 'upnp:event' or @http[:nts] != 'upnp:propchange' or
      !@http.has_key?(:sid) or @http[:sid] == ''
    log :warn, 'EventServer: precondition failed'
    log :debug, "#@http_headers\n#@http_content"
    response.status = 412
    response.send_response
    return
  end

  if event.sid != @http[:sid]
    log :warn, 'EventServer: precondition failed (unknown SID)'
    log :debug, "#@http_headers\n#@http_content"
    response.status = 412
    response.send_response
    return
  end

  seq = @http[:seq].nil? ? 0 : @http[:seq].to_i
  notification = {
    :seq => seq,
    :content => Nori.new.parse(@http_content)
  }

  log :debug, "send notification #{notification.inspect}\n" +
    "    to #{event}"
  event << notification

  response_status = 200
  response.send_response
end