def _set_tag(self, tag, value): assert isinstance(tag, unicode) assert tag in [u"store", u"ignore", u"drop"] assert isinstance(value, bool) if __debug__: dprint(tag, " -> ", value) if value: if tag in self._tags: # the tag is already set return False self._tags.append(tag) else: if not tag in self._tags: # the tag isn't there to begin with return False self._tags.remove(tag) # todo: at some point we may want to optimize this. for now this is a feature that will # probably not be used often hence we leave it like this. with DispersyDatabase.get_instance() as database: tags = list(database.execute(u"SELECT key, value FROM tag")) int_tags = [0, 0] + [key for key, value in tags if value in self._tags] reduced = reduce(lambda a, b: a | b, int_tags) database.execute(u"UPDATE user SET tags = ? WHERE public_key = ?", (reduced, buffer(self._public_key),)) return True
def give_messages(self, messages, verbose=False): assert isinstance(messages, list) assert isinstance(verbose, bool) map(self.encode_message, messages) if verbose: dprint("giving ", len(messages), " messages (", sum(len(message.packet) for message in messages), " bytes)") self.give_packets([message.packet for message in messages], verbose=verbose) return messages
def give_message(self, message, verbose=False): assert isinstance(message, Message.Implementation) assert isinstance(verbose, bool) self.encode_message(message) if verbose: dprint("giving ", message.name, " (", len(message.packet), " bytes)") self.give_packet(message.packet, verbose=verbose) return message
def give_packets(self, packets, verbose=False): assert isinstance(packets, list) assert isinstance(verbose, bool) if verbose: dprint("giving ", sum(len(packet) for packet in packets), " bytes") address = self.socket.getsockname() self._community.dispersy.on_incoming_packets([(address, packet) for packet in packets]) return packets
def define(value, name, encode, decode): try: meta = community.get_meta_message(name) except KeyError: if __debug__: dprint("unable to define non-available message ", name, level="warning") else: self.define_meta_message(chr(value), meta, encode, decode)
def __init__(self, pattern, callback, packets): """ Receiving a message matching PATTERN triggers a call to the on_incoming_packet method with PACKETS. PATTERN is a python regular expression string. CALLBACK is called when PATTERN matches the incoming message footprint. The only argument is PACKETS. PACKETS is a list containing (Candidate, packet) tuples. These packets are effectively delayed until a message matching PATTERN was received. When a timeout is received this Trigger is removed and PACKETS are lost. """ if __debug__: assert isinstance(pattern, str) assert hasattr(callback, "__call__") assert isinstance(packets, list) assert len(packets) > 0 for packet in packets: assert isinstance(packet, tuple) assert len(packet) == 2 assert isinstance(packet[0], Candidate) assert isinstance(packet[1], str) if __debug__: dprint("create new trigger for ", len(packets), " packets") dprint("pattern: ", pattern) self._pattern = pattern self._search = expression_compile(pattern).search self._callback = callback self._packets = packets
def send(self, candidates, packets): if __debug__: dprint("Thrown away ", sum(len(data) for data in packets), " bytes worth of outgoing data to ", ",".join(str(candidate) for candidate in candidates), level="warning")
def __init__(self, pattern, response_func, response_args, max_responses): """ Receiving a message matching PATTERN triggers a call to RESPONSE_FUNC. PATTERN is a python regular expression string. RESPONSE_FUNC is called when PATTERN matches the incoming message footprint. The first argument is the incoming message, following this are optional values from RESPONSE_ARGS. RESPONSE_ARGS is an optional tuple containing arguments passed to RESPONSE_ARGS. MAX_RESPONSES is a number. Once MAX_RESPONSES messages are received no further calls are made to RESPONSE_FUNC. When a timeout is received and MAX_RESPONSES has not yet been reached, RESPONSE_FUNC is immediately called. The first argument will be ('', -1), the second will be None, following this are the optional values from RESPONSE_FUNC. """ assert isinstance(pattern, str) assert hasattr(response_func, "__call__") assert isinstance(response_args, tuple) assert isinstance(max_responses, int) assert max_responses > 0 if __debug__: self._debug_pattern = pattern dprint("create new trigger for one callback") dprint("pattern: ", self._debug_pattern) self._search = expression_compile(pattern).search self._response_func = response_func self._response_args = response_args self._responses_remaining = max_responses
def __init__(self, pattern, callback, messages): """ Receiving a message matching PATTERN triggers a call to the on_incoming_message message with ADDRESS and MESSAGE. PATTERN is a python regular expression string. CALLBACK is called when PATTERN matches the incoming message footprint. As an argument it receives MESSAGES. MESSAGES is a list with Message.Implementation instances. These messages are effectively delayed until a message matching PATTERN is received. When a timeout is received this Trigger is removed MESSAGES are lost. """ if __debug__: from message import Message assert isinstance(pattern, str) assert hasattr(callback, "__call__") assert isinstance(messages, list) assert len(messages) > 0 assert not filter(lambda x: not isinstance(x, Message.Implementation), messages) if __debug__: dprint("create new trigger for ", len(messages), " messages") dprint("pattern: ", pattern) self._pattern = pattern self._search = expression_compile(pattern).search self._callback = callback self._messages = messages
def action(self, vehicule): if self.__stock: self.__stock -= 1 vehicule.reservoir += self.impact_sante else: #self._ecosysteme.cases_besoin_maj.append(self.coords) dprint("une case de carburant vidée par " + str(vehicule))
def give_packet(self, packet, verbose=False, cache=False): assert isinstance(packet, str) assert isinstance(verbose, bool) assert isinstance(cache, bool) if verbose: dprint("giving ", len(packet), " bytes") self._dispersy.on_socket_endpoint([(self.lan_address, packet)], cache=cache, timestamp=time()) return packet
def __new__(cls, public_key, private_key="", sync_with_database=True, public_key_available=True): """ Some member instances may be indexed using the sha1 digest instead of the public key. we must check if this new instance replaces a previously made instance. """ assert isinstance(public_key, str) assert isinstance(private_key, str) assert isinstance(sync_with_database, bool) assert isinstance(public_key_available, bool) assert (public_key_available and len(public_key) > 0 and not public_key.startswith("-----BEGIN")) or \ (not public_key_available and len(public_key) == 20), (len(public_key), public_key_available, public_key.encode("HEX")) assert (public_key_available and len(private_key) > 0 and not private_key.startswith("-----BEGIN")) or len(private_key) == 0 if public_key_available: # determine if there already was a member with this mid (given the known public key) mid = sha1(public_key).digest() member = cls.has_instance(mid) if member: # TODO we might want to remove the old singleton link (indexed by mid), however, we # need to force the issue since it is currently not allowed as there are still # references to it if __debug__: dprint("singleton fix!", force=1) return member else: # determine if there already was a member with this public key (given the known mid) for member in cls.get_instances(): # note that public_key, in this case, contains the mid if member.mid == public_key: if __debug__: dprint("singleton fix", force=1) return member return super(Member, cls).__new__(cls)#, public_key, private_key, sync_with_database, public_key_available)
def give_packets(self, packets, verbose=False, cache=False): assert isinstance(packets, list) assert isinstance(verbose, bool) assert isinstance(cache, bool) if verbose: dprint("giving ", sum(len(packet) for packet in packets), " bytes") self._dispersy.on_socket_endpoint([(self.lan_address, packet) for packet in packets], cache=cache, timestamp=time()) return packets
def send_packet(self, packet, address, verbose=False): assert isinstance(packet, str) assert isinstance(address, tuple) assert isinstance(verbose, bool) if verbose: dprint(len(packet), " bytes to ", address[0], ":", address[1]) self._socket.sendto(packet, address) return packet
def undo_text(self, descriptors): """ Received an undo for a text message. """ for member, global_time, packet in descriptors: message = packet.load_message() dprint("undo \"", message.payload.text, "\" @", global_time)
def on_text(self, messages): """ Received a text message. """ for message in messages: if not "Dprint=False" in message.payload.text: dprint(message, " \"", message.payload.text, "\" @", message.distribution.global_time)
def __init__(self, sock_addr, tunnel, lan_address, wan_address, connection_type): assert is_address(sock_addr), sock_addr assert isinstance(tunnel, bool), type(tunnel) assert is_address(lan_address) assert is_address(wan_address) assert isinstance(connection_type, unicode) and connection_type in ( u"unknown", u"public", u"symmetric-NAT") super(WalkCandidate, self).__init__(sock_addr, tunnel) self._lan_address = lan_address self._wan_address = wan_address self._connection_type = connection_type self._associations = set() self._timestamps = dict() self._global_times = dict() if __debug__: if not (self.sock_addr == self._lan_address or self.sock_addr == self._wan_address): dprint("Either LAN ", self._lan_address, " or the WAN ", self._wan_address, " should be SOCK_ADDR ", self.sock_addr, level="error", stack=True) assert False
def get_resolution_policy(self, message, global_time): """ Returns the resolution policy and associated proof that is used for MESSAGE at time GLOBAL_TIME. """ if __debug__: from message import Message assert isinstance(message, Message) assert isinstance(global_time, (int, long)) key = u"resolution^" + message.name for policy_time, policies in reversed(self._policies): if policy_time < global_time and key in policies: if __debug__: dprint( "using ", policies[key][0].__class__.__name__, " for time ", global_time, " (configured at ", policy_time, ")", ) return policies[key] if __debug__: dprint("using ", message.resolution.default.__class__.__name__, " for time ", global_time, " (default)") return message.resolution.default, []
def on_text(self, messages): """ Received a text message. """ for message in messages: if not "Dprint=False" in message.payload.text: dprint(message, " \"", message.payload.text, "\"")
def update(self, tunnel, lan_address, wan_address, connection_type): assert isinstance(tunnel, bool) assert is_address(lan_address), lan_address assert is_address(wan_address), wan_address assert isinstance(connection_type, unicode), type(connection_type) assert connection_type in (u"unknown", u"public", "symmetric-NAT"), connection_type self._tunnel = tunnel self._lan_address = lan_address self._wan_address = wan_address # someone can also reset from a known connection_type to unknown (i.e. it now believes it is # no longer public nor symmetric NAT) self._connection_type = u"public" if connection_type == u"unknown" and lan_address == wan_address else connection_type if __debug__: if not (self.sock_addr == self._lan_address or self.sock_addr == self._wan_address): dprint("Either LAN ", self._lan_address, " or the WAN ", self._wan_address, " should be SOCK_ADDR ", self.sock_addr, level="error", stack=True)
def __replay(self, ctx, chunksize=8): dtag = '[ STEP %03d ] %s' % (self.id, self.type) nqueries = len(self.queries) if len(self.args) > 0 and self.args[0].isdigit(): nqueries = int(self.args.pop(0)) destination = ctx.client[ctx.client.keys()[0]] if 'VERBOSE' in os.environ: dprint( dtag, 'replaying %d queries to %s@%d (%s)' % (nqueries, destination[0], destination[1], ' '.join( self.args))) if 'INTENSIFY' in os.environ: nqueries *= int(os.environ['INTENSIFY']) tstart = datetime.now() nsent, nrcvd = replay_rrs(self.queries, nqueries, destination, self.args) # Keep/print the statistics rtt = (datetime.now() - tstart).total_seconds() * 1000 pps = 1000 * nrcvd / rtt if 'VERBOSE' in os.environ: dprint( dtag, 'sent: %d, received: %d (%d ms, %d p/s)' % (nsent, nrcvd, rtt, pps)) tag = None for arg in self.args: if arg.upper().startswith('PRINT'): _, tag = tuple(arg.split('=')) if '=' in arg else (None, 'replay') if tag: print( ' [ REPLAY ] test: %s pps: %5d time: %4d sent: %5d received: %5d' % (tag.ljust(11), pps, rtt, nsent, nrcvd))
def allow_triple_signed_text(self, message): """ Received a request to sign MESSAGE. """ dprint(message) assert message.payload.text in ("Allow=True", "Allow=False") return message.payload.text == "Allow=True"
def __init__(self, sock_addr, tunnel, lan_address, wan_address, connection_type): assert is_address(sock_addr), sock_addr assert isinstance(tunnel, bool), type(tunnel) assert is_address(lan_address) assert is_address(wan_address) assert isinstance(connection_type, unicode) and connection_type in (u"unknown", u"public", u"symmetric-NAT") super(WalkCandidate, self).__init__(sock_addr, tunnel) self._lan_address = lan_address self._wan_address = wan_address self._connection_type = connection_type self._associations = set() self._timestamps = dict() self._global_times = dict() if __debug__: if not (self.sock_addr == self._lan_address or self.sock_addr == self._wan_address): dprint( "Either LAN ", self._lan_address, " or the WAN ", self._wan_address, " should be SOCK_ADDR ", self.sock_addr, level="error", stack=True, ) assert False
def update(self, tunnel, lan_address, wan_address, connection_type): assert isinstance(tunnel, bool) assert is_address(lan_address), lan_address assert is_address(wan_address), wan_address assert isinstance(connection_type, unicode), type(connection_type) assert connection_type in (u"unknown", u"public", "symmetric-NAT"), connection_type self._tunnel = tunnel self._lan_address = lan_address self._wan_address = wan_address # someone can also reset from a known connection_type to unknown (i.e. it now believes it is # no longer public nor symmetric NAT) self._connection_type = ( u"public" if connection_type == u"unknown" and lan_address == wan_address else connection_type ) if __debug__: if not (self.sock_addr == self._lan_address or self.sock_addr == self._wan_address): dprint( "Either LAN ", self._lan_address, " or the WAN ", self._wan_address, " should be SOCK_ADDR ", self.sock_addr, level="error", stack=True, )
def persistent_register(self, id_, call, args=(), kargs=None, delay=0.0, priority=0): """ Register a callback only if ID_ has not already been registered. Example: > callback.persistent_register("my-id", my_func, ("first",), delay=60.0) > callback.persistent_register("my-id", my_func, ("second",)) > -> my_func("first") will be called after 60 seconds, my_func("second") will not be called at all Example: > callback.register("my-id", my_func, ("first",), delay=60.0) > callback.persistent_register("my-id", my_func, ("second",)) > -> my_func("first") will be called after 60 seconds, my_func("second") will not be called at all """ assert isinstance(id_, str), "ID_ has invalid type: %s" % type(id_) assert id_, "ID_ may not be an empty string" assert hasattr(call, "__call__"), "CALL must be callable" assert isinstance(args, tuple), "ARGS has invalid type: %s" % type(args) assert kargs is None or isinstance(kargs, dict), "KARGS has invalid type: %s" % type(kargs) assert isinstance(delay, float), "DELAY has invalid type: %s" % type(delay) assert isinstance(priority, int), "PRIORITY has invalid type: %s" % type(priority) if __debug__: dprint("reregister ", call, " after ", delay, " seconds") if kargs is None: kargs = {} with self._lock: self._new_actions.append(("persistent-register", (self._timestamp + delay, 512 - priority, id_, (call, args, kargs)))) # wakeup if sleeping self._event.set() return id_
def replace_register(self, id_, call, args=(), kargs=None, delay=0.0, priority=0, callback=None, callback_args=(), callback_kargs=None): """ Replace (if present) the currently registered call ID_ with CALL. This is a faster way to handle an unregister and register call. All parameters behave as in register(...). """ assert isinstance(id_, (basestring, int)), "ID_ has invalid type: %s" % type(id_) assert id_, "ID_ may not be zero or an empty (unicode)string" assert hasattr(call, "__call__"), "CALL must be callable" assert isinstance(args, tuple), "ARGS has invalid type: %s" % type(args) assert kargs is None or isinstance(kargs, dict), "KARGS has invalid type: %s" % type(kargs) assert isinstance(delay, float), "DELAY has invalid type: %s" % type(delay) assert isinstance(priority, int), "PRIORITY has invalid type: %s" % type(priority) assert callback is None or callable(callback), "CALLBACK must be None or callable" assert isinstance(callback_args, tuple), "CALLBACK_ARGS has invalid type: %s" % type(callback_args) assert callback_kargs is None or isinstance(callback_kargs, dict), "CALLBACK_KARGS has invalid type: %s" % type(callback_kargs) if __debug__: dprint("replace register ", call, " after ", delay, " seconds") with self._lock: self._new_actions.append(("unregister", id_)) self._new_actions.append(("register", (delay + time(), -priority, id_, (call, args, {} if kargs is None else kargs), None if callback is None else (callback, callback_args, {} if callback_kargs is None else callback_kargs)))) # wakeup if sleeping self._event.set() return id_
def periodically_cleanup_member_instances(cls): """ Must be called periodically to remove unused Member instances. The cleanup will increase the unreferenced counter of each instance that is unreferenced. The unreferenced counter is set to zero whenever it is referenced. Because reference counting is very expensive, we only count the references of a sample. This will ensure that each call will only block for a reasonable time while still allowing eventual member cleanup. """ while True: # this is very expensive. only count references every 10 minutes yield 10 * 60.0 for references, member in cls.sample_reference_instances(10): if references: member._unreferenced = 0 else: member._unreferenced += 1 # when a member has been seen unreferenced 3 times we will remove it. since we # perform the check once every 10 minutes, an unused member can be removed after # 30 minutes IF it is selected in the sample every time. if member._unreferenced > 3: cls.del_instance(member._public_key) cls.del_instance(member._mid) if __debug__: dprint("cleanup for member ", member.database_id, " (", member.mid.encode("HEX"), ")")
def __init__(self, file_path): """ Initialize a new Database instance. @param file_path: the path to the database file. @type file_path: unicode """ if __debug__: assert isinstance(file_path, unicode) dprint(file_path) self._debug_thread_ident = thread.get_ident() self._debug_file_path = file_path self._connection = sqlite3.Connection(file_path) # self._connection.setrollbackhook(self._on_rollback) self._cursor = self._connection.cursor() # # PRAGMA synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL; # # Query or change the setting of the "synchronous" flag. The first (query) form will return # the synchronous setting as an integer. When synchronous is FULL (2), the SQLite database # engine will use the xSync method of the VFS to ensure that all content is safely written # to the disk surface prior to continuing. This ensures that an operating system crash or # power failure will not corrupt the database. FULL synchronous is very safe, but it is # also slower. When synchronous is NORMAL (1), the SQLite database engine will still sync # at the most critical moments, but less often than in FULL mode. There is a very small # (though non-zero) chance that a power failure at just the wrong time could corrupt the # database in NORMAL mode. But in practice, you are more likely to suffer a catastrophic # disk failure or some other unrecoverable hardware fault. With synchronous OFF (0), SQLite # continues without syncing as soon as it has handed data off to the operating system. If # the application running SQLite crashes, the data will be safe, but the database might # become corrupted if the operating system crashes or the computer loses power before that # data has been written to the disk surface. On the other hand, some operations are as much # as 50 or more times faster with synchronous OFF. # # The default setting is synchronous = FULL. # if __debug__: dprint("PRAGMA synchronous = NORMAL") self._cursor.execute(u"PRAGMA synchronous = NORMAL") # check is the database contains an 'option' table try: count, = self.execute(u"SELECT COUNT(1) FROM sqlite_master WHERE type = 'table' AND name = 'option'").next() except StopIteration: raise RuntimeError() if count: # get version from required 'option' table try: version, = self.execute(u"SELECT value FROM option WHERE key == 'database_version' LIMIT 1").next() except StopIteration: # the 'database_version' key was not found version = u"0" else: # the 'option' table probably hasn't been created yet version = u"0" self.check_database(version)
def runtime_duration_warning_helper(*args, **kargs): start = time() try: return func(*args, **kargs) finally: end = time() if end - start >= threshold: dprint("%.2fs " % (end - start), func, level="warning")
def call2(): delay = 3.0 for i in range(10): dprint(time(), " ", i) sleep(delay) if delay > 0.0: delay -= 1.0 yield 1.0
def drop_packets(self): while True: try: packet, address = self._socket.recvfrom(10240) except: break dprint("droped ", len(packet), " bytes from ", address[0], ":", address[1])
def send_message(self, message, address, verbose=False): assert isinstance(message, Message.Implementation) assert isinstance(address, tuple) assert isinstance(verbose, bool) self.encode_message(message) if verbose: dprint(message.name, " (", len(message.packet), " bytes) to ", address[0], ":", address[1]) self.send_packet(message.packet, address) return message
def unregister(self, id_): """ Unregister a callback using the ID_ obtained from the register(...) method """ assert isinstance(id_, (str, int)), "ROOT_ID has invalid type: %s" % type(id_) if __debug__: dprint(id_) with self._lock: self._new_actions.append(("unregister", id_))
def on_timeout(self): if self._responses_remaining > 0: if __debug__: dprint("timeout on trigger with callback ", self._response_func, level="warning") dprint("pattern: ", self._debug_pattern, level="warning") self._responses_remaining = 0 # note: this callback may raise DelayMessage, etc self._response_func(None, *self._response_args)
def _on_timeout(self, identifier): assert identifier in self._identifiers cache = self._identifiers.get(identifier) if __debug__: dprint("timeout on ", identifier, " for ", cache) cache.on_timeout() if cache.cleanup_delay: self._callback.register(self._on_cleanup, (identifier,), id_="requestcache-%s" % identifier, delay=cache.cleanup_delay) else: del self._identifiers[identifier]
def unregister(self, id_): """ Unregister a callback using the ID_ obtained from the register(...) method """ assert isinstance(id_, (basestring, int)), "ROOT_ID has invalid type: %s" % type(id_) assert id_, "ID_ may not be zero or an empty (unicode)string" if __debug__: dprint(id_) with self._lock: self._new_actions.append(("unregister", id_))
def set(self, identifier, cache): assert isinstance(identifier, (int, long, str)), type(identifier) assert isinstance(cache, Cache) assert isinstance(cache.timeout_delay, float) assert cache.timeout_delay > 0.0 if __debug__: dprint("set ", identifier, " for ", cache, " (", cache.timeout_delay, "s timeout)") self._callback.register(self._on_timeout, (identifier,), id_="requestcache-%s" % identifier, delay=cache.timeout_delay) self._identifiers[identifier] = cache
def claim(self, cache): while True: identifier = int(random() * 2**16) if not identifier in self._identifiers: if __debug__: dprint("claiming on ", identifier, " for ", cache) break self.set(identifier, cache) return identifier
def _init_n_f(self, f_error_rate, n_capacity, prefix=""): assert isinstance(f_error_rate, float) assert 0 < f_error_rate < 1 assert isinstance(n_capacity, int) assert 0 < n_capacity m_size = abs((n_capacity * log(f_error_rate)) / (log(2) ** 2)) m_size = int(ceil(m_size / 8.0) * 8) if __debug__: dprint("constructing bloom filter based on f_error_rate ", f_error_rate, " and ", n_capacity, " capacity") self._init_(m_size, self._get_k_functions(m_size, n_capacity), prefix, 0L)
def _call_exception_handlers(self, exception, fatal): with self._lock: exception_handlers = self._exception_handlers[:] for exception_handler in exception_handlers: try: exception_handler(exception, fatal) except Exception: dprint(exception=True, level="error") assert False, "the exception handler should not cause an exception"
def acceleration(self,nvaccelartion): """ mis à jour de l'accélération en évitant """ debug.dprint(" nv acc {} ".format(nvaccelartion)) a = nvaccelartion a = min(a,self._accelerationmax) a = max(a,0) self.__acceleration=a
def loop(self): """ Use the calling thread for this Callback instance. """ if __debug__: dprint() with self._lock: self._state = "STATE_PLEASE_RUN" if __debug__: dprint("STATE_PLEASE_RUN") self._loop()
def filter_non_bootstrap_nodes(): for candidate, packet in packets: cid = packet[2:22] if not cid in self._communities and candidate.sock_addr[0] in self._non_autoload: if __debug__: dprint("drop a ", len(packet), " byte packet (received from non-autoload node) from ", candidate, level="warning", force=1) self._statistics.drop("_convert_packets_into_batch:from bootstrap node for unloaded community", len(packet)) continue yield candidate, packet
def _init_m_f(self, m_size, f_error_rate, prefix=""): assert isinstance(m_size, int) assert 0 < m_size assert m_size % 8 == 0, "size must be a multiple of eight (%d)" % m_size assert isinstance(f_error_rate, float) assert 0 < f_error_rate < 1 # calculate others # self._n = int(m * ((log(2) ** 2) / abs(log(f)))) # self._k = int(ceil(log(2) * (m / self._n))) if __debug__: dprint("constructing bloom filter based on m_size ", m_size, " bits and f_error_rate ", f_error_rate) self._init_(m_size, self._get_k_functions(m_size, self._get_n_capacity(m_size, f_error_rate)), prefix, 0L)
def __enter__(self): """ Enters a no-commit state. The commit will be performed by __exit__. @return: The method self.execute """ assert self._debug_thread_ident == thread.get_ident() if __debug__: dprint("disabling Database.commit()") self._pending_commits = max(1, self._pending_commits) return self
def call3(): delay = 3.0 for i in range(10): dprint(time(), " ", i) yield Switch(d) # perform code on Callback d sleep(delay) if delay > 0.0: delay -= 1.0 yield Switch(c)
def __init__(self): # _event is used to wakeup the thread when new actions arrive self._event = Event() self._event_set = self._event.set self._event_is_set = self._event.isSet # _lock is used to protect variables that are written to on multiple threads self._lock = Lock() # _thread_ident is used to detect when methods are called from the same thread self._thread_ident = 0 # _state contains the current state of the thread. it is protected by _lock and follows the # following states: # # --> fatal-exception -> STATE_EXCEPTION # / # STATE_INIT -> start() -> PLEASE_RUN -> STATE_RUNNING # \ \ # --------------> stop() -> PLEASE_STOP -> STATE_FINISHED # self._state = "STATE_INIT" if __debug__: dprint("STATE_INIT") # _exception is set to SystemExit, KeyboardInterrupt, GeneratorExit, or AssertionError when # any of the registered callbacks raises any of these exceptions. in this case _state will # be set to STATE_EXCEPTION. it is protected by _lock self._exception = None # _exception_handlers contains a list with callable functions of methods. all handlers are # called whenever an exception occurs. first parameter is the exception, second parameter # is a boolean indicating if the exception is fatal (i.e. True indicates SystemExit, # KeyboardInterrupt, GeneratorExit, or AssertionError) self._exception_handlers = [] # _id contains a running counter to ensure that every scheduled callback has its own unique # identifier. it is protected by _lock self._id = 0 # requests are ordered by deadline and moved to -expired- when they need to be handled # (deadline, priority, root_id, (call, args, kargs), callback) self._requests = [] # expired requests are ordered and handled by priority # (priority, root_id, None, (call, args, kargs), callback) self._expired = [] if __debug__: def must_close(callback): assert callback.is_finished atexit_register(must_close, self)
def __check_answer(self, ctx): """ Compare answer from previously resolved query. """ if len(self.data) == 0: raise Exception("response definition required") expected = self.data[0] if expected.is_raw_data_entry is True: dprint("", ctx.last_raw_answer.to_text()) expected.cmp_raw(ctx.last_raw_answer) else: if ctx.last_answer is None: raise Exception("no answer from preceding query") dprint("", ctx.last_answer.to_text()) expected.match(ctx.last_answer)
def handle_query(self, client): """ Handle incoming queries. """ client_address = client.getsockname()[0] query, addr = recvfrom_msg(client) if query is None: return False dprint("[ handle_query ]", "%s incoming query from %s\n%s" % (client_address, addr, query)) response = dns.message.make_response(query) is_raw_data = False if self.scenario is not None: response, is_raw_data = self.scenario.reply(query, client_address) if response: if is_raw_data is False: data_to_wire = response.to_wire(max_size=65535) dprint("[ handle_query ]", "response\n%s" % response) else: data_to_wire = response dprint("[ handle_query ]", "raw response found") else: response = dns.message.make_response(query) response.set_rcode(dns.rcode.SERVFAIL) data_to_wire = response.to_wire() dprint("[ handle_query ]", "response failed, SERVFAIL") sendto_msg(client, data_to_wire, addr) return True
def helper(*args, **kargs): filename = "profile-%s-%d.out" % (current_thread().name, get_ident()) if filename in profiled_threads: raise RuntimeError( "Can not attach profiler on the same thread twice") dprint("running with profiler [", filename, "]", level="warning") profiled_threads.add(filename) profiler = Profile() try: return profiler.runcall(func, *args, **kargs) finally: dprint("profiler results [", filename, "]", level="warning") profiler.dump_stats(filename)
def play(self, family, paddr): """ Play given scenario. """ # Store test subject => address mapping self.client = paddr step = None i = 0 while i < len(self.steps): step = self.steps[i] self.current_step = step try: step.play(self) except Exception as e: if (step.repeat_if_fail > 0): dprint( '[play]', "step %d: exception catched - '%s', retrying step %d (%d left)" % (step.id, e, step.next_if_fail, step.repeat_if_fail)) step.repeat_if_fail -= 1 if (step.pause_if_fail > 0): time.sleep(step.pause_if_fail) if (step.next_if_fail != -1): next_steps = [ j for j in range(len(self.steps)) if self.steps[j].id == step.next_if_fail ] if (len(next_steps) == 0): raise Exception('step %d: wrong NEXT value "%d"' % (step.id, step.next_if_fail)) next_step = next_steps[0] if (next_step < len(self.steps)): i = next_step else: raise Exception('step %d: Can' 't branch to NEXT value "%d"' % (step.id, step.next_if_fail)) continue else: raise Exception('%s step %d %s' % (self.file, step.id, str(e))) i = i + 1 for r in self.ranges: for e in r.stored: if e.mandatory is True and e.fired == 0: raise Exception( 'Mandatory section at line %d is not fired' % e.lineno)
def mouvCarburant(self): """ dirigé le véhicule vers la station de carburant la plus proche """ debug.dprint(" id {} je cherche le car".format(self.id)) liste=self._circuit.vue(self.x,self.y,10) listeSuppr=[] couche_terrain = self._circuit.Couche_terrain couche_vehicule= self._circuit.Couche_vehicules for case in liste: if self._circuit.plateau[case[1],case[0],couche_vehicule]!=None or self._circuit.plateau[case[1],case[0],couche_terrain].getCaractere()!='C': listeSuppr.append(case)
def __init__(self, rawserver, dispersy, port, ip="0.0.0.0"): super(RawserverEndpoint, self).__init__() while True: try: self._socket = rawserver.create_udpsocket(port, ip) if __debug__: dprint("Dispersy listening at ", port, force=True) except socket.error: port += 1 continue break self._rawserver = rawserver self._rawserver.start_listening_udp(self._socket, self) self._dispersy = dispersy self._sendqueue_lock = threading.Lock() self._sendqueue = []
def unregister(self, id_): """ Unregister a callback using the ID_ obtained from the register(...) method """ assert isinstance( id_, (basestring, int)), "ROOT_ID has invalid type: %s" % type(id_) assert id_, "ID_ may not be zero or an empty (unicode)string" if __debug__: dprint(id_) with self._lock: # un-register for index, tup in enumerate(self._requests): if tup[2] == id_: self._requests[index] = (tup[0], tup[1], tup[2], None, None) for index, tup in enumerate(self._expired): if tup[1] == id_: self._expired[index] = (tup[0], tup[1], tup[2], None, None)
def pop(self, identifier, cls): assert isinstance(identifier, (int, long, str)), type(identifier) assert issubclass(cls, Cache), cls cache = self._identifiers.get(identifier) if cache and isinstance(cache, cls): assert isinstance(cache.cleanup_delay, float) assert cache.cleanup_delay >= 0.0 if __debug__: dprint("canceling timeout on ", identifier, " for ", cache) if cache.cleanup_delay: self._callback.replace_register("requestcache-%s" % identifier, self._on_cleanup, (identifier,), delay=cache.cleanup_delay) else: self._callback.unregister("requestcache-%s" % identifier) del self._identifiers[identifier] return cache