~ubuntu-dev/ubuntu/lucid/coherence/lucid-201002101853

« back to all changes in this revision

Viewing changes to coherence/upnp/devices/media_server.py

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2008-03-05 13:43:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080305134301-pg572ehbze3h7cii
Tags: 0.5.2-1
* New upstream release
* debian/control: depend on python-pkg-resources instead of
  python-setuptools for the runtime dependency (Closes: #468724)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from coherence.upnp.services.servers.media_receiver_registrar_server import MediaReceiverRegistrarServer
33
33
from coherence.upnp.services.servers.media_receiver_registrar_server import FakeMediaReceiverRegistrarBackend
34
34
 
 
35
from coherence.upnp.devices.basics import BasicAVMixin
 
36
 
35
37
import louie
36
38
 
37
39
from coherence import log
38
40
 
39
 
COVER_REQUEST_INDICATOR = re.compile(".*cover\.[A-Z|a-z]{3,4}$")
 
41
COVER_REQUEST_INDICATOR = re.compile(".*?cover\.[A-Z|a-z]{3,4}$")
 
42
 
 
43
ATTACHMENT_REQUEST_INDICATOR = re.compile(".*?attachment=.*$")
40
44
 
41
45
class MSRoot(resource.Resource, log.Loggable):
42
46
    logCategory = 'mediaserver'
53
57
        headers = request.getAllHeaders()
54
58
        self.msg( request.getAllHeaders())
55
59
 
56
 
        if(request.method == 'GET' and
57
 
           COVER_REQUEST_INDICATOR.match(request.uri)):
58
 
            self.info("request cover for id %s" % path)
59
 
            ch = self.store.get_by_id(path)
60
 
            if ch is not None:
61
 
                request.setResponseCode(200)
62
 
                file = ch.get_cover()
63
 
                if os.path.exists(file):
64
 
                    self.info("got cover %s" % file)
65
 
                    return StaticFile(file)
66
 
            request.setResponseCode(404)
67
 
            return static.Data('<html><p>cover requested not found</p></html>','text/html')
 
60
        if request.method == 'GET':
 
61
            if COVER_REQUEST_INDICATOR.match(request.uri):
 
62
                self.info("request cover for id %s" % path)
 
63
                ch = self.store.get_by_id(path)
 
64
                if ch is not None:
 
65
                    request.setResponseCode(200)
 
66
                    file = ch.get_cover()
 
67
                    if os.path.exists(file):
 
68
                        self.info("got cover %s" % file)
 
69
                        return StaticFile(file)
 
70
                request.setResponseCode(404)
 
71
                return static.Data('<html><p>cover requested not found</p></html>','text/html')
 
72
 
 
73
            if ATTACHMENT_REQUEST_INDICATOR.match(request.uri):
 
74
                self.info("request attachment %r for id %s" % (request.args,path))
 
75
                ch = self.store.get_by_id(path)
 
76
                try:
 
77
                    return ch.item.attachments[request.args['attachment'][0]]
 
78
                except:
 
79
                    request.setResponseCode(404)
 
80
                    return static.Data('<html><p>the requested attachment was not found</p></html>','text/html')
68
81
 
69
82
        if(request.method == 'POST' and
70
83
           request.uri.endswith('?import')):
85
98
            return self
86
99
        return self.getChild(path, request)
87
100
 
88
 
    def requestFinished(self, result, id):
 
101
    def requestFinished(self, result, id, request):
89
102
        self.info("finished, remove %d from connection table" % id)
 
103
        self.info("finished, sentLength: %d chunked: %d code: %d" % (request.sentLength, request.chunked, request.code))
 
104
        self.info("finished %r" % request.headers)
90
105
        self.server.connection_manager_server.remove_connection(id)
91
106
 
92
107
    def import_file(self,name,request):
136
151
                                                                                '')
137
152
                    self.info("startup, add %d to connection table" % new_id)
138
153
                    d = request.notifyFinish()
139
 
                    d.addCallback(self.requestFinished, new_id)
140
 
                    d.addErrback(self.requestFinished, new_id)
 
154
                    d.addBoth(self.requestFinished, new_id, request)
 
155
                    request.setHeader('transferMode.dlna.org', 'Streaming')
 
156
                    if hasattr(ch.item, 'res'):
 
157
                        if ch.item.res[0].protocolInfo is not None:
 
158
                            _,_,_,additional_info = ch.item.res[0].protocolInfo.split(':')
 
159
                            if additional_info != '*':
 
160
                                request.setHeader('contentFeatures.dlna.org', additional_info)
141
161
                    return ch.location
142
162
            try:
143
163
                p = ch.get_path()
153
173
                                                                            '')
154
174
                self.info("startup, add %d to connection table" % new_id)
155
175
                d = request.notifyFinish()
156
 
                d.addCallback(self.requestFinished, new_id)
157
 
                d.addErrback(self.requestFinished, new_id)
 
