Ticket #133: event.dif

File event.dif, 6.8 kB (added by Lawrence, 4 years ago)

dif file, for those who read diffs

Line 
1 Index: event.py
2 ===================================================================
3 --- event.py    (revision 872)
4 +++ event.py    (working copy)
5 @@ -17,24 +17,55 @@
6  
7  import louie
8  
9 -global hostname, web_server_port
10 -hostname = None
11 -web_server_port = None
12 +global myurl
13 +myurl = ""
14  
15 +# these are services that are currently subscribed to events.
16 +global subscribed_services
17 +subscribed_services = {}
18  
19 +def add_subscribed_service( service, subscribeid ):
20 +    global subscribed_services
21 +    log.info( Event.__module__, "add_subscribed_service %s (%s)", subscribeid, service )
22 +    if subscribeid:
23 +       subscribed_services[subscribeid] = service
24 +    elif subscribed_services.has_key(subscribeid):
25 +       del subscribed_services[subscribeid]
26 +
27 +def clear_subscribed_service( subscribeid ):
28 +    log.info( Event.__module__, "clear_subscribed_service %s", subscribeid )
29 +    global subscribed_services
30 +    if subscribed_services.has_key(subscribeid):
31 +       del subscribed_services[subscribeid]
32 +
33 +def get_service_by_subscribeid( subscribeid ):
34 +    log.info(Event.__module__, "get_service_by_subscribeid %s", subscribeid )
35 +    if subscribed_services.has_key(subscribeid):
36 +       return subscribed_services[subscribeid]
37 +    return None
38 +
39 +def propagate(event):
40 +    target_service = get_service_by_subscribeid(event.get_sid())
41 +    if target_service:
42 +           target_service.process_event(event)
43 +
44  class EventServer(resource.Resource, log.Loggable):
45      logCategory = 'event_server'
46  
47 -    def __init__(self, control_point):
48 -        self.coherence = control_point.coherence
49 -        self.control_point = control_point
50 -        self.coherence.add_web_resource('events',
51 -                                        self)
52 -        global hostname, web_server_port
53 -        hostname = self.coherence.hostname
54 -        web_server_port = self.coherence.web_server_port
55 -        self.info("EventServer ready...")
56 +#    def __init__(self, control_point):
57 +#        self.control_point = control_point
58 +    def __init__(self, webserver):
59 +
60 +        global myurl
61  
62 +# If using global coherence object
63 +#        myurl = 'http://%s:%d/events' % (control_point.coherence.hostname, control_point.coherence.web_server_port)
64 +#        control_point.coherence.add_web_resource('events', self)
65 +
66 +# if webserver separated from coherence
67 +        myurl = webserver.add_web_resource('events', self)
68 +        self.info("EventServer ready. %s", myurl)
69 +
70      def render_NOTIFY(self, request):
71          self.info("EventServer received notify from %s, code: %d" % (request.client, request.code))
72          data = request.content.getvalue()
73 @@ -45,9 +76,9 @@
74          louie.send('UPnP.Event.Server.message_received', None, command, headers, data)
75  
76          if request.code != 200:
77 -            self.info("data:", data)
78 +            self.info("data: %s", data)
79          else:
80 -            self.debug("data:", data)
81 +            self.debug("data: %s", data)
82              headers = request.getAllHeaders()
83              sid = headers['sid']
84              try:
85 @@ -64,7 +95,7 @@
86                      tag = var.tag
87                      idx = tag.find('}') + 1
88                      event.update({tag[idx:]: var.text})
89 -            self.control_point.propagate(event)
90 +            propagate(event)
91          return ""
92  
93  
94 @@ -117,7 +148,7 @@
95          louie.send('UPnP.Event.Client.message_received', None, command, headers, data)
96  
97          if request.code != 200:
98 -            self.debug("data:", data)
99 +            self.info("data: %s", data)
100          else:
101              headers = request.getAllHeaders()
102              try:
103 @@ -161,7 +192,7 @@
104          louie.send('UPnP.Event.Client.message_received', None, command, headers, data)
105  
106          if request.code != 200:
107 -            self.debug("data:", data)
108 +            self.info("data: %s", data)
109          else:
110              headers = request.getAllHeaders()
111              try:
112 @@ -191,7 +222,8 @@
113          self.action = action
114  
115      def connectionMade(self):
116 -        self.timeout_checker = reactor.callLater(30, lambda : self.transport.loseConnection())
117 +        #self.timeout_checker = reactor.callLater(30, lambda : self.transport.loseConnection())
118 +       pass
119  
120      def dataReceived(self, data):
121          self.info("response received from the Service Events HTTP server ")
122 @@ -199,10 +231,17 @@
123          cmd, headers = utils.parse_http_response(data)
124          self.debug("%r %r", cmd, headers)
125          if int(cmd[1]) != 200:
126 -            self.warning("response with error code %r received upon our %r request", cmd[1], self.action)
127 +            self.warning("response with error code %r received upon our %r request", cmd[1], self.action)
128 +           clear_subscribed_service(self.service.get_sid())
129 +            self.service.set_sid(None)
130          else:
131 -            try:
132 -                self.service.set_sid(headers['sid'])
133 +            try:
134 +                self.service.set_sid(headers.get('sid',None))  # this may be an unsubscribe request
135 +
136 +               add_subscribed_service(self.service, self.service.get_sid())
137 +
138 +               self.debug("add subscription for %s", self.id)
139 +
140                  timeout = headers['timeout']
141                  self.debug("%r %r", headers['sid'], headers['timeout'])
142                  if timeout == 'infinite':
143 @@ -218,7 +257,10 @@
144  
145      def connectionLost( self, reason):
146          try:
147 -            self.timeout_checker.cancel()
148 +            self.timeout_checker.cancel()
149 +           # If connection goes need to restart, maybe.
150 +#          self.service.set_sid( None )
151 +           self.service.event_connection = None
152          except:
153              pass
154          self.debug( "connection closed %r from the Service Events HTTP server", reason)
155 @@ -232,7 +274,7 @@
156      send a subscribe/renewal/unsubscribe request to a service
157      return the device response
158      """
159 -    log_category = "event_protocol"
160 +    log_category = Event.__module__
161      log.info(log_category, "event.subscribe, action: %r", action)
162  
163      _,host_port,path,_,_ = urlsplit(service.get_base_url())
164 @@ -260,12 +302,7 @@
165          if service.get_sid():
166              request.append("SID: %s" % service.get_sid())
167          else:
168 -            # XXX use address and port set in the coherence instance
169 -            #ip_address = p.transport.getHost().host
170 -            global hostname, web_server_port
171 -            #print hostname, web_server_port
172 -            url = 'http://%s:%d/events' % (hostname, web_server_port)
173 -            request.append("CALLBACK: <%s>" % url)
174 +            request.append("CALLBACK: <%s>" % myurl)
175              request.append("NT: upnp:event")
176  
177          request.append('Date: %s' % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()))
178 @@ -277,6 +314,7 @@
179          try:
180              p.transport.writeSomeData(request)
181          except AttributeError:
182 +            service.event_connection = None # need new connection
183              log.info(log_category, "transport for event %r already gone", action)
184         # print "event.subscribe.send_request", d
185          #return d