def main(nodeName, server=False): cookie = readCookie() nodeName = buildNodeName(nodeName) process = Process(nodeName, cookie) process.registerModule("api", RemoteCalls(process)) if server: process.listen().addCallback(lambda x: testReceive(process)) else: testPing(process) reactor.run()
def test_ping(self): """ L{Process.ping} returns a L{Deferred} which fires with "pong" if the process was found. """ process = Process(self.nodeName, self.cookie) def check(response): self.assertEqual(response, "pong") return process.ping(self.erlangName).addCallback(check)
def test_callRemote(self): """ L{Process.callRemote} executes the given function on the remove process and returns the reponse. """ process = Process(self.nodeName, self.cookie) def check(response): expected = (Atom("ok"), map(ord, os.getcwd())) self.assertEqual(response, expected) return process.callRemote( self.erlangName, "file", "get_cwd").addCallback(check)
def test_setCompatibility(self): """ Sets created by twotp are compatible with erlang sets. """ s = set([Atom("foo"), 3]) process = Process(self.nodeName, self.cookie) def check(response): expected = set([Atom("foo"), 3, Atom("egg")]) self.assertEqual(response, expected) deferred = process.callRemote( self.erlangName, "sets", "add_element", Atom("egg"), s) return deferred.addCallback(check)
def test_dictCompatibility(self): """ Dicts created by twotp are compatible with erlang dict. """ d = {Atom("foo"): "spam"} process = Process(self.nodeName, self.cookie) def check(response): expected = {Atom("egg"): 4, Atom("foo"): map(ord, "spam")} self.assertEqual(response, expected) deferred = process.callRemote( self.erlangName, "dict", "store", Atom("egg"), 4, d) return deferred.addCallback(check)
def test_spawn(self): """ Spawn a process on the erlang side, send data from python, and receive data after. """ d = Deferred() class TestProcess(SpawnProcess): def start(oself, pid, *args): oself.send(pid, Tuple(["ok", "connect"])) oself.receive().chainDeferred(d) class RemoteCalls(object): test = TestProcess process = Process(self.nodeName, self.cookie) process.registerModule("api", RemoteCalls()) def spawn(ignore): self.protocol.transport.write( "Pid = spawn(%s, api, test, []).\n" % self.nodeName) self.protocol.transport.write( "receive Msg -> io:format(\"**~p**~n\", [Msg]) end.\n") self.protocol.transport.write( "Pid ! {self(), \"ok\", \"disconnect\"}.\n") return d def check(result): self.assertIn("**{\"ok\",\"connect\"}**", self.protocol.data[1]) self.assertEqual("ok", "".join(map(chr, result[1]))) self.assertEqual("disconnect", "".join(map(chr, result[2]))) return process.stop() d.addCallback(check) return process.listen().addCallback(spawn)
# do something interesting print "user %s available"%(l2u(jid[1])) return Atom("ok") def remote_set_presence(self, user, server, resource, p): pprint(p) print "new presence: %s %s %s"%(l2u(user), l2u(server), l2u(resource)) print to_domish(p).toXml() return Atom("ok") def remote_receive_packet(self, jid, frm, to, packet): print ">>", to_domish(packet).toXml() def remote_send_packet(self, frm, to, packet): print "<<", to_domish(packet).toXml() if __name__ == "__main__": cookie = readCookie() nodeName = "*****@*****.**" process1 = Process(nodeName, cookie, "olindo.bluendo.priv") reactor.callWhenRunning(register_hook, process1) nodeName = "*****@*****.**" process2 = Process(nodeName, cookie, "olindo.bluendo.priv") process2.registerModule("proxy", UserMonitor()) process2.listen() reactor.run()