Example #1
0
 def handle_WHATSAT(self, info, command):
     clt_id, r_str, max_info_str = info
     # check radius format
     try:
         radius = valid_radius(r_str)[0]
     except TypeError:
         print('Invalid radius: {}'.format(r_str))
         self.handle_invalid(command)
         return
     # check max_info format
     try:
         max_info = valid_max(max_info_str)[0]
     except TypeError:
         print('Invalid bound for number of places: {}'\
               .format(max_info_str))
         self.handle_invalid(command)
         return
     # response1
     if clt_id not in myDatabase.keys():
         print('Client {} not found.'.format(clt_id))
         self.handle_invalid(command)
         return
     srv, lag, coord, time_str = myDatabase[clt_id]
     response1 = ('AT {0} {1} {2} {3} {4}\r\n'.format(
         srv, lag, clt_id, coord, time_str))
     self.transport.write(response1.encode())
     # response2
     lat, lng = valid_coord(coord)
     lat_lng = '{0},{1}'.format(lat, lng)
     coro = search_resp(lat_lng, r_str, max_info, self.transport)
     self.loop.create_task(coro)
     print('Request info from Google Places. loc:{0} rad:{1}'.format(
         lat_lng, r_str))
Example #2
0
 def handle_IAMAT(self, info, command):
     clt_id, coord, time_str = info
     # check location format
     try:
         lat, lng = valid_coord(coord)
     except TypeError:
         logging.error('Invalid location format: {}'.format(coord))
         self.handle_invalid(command)
         return
     # check time format
     try:
         user_time = valid_time(time_str)[0]
     except TypeError:
         logging.error('Invalid time: {}'.format(time_str))
         self.handle_invalid(command)
         return
     lag = time.time() - user_time
     if lag > 0: lag_str = '+' + str(lag)
     else: lag_str = str(lag)
     # respond to client
     response = ('AT {0} {1} {2} {3} {4}\n'.format(self.srv, lag_str,
                                                   clt_id, coord, time_str))
     self.transport.write(response.encode())
     logging.info('Send:\n\t{!r}'.format(response))
     # update server's database (& prop)
     new_data = (self.srv, lag, coord, time_str)
     self.update_database(clt_id, new_data)
Example #3
0
 def handle_PEER(self, info):
     #info = [srv, lag, coord, time_str, clt_id], str
     clt_id = info[4]
     srv, lag_str, coord, time_str = info[0:4]
     # make sure everything is valid
     i = list(srv2port.keys()).index(srv)
     lag = float(lag_str)
     lat, lng = valid_coord(coord)
     user_time = valid_time(time_str)[0]
     new_data = (srv, lag, coord, time_str)
     self.update_database(clt_id, new_data)
Example #4
0
 def handle_WHATSAT(self, info, command):
     clt_id, r_str, max_info_str = info
     # check radius format
     try:
         radius = valid_radius(r_str)[0]
         radius *= 1000
     except TypeError:
         logging.error('Invalid radius: {}'.format(r_str))
         self.handle_invalid(command)
         return
     # check max_info format
     try:
         max_info = valid_max(max_info_str)[0]
     except TypeError:
         logging.error('Invalid bound for number of places: {}'\
                       .format(max_info_str))
         self.handle_invalid(command)
         return
     # response1
     if clt_id not in myDatabase.keys():
         logging.error('Client {} not found.'.format(clt_id))
         self.handle_invalid(command)
         return
     srv, lag, coord, time_str = myDatabase[clt_id]
     if lag > 0: lag_str = '+' + str(lag)
     else: lag_str = str(lag)
     response1 = ('AT {0} {1} {2} {3} {4}\n'.format(srv, lag_str, clt_id,
                                                    coord, time_str))
     self.transport.write(response1.encode())
     logging.info('Send:\n\t{!r}'.format(response1))
     # response2
     lat, lng = valid_coord(coord)
     lat_lng = '{0},{1}'.format(lat, lng)
     logging.debug('Request to GooglePlaces. loc:{0} rad:{1}m max_info:{2}'\
                   .format(lat_lng,radius,max_info))
     coro = search_resp(lat_lng, str(radius), max_info, self.transport)
     self.loop.create_task(coro)
Example #5
0
 def handle_IAMAT(self, info, command):
     clt_id, coord, time_str = info
     # check location format
     try:
         lat, lng = valid_coord(coord)
     except TypeError:
         print('Invalid location format: {}'.format(coord))
         self.handle_invalid(command)
         return
     # check time format
     try:
         user_time = valid_time(time_str)[0]
     except TypeError:
         print('Invalid time: {}'.format(time_str))
         self.handle_invalid(command)
         return
     # update server's database
     lag = time.time() - user_time
     new_data = (self.srv, lag, coord, time_str)
     self.update_database(clt_id, new_data)
     # respond to client
     response = ('AT {0} {1} {2} {3} {4}\r\n'.format(
         self.srv, lag, clt_id, coord, time_str))
     self.transport.write(response.encode())