This repository has been archived on 2025-06-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fl0w-old/Server/Broadcast.py
2016-01-31 20:41:02 +01:00

47 lines
No EOL
1.2 KiB
Python

class Broadcast:
class ChannelError(IndexError):
def __init__(self, channel):
super(Broadcast.ChannelError, self).__init__("channel '%s' does not exist" % channel)
def __init__(self):
self.channels = {}
def broadcast(self, data, channel, exclude=[]):
if channel in self.channels:
for sock in self.channels[channel]:
if not sock in exclude:
sock.send(data)
else:
raise Broadcast.ChannelError(channel)
def remove(self, sock, channel):
if channel in self.channels:
if sock in self.channels[channel]:
del self.channels[channel][self.channels[channel].index(sock)]
else:
raise Broadcast.ChannelError(channel)
def add(self, sock, channel):
if channel in self.channels:
if not sock in self.channels[channel]:
self.channels[channel].append(sock)
else:
raise Broadcast.ChannelError(channel)
def add_channel(self, channel):
self.channels[channel] = []
def remove_channel(self, channel):
if channel in self.channels:
del self.channels[channel]
else:
raise Broadcast.ChannelError(channel)
def __repr__(self):
out = "Channels:\n"
for channel in self.channels:
out += "%s: %d socks\n" % (channel, len(self.channels[channel]))
return out.rstrip("\n")
def __str__(self):
return self.__repr__()