1. Owen Nelson
  2. game-of-life

Commits

Owen Nelson  committed de7f15e

finally works (kind of)

  • Participants
  • Parent commits 6231702
  • Branches twisted

Comments (0)

Files changed (1)

File client.py

View file
  • Ignore whitespace
 import sys
 import json
 from twisted.python import log
+
+log.startLogging(sys.stdout)
+
+
 from twisted.internet.protocol import ClientFactory
-from twisted.protocols.basic import LineReceiver
+from twisted.protocols.basic import LineOnlyReceiver
 from twisted.internet import reactor
+from twisted.internet.task import LoopingCall
 from cgol import Game
 
 
 game = None
 
 
-class GOLClient(LineReceiver):
+class GOLClient(LineOnlyReceiver):
+
+    delimiter = '\n'
+    MAX_LENGTH = sys.maxint
 
     def connectionMade(self):
         print "connected"
         self.start_game()
 
-    def lineReceived(self, data):
-        print "receive:", repr(data)
+    def lineReceived(self, line):
+        print "receive:", repr(line)
 
     def start_game(self):
         print 'starting game'
         self.sendLine(json.dumps({'shape': game.state.shape}))
-        while 1:
+
+        def _tick():
             self.sendLine(json.dumps({'frame': list(game.next())}))
 
+        lc = LoopingCall(_tick)
+        lc.start(0.01)
+
 
 class GOLClientFactory(ClientFactory):
     protocol = GOLClient
         shape = int(sys.argv[2]), int(sys.argv[3])
     except:
         port = 9999
-        shape = (50, 50)
+        shape = (500, 500)
 
     global game
     game = Game(size=shape)
 
-    log.startLogging(sys.stdout)
     factory = GOLClientFactory()
     reactor.connectTCP('localhost', port, factory)
     reactor.run()