예제 #1
0
 async def handle_pingreq(self, pingreq: PingReqPacket):
     await self._send_packet(PingRespPacket.build())
예제 #2
0
 def handle_pingreq(self, pingreq: PingReqPacket):
     yield from self._send_packet(PingRespPacket.build())
예제 #3
0
 def handle_pingreq(self, pingreq: PingReqPacket):
     yield from self.outgoing_queue.put(PingRespPacket.build())
예제 #4
0
 def handle_pingreq(self, pingreq: PingReqPacket):
     yield from self._send_packet(PingRespPacket.build())
예제 #5
0
    def client_connected(self, reader, writer):

        #handle connection and create session to add to the list of sessions
        #read connection packet from the client

        connect = yield from MQTTConnect.ConnectPacket.from_stream(reader)
        if (connect.payload.client_id is None):
            generate_id = True
        else:
            generate_id = False

        #send a connack packet to client depending on correct credentials
        connack = None
        if (connect.username == self._username
                and connect.password == self._password):
            connack = MQTTConnack.ConnackPacket.build(
                0, MQTTConnack.CONNECTION_ACCEPTED)
            connected = True
        else:
            connack = MQTTConnack.ConnackPacket.build(
                0, MQTTConnack.BAD_USERNAME_PASSWORD)
            connected = False

        writer.write(connack.to_bytes())

        if (not connected):
            return

        new_session = Session()
        if (generate_id):
            new_session.client_id = new_session.generate_client_id()
        else:
            new_session.client_id = connect.client_id
        new_session.remote_address = writer.get_extra_info('peername')
        new_session.remote_port = writer.get_extra_info('socket')
        new_session.username = connect.username
        new_session.password = connect.password
        new_session.keep_alive = connect.keep_alive
        new_session.writer = writer
        new_session.reader = reader

        self._sessions[new_session.client_id] = new_session
        print("New client; %s connected from %s with keep_alive: %s" %
              (new_session.client_id, new_session.remote_address,
               new_session.keep_alive))

        #received packets are checked and the corresponding method is used (publish, sub/unsub, disconnect)
        try:
            while connected:
                mqtt_header = yield from asyncio.wait_for(
                    MQTTFixedHeader.from_stream(reader),
                    new_session.keep_alive + 1,
                    loop=self._loop)

                cls = packet_class(fixed_header=mqtt_header)
                packet = yield from cls.from_stream(reader,
                                                    fixed_header=mqtt_header)

                if (packet.fixed_header.packet_type == SUBSCRIBE):
                    for (topic, qos) in packet.payload.topics:
                        if (qos != 0):
                            print("QoS is different than expected")
                            print(qos)
                        self.add_subscription(topic, new_session)
                elif (packet.fixed_header.packet_type == UNSUBSCRIBE):
                    for (topic, qos) in packet.payload.topics:
                        if (qos != 0):
                            print("QoS is different than expected")
                            print(qos)
                        self.del_subscription(topic, new_session)
                elif (packet.fixed_header.packet_type == PUBLISH):
                    topic = packet.variable_header.topic_name
                    message = packet.payload.data
                    print("Broadcast; Topic: %s, Payload: %s" %
                          (topic, message))
                    yield from self.broadcast_message(new_session, topic,
                                                      message)
                elif (packet.fixed_header.packet_type == DISCONNECT):
                    self.del_all_subscriptions(new_session)
                    self.del_session(new_session.client_id)
                    connected = False
                    print("Client: %s disconnected" % (new_session.client_id))
                elif (packet.fixed_header.packet_type == PINGREQ):
                    pingresp_packet = PingRespPacket.build()
                    writer.write(pingresp_packet.to_bytes())
                else:
                    print('Not yet supported packet received')
        except asyncio.TimeoutError:
            print("Client %s didn't respond; diconnecting" %
                  new_session.client_id)
            self.del_session(new_session.client_id)