@@ -37,18 +37,11 @@ class SSDPServer(DatagramProtocol):
stdheaders = [ ('Server', 'Twisted, UPnP/1.0, python-upnp'), ]
stdheaders = [ ('Server', 'Twisted, UPnP/1.0, python-upnp'), ]
elements = {}
elements = {}
known = {}
known = {}
tasks = []
maxage = 7 * 24 * 60 * 60
def doStop(self):
def doStop(self):
'''Make sure we send out the byebye notifications.'''
'''Make sure we send out the byebye notifications.'''
while True:
try:
t = self.tasks.pop()
t.stop()
except IndexError:
break
for st in self.known.keys():
for st in self.known.keys():
self.doByebye(st)
self.doByebye(st)
del self.known[st]
del self.known[st]
@@ -120,12 +113,16 @@ class SSDPServer(DatagramProtocol):
self.known[st]['ST'] = st
self.known[st]['ST'] = st
self.known[st]['EXT'] = ''
self.known[st]['EXT'] = ''
self.known[st]['SERVER'] = 'Twisted, UPnP/1.0, python-upnp'
self.known[st]['SERVER'] = 'Twisted, UPnP/1.0, python-upnp'
self.known[st]['CACHE-CONTROL'] = 'max-age=1800'
self.doNotify(st)
self.known[st]['CACHE-CONTROL'] = 'max-age=%d' % self.maxage
self.doNotifySchedule (st)
t = task.LoopingCall(lambda: self.doNotify(st))
t.start(7 * 60)
self.tasks.append(t)
reactor.callLater(random.uniform(.5, 1), lambda: self.doNotify(st))
reactor.callLater(random.uniform(1, 5), lambda: self.doNotify(st))
def doNotifySchedule(self, st):
self.doNotify(st)
reactor.callLater(random.uniform(self.maxage / 3,
self.maxage / 2), lambda: self.doNotifySchedule(st))
def doByebye(self, st):
def doByebye(self, st):
"""Do byebye"""
"""Do byebye"""
@@ -141,7 +138,9 @@ class SSDPServer(DatagramProtocol):
del stcpy['ST']
del stcpy['ST']
resp.extend(map(lambda x: ': '.join(x), stcpy.iteritems()))
resp.extend(map(lambda x: ': '.join(x), stcpy.iteritems()))
resp.extend(('', ''))
resp.extend(('', ''))
self.transport.write('\r\n'.join(resp), (SSDP_ADDR, SSDP_PORT))
resp = '\r\n'.join(resp)
self.transport.write(resp, (SSDP_ADDR, SSDP_PORT))
self.transport.write(resp, (SSDP_ADDR, SSDP_PORT))
def doNotify(self, st):
def doNotify(self, st):
"""Do notification"""
"""Do notification"""