def doMath(): destination = TCP4ClientEndpoint(reactor, '127.0.0.1', 1234) sumDeferred = connectProtocol(destination, AMP()) def connected(ampProto): return ampProto.callRemote(Sum, a=13, b=81) sumDeferred.addCallback(connected) def summed(result): return result['total'] sumDeferred.addCallback(summed) divideDeferred = connectProtocol(destination, AMP()) def connected(ampProto): return ampProto.callRemote(Divide, numerator=1234, denominator=2) divideDeferred.addCallback(connected) def trapZero(result): result.trap(ZeroDivisionError) print("Divided by zero: returning INF") return 1e1000 divideDeferred.addErrback(trapZero) def done(result): print('Done with math:', result) reactor.stop() defer.DeferredList([sumDeferred, divideDeferred]).addCallback(done)
def test_reconnected(self): """ ``IConvergenceAgent.connected()`` can be called after ``IConvergenceAgent.disconnected()``. """ agent = fixture(self) agent.connected(AMP()) agent.disconnected() agent.connected(AMP())
def searchUserAvailableBallots(user_id): """ http://crochet.readthedocs.io/en/latest/api.html#run-in-reactor-asynchronous-results Blocking call to the ballot server to request the ballots for a particular user. :return: EventualResult """ # NOTE: using inline callbacks here so we dont have to write/wait for callbacks. destination_deferred = yield TCP4ClientEndpoint(reactor, ballotregulator_ip, ballotregulator_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineBallotRegulator_SearchBallotRegisterForUserId, user_id=user_id) def format_results(pickled_result): # First unpickle the results. result = pickle.loads(pickled_result['ok']) # Transform the list results into a dictionary. record_list = [] for record in result: mapper = {} mapper['user_id'] = record[0] mapper['ballot_id'] = record[1] mapper['timestamp'] = record[2] mapper['ballot_name'] = record[3] mapper['ballot_address'] = record[4] # Append each row's dictionary to a list record_list.append(mapper) return record_list # Inlinecallback return value. returnValue(format_results(result_deferred))
def connected_amp_protocol(): """ :return: ``AMP`` hooked up to transport. """ p = AMP() p.makeConnection(StringTransport()) return p
def searchUserAddressRegisterVote(voter_address): """ http://crochet.readthedocs.io/en/latest/api.html#run-in-reactor-asynchronous-results Blocking call to the acountverifier server to request the ballots for a particular user has already registered for.. :return: EventualResult """ # NOTE: using inline callbacks here so we dont have to write/wait for callbacks. destination_deferred = yield TCP4ClientEndpoint(reactor, accountverifier_ip, accountverifier_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineAccountVerifier_SearchRegisterVoteForAddress, voter_address=voter_address) def format_results(pickled_result): # First unpickle the results. result = pickle.loads(pickled_result['ok']) # Transform the list results into a dictionary. record_list = [] for record in result: mapper = {} mapper['register_vote_id'] = record[0] mapper['signed_token_hash'] = record[1] mapper['voter_address'] = record[2] mapper['ballot_id'] = record[3] mapper['timestamp'] = record[4] # Append each row's dictionary to a list record_list.append(mapper) return record_list # Inlinecallback return value. returnValue(format_results(result_deferred))
def setUp(self): yield super(DPSClientAugmentedAggregateDirectoryTest, self).setUp() # The "local" directory service self.client = DirectoryService(None) # The "remote" directory service remoteDirectory = self.directory # Connect the two services directly via an IOPump client = AMP() server = DirectoryProxyAMPProtocol(remoteDirectory) pump = returnConnected(server, client) # Replace the normal _getConnection method with one that bypasses any # actual networking self.patch(self.client, "_getConnection", lambda: succeed(client)) # Wrap the normal _sendCommand method with one that flushes the IOPump # afterwards origCall = self.client._sendCommand def newCall(*args, **kwds): d = origCall(*args, **kwds) pump.flush() return d self.patch(self.client, "_sendCommand", newCall)
def setUp(self): # The "local" directory service self.directory = DirectoryService(None) # The "remote" directory service if testMode == "xml": # Need a copy as it might change path = FilePath(os.path.join(os.path.dirname(__file__), "test.xml")) copy = FilePath(self.mktemp()) path.copyTo(copy) remoteDirectory = CalendarXMLDirectoryService(copy) elif testMode == "od": remoteDirectory = CalendarODDirectoryService() # Connect the two services directly via an IOPump client = AMP() server = DirectoryProxyAMPProtocol(remoteDirectory) pump = returnConnected(server, client) # Replace the normal _getConnection method with one that bypasses any # actual networking self.patch(self.directory, "_getConnection", lambda: succeed(client)) # Wrap the normal _sendCommand method with one that flushes the IOPump # afterwards origCall = self.directory._sendCommand def newCall(*args, **kwds): d = origCall(*args, **kwds) pump.flush() return d self.patch(self.directory, "_sendCommand", newCall)
def test_adds_user(self): """ When L{UserAdder} is connected to L{IdentityAdmin}, the L{AddUser} command is called and L{IdentityAdmin} adds the user to its factory's store. """ admin = IdentityAdmin() admin.factory = self.adminFactory serverTransport = makeFakeServer(admin) serverTransport.getQ2QHost = lambda: Q2QAddress('Q2Q Host') client = AMP() pump = connect(admin, serverTransport, client, makeFakeClient(client)) d = client.callRemote(AddUser, name='q2q username', password='******') pump.flush() # The username and password are added, along with the domain=q2q # host, to the IdentityAdmin's factory's store self.assertEqual([call('Q2Q Host', 'q2q username', 'q2q password')], self.addUser.calls) # The server responds with {} self.assertEqual({}, self.successResultOf(d))
def spawnWithStore(self, here, there): """ 'here' and 'there' are the helper protocols 'there' will expect to be created with an instance of a store. """ master = yield self.spawn(AMP(), StoreCreator) yield master.callRemote(CreateStore, delegateTo=qual(there)) returnValue(swapAMP(master, here))
def callRemote(command, port, **kwargs): dest = TCP4ClientEndpoint(reactor, '127.0.0.1', port) d = connectProtocol(dest, AMP()) def connected(ampProto): return ampProto.callRemote(command, **kwargs) return d.addCallback(connected)
def test_cluster_updated(self): """ ``IConvergenceAgent.cluster_updated()`` takes two ``Deployment`` instances. """ agent = fixture(self) agent.connected(AMP()) agent.cluster_updated(Deployment(nodes=frozenset()), Deployment(nodes=frozenset()))
def setUp(self): self.numUsers = 1000 # The "local" directory service self.directory = DirectoryService(None) # The "remote" directory service remoteDirectory = CalendarInMemoryDirectoryService(None) # Add users records = [] fieldName = remoteDirectory.fieldName for i in xrange(self.numUsers): records.append( TestRecord( remoteDirectory, { fieldName.uid: u"foo{ctr:05d}".format(ctr=i), fieldName.shortNames: (u"foo{ctr:05d}".format(ctr=i), ), fieldName.fullNames: (u"foo{ctr:05d}".format(ctr=i), ), fieldName.recordType: RecordType.user, })) # Add a big group records.append( TestRecord( remoteDirectory, { fieldName.uid: u"bigGroup", fieldName.recordType: RecordType.group, })) yield remoteDirectory.updateRecords(records, create=True) group = yield remoteDirectory.recordWithUID(u"bigGroup") members = yield remoteDirectory.recordsWithRecordType(RecordType.user) yield group.setMembers(members) # Connect the two services directly via an IOPump client = AMP() self.server = DirectoryProxyAMPProtocol(remoteDirectory) pump = returnConnected(self.server, client) # Replace the normal _getConnection method with one that bypasses any # actual networking self.patch(self.directory, "_getConnection", lambda: succeed(client)) # Wrap the normal _call method with one that flushes the IOPump # afterwards origCall = self.directory._call def newCall(*args, **kwds): d = origCall(*args, **kwds) pump.flush() return d self.patch(self.directory, "_call", newCall)
def request_contract_abi(): destination_deferred = yield TCP4ClientEndpoint(reactor, ballotregulator_ip, ballotregulator_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineBallotRegulator_RequestContractABI) def format_results(pickled_result): # First unpickle the results. return pickle.loads(pickled_result['ok']) returnValue(format_results(result_deferred))
def build_agent_client(agent): """ Create convergence agent side of the protocol. :param IConvergenceAgent agent: Convergence agent to notify of changes. :return AMP: protocol instance setup for client. """ locator = _AgentLocator(agent) return AMP(boxReceiver=_AgentBoxReceiver(agent, locator), locator=locator)
def spawnWithConfig(self, config, here, there): """ Similar to spawnWithStore except the child process gets a configuration object instead. """ master = yield self.spawn(AMP(), StoreCreator) subcfg = copy.deepcopy(self.config) del subcfg._postUpdateHooks[:] yield master.callRemote(PickleConfig, config=subcfg, delegateTo=qual(there)) returnValue(swapAMP(master, here))
def getBallotPublicKey(ballot_id): destination_deferred = yield TCP4ClientEndpoint(reactor, accountverifier_ip, accountverifier_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineAccountVerifier_GetPublicKeyForBallot, ballot_id=int(ballot_id)) def format_results(pickled_result): ballot_public_key = pickle.loads(pickled_result['ok']) return ballot_public_key # Inlinecallback return value. returnValue(format_results(result_deferred))
def main(reactor, username, password): startLogging(stdout) router = Router() proto = AMP(router) router.bindRoute(proto, None).connectTo(None) cc = ClientCreator(reactor, lambda: proto) d = cc.connectTCP(username.split('@')[1], 7805) d.addCallback(login, UsernamePassword(username, password)) d.addCallback(connectRoute, router, BoxPrinter(), u'http://divmod.org/ns/echo') d.addCallback(sendBox) return d
def requestSignOfToken(user_id, ballot_id, blind_token): destination_deferred = yield TCP4ClientEndpoint(reactor, accountverifier_ip, accountverifier_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineAccountVerifier_SignBlindToken, user_id=user_id, ballot_id=ballot_id, blind_token=blind_token) def format_results(pickled_result): signed_blind_token = pickle.loads(pickled_result['ok']) return signed_blind_token # Inlinecallback return value. returnValue(format_results(result_deferred))
def testAddUser(user_id, password): destination_deferred = yield TCP4ClientEndpoint(reactor, '127.0.0.1', 5436) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote( ApplicationServer_RegisterNewUser, user_id=int(user_id), password=password) def format_results(result): return result['ok'] # Inlinecallback return value. returnValue(format_results(result_deferred))
def test_protocol(self): self.patch(AlertPoster, "postAlert", self.stubPostAlert) client = AMP() server = AMPAlertProtocol() pump = returnConnected(server, client) sender = AMPAlertSender(protocol=client) sender.sendAlert("alertType", ["arg1", "arg2"]) pump.flush() self.assertEquals(self.alertType, "alertType") self.assertEquals(self.ignoreWithinSeconds, 0) self.assertEquals(self.args, ["arg1", "arg2"])
def main(reactor, markup, styles): def _readFile(name): with file(name, 'rb') as fd: return fd.read() def _writeResponse(response): sys.stdout.write(response['data']) sys.stdout.flush() endpoint = clientFromString(reactor, 'tcp:host=127.0.0.1:port=8750') d = connectProtocol(endpoint, AMP()) d.addCallback(render, _readFile(markup), map(_readFile, styles)) d.addCallback(_writeResponse) return d
def callRemote(self, command, **kwargs): """ Return a previously registered response. @param commandType: a subclass of C{amp.Command}. @param kwargs: Keyword arguments taken by the command, a C{dict}. @return: A C{Deferred} that fires with the registered response for this particular combination of command and arguments. """ self.calls.append((command, kwargs)) command.makeArguments(kwargs, AMP()) return succeed(self._responses[self._makeKey(command, kwargs)])
def requestRegisterBallotidVoteraddress(ballot_id, signed_token, token, voter_address): pickled_signed_token = pickle.dumps(int(signed_token)) pickled_token = pickle.dumps(token) pickled_voter_address = pickle.dumps(voter_address) destination_deferred = yield TCP4ClientEndpoint(reactor, accountverifier_ip, accountverifier_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote(OnlineAccountVerifier_RegisterAddressToBallot, ballot_id=int(ballot_id), pickled_signed_token=pickled_signed_token, pickled_token=pickled_token, pickled_voter_address=pickled_voter_address) def format_results(result): return result['ok'] # Inlinecallback return value. returnValue(format_results(result_deferred))
def register_response(self, command, kwargs, response): """ Register a response to a L{callRemote} command. @param commandType: a subclass of C{amp.Command}. @param kwargs: Keyword arguments taken by the command, a C{dict}. @param response: The response to the command. """ try: command.makeResponse(response, AMP()) except KeyError: raise InvalidSignature("Bad registered response") self._responses[self._makeKey(command, kwargs)] = response
def test_periodic_noops(self): """ When connected, the protocol sends ``NoOp`` commands at a fixed interval. """ expected_pings = 3 reactor = Clock() locator = _NoOpCounter() peer = AMP(locator=locator) protocol = self.build_protocol(reactor) pump = connectedServerAndClient(lambda: protocol, lambda: peer)[2] for i in range(expected_pings): reactor.advance(PING_INTERVAL.total_seconds()) pump.flush() self.assertEqual(locator.noops, expected_pings)
def main(reactor, duration): concurrency = 15 server = ServerFactory() server.protocol = lambda: AMP(locator=BenchmarkLocator()) port = reactor.listenTCP(0, server) client = Client(reactor, port.getHost().port) d = client.run(concurrency, duration) def cleanup(passthrough): d = port.stopListening() d.addCallback(lambda ignored: passthrough) return d d.addCallback(cleanup) return d
def setUp(self): self.managerTransport = StringTransport() self.managerAMP = LocalWorkerAMP() self.managerAMP.makeConnection(self.managerTransport) self.result = TestResult() self.workerTransport = StringTransport() self.worker = AMP() self.worker.makeConnection(self.workerTransport) config = trial.Options() self.testName = "twisted.doesnexist" config["tests"].append(self.testName) self.testCase = trial._getSuite(config)._tests.pop() self.managerAMP.run(self.testCase, self.result) self.managerTransport.clear()
def requestRegisterUseridForBallotid(user_id, ballot_id): destination_deferred = yield TCP4ClientEndpoint(reactor, ballotregulator_ip, ballotregulator_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote( OnlineBallotRegulator_RegisterUserIdForBallotId, user_id=int(user_id), ballot_id=int(ballot_id)) def format_results(result): return result['ok'] # Inlinecallback return value. returnValue(format_results(result_deferred))
def setUp(self): """ Create a L{CredReceiver} hooked up to a fake L{IBoxSender} which records boxes sent through it. """ self.username = '******' self.password = '******' self.checker = InMemoryUsernamePasswordDatabaseDontUse() self.checker.addUser(self.username, self.password) self.avatar = StubAvatar() self.realm = StubRealm(self.avatar) self.portal = Portal(self.realm, [self.checker]) self.server = CredReceiver() self.server.portal = self.portal self.client = AMP() self.finished = loopbackAsync(self.server, self.client)
def detect(): try: from twisted.trial._dist.workerreporter import WorkerReporter except ImportError: return False from unittest import TestCase from twisted.protocols.amp import AMP from twisted.python.failure import Failure case = TestCase("run") reporter = WorkerReporter(AMP()) failure = Failure(Exception(u"\N{SNOWMAN}".encode("utf-8"))) try: reporter.addFailure(case, failure) except UnicodeDecodeError: return True return False