On Sun, 2009-09-06 at 20:07 +0200, Loic Dachary wrote: > While implementing it, I realized the PokerRestClient API is wrong. Instead > of setting a callback ( receive function ) it must fire a deferred > that is returned by the sendPacket function. Callbacks are not in the > twisted tradition anyway. The use of the deferred is illustrated below. >
Hi, I attached a patch that I believe implement the new pokerrestclient API you described, let me know if it fits your needs. -- Johan Euphrosine <[email protected]>
Index: pokernetwork/pokerrestclient.py
===================================================================
--- pokernetwork/pokerrestclient.py (revision 6211)
+++ pokernetwork/pokerrestclient.py (working copy)
@@ -108,9 +108,13 @@
if self.verbose > 3:
self.message('sendPacket PacketPokerLongPollReturn')
self.sendPacketData('{ "type": "PacketPokerLongPollReturn" }')
+ d = defer.Deferred()
+ d.addCallbacks(self.receivePacket, self.receiveError)
self.queue.addCallback(lambda status: self.sendPacketData(data))
+ self.queue.chainDeferred(d)
if packet.type == PACKET_POKER_LONG_POLL:
self.pendingLongPoll = True
+ return d
def receivePacket(self, data):
print "receivePacket", data
@@ -121,6 +125,7 @@
args = pokersite.fromutf8(args)
packets = pokersite.args2packets(args)
self.received(packets)
+ return packets
def receiveError(self, data):
self.errorPacket(data)
@@ -129,11 +134,11 @@
self.received([ PacketError(message = str(reason)) ])
def sendPacketData(self, data):
+ print "send", data
factory = RestClientFactory(self.host, self.port, self.path, data, self.timeout)
reactor.connectTCP(self.host, self.port, factory)
- factory.deferred.addCallbacks(self.receivePacket, self.receiveError)
- self.queue.addCallback(lambda arg: factory.deferred)
self.sentTime = seconds()
+ return factory.deferred
def clearTimeout(self):
if self.timer and self.timer.active():
Index: tests/test-pokerrestclient.py.in
===================================================================
--- tests/test-pokerrestclient.py.in (revision 6211)
+++ tests/test-pokerrestclient.py.in (working copy)
@@ -115,6 +115,25 @@
client.longPoll()
self.assertNotEquals(None, client.timer)
return client.queue
+ # --------------------------------------------------------------
+ def test04_sendPacketData(self):
+ client = pokerrestclient.PokerRestClient('127.0.0.1', 19481, '/POKER_REST', verbose=6, timeout=10)
+ d = client.sendPacketData('{"type": "PacketPokerTableSelect"}')
+ d.addCallback(lambda data: self.assertSubstring('PacketPokerTableList', data))
+ return d
+ # --------------------------------------------------------------
+ def test05_sendPacket(self):
+ client = pokerrestclient.PokerRestClient('127.0.0.1', 19481, '/POKER_REST', verbose=6, timeout=10)
+ d = client.sendPacket(PacketPokerTableSelect(), '{"type": "PacketPokerTableSelect"}')
+ d.addCallback(lambda packets: self.assertEquals(PACKET_POKER_TABLE_LIST, packets[0].type))
+ return d
+ # --------------------------------------------------------------
+ def test06_404(self):
+ client = pokerrestclient.PokerRestClient('127.0.0.1', 19481, '/POKER_REST2', verbose=6, timeout=10)
+ d = defer.Deferred()
+ d.addCallback(lambda packets: self.assertEquals(PACKET_ERROR, packets[0].type))
+ client.received = d.callback
+ return d
class MockRequest:
def finish(self): pass
signature.asc
Description: This is a digitally signed message part
_______________________________________________ Pokersource-users mailing list [email protected] https://mail.gna.org/listinfo/pokersource-users
