예제 #1
0
    def test_error(self):
        numbers = Subject()
        windows = Subject()

        expected_numbers = []
        expected_error = None
        actual_numbers = []
        actual_error = None

        def on_next(i):
            actual_numbers.append(i)

        def on_error(e):
            nonlocal actual_error
            actual_error = e

        numbers.pipe(ops.buffer(windows)).subscribe(on_next=on_next,
                                                    on_error=on_error)

        numbers.on_next(1)
        numbers.on_next(2)
        numbers.on_error(ValueError())

        self.assertIsInstance(actual_error, ValueError)
        self.assertEqual(expected_numbers, actual_numbers)
예제 #2
0
 def test_base_on_error(self):
     source = Subject()
     source.pipe(
         trace_observable(prefix='foo',
                          date=datetime.datetime(
                              year=2018, month=8,
                              day=3))).subscribe(on_error=lambda _: None)
     source.on_error('error')
     self.assertEqual(
         '2018-08-03 00:00:00:foo - on_subscribe\n'
         '2018-08-03 00:00:00:foo - on_error: error\n'
         '2018-08-03 00:00:00:foo - dispose',
         self.out.getvalue().strip())
class DeviceSniffer:
    def __init__(self, user_querier: UserQuerier, interface="mon0"):
        self.async_sniffer = AsyncSniffer(prn=self.handle_packet,
                                          store=False,
                                          iface=interface,
                                          monitor=True)
        self.device_dectect_stream = Subject()
        self.user_querier = user_querier

    def __del__(self):
        self.device_dectect_stream.on_completed()

    @staticmethod
    def is_probe_request(packet: Packet):
        return packet.type == 0 and packet.subtype == 4

    def handle_packet(self, packet: Packet):
        if not DeviceSniffer.is_probe_request(packet):
            return

        try:
            target_ssid = packet.getlayer(Dot11Elt).getfieldval("info").decode(
                "utf-8")
            if len(target_ssid) == 0:
                return

            source_mac_addr = packet.addr2.upper()
            userid = self.user_querier.get_userid(target_ssid, source_mac_addr)
            if userid is not None:
                self.device_dectect_stream.on_next(userid)

        except Exception as err:
            self.device_dectect_stream.on_error(err)

    def get_observable(self) -> Subject:
        return self.device_dectect_stream

    def start(self):
        self.async_sniffer.start()

    def stop(self):
        self.async_sniffer.stop()
예제 #4
0
파일: Inquiry.py 프로젝트: Tilo15/LibPeer2
class Inquiry:

    def __init__(self, target: InstanceReference, timeout = 10):
        self.id = uuid.uuid4().bytes
        self.target = target
        self.time = time.time()
        self.complete = Subject()

        self.timer = Timer(timeout, self.__timeout)
        self.timer.start()

    def response_received(self) -> float:
        self.timer.cancel()
        delay = time.time() - self.time

        self.complete.on_next(delay)
        self.complete.on_completed()

        return delay

    def __timeout(self):
        Log.warn("Inquiry timed out")
        self.complete.on_error(TimeoutError("The inquiry timed out."))
예제 #5
0
파일: Builder.py 프로젝트: Tilo15/Timavald
class TimeCertificateBuilder:
    def __init__(self,
                 trust_set: TrustSet,
                 trust_set_key: VerifyKey,
                 digest: bytes,
                 timeout: int = -1):
        # Save the paramaters
        self.trust_set = trust_set
        self.trust_set_key = trust_set_key
        self.timeout = timeout
        self.signatures: Set[TimeSignature] = set()
        self.result = Subject()
        self.digest = digest

        if (self.timeout == -1):
            self.timeout = trust_set.maximum_deviation

        self.__timer = None
        self.__complete = False

    def add_signature(self, signature: TimeSignature):
        # Do we have a timer?
        if (self.__timer == None):
            # No, create it
            self.__timer = Timer(self.timeout, self.__finalise)
            self.__timer.start()

        # Have we timed out?
        if (self.__complete):
            # Don't do anything
            return

        # Is the signature valid for this trust set?
        if (signature.public_key.encode()
                not in self.trust_set.valid_key_data):
            # Drop it
            return

        # Add to the signature set
        self.signatures.add(signature)

        # Have we got all the signatures?
        if (len(self.signatures) == len(self.trust_set.valid_keys)):
            # Yes, cancel timer and finalise
            self.__timer.cancel()
            self.__finalise()

    def __finalise(self):
        # We are complete
        self.__complete = True

        # Do we have enough signatures?
        if (len(self.signatures) < self.trust_set.required_signatures):
            # No, return error
            self.result.on_error(
                Exception(
                    "Could not get required number of signatures in time"))
            return

        # We have enough signatures for a certificate, build it
        certificate = TimeCertificate(self.trust_set, self.trust_set_key,
                                      self.signatures)

        # Verify the certificate
        try:
            certificate.validate(self.digest)
        except Exception as e:
            self.result.on_error(e)
            return

        # Send to the observer
        self.result.on_next(certificate)
        self.result.on_completed()