diff options
author | Russell Cohen <rcoh@mit.edu> | 2010-11-24 01:09:12 -0500 |
---|---|---|
committer | Russell Cohen <rcoh@mit.edu> | 2010-11-24 01:09:12 -0500 |
commit | b042647b68abdc82490ca6e059993b8eba28904c (patch) | |
tree | a9ee95a38e98b377c251b7b2e9af9cbd8056cf7c /inputs | |
parent | 407ac922fc4178021cf3a16dfb1bd875b6083ac4 (diff) |
Refactoring complete! Made modules/packages as appropriate. Finally.
Diffstat (limited to 'inputs')
-rw-r--r-- | inputs/PygameInput.py | 17 | ||||
-rw-r--r-- | inputs/TCPInput.py | 52 | ||||
-rw-r--r-- | inputs/UDPInput.py | 17 |
3 files changed, 86 insertions, 0 deletions
diff --git a/inputs/PygameInput.py b/inputs/PygameInput.py new file mode 100644 index 0000000..6c84664 --- /dev/null +++ b/inputs/PygameInput.py @@ -0,0 +1,17 @@ +import time, Util +from operationscore.Input import * +import pygame +from pygame.locals import * +#This class processes input from an already running pygame instance and passes +#it to the parent. This class requires an already running pygame instance. +class PygameInput(Input): + def sensingLoop(self): + #try: + for event in pygame.event.get(): + if event.type is KEYDOWN: + self.respond({Util.location: (5,5),'Key': event.key}) + if event.type is MOUSEBUTTONDOWN: + self.respond({Util.location: pygame.mouse.get_pos()}) + #except: + #raise Exception('Pygame not initialized. Pygame must be \ + #initialized.') diff --git a/inputs/TCPInput.py b/inputs/TCPInput.py new file mode 100644 index 0000000..72d8742 --- /dev/null +++ b/inputs/TCPInput.py @@ -0,0 +1,52 @@ +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() + print "%s wrote:" % self.client_address[0] + print self.data + + 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; diff --git a/inputs/UDPInput.py b/inputs/UDPInput.py new file mode 100644 index 0000000..b0d6c93 --- /dev/null +++ b/inputs/UDPInput.py @@ -0,0 +1,17 @@ +import Util +from operationscore.Input import * +import socket +class UDPInput(Input): + def inputInit(self): + HOST = '' # Symbolic name meaning all available interfaces + PORT = self.argDict['Port'] # Arbitrary non-privileged port + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.bind((HOST, PORT)) + print 'UDPINIT' + def sensingLoop(self): + print 'udploop' + (data,address) = self.sock.recvfrom(1024) + dataDict = {'data':data, 'address':address} + print 'LOLOLOLOL' + self.respond(dataDict) + |