示例#1
0
    def start(self):
        signal.signal(signal.SIGINT, server.signal_handler)
        self.serversocket.listen(5)
        self.inputs.append(self.serversocket)

        while self.inputs:
            readable, writable, exceptional = select.select(self.inputs, self.outputs, self.inputs)
            
            for s in readable:
                if s is self.serversocket:
                    # a readable server socket means we can accept
                    clientsocket, address = self.serversocket.accept()
                    print 'accepted connection from ' + str(address[0]) + ':' + str(address[1])
                    clientsocket.setblocking(0)
                    
                    self.inputs.append(clientsocket)
                    self.setup_repeat[clientsocket] = False
                    self.imei[clientsocket] = ''
                    self.message_queue[clientsocket] = Queue.Queue()
                    
                else: # s is an established connection, we can recv 
                    data = self.myrecv(s)
                    if data:
                        message_type = gps_functions.get_msg_type(data)

                        if message_type == 'init':
                            self.imei[s] = gps_functions.get_imei(data)

                        if message_type in self.answer:
                            self.queue_message(s, self.answer[message_type])
                        elif message_type == "help me" or message_type == "low battery": # SOS: SOMETHING HAPPEND TO A PERSON!
                            self.queue_message(s, "**,imei:%s,E" % self.imei[s])
                            
                        if not self.setup_repeat[s]:
                            self.setup_repeat[s] = True
                            self.queue_message(s, '**,imei:' + self.imei[s] +',C,20s')
                            
                        if message_type in self.signal_types:
                            if gps_functions.is_valid_gps_signal(data):
                                row = sql_functions.write_location_to_log(self.imei[s] , data)
                                sql_functions.check_alerts(row)
                                
                    else: # readable socket without data means the client disconnected 
                        self.cleanup_socket(s)
            
            for s in writable:
                try: 
                    next_msg = self.message_queue[s].get_nowait()
                except Queue.Empty:
                    print 'Queue.Empty exception: output queue for socket ' , s.getpeername() , ' is empty. Removing socket from writeable queue.'
                else:
                    self.mysendall(s, next_msg)
                finally:
                    self.outputs.remove(s)
                    
            
            for s in exceptional:
                print  s.getpeername(), ' is in an exceptional state. cleaning up.'
                cleanup_socket(s)
示例#2
0
文件: temp.py 项目: wat-izz/octoninja
import sql_functions
imei = '863070011991451'
data = 'imei:863070011991451,tracker,1301020229,,F,182949.000,A,3205.5019,N,03447.3352,E,21.00,11.0,;' 

print 'testing function sql_functions.write_location_to_log(imei,data)...'
res = sql_functions.write_location_to_log(imei,data)
print 'function returned: ' + res
sql_functions.check_alerts(res)