-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanager.py
More file actions
252 lines (218 loc) · 10.1 KB
/
Copy pathmanager.py
File metadata and controls
252 lines (218 loc) · 10.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import threading
import config
import collections
import logging
import bcrypt
from buffers import Buffer
from audio import icecast
from database import MySQLCursor
logger = logging.getLogger('server.manager')
STuple = collections.namedtuple('STuple', ['buffer', 'info'])
ITuple = collections.namedtuple('ITuple', ['user', 'useragent', 'stream_name'])
def generate_info(mount):
return {'host': config.icecast_host,
'port': config.icecast_port,
'password': config.icecast_pass,
'format': config.icecast_format,
'protocol': config.icecast_protocol,
'name': config.meta_name,
'url': config.meta_url,
'genre': config.meta_genre,
'mount': mount}
class IcyManager(object):
def __init__(self):
super(IcyManager, self).__init__()
# : Lock to acquire when fetching a context object.
self.context_lock = threading.RLock()
self.context = {}
def login(self, user=None, password=None, privilege=1):
if user is None or password is None:
return False
if user == 'source':
try:
user, password = password.split('|')
except ValueError as err:
return False
with MySQLCursor() as cur:
cur.execute(("SELECT * FROM users WHERE user=%s "
"AND privileges>%s LIMIT 1;"),
(user, privilege))
for row in cur:
hash = row['pass']
if bcrypt.hashpw(password, hash) == hash:
return True
return False
def register_source(self, client):
"""Register a connected icecast source to be used for streaming to
the main server."""
with self.context_lock:
try:
context = self.context[client.mount]
except KeyError:
context = IcyContext(client.mount)
self.context[client.mount] = context
with context:
context.append(client)
if not context.icecast.connected():
context.start_icecast()
def remove_source(self, client):
"""Removes a connected icecast source from the list of tracked
sources to be used for streaming."""
with self.context_lock:
try:
context = self.context[client.mount]
except KeyError:
# We can be sure there is no source when the mount is unknown
return
with context:
try:
context.remove(client)
except ValueError:
# Source isn't in the sources list?
logger.warning('An unknown source tried to be removed. Logic error')
finally:
if not context.sources:
context.stop_icecast()
def send_metadata(self, metadata, client):
"""Sends a metadata command to the underlying correct
:class:`IcyContext`: class."""
try:
self.context[client.mount].send_metadata(metadata, client)
except KeyError:
logger.info("Received metadata for non-existant mountpoint %s",
client.mount)
class IcyContext(object):
"""A class that is the context of a single icecast mountpoint."""
def __init__(self, mount):
super(IcyContext, self).__init__()
# : Set to last value returned by :attr:`source`:
self.current_source = None
# Threading sync lock
self.lock = threading.RLock()
# Create a buffer that always returns an empty string (EOF)
self.eof_buffer = Buffer(1)
self.eof_buffer.close()
self.mount = mount
# : Deque of tuples of the format STuple(source, ITuple(user, useragent, stream_name))
self.sources = collections.deque()
self.icecast_info = generate_info(mount)
self.icecast = icecast.Icecast(self, self.icecast_info)
self.saved_metadata = {}
def __enter__(self):
self.lock.acquire()
def __exit__(self, type, value, traceback):
self.lock.release()
def __repr__(self):
return "IcyContext(mount={:s}, user count={:d})".format(
self.mount,
len(self.sources)
)
def append(self, source):
"""Append a source client to the list of sources for this context."""
source_tuple = STuple(source.buffer, ITuple(source.user,
source.useragent,
source.stream_name))
logger.debug("Adding source '{source:s}' from '{context:s}'".format(
source=repr(source_tuple),
context=repr(self),
))
self.sources.append(source_tuple)
logger.debug("Current sources are '{sources:s}'.".format(
sources=repr(self.sources))
)
def remove(self, source):
"""Remove a source client of the list of sources for this context."""
source_tuple = STuple(source.buffer, ITuple(source.user,
source.useragent,
source.stream_name))
logger.debug("Removing source '{source:s}' from '{context:s}'".format(
source=repr(source_tuple),
context=repr(self),
))
self.sources.remove(source_tuple)
logger.debug("Current sources are '{sources:s}'.".format(
sources=repr(self.sources))
)
# Close our buffer to make sure we EOF
source_tuple.buffer.close()
@property
def source(self):
"""Returns the first source in the :attr:`sources` deque.
If :attr:`sources` is empty it returns :const:`None` instead
"""
try:
source = self.sources[0]
except IndexError:
logger.debug("Returning EOF in source acquiring.")
return None
else:
if not self.current_source is source:
logger.info("%s: Changing source from '%s' to '%s'.",
self.mount, 'None' if self.current_source is None \
else self.current_source.info.user,
source.info.user)
# We changed source sir. Send saved metadata if any.
if source in self.saved_metadata:
metadata = self.saved_metadata[source]
self.icecast.set_metadata(metadata)
else:
# No saved metadata, send an empty one
self.icecast.set_metadata(u'')
self.current_source = source
return source.buffer
def read(self, size=4096, timeout=None):
"""Reads at most :obj:`size`: of bytes from the first source in the
:attr:`sources`: deque.
:obj:`timeout`: is unused in this implementation."""
# Acquire source once, then use that one return everywhere else.
# Classic example of not-being-thread-safe in the old method.
source = self.source
while source is not None:
# Read data from the returned buffer
data = source.read(size)
# Refresh our source variable to point to the top source
source = self.source
if data == b'':
# If we got an EOF from the read it means we should check if
# there is another source available and continue the loop.
continue
else:
# Else we can just return the data we found from the source.
return data
# If we got here it means `self.source` returned None and we have no
# more sources left to read from. So we can return an EOF.
return b''
def start_icecast(self):
"""Calls the :class:`icecast.Icecast`: :meth:`icecast.Icecast.start`:
method of this context."""
self.icecast.start()
def stop_icecast(self):
"""Calls the :class:`icecast.Icecast`: :meth:`icecast.Icecast.close`:
method of this context."""
self.icecast.close()
self.current_source = None
def send_metadata(self, metadata, client):
"""Checks if client is the currently active source on this mountpoint
and then sends the metadata. If the client is not the active source
the metadata is saved for if the current source drops out."""
try:
source = self.sources[0]
except IndexError:
# No source, why are we even getting metadata ignore it
# By Vin:
# Some clients send meta slightly before connecting; save the
# data for a second and attribute it to the next source?
logger.warning("%s: Received metadata while we have no source.",
self.mount)
return
if (source.info.user == client.user):
# Current source send metadata to us! yay
logger.info("%s:metadata.update: %s", self.mount, metadata)
self.saved_metadata[source] = metadata
self.icecast.set_metadata(metadata) # Lol consistent naming (not)
else:
for source in self.sources:
if (source.info.user == client.user):
# Save the metadata
logger.info("%s:metadata.save: %s", self.mount, metadata)
self.saved_metadata[source] = metadata