2016-10-04 20:10:07 +00:00
|
|
|
from common.log import logUtils as log
|
|
|
|
|
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 = []
|
|
|
|
|
|
|
|
def addClient(self, client):
|
|
|
|
"""
|
|
|
|
Add a client to this stream if not already in
|
|
|
|
|
|
|
|
:param client: client (osuToken) object
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if client not in self.clients:
|
2016-10-04 20:10:07 +00:00
|
|
|
log.info("{} has joined stream {}".format(client.username, self.name))
|
2016-10-01 19:19:03 +00:00
|
|
|
self.clients.append(client)
|
|
|
|
|
|
|
|
def removeClient(self, client):
|
|
|
|
"""
|
|
|
|
Remove a client from this stream if in
|
|
|
|
|
|
|
|
:param client: client (osuToken) object
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if client in self.clients:
|
2016-10-04 20:10:07 +00:00
|
|
|
log.info("{} has left stream {}".format(client.username, self.name))
|
2016-10-01 19:19:03 +00:00
|
|
|
self.clients.remove(client)
|
|
|
|
|
|
|
|
def broadcast(self, data):
|
|
|
|
"""
|
|
|
|
Send some data to all clients connected to this stream
|
|
|
|
|
|
|
|
:param data: data to send
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
for i in self.clients:
|
2016-10-04 21:43:02 +00:00
|
|
|
if i is not None:
|
|
|
|
i.enqueue(data)
|