def outgoing_tube_factory(connecting_fount, connecting_drain): client_filter = or_command_filter(self.client_allowed, self.client_allowed_prefixes) client_sieve = tubeFilter(client_filter.is_allowed) client_replace = replacerTubeFactory(self.client_replacements) proxy_client_sieve = series(bytesToLines(), client_replace, client_sieve, linesToBytes()) client_fanout = Out() client_err_fount = client_fanout.newFount() client_sieve_fount = client_fanout.newFount() client_sieve_fount.flowTo(proxy_client_sieve).flowTo(connecting_drain) server_fanin = In() server_fanin.fount.flowTo(display_received("server")).flowTo(listening_drain) #server_fanin.fount.flowTo(listening_drain) server_fanin_proxy_drain = server_fanin.newDrain() server_fanin_err_drain = server_fanin.newDrain() error_sieve = tubeFilter(lambda item: not client_filter.is_allowed(item)) replace_with_error_tube = lambda err_message: tubeMap(lambda item: err_message) proxy_error_sieve = series(bytesToLines(), error_sieve, replace_with_error_tube(self.filtered_msg), linesToBytes()) client_err_fount.flowTo(series(proxy_error_sieve, server_fanin_err_drain)) server_filter = or_command_filter(self.server_allowed, self.server_allowed_prefixes) server_sieve = tubeFilter(server_filter.is_allowed) proxy_server_sieve = series(bytesToLines(), server_sieve, linesToBytes()) connecting_fount.flowTo(proxy_server_sieve).flowTo(server_fanin_proxy_drain) listening_fount.flowTo(series(display_received("client"), client_fanout.drain))
class Channel(object): """ A chat room. """ def __init__(self, name): self._name = name self._out = Out() self._in = In() self._in.fount.flowTo(self._out.drain) def participate(self, participant): """ Create a new drain of messages going to this channel and a new fount of messages coming from this channel, for the given participant. @param participant: the name of the participant joining. @return: a 2-tuple of (new fount, new drain) """ @receiver(IMapping, IMapping, name="->addSender({}, {})".format(participant.name, self._name)) def addSender(item): yield dict(item, sender=participant.name, channel=self._name) return (self._out.newFount(), series(addSender, self._in.newDrain()))
class Channel(object): def __init__(self, name): self._name = name self._out = Out() self._in = In() self._in.fount.flowTo(self._out.drain) def participate(self, participant): @receiver(IMapping, IMapping) def addSender(item): yield dict(item, sender=participant, channel=self._name) return (self._out.newFount(), series(addSender, self._in.newDrain()))
class Participant(object): outputType = Routed(IMapping) def __init__(self, hub, requestsFount, responsesDrain): self._hub = hub self._participation = {} self._in = In() self._router = Router() self._participating = {} # self._in is both commands from our own client and also messages from # other clients. requestsFount.flowTo(self._in.newDrain()) self._in.fount.flowTo(series(self, self._router.drain)) self.client = self._router.newRoute() self.client.flowTo(responsesDrain) def received(self, item): kwargs = item.copy() return getattr(self, "do_" + kwargs.pop("type"))(**kwargs) def do_name(self, name): self.name = name yield to(self.client, dict(named=name)) def do_joined(self, sender, channel): """ Someone joined a channel I'm participating in. """ yield to(self.client, dict(type="joined")) def do_join(self, channel): fountFromChannel, drainToChannel = ( self._hub.channelNamed(channel).participate(self)) fountFromChannel.flowTo(self._in.newDrain()) fountToChannel = self._router.newRoute() fountToChannel.flowTo(drainToChannel) self._participating[channel] = fountToChannel yield to(self._participating[channel], dict(type="joined")) def do_speak(self, channel, message, id): yield to(self._participating[channel], dict(type="spoke", message=message, id=id)) def do_shout(self, message, id): for channel in self._participating.values(): yield to(channel, dict(type="spoke", message=message, id=id)) yield to(self.client, dict(type="shouted", id=id)) def do_tell(self, receiver, message): # TODO: implement _establishRapportWith; should be more or less like # joining a channel. rapport = self._establishRapportWith(receiver) yield to(rapport, dict(type="told", message=message)) # TODO: when does a rapport end? timeout as soon as the write buffer # is empty? def do_told(self, sender, message): yield to(self.client, message) def do_spoke(self, channel, sender, message, id): yield to( self.client, dict(type="spoke", channel=channel, sender=sender.name, message=message, id=id))
class Participant(object): """ A single participant in a chat system. """ outputType = Routed(IMapping) def __init__(self, hub, requestsFount, responsesDrain): """ Create a L{Participant}. """ self._hub = hub self._in = In() self._in.fount.flowTo(responsesDrain) self._router = Router() self._participating = {} # `self._in' is both commands from our own client and also messages # from other clients. requestsFount.flowTo(series(self, self._router.drain)) self.client = self._router.newRoute("client") self.client.flowTo(self._in.newDrain()) def received(self, item): """ An item was received. @param item: A dictionary featuring a 'type' indicating which command it is. @return: a response routed to the router. """ kwargs = item.copy() return getattr(self, "do_" + kwargs.pop("type"))(**kwargs) def do_name(self, name): """ From client; set the name of this client. @param name: The nickname for this client. """ self.name = name yield to(self.client, dict(named=name)) def do_join(self, channel): """ From client; instruct this client to join a channel with the given name. @param channel: the name of the channel to join. """ fountFromChannel, drainToChannel = ( self._hub.channelNamed(channel).participate(self) ) fountFromChannel.flowTo(self._in.newDrain()) fountToChannel = self._router.newRoute("->{}".format(channel)) fountToChannel.flowTo(drainToChannel) self._participating[channel] = fountToChannel yield to(self._participating[channel], dict(type="joined")) def do_speak(self, channel, message, id): """ From client; say something on the given channel. @param channel: the name of the channel @param message: the text of the message to relay @param id: a unique identifier for this message """ yield to(self._participating[channel], dict(type="spoke", message=message, id=id))
class Participant(object): outputType = Routed(IMapping) def __init__(self, hub, requestsFount, responsesDrain): self._hub = hub self._participation = {} self._in = In() self._router = Router() self._participating = {} # self._in is both commands from our own client and also messages from # other clients. requestsFount.flowTo(self._in.newDrain()) self._in.fount.flowTo(series(self, self._router.drain)) self.client = self._router.newRoute() self.client.flowTo(responsesDrain) def received(self, item): kwargs = item.copy() return getattr(self, "do_" + kwargs.pop("type"))(**kwargs) def do_name(self, name): self.name = name yield to(self.client, dict(named=name)) def do_joined(self, sender, channel): """ Someone joined a channel I'm participating in. """ yield to(self.client, dict(type="joined")) def do_join(self, channel): fountFromChannel, drainToChannel = ( self._hub.channelNamed(channel).participate(self) ) fountFromChannel.flowTo(self._in.newDrain()) fountToChannel = self._router.newRoute() fountToChannel.flowTo(drainToChannel) self._participating[channel] = fountToChannel yield to(self._participating[channel], dict(type="joined")) def do_speak(self, channel, message, id): yield to(self._participating[channel], dict(type="spoke", message=message, id=id)) def do_shout(self, message, id): for channel in self._participating.values(): yield to(channel, dict(type="spoke", message=message, id=id)) yield to(self.client, dict(type="shouted", id=id)) def do_tell(self, receiver, message): # TODO: implement _establishRapportWith; should be more or less like # joining a channel. rapport = self._establishRapportWith(receiver) yield to(rapport, dict(type="told", message=message)) # TODO: when does a rapport end? timeout as soon as the write buffer # is empty? def do_told(self, sender, message): yield to(self.client, message) def do_spoke(self, channel, sender, message, id): yield to(self.client, dict(type="spoke", channel=channel, sender=sender.name, message=message, id=id))