176
                d.addBoth(self.requestFinished, new_id, request)
 
177
                request.setHeader('transferMode.dlna.org', 'Streaming')
 
178
                if hasattr(ch, 'item') and hasattr(ch.item, 'res'):
 
179
                    if ch.item.res[0].protocolInfo is not None:
 
180
                        _,_,_,additional_info = ch.item.res[0].protocolInfo.split(':')
 
181
                        if additional_info != '*':
 
182
                            request.setHeader('contentFeatures.dlna.org', additional_info)
158
183
                ch = StaticFile(p.encode('utf-8'))
159
184
            else:
160
185
                self.debug("accessing path %r failed" % p)
324
349
        self.xml = """<?xml version="1.0" encoding="utf-8"?>""" + ET.tostring( root, encoding='utf-8')
325
350
        static.Data.__init__(self, self.xml, 'text/xml')
326
351
 
327
 
class MediaServer(log.Loggable):
 
352
class MediaServer(log.Loggable,BasicAVMixin):
328
353
    logCategory = 'mediaserver'
329
354
 
330
355
    def __init__(self, coherence, backend, **kwargs):
331
356
        self.coherence = coherence
332
357
        self.device_type = 'MediaServer'
333
 
        self.version = int(kwargs.get('version',2))
 
358
        self.version = int(kwargs.get('version',self.coherence.config.get('version',2)))
334
359
 
335
360
        try:
336
361
            self.uuid = kwargs['uuid']
337
362
        except KeyError:
338
363
            from coherence.upnp.core.uuid import UUID
339
364
            self.uuid = UUID()
340
 
            
 
365
 
341
366
        self.backend = None
342
367
        urlbase = self.coherence.urlbase
343
368
        if urlbase[-1] != '/':
447
472
 
448
473
        self.register()
449
474
        self.info("%s MediaServer (%s) activated" % (self.backend.name, self.backend))
450
 
 
451
 
 
452
 
    def register(self):
453
 
        s = self.coherence.ssdp_server
454
 
        uuid = str(self.uuid)
455
 
        host = self.coherence.hostname
456
 
        self.msg('%s register' % self.device_type)
457
 
        # we need to do this after the children are there, since we send notifies
458
 
        s.register('local',
459
 
                    '%s::upnp:rootdevice' % uuid,
460
 
                    'upnp:rootdevice',
461
 
                    self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % self.version,
462
 
                    host=host)
463
 
 
464
 
        s.register('local',
465
 
                    uuid,
466
 
                    uuid,
467
 
                    self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % self.version,
468
 
                    host=host)
469
 
 
470
 
        version = self.version
471
 
        while version > 0:
472
 
            if version == self.version:
473
 
                silent = False
474
 
            else:
475
 
                silent = True
476
 
            s.register('local',
477
 
                        '%s::urn:schemas-upnp-org:device:%s:%d' % (uuid, self.device_type, version),
478
 
                        'urn:schemas-upnp-org:device:%s:%d' % (self.device_type, version),
479
 
                        self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % version,
480
 
                        silent=silent,
481
 
                        host=host)
482
 
 
483
 
            for service in self._services:
484
 
                silencio = silent
485
 
                if hasattr(service,'version'):
486
 
                    if service.version < version:
487
 
                        continue
488
 
                    elif service.version == version:
489
 
                        silencio = False
490
 
                try:
491
 
                    namespace = service.namespace
492
 
                except:
493
 
                    namespace = 'schemas-upnp-org'
494
 
 
495
 
                s.register('local',
496
 
                            '%s::urn:%s:service:%s:%d' % (uuid,namespace,service.id, version),
497
 
                            'urn:%s:service:%s:%d' % (namespace,service.id, version),
498
 
                            self.coherence.urlbase + uuid[5:] + '/' + 'description-%d.xml' % version,
499
 
                            silent=silencio,
500
 
                            host=host)
501
 
 
502
 
            version -= 1
503
 
 
504
 
    def unregister(self):
505
 
        s = self.coherence.ssdp_server
506
 
        uuid = str(self.uuid)
507
 
        self.coherence.remove_web_resource(uuid[5:])
508
 
 
509
 
        version = self.version
510
 
        while version > 0:
511
 
            s.doByebye('%s::urn:schemas-upnp-org:device:%s:%d' % (uuid, self.device_type, version))
512
 
            for service in self._services:
513
 
                if hasattr(service,'version') and service.version < version:
514
 
                    continue
515
 
                try:
516
 
                    namespace = service.namespace
517
 
                except AttributeError:
518
 
                    namespace = 'schemas-upnp-org'
519
 
                s.doByebye('%s::urn:%s:service:%s:%d' % (uuid,namespace,service.id, version))
520
 
 
521
 
            version -= 1
522
 
 
523
 
        s.doByebye(uuid)
524
 
        s.doByebye('%s::upnp:rootdevice' % uuid)
 
 
b'\\ No newline at end of file'