2016-10-01 19:19:03 +00:00
|
|
|
from objects import stream
|
2016-10-07 11:15:50 +00:00
|
|
|
from objects import glob
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
# TODO: use *args and **kwargs
|
2016-10-02 21:11:18 +00:00
|
|
|
class streamList:
|
2016-10-01 19:19:03 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.streams = {}
|
|
|
|
|
|
|
|
def add(self, name):
|
|
|
|
"""
|
|
|
|
Create a new stream list if it doesn't already exist
|
|
|
|
|
|
|
|
:param name: stream name
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if name not in self.streams:
|
|
|
|
self.streams[name] = stream.stream(name)
|
|
|
|
|
|
|
|
def remove(self, name):
|
|
|
|
"""
|
|
|
|
Removes an existing stream and kick every user in it
|
|
|
|
|
|
|
|
:param name: stream name
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if name in self.streams:
|
|
|
|
for i in self.streams[name].clients:
|
2016-10-07 11:15:50 +00:00
|
|
|
if i in glob.tokens.tokens:
|
|
|
|
glob.tokens.tokens[i].leaveStream(name)
|
2016-10-01 19:19:03 +00:00
|
|
|
self.streams.pop(name)
|
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
|
|
|
|
def join(self, streamName, client=None, token=None):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
Add a client to a stream
|
2016-10-01 19:19:03 +00:00
|
|
|
|
|
|
|
:param streamName: stream name
|
2016-10-07 10:42:02 +00:00
|
|
|
:param client: client (osuToken) object
|
|
|
|
:param token: client uuid string
|
2016-10-01 19:19:03 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if streamName not in self.streams:
|
|
|
|
return
|
2016-10-07 10:42:02 +00:00
|
|
|
self.streams[streamName].addClient(client=client, token=token)
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
def leave(self, streamName, client=None, token=None):
|
2016-10-04 20:10:07 +00:00
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
Remove a client from a stream
|
2016-10-04 20:10:07 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
:param streamName: stream name
|
|
|
|
:param client: client (osuToken) object
|
|
|
|
:param token: client uuid string
|
2016-10-04 20:10:07 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if streamName not in self.streams:
|
|
|
|
return
|
2016-10-07 10:42:02 +00:00
|
|
|
self.streams[streamName].removeClient(client=client, token=token)
|
2016-10-04 20:10:07 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
def broadcast(self, streamName, data, but=None):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
Send some data to all clients in a stream
|
2016-10-01 19:19:03 +00:00
|
|
|
|
|
|
|
:param streamName: stream name
|
2016-10-07 10:42:02 +00:00
|
|
|
:param data: data to send
|
2016-12-11 10:07:35 +00:00
|
|
|
:param but: array of tokens to ignore. Default: None (send to everyone)
|
2016-10-01 19:19:03 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if streamName not in self.streams:
|
|
|
|
return
|
2016-12-11 10:07:35 +00:00
|
|
|
self.streams[streamName].broadcast(data, but)
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
def dispose(self, streamName, *args, **kwargs):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
Call `dispose` on `streamName`
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
:param streamName: name of the stream
|
2016-12-11 10:07:35 +00:00
|
|
|
:param args:
|
|
|
|
:param kwargs:
|
2016-10-01 19:19:03 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if streamName not in self.streams:
|
|
|
|
return
|
2016-12-11 10:07:35 +00:00
|
|
|
self.streams[streamName].dispose(*args, **kwargs)
|