diff options
author | Sun <sun@sun-nb.(none)> | 2010-12-08 02:41:06 -0500 |
---|---|---|
committer | Sun <sun@sun-nb.(none)> | 2010-12-08 02:41:06 -0500 |
commit | 4371fee2b48dd46861e6386eb04987ad8c6d5329 (patch) | |
tree | 6b76d56a22e5927401579db0a81501760dafaa61 /inputs/TCPInput_backup.py | |
parent | 353ab16db64c86122c0fcb9e1852b85c14b354b8 (diff) |
Put together TCPInput; added TCPInput and a colorchange Behavior for it in config xml; for testing, now phone can control pixel's location on pygame screen.
Diffstat (limited to 'inputs/TCPInput_backup.py')
-rw-r--r-- | inputs/TCPInput_backup.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/inputs/TCPInput_backup.py b/inputs/TCPInput_backup.py new file mode 100644 index 0000000..01b6a99 --- /dev/null +++ b/inputs/TCPInput_backup.py @@ -0,0 +1,50 @@ +import SocketServer +import Util +from operationscore.Input import * + +""" +A rough sketch about how a TCP socket server receives data from the phone (or other stuff). +Some corrections are probably needed from Russell. +Looks good to me -- not really the way I envisioned it, but since the server +we're using has a built in loop. When we call the reponse method to pass the +data up the pipe, we should use the sensingLoop so that everything stays +thread-safe. +""" +class TCPInput(Input.Input): + class InputTCPHandler(SocketServer.BaseRequestHandler): + def handle(self): + # get data from the TCP socket connected to the client + self.data = self.request.recv(1024).strip() + + pydict = json.loads(self.data) # decode and add to queue + self.responseQueue.append(pydict) + + """ + do something to the dict + """ + + self.request.send("yes") # send back confirmation. + + def inputInit(self): + # initialize + self.host = "localhost" + self.port = 9999 + self.responseQueue = [] + # start server + self.server = SocketServer.TCPServer((self.host, self.port), InputTCPHandler) + self.server.responseQueue = self.responseQueue + self.server.serve_forever() # server keeps running till Ctrl+C or self.server.shutdown() is called. + + def sensingLoop(self): + # loop action handled through TCPHandler? + # if check says to shut down the server, shut it. + if self.doShutDown(): + self.server.shutdown() + else: + for event in self.responseQueue: + self.respond(event) + self.responseQueue = [] + + def doShutDown(self): + # do some checks to see if server should be shut down + return False; |