def serverWork(self, in_str_json): if self.DEBUG: print timestamp(), 'Starting serverWork...' self._getJSONin(in_str_json) self._handleJSON() temp = self._getJSONout() if self.DEBUG: print timestamp(), 'Done serverWork...' return temp
def main(DEBUG=True): data = Database() if LOAD_LAST_DUMP: t = data.load() #load any previous instance, with timestamp 't' print 'Loaded Previous Data From: ' + str(t) if DEBUG: #for id in range(200,200): #add dummy users # data.addUser(id) try: data.users = dummyUsers(users=3) print 'loading dummyUsers() Data' last = timestamp() except: pass #if DEBUG: print '' #if DEBUG: s = data.serverWork('{"list_users":1}') #if DEBUG: print '' #if DEBUG: print 'list_users', s #if DEBUG: print '' data.lock = Lock() #add Lock() to database object sock = socket.socket() while (1): try: sock.bind((SOCKETHOST, SOCKETPORT)) break except: print 'Trying To Bind Socket', (SOCKETHOST, SOCKETPORT) sleep(1) pass print['(SOCKETHOST, SOCKETPORT)', (SOCKETHOST, SOCKETPORT)] sock.listen(5) while True: print '' if DEBUG: try: if (timestamp() - last) > 2: temp = dummyUsers(users=3) for k in temp.keys(): data.users[k] = temp[k] print 'loading dummyUsers() Data' last = timestamp() except: pass client_instance, addr = sock.accept() #thread.start_new_thread(handler, (data, client_instance, addr, 1024, DEBUG)) while (1): try: Thread(target=handler, args=(data, client_instance, addr, 1024, DEBUG)).start() break except: print 'Trying to Launch Thread' sleep(0.01) pass
def logIP(client_ip, filename='ip.log'): with open(filename, 'a') as file: line = '' try: line += str(client_ip) line += ' ' line += str(timestamp()) line += '\n' except: print 'ERROR logIP(): ' + line file.write(line)
def _specialLogging(self, logname='position.log'): #Print: timestamp,[uid,Lat,Long,Heading],[],[],... parts = [] parts.append('timestamp') for k in self.users.keys(): u = self.users[k] parts.append(str(k)) parts.append(repr(u['posStruct']['lat'])) parts.append(repr(u['posStruct']['long'])) parts.append(repr(u['posStruct']['heading'])) parts[0] = str(timestamp()) parts = ','.join(parts) parts += '\n' with open(logname, 'a') as f: f.write(parts)
def _ack(self): #return (socket.gethostbyname(socket.gethostname()), self.port) return timestamp()
def _handleJSON(self): if self.DEBUG: print timestamp(), 'Inside _handleJSON(), Starting handles...' if self.DEBUG: print timestamp( ), 'Inside _handleJSON(), calling _hUpdateUser()...' self._hUpdateUser() if self.DEBUG: print timestamp( ), 'Inside _handleJSON(), calling _hUpdateMarker()...' self._hUpdateMarker() if self.DEBUG: print timestamp(), 'Inside _handleJSON(), calling _hUpdatePos()...' self._hUpdatePos() if self.DEBUG: print timestamp(), 'Inside _handleJSON(), calling _hGetUser()...' self._hGetUser() if self.DEBUG: print timestamp(), 'Inside _handleJSON(), calling _hListUsers()...' self._hListUsers() if self.DEBUG: print timestamp(), 'Inside _handleJSON(), calling _hPing()...' self._hPing() return None
def handler(database, socket_connection, addr, BUFF_SIZE=1024, DEBUG=False): if DEBUG: print timestamp(), 'Opening socket...' client_msg_str = socket_connection.recv(BUFF_SIZE) if DEBUG: print timestamp(), 'Socket opened' if client_msg_str: if DEBUG: print 'From: ' + str(addr) + ', Data: ' + str([client_msg_str]) # if DEBUG: print timestamp(), 'Acquiring lock...' database.lock.acquire() if DEBUG: print timestamp(), 'Lock acquired' if DEBUG: logIP(addr) output_json_str = database.serverWork(client_msg_str) database.dump() if DEBUG: print timestamp(), 'Releasing lock...' database.lock.release() if DEBUG: print timestamp(), 'Lock released' # socket_connection.send(output_json_str) if DEBUG: print 'From: ' + 'Server (me)' + ', ' + 'Data: ' + str( [output_json_str]) if DEBUG: print timestamp(), 'Closing socket...' socket_connection.close() if DEBUG: print timestamp(), 'Socket closed' if DEBUG: print 'Closed: ' + 'Client on ' + str(addr) return None