def newParticipantFlow(self, flow): commandFount = flow.fount.flowTo( series(OnStop(lambda: self.participants.remove(participant)), bytesToLines(), linesToCommands)) commandDrain = series(commandsToLines, linesToBytes(), flow.drain) participant = Participant(self, commandFount, commandDrain) self.participants.append(participant)
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))
def newParticipantFlow(self, flow): commandFount = flow.fount.flowTo( series(OnStop(lambda: self.participants.remove(participant)), bytesToLines(), linesToCommands) ) commandDrain = series(commandsToLines, linesToBytes(), flow.drain) participant = Participant(self, commandFount, commandDrain) self.participants.append(participant)
def promptingCalculatorSeries(): from tubes.fan import Thru from tubes.tube import series from tubes.framing import bytesToLines, linesToBytes full = series(bytesToLines(), Thru([series(linesToNumbersOrOperators, CalculatingTube(Calculator()), numbersToLines, linesToBytes()), series(Prompter())])) return full
def promptingCalculatorSeries(): from tubes.fan import Thru from tubes.tube import series from tubes.framing import bytesToLines, linesToBytes full = series( bytesToLines(), Thru([ series(linesToNumbersOrOperators, CalculatingTube(Calculator()), numbersToLines, linesToBytes()), series(Prompter()) ])) return full
def test_tubeStoppedDeferredly(self): """ The L{_Siphon} stops its L{Tube} and propagates C{flowStopped} downstream upon the completion of all L{Deferred}s returned from its L{Tube}'s C{stopped} implementation. """ reasons = [] conclusion = Deferred() @tube class SlowEnder(object): def stopped(self, reason): reasons.append(reason) yield conclusion self.ff.flowTo(series(SlowEnder(), deferredToResult(), self.fd)) self.assertEquals(reasons, []) self.assertEquals(self.fd.received, []) stopReason = Failure(ZeroDivisionError()) self.ff.drain.flowStopped(stopReason) self.assertEquals(self.fd.received, []) self.assertEquals(len(reasons), 1) self.assertIdentical(reasons[0].type, ZeroDivisionError) self.assertEqual(self.fd.stopped, []) conclusion.callback("conclusion") # Now it's really done. self.assertEquals(self.fd.received, ["conclusion"]) self.assertEqual(self.fd.stopped, [stopReason])
def test_tubeYieldsMultipleDeferreds(self): """ When a tube yields multiple deferreds their results should be delivered in order. """ d = Deferred() @tube class MultiDeferredTube(object): didYield = False def received(self, data): yield d MultiDeferredTube.didYield = True yield succeed("goodbye") fakeDrain = self.fd self.ff.flowTo(series(MultiDeferredTube(), deferredToResult())).flowTo(fakeDrain) self.ff.drain.receive("ignored") self.assertEquals(self.fd.received, []) d.callback("hello") self.assertEquals(self.fd.received, ["hello", "goodbye"])
def test_tubeYieldedDeferredFiresWhileFlowIsPaused(self): """ When a L{Tube} yields an L{Deferred} and that L{Deferred} fires when the L{_SiphonFount} is paused it should buffer it's result and deliver it when L{_SiphonFount.resumeFlow} is called. """ d = Deferred() @tube class DeferredTube(object): def received(self, data): yield d fakeDrain = self.fd self.ff.flowTo(series(DeferredTube(), deferredToResult())).flowTo(fakeDrain) self.ff.drain.receive("ignored") anPause = self.fd.fount.pauseFlow() d.callback("hello") self.assertEquals(self.fd.received, []) anPause.unpause() self.assertEquals(self.fd.received, ["hello"])
def display_received(label): # tube used for debuging @receiver() def received(item): print "%r DISPLAY %r" % (label, item) yield item return series(received)
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()))
def calculatorSeries(): from tubes.tube import series from tubes.framing import bytesToLines, linesToBytes return series(bytesToLines(), linesToNumbersOrOperators, CalculatingTube(Calculator()), numbersToLines, linesToBytes())
def calculatorSeries(): from tubes.tube import series from tubes.framing import bytesToLines, linesToBytes return series( bytesToLines(), linesToNumbersOrOperators, CalculatingTube(Calculator()), numbersToLines, linesToBytes() )
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 newParticipantFlow(self, flow): """ Create a flow for a new participant. @param flow: a L{Flow} with a drain and a fount for receiving commands; JSON-style dictionaries with a 'type' key indicating which verb to invoke on L{Participant}. """ commandFount = flow.fount.flowTo( series(OnStop(lambda: self.participants.remove(participant))) ) commandDrain = flow.drain participant = Participant(self, commandFount, commandDrain) self.participants.append(participant)
def test_tubeYieldsFiredDeferred(self): """ When a tube yields a fired L{Deferred} its result is synchronously delivered. """ @tube class SucceedingTube(object): def received(self, data): yield succeed(''.join(reversed(data))) fakeDrain = self.fd self.ff.flowTo(series(SucceedingTube(), deferredToResult())).flowTo(fakeDrain) self.ff.drain.receive("hello") self.assertEquals(self.fd.received, ["olleh"])
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()))
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 test_tubeYieldsUnfiredDeferred(self): """ When a tube yields an unfired L{Deferred} its result is asynchronously delivered. """ d = Deferred() @tube class WaitingTube(object): def received(self, data): yield d fakeDrain = self.fd self.ff.flowTo(series(WaitingTube(), deferredToResult())).flowTo(fakeDrain) self.ff.drain.receive("ignored") self.assertEquals(self.fd.received, []) d.callback("hello") self.assertEquals(self.fd.received, ["hello"])
def mathFlow(fount): fount.flowTo( series(packedPrefixToStrings(16), StringsToBoxes(), BoxConsumer(Math()), BoxesToData(), fount.drain))
def reverseFlow(flow): from tubes.framing import bytesToLines, linesToBytes lineReverser = series(bytesToLines(), Reverser(), linesToBytes()) flow.fount.flowTo(lineReverser).flowTo(flow.drain)
def tubeMap(func): @receiver() def received(item): yield func(item) return series(received)
def tubeFilter(pred): @receiver() def received(item): if pred(item): yield item return series(received)
def reverseFlow(fount, drain): from tubes.framing import bytesToLines, linesToBytes lineReverser = series(bytesToLines(), Reverser(), linesToBytes()) fount.flowTo(lineReverser).flowTo(drain)