Hi,

Looking forward implementing the server side part of this jpoker bug
fix:

http://jspoker.pokersource.info/ideas.html#[[two%20tabs%20open%20on%20the%20same%20game%20%3A%20packets%20get%20distributed%20to%20both%20and%20its%20a%20mess]]
#two tabs open on the same game : packets get distributed to both and its a mess

I would like to propose the attached poker-network patch for review:

It adds a new rest filter: countfilter which discard packet by
throwing and Exception if COUNT_URLHASH is < to last received
COUNT_URLHASH value.

Please note that currently the relevant cookie in jpoker (currently
jpoker_poker_serial_URLHASH) would have to be renamed.

Feel free to reply with comments.
-- 
bou ^
Index: configure.ac
===================================================================
--- configure.ac	(revision 4702)
+++ configure.ac	(working copy)
@@ -338,6 +338,7 @@
         tests/test-pokerauth.py
         tests/test-pokertable.py
         tests/test-proxyfilter.py
+        tests/test-countfilter.py
         tests/test-string.py
         tests/test-pokerchildren.py
         tests/test-webservice.py
Index: tests/test-countfilter.py.in
===================================================================
--- tests/test-countfilter.py.in	(revision 0)
+++ tests/test-countfilter.py.in	(revision 0)
@@ -0,0 +1,107 @@
[EMAIL PROTECTED]@
+# -*- mode: python -*-
+#
+# Copyright (C) 2008 Johan Euphrosine <[EMAIL PROTECTED]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+
+import sys, os
+sys.path.insert(0, "./..")
+sys.path.insert(0, "..")
+
+from twisted.trial import unittest, runner, reporter
+
+from tests import testmessages
+verbose = int(os.environ.get('VERBOSE_T', '-1'))
+if verbose < 0: testmessages.silence_all_messages()
+
+from tests import testclock
+from pokernetwork import pokermemcache
+from pokernetwork import countfilter
+
+class CountTestCase(unittest.TestCase):
+    def setUp(self):
+        testclock._seconds_reset()
+        self.memcache = pokermemcache.MemcacheMockup.Client([])
+    def tearDown(self):
+        pass
+    def test01_set_count_memcache(self):
+        memcache = self.memcache
+        uid = 42
+        cookie_timeout = 60
+        memcache_timeout = cookie_timeout * 2
+        sitepath = 'URLID'
+        count_prefix = 'COUNT'
+        cookie_name = "_".join([count_prefix,str(sitepath)])
+        memcache_key = "_".join([count_prefix,str(sitepath),str(uid)])
+
+        class Session:
+            def __init__(self):
+                self.uid = uid
+        class Request:            
+            def __init__(self, count):
+                self.sitepath = sitepath
+                self.count = count
+            def getCookie(self, name):
+                if name == cookie_name:
+                    return self.count
+            def getSession(self):
+                return Session()
+        class Site:
+            def __init__(self):
+                self.memcache = memcache
+                self.cookieTimeout = cookie_timeout
+        class Packet:
+            pass
+        
+        count = 1
+        countfilter.rest_filter(Site(), Request(count), Packet())
+        self.assertEqual(self.memcache.get(memcache_key), count)
+        self.assertEqual(memcache_timeout, self.memcache.expiration[memcache_key])
+
+        count = 1
+        countfilter.rest_filter(Site(), Request(count), Packet())
+        self.assertEqual(self.memcache.get(memcache_key), count)
+        self.assertEqual(memcache_timeout, self.memcache.expiration[memcache_key])
+
+        count = 2
+        countfilter.rest_filter(Site(), Request(count), Packet())
+        self.assertEqual(self.memcache.get(memcache_key), count)
+        self.assertEqual(memcache_timeout, self.memcache.expiration[memcache_key])
+        
+        count = 1
+        self.assertRaises(Exception, lambda: countfilter.rest_filter(Site(), Request(count), Packet()))
+
+def Run():
+      loader = runner.TestLoader()
+#      loader.methodPrefix = "test01"
+      suite = loader.suiteFactory()
+      suite.addTest(loader.loadClass(CountTestCase))
+      return runner.TrialRunner(
+            reporter.VerboseTextReporter,
+            tracebackFormat='default',
+            ).run(suite)
+
+if __name__ == '__main__':
+      if Run().wasSuccessful():
+            sys.exit(0)
+      else:
+            sys.exit(1)
+
+# Interpreted by emacs
+# Local Variables:
+# compile-command: "( cd .. ; ./config.status tests/test-countfilter.py ) ; ( cd ../tests ; make COVERAGE_FILES='../pokernetwork/countfilter.py' VERBOSE_T=-1 TESTS='coverage-reset test-countfilter.py coverage-report' check )"
+# End:
Index: pokernetwork/countfilter.py
===================================================================
--- pokernetwork/countfilter.py	(revision 0)
+++ pokernetwork/countfilter.py	(revision 0)
@@ -0,0 +1,43 @@
+#
+# Copyright (C) 2008 Johan Euphrosine <[EMAIL PROTECTED]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+
+from pokernetwork import pokermemcache
+
+#
+# return a value if all actions were complete
+#
+def rest_filter(site, request, packet):    
+    session = request.getSession()
+    count_prefix = 'COUNT'
+    cookie_name = "_".join([count_prefix, str(request.sitepath)])
+    count = request.getCookie(cookie_name)
+    memcache_key = "_".join([count_prefix, str(request.sitepath), str(session.uid)])
+    last_count = site.memcache.get(memcache_key)
+    if count < last_count:
+        raise Exception("%s cookie (%s) < last receivied %s cookie (%s)" % count_prefix, count, count_prefix, last_count)
+    site.memcache.set(memcache_key, count, site.cookieTimeout*2)
+    return True
+
+#
+# return a deferred if there is a pending action
+#
+
+#from twisted.internet import defer
+
+#def rest_filter(site, request, packet):
+#    return defer.Deferred()
_______________________________________________
Pokersource-users mailing list
[email protected]
https://mail.gna.org/listinfo/pokersource-users

Reply via email to