def run(self): """ Peridocially send an announcement packet over our datalink """ tx_client = TxClient(self.tx_socket) gps_prev = None while True: logger.debug("Announcing Presence.") if self.db_retrans is not None: # Get a dict of all our current message ids msg_dict = self.db_retrans.get_message_id_dict(self.node_id) tx_client.send(G.MESSAGE_TYPE.RT_NODE_ANNOUNCEMENT, json.dumps(msg_dict)) else: tx_client.send(G.MESSAGE_TYPE.RT_NODE_ANNOUNCEMENT) # Checking for GPS if self.gps_receiver is not None and self.db_client is not None: # Get our GPS coords logger.debug("Getting GPS coordinates") gps_info = self.gps_receiver.get_coordinates() # If we got GPS data, report it. if gps_info is None: logger.error("Could not receive GPS info.") elif gps_info != gps_prev: logger.debug("Sending GPS info: %s" % gps_info) db_obj = CatanDatabaseNodeObject() db_obj.node_info.gps_latitude = gps_info['latitude'] db_obj.node_info.gps_longitude = gps_info['longitude'] db_obj.node_info.gps_altitude = gps_info['altitude'] db_obj.node_info.gps_sat_count = gps_info[ 'satillite_count'] logger.debug("Sending GPS info to databse.") self.db_client.send(G.MESSAGE_TYPE.DB_GPS, ` db_obj `) logger.debug("Sending GPS info to other nodes.") tx_client.send(G.MESSAGE_TYPE.DB_GPS, ` db_obj `) # Update previous gps_prev = gps_info # Update our time using our GPS logger.debug("Getting time/date date from GPS.") gps_time = self.gps_receiver.get_time() if gps_time is not None: logger.debug("Updated time to: %s" % gps_time) utils.linux_set_time(gps_time.timetuple()) # No reason to beat this to death time.sleep(G.RT_NODE_ANNOUNCEMENT_DELAY)
class DbHandler(SocketServer.BaseRequestHandler): """ This class is our basic handler for all inputs """ tx_client = TxClient() def handle(self): """ This function handles any inputs to our tx socket """ sock = self.request logger.debug("Database handler got message, processing...") # Receive our header node_message = NodeMessage() header = sock.recv(len(node_message)) # Receive our data node_message._unpack(header) if node_message.length > 0: node_message.data = sock.recv(node_message.length) # Grab our mutex to ensure only 1 write at time. with db_mutex: if node_message.type == G.MESSAGE_TYPE.DB_GPS: update_fn = DatabaseServer.DB.update_gps elif node_message.type == G.MESSAGE_TYPE.DB_PERSON: update_fn = DatabaseServer.DB.update_person elif node_message.type == G.MESSAGE_TYPE.DB_SERVICE: update_fn = DatabaseServer.DB.update_service else: logger.error("Got unrecognized DB packet. (Type: %d)" % node_message.type) return False rtn_data = update_fn(node_message) if rtn_data is not False: logger.debug("Successfully updated db.") sock.sendall( ` rtn_data `) else: logger.debug("Db update failed.") sock.sendall("0")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="Enable debugging output.") parser.add_argument("-m", "--message", type=str, default="Test", help="Message to send as data") args = parser.parse_args() if args.debug: print "* DEBUG Enabled." logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig() data = args.message # Create a TxClient client = TxClient(G.TX_DEFAULT_SOCK) rtn = client.send(0, data, destination=args.remote_node_id) if rtn: print "Sent: ", print data else: print "Error Sending message"