2016-10-04 20:10:07 +00:00
|
|
|
from common.log import logUtils as log
|
2016-10-07 10:42:02 +00:00
|
|
|
from objects import glob
|
2016-10-04 20:10:07 +00:00
|
|
|
|
2016-10-02 21:11:18 +00:00
|
|
|
class stream:
|
2016-10-01 19:19:03 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
"""
|
|
|
|
Initialize a stream object
|
|
|
|
|
|
|
|
:param name: stream name
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.clients = []
|
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
def addClient(self, client=None, token=None):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
|
|
|
Add a client to this stream if not already in
|
|
|
|
|
|
|
|
:param client: client (osuToken) object
|
2016-10-07 10:42:02 +00:00
|
|
|
:param token: client uuid string
|
2016-10-01 19:19:03 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
if client is None and token is None:
|
|
|
|
return
|
|
|
|
if client is not None:
|
|
|
|
token = client.token
|
|
|
|
if token not in self.clients:
|
|
|
|
log.info("{} has joined stream {}".format(token, self.name))
|
|
|
|
self.clients.append(token)
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-10-07 10:42:02 +00:00
|
|
|
def removeClient(self, client=None, token=None):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
|
|
|
Remove a client from this stream if in
|
|
|
|
|
|
|
|
:param client: client (osuToken) object
|
2016-10-07 10:42:02 +00:00
|
|
|
:param token: client uuid string
|
2016-10-01 19:19:03 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2016-10-07 10:42:02 +00:00
|
|
|
if client is None and token is None:
|
|
|
|
return
|
|
|
|
if client is not None:
|
|
|
|
token = client.token
|
|
|
|
if token in self.clients:
|
|
|
|
log.info("{} has left stream {}".format(token, self.name))
|
|
|
|
self.clients.remove(token)
|
2016-10-01 19:19:03 +00:00
|
|
|
|
2016-12-11 10:07:35 +00:00
|
|
|
def broadcast(self, data, but=None):
|
2016-10-01 19:19:03 +00:00
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
Send some data to all (or some) clients connected to this stream
|
2016-10-01 19:19:03 +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:
|
|
|
|
"""
|
2016-12-11 10:07:35 +00:00
|
|
|
if but is None:
|
|
|
|
but = []
|
2016-10-01 19:19:03 +00:00
|
|
|
for i in self.clients:
|
2016-10-07 10:42:02 +00:00
|
|
|
if i in glob.tokens.tokens:
|
2016-12-11 10:07:35 +00:00
|
|
|
if i not in but:
|
|
|
|
glob.tokens.tokens[i].enqueue(data)
|
2016-10-07 10:42:02 +00:00
|
|
|
else:
|
2016-12-11 10:07:35 +00:00
|
|
|
self.removeClient(token=i)
|
|
|
|
|
|
|
|
def dispose(self):
|
|
|
|
"""
|
|
|
|
Tell every client in this stream to leave the stream
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
for i in self.clients:
|
|
|
|
if i in glob.tokens.tokens:
|
|
|
|
glob.tokens.tokens[i].leaveStream(self.name)
|