def insert_local_contact(self, uuid, datetime, mycall, theircall, band, mode): from dbframe import framer "store a new contact and its insert frame" c = self.conn.cursor() uuid_idx = self._insert_uuid_if_needed(c, uuid) c.execute("SELECT MAX( client_rec ) FROM contacts WHERE client_uuid == ?", [uuid_idx]) row = c.fetchone() if row == None: client_rec = 0 elif row[0] == None: client_rec = 0 else: client_rec = row[0] + 1 c.execute( "INSERT INTO contacts VALUES(?,?,?,?,?,?,?)", (uuid_idx, client_rec, mycall, theircall, band, datetime, mode), ) f = framer() seq = self._get_current_client_seq(c, uuid) + 1 f.frame_upsert(uuid, seq, client_rec, datetime, mycall, theircall, band, mode) self._insert_frames(c, [f.pop_tail()]) self._set_current_client_seq(c, uuid, seq) c.close() self.conn.commit()
def __init__( _self ): """hook up database, framer, and bind UDP""" _self.db = db_manager() settings = settings_manager() _self.my_uuid = settings.get( "uuid" ) _self.my_last_seq = _self.db.get_seq_from_uuid( _self.my_uuid ) _self.handlers={ dbframe.framer.typeDbUpsert:_self.handle_frame_upsert, dbframe.framer.typeDbDelete:_self.handle_frame_delete, dbframe.framer.typeNetHello:_self.handle_frame_hello, dbframe.framer.typeNetReqClientList:_self.handle_frame_req_client_list, dbframe.framer.typeNetClientList:_self.handle_frame_client_list, dbframe.framer.typeNetReqClientUpdates:_self.handle_frame_req_client_updates, } _self.framer = dbframe.framer() #broadcast to everybody. #Should be able to programatically #compute local broadcast address #but also seems that most routers drop these #so this should work fine _self.UDP_IP = "255.255.255.255" _self.UDP_PORT = 32250 _self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) _self.sock.bind((_self.UDP_IP, _self.UDP_PORT)) if hasattr(socket,'SO_BROADCAST'): #add broadcast abilities _self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
def insert_local_contact(self, uuid, datetime, mycall, theircall): from dbframe import framer "store a new contact and its insert frame" c = self.conn.cursor() uuid_idx = self._insert_uuid_if_needed(c, uuid) c.execute( "SELECT MAX( client_rec ) FROM contacts WHERE client_uuid == ?", [uuid_idx]) row = c.fetchone() if row == None: client_rec = 0 elif row[0] == None: client_rec = 0 else: client_rec = row[0] + 1 c.execute("INSERT INTO contacts VALUES(?,?,?,?)", (uuid_idx, client_rec, mycall, theircall)) f = framer() seq = self._get_client_seq(c, uuid) + 1 f.frame_upsert(uuid, seq, client_rec, datetime, mycall, theircall) self._insert_frames(c, [f.pop_tail()]) self._set_client_seq(c, uuid, seq) c.close() self.conn.commit()
def send_periodic_packets(): global id s = settings_manager() uuid = s.get( "uuid" ) messages = dbframe.framer() messages.frame_upsert( uuid, id, 3, "Wednesday", "KD0LIX", "KD0IXY" ) id = id + 1 messages.frame_delete( uuid, id, 3 ) id = id + 1 packets = messages.pack( 1200 ) for p in packets: sock.sendto( p, (UDP_IP, UDP_PORT) )
def send_periodic_packets(): global id s = settings_manager() uuid = s.get("uuid") messages = dbframe.framer() messages.frame_upsert(uuid, id, 3, "Wednesday", "KD0LIX", "KD0IXY") id = id + 1 messages.frame_delete(uuid, id, 3) id = id + 1 packets = messages.pack(1200) for p in packets: sock.sendto(p, (UDP_IP, UDP_PORT))
def process_incoming_packets( sock ): "receive and handle new packets, for up to 1 second" timeout = 1.0 stop_time = time.time() + timeout while( timeout > 0.0 ): sock.settimeout(timeout) try: blob1, addr = sock.recvfrom(2048) print "received ", len( blob1 ), " byte message" frame = dbframe.framer() frames = frame.unpack( blob1 ) for f in frames: handle_frame( f ) except socket.timeout: break; timeout = stop_time - time.time()
def process_incoming_packets(sock): "receive and handle new packets, for up to 1 second" timeout = 1.0 stop_time = time.time() + timeout while (timeout > 0.0): sock.settimeout(timeout) try: blob1, addr = sock.recvfrom(2048) print "received ", len(blob1), " byte message" frame = dbframe.framer() frames = frame.unpack(blob1) for f in frames: handle_frame(f) except socket.timeout: break timeout = stop_time - time.time()
def process_incoming_packets( _self ): """receive and handle new packets, for 1-2 seconds""" timeout = 1 + random.uniform(0,1) stop_time = time.time() + timeout while( timeout > 0.0 ): _self.sock.settimeout(timeout) try: blob1, addr = _self.sock.recvfrom(2048) print "received ", len( blob1 ), " byte message from ", addr frame = dbframe.framer() frames = frame.unpack( blob1 ) for f in frames: _self.handle_frame( f ) except socket.timeout: break; timeout = stop_time - time.time() #restore socket back to blocking _self.sock.settimeout(None)