def __init__(self, user_name, application_name, service_name, service_location): assert(user_name != "") assert(application_name != "") assert(service_name != "") assert(service_location != "") # Each instance of this object creates it's own identity. # This means we generate an UUID here and it is used # to identify this object (and it's caller) uniquely. self.user_name = user_name self.application_name = application_name self.service_name = service_name self.service_location = service_location self.uuid = uuid.uuid4() self.udp = udplib.UDP() self.period = DiscoveryServer.period # Verify the location makes sense, syntactically location_check = location.check_location(service_location) assert(location_check is True) self.alive = True self.thread = threading.Thread(target=self.__thread_entry) self.thread.daemon = True self.thread.start()
def __process_beacon(self, beacon): pieces = beacon.split() if len(pieces) != 6: Llog.LogError("Invalid beacon received: " + beacon) return if pieces[0] != "BEACON": Llog.LogError("Invalid beacon header received: " + beacon) return Llog.LogDebug("Beacon: <" + beacon + ">") # # We have a valid beacon frame. # Process the services to see if there are any # new services contained that we do not have in # our discovered_services_list. # service_uuid, \ user_name, \ application_name, \ service_name, \ service_location = pieces[1:6] # Attempt to parse the UUID string to ensure it is well-formed try: r_uuid = uuid.UUID(service_uuid) except ValueError: # The UUID was most likely malformed in the beacon. # Just discard this beacon. Llog.LogError("Malformed UUID string: " + service_uuid) return # Lets make sure the location meets our expectations # for format location_check = location.check_location(service_location) if location_check is False: Llog.LogError("Malformed/invalid location!: " + service_location) return new_service = DiscoveryService(user_name, application_name, service_name, service_location, service_uuid) # Check to see if we already have this entry in our internal # list of services service = self.find_discovered_service(new_service) if service is None: # Service does not already exist in our list, add it. # Timestamp the discovered service so we can # remove them if they expire. self.discovered_service_list.append(new_service) # Callout to notify the user of this new service if self.service_add_cback is not None: self.service_add_cback(new_service) else: # The service exists. Update the timestamp # in the service entry to keep it from # expiring service.time = time.time()