summaryrefslogtreecommitdiff
path: root/python/qpid/reference.py
blob: 48ecb6765639919b2e7e3ebc63305ec03f60ae4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

"""
Support for amqp 'reference' content (as opposed to inline content)
"""

import threading
from queue import Queue, Closed

class NotOpened(Exception): pass

class AlreadyOpened(Exception): pass

"""
A representation of a reference id; can be passed wherever amqp
content is required in place of inline data
"""
class ReferenceId:

    def __init__(self, id):
        self.id = id

"""
Holds content received through 'reference api'. Instances of this
class will be placed in the consumers queue on receiving a transfer
(assuming the reference has been opened). Data can be retrieved in
chunks (as append calls are received) or in full (after reference has
been closed signalling data s complete).
"""

class Reference:

    def __init__(self, id):
        self.id = id
        self.chunks = Queue(0)

    def close(self):
        self.chunks.close()

    def append(self, bytes):    
        self.chunks.put(bytes)

    def get_chunk(self):
        return self.chunks.get()

    def get_complete(self):
        data = ""
        for chunk in self:
            data += chunk
        return data

    def next(self):
        try:
            return self.get_chunk()
        except Closed, e:
            raise StopIteration

    def __iter__(self):
        return self

"""
Manages a set of opened references. New references can be opened and
existing references can be retrieved or closed.
"""
class References:

    def __init__(self):
        self.map = {}
        self.lock = threading.Lock()

    def get(self, id):
        self.lock.acquire()
        try:
            try:
                ref = self.map[id]
            except KeyError:
                raise NotOpened()
        finally:
            self.lock.release()
        return ref

    def open(self, id):
        self.lock.acquire()
        try:
            if id in self.map: raise AlreadyOpened()
            self.map[id] = Reference(id)
        finally:
            self.lock.release()


    def close(self, id):
        self.get(id).close()
        self.lock.acquire()
        try:
            self.map.pop(id)
        finally:
            self.lock.release()