Package SmootLight :: Package inputs :: Module TCPInput_backup
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.inputs.TCPInput_backup

 1  import SocketServer 
 2  from operationscore.Input import * 
 3   
 4  """ 
 5  A rough sketch about how a TCP socket server receives data from the phone (or other stuff). 
 6  Some corrections are probably needed from Russell. 
 7  Looks good to me -- not really the way I envisioned it, but since the server 
 8  we're using has a built in loop.  When we call the reponse method to pass the 
 9  data up the pipe, we should use the sensingLoop so that everything stays 
10  thread-safe. 
11  """ 
12 -class TCPInput(Input.Input):
13 - class InputTCPHandler(SocketServer.BaseRequestHandler):
14 - def handle(self):
15 # get data from the TCP socket connected to the client 16 self.data = self.request.recv(1024).strip() 17 18 pydict = json.loads(self.data) # decode and add to queue 19 self.responseQueue.append(pydict) 20 21 """ 22 do something to the dict 23 """ 24 25 self.request.send("yes") # send back confirmation.
26
27 - def inputInit(self):
28 # initialize 29 self.host = "localhost" 30 self.port = 9999 31 self.responseQueue = [] 32 # start server 33 self.server = SocketServer.TCPServer((self.host, self.port), InputTCPHandler) 34 self.server.responseQueue = self.responseQueue 35 self.server.serve_forever() # server keeps running till Ctrl+C or self.server.shutdown() is called.
36
37 - def sensingLoop(self):
38 # loop action handled through TCPHandler? 39 # if check says to shut down the server, shut it. 40 if self.doShutDown(): 41 self.server.shutdown() 42 else: 43 for event in self.responseQueue: 44 self.respond(event) 45 self.responseQueue = []
46
47 - def doShutDown(self):
48 # do some checks to see if server should be shut down 49 return False;
50