Exemple #1
0
def make_test_items(handler):

    r = Clock()
    t = StringTransport()
    p = MQTTServerTwistedProtocol(handler, r)
    cp = MQTTClientParser()

    p.makeConnection(t)

    return r, t, p, cp
Exemple #2
0
 def __init__(self, factory):
     self.factory = factory
     self._record = deque(self.factory.record)
     self._waiting_for_nothing = None
     self._client = MQTTClientParser()
Exemple #3
0
class ReplayProtocol(Protocol):

    def __init__(self, factory):
        self.factory = factory
        self._record = deque(self.factory.record)
        self._waiting_for_nothing = None
        self._client = MQTTClientParser()

    def connectionMade(self):

        if self._record[0].send:
            to_send = self._record.popleft()
            if isinstance(to_send.data, bytes):
                self.transport.write(to_send.data)
            else:
                self.transport.write(to_send.data.serialise())

    def dataReceived(self, data):
        self.factory._timer.reset(7)

        got_data = self._client.data_received(data)
        self.factory.client_transcript.extend(got_data)

        if self._waiting_for_nothing:
            if data == b"":
                got_data.append(b"")
                self._waiting_for_nothing = None
            else:
                self.factory.reason = "Got unexpected data " + repr(got_data)
                self.factory.success = False
                self.factory.reactor.stop()
                return

        if len(self._record) > 0 and got_data:
            for x in got_data:
                reading = self._record.popleft()

                if x == reading.data:
                    pass
                elif isinstance(reading.data, list) and x in reading.data:
                    reading.data.remove(x)
                else:
                    self.factory.success = False
                    self.factory.reason = (x, reading.data)
                    self.factory.reactor.stop()
                    return

                if len(self._record) > 0:
                    while len(self._record) > 0 and self._record[0].send:
                        to_send = self._record.popleft()
                        if isinstance(to_send.data, bytes):
                            self.transport.write(to_send.data)
                        else:
                            self.transport.write(to_send.data.serialise())

                if isinstance(reading.data, list):
                    if reading.data:
                        self._record.appendleft(reading)

                if len(self._record) > 0:

                    # Then if we are supposed to wait...
                    if isinstance(self._record[0], Frame) and self._record[0].send is False and self._record[0].data == b"":
                        def wait():
                            self.dataReceived(b"")
                        self._waiting_for_nothing = self.factory.reactor.callLater(2, wait)
                        return

    def connectionLost(self, reason):
        if self.factory.reactor.running:
            if self._record and isinstance(self._record[0], ConnectionLoss):
                self.factory.success = True
            else:
                self.factory.success = False
                self.factory.reason = "Premature disconnection"
            self.factory.reactor.stop()
Exemple #4
0
 def __init__(self, factory):
     self.factory = factory
     self._record = deque(self.factory.record)
     self._waiting_for_nothing = None
     self._client = MQTTClientParser()
Exemple #5
0
class ReplayProtocol(Protocol):
    def __init__(self, factory):
        self.factory = factory
        self._record = deque(self.factory.record)
        self._waiting_for_nothing = None
        self._client = MQTTClientParser()

    def connectionMade(self):

        if self._record[0].send:
            to_send = self._record.popleft()
            if isinstance(to_send.data, bytes):
                self.transport.write(to_send.data)
            else:
                self.transport.write(to_send.data.serialise())

    def dataReceived(self, data):
        self.factory._timer.reset(7)

        got_data = self._client.data_received(data)
        self.factory.client_transcript.extend(got_data)

        if self._waiting_for_nothing:
            if data == b"":
                got_data.append(b"")
                self._waiting_for_nothing = None
            else:
                self.factory.reason = "Got unexpected data " + repr(got_data)
                self.factory.success = False
                self.factory.reactor.stop()
                return

        if len(self._record) > 0 and got_data:
            for x in got_data:
                reading = self._record.popleft()

                if x == reading.data:
                    pass
                elif isinstance(reading.data, list) and x in reading.data:
                    reading.data.remove(x)
                else:
                    self.factory.success = False
                    self.factory.reason = (x, reading.data)
                    self.factory.reactor.stop()
                    return

                if len(self._record) > 0:
                    while len(self._record) > 0 and self._record[0].send:
                        to_send = self._record.popleft()
                        if isinstance(to_send.data, bytes):
                            self.transport.write(to_send.data)
                        else:
                            self.transport.write(to_send.data.serialise())

                if isinstance(reading.data, list):
                    if reading.data:
                        self._record.appendleft(reading)

                if len(self._record) > 0:

                    # Then if we are supposed to wait...
                    if isinstance(self._record[0], Frame) and self._record[
                            0].send is False and self._record[0].data == b"":

                        def wait():
                            self.dataReceived(b"")

                        self._waiting_for_nothing = self.factory.reactor.callLater(
                            2, wait)
                        return

    def connectionLost(self, reason):
        if self.factory.reactor.running:
            if self._record and isinstance(self._record[0], ConnectionLoss):
                self.factory.success = True
            else:
                self.factory.success = False
                self.factory.reason = "Premature disconnection"
            self.factory.reactor.stop()