async def handle_ack(self, server_connection: ServerConnection,
                         ipap_message: IpapMessage):
        """
        The agent received a message witn the ack flag active, it can be a auction message or a disconnect
        message

        :param server_connection: websocket and aiohttp session created for the connection
        :param ipap_message: message sent from the server.
        """
        self.logger.debug('start method handle_ack')
        # verifies ack sequence number
        ack_seqno = ipap_message.get_ackseqno()

        # verifies the connection state
        if server_connection.state == ServerConnectionState.FIN_WAIT_1:
            try:
                server_connection.get_auction_session().confirm_message(ack_seqno)
                server_connection.set_state(ServerConnectionState.FIN_WAIT_2)

            except ValueError:
                self.logger.info("A msg with ack nbr not expected was received, ignoring it")

        else:
            from auction_client.auction_client_handler import HandleAuctionMessage
            when = 0
            handle_auction_message = HandleAuctionMessage(server_connection, ipap_message, when)
            handle_auction_message.start()

        self.logger.debug('End method handle_ack -new state: {0}'.format(str(server_connection.get_state())))
Ejemplo n.º 2
0
    async def handle_ack(self, session: AuctionSession,
                         ipap_message: IpapMessage):
        """
        Handles the arrival of a ack message.

        :param session: session that is handling the connection
        :param ipap_message: message sent from the server.
        :return:
        """
        self.logger.debug('starting handle_ack')
        from auction_server.auction_server_handler import HandleAuctionMessage

        # verifies ack sequence number
        ack_seqno = ipap_message.get_ackseqno()

        # verifies the connection state
        if session.get_connection().state == ClientConnectionState.SYN_RCVD:
            try:
                session.confirm_message(ack_seqno)
                session.get_connection().set_state(
                    ClientConnectionState.ESTABLISHED)

                self.logger.info('New established connection')

            except ValueError:
                self.logger.info(
                    "The message ack is not the expected, so ignore the message"
                )

        # verifies the connection state is in fin wait 1
        elif session.get_connection(
        ).state == ClientConnectionState.FIN_WAIT_1:
            try:
                session.confirm_message(ack_seqno)
                session.get_connection().set_state(
                    ClientConnectionState.FIN_WAIT_2)

            except ValueError:
                self.logger.info(
                    "A msg with ack nbr not expected was received, ignoring it"
                )

        elif session.get_connection().state == ClientConnectionState.LAST_ACK:
            try:
                session.get_connection().set_state(
                    ClientConnectionState.CLOSED)

                self.logger.info("The connection has been closed")
            except ValueError:
                self.logger.info(
                    "The message ack is not the expected, so ignore the message"
                )

        else:
            when = 0
            handle_auction_message = HandleAuctionMessage(
                session, ipap_message, when)
            handle_auction_message.start()

        self.logger.debug('Ending handle_ack')
    async def handle_syn_ack_message(self, server_connection: ServerConnection,
                                     ipap_message: IpapMessage):
        """
        Establishes the session
        :param server_connection: websocket and aiohttp session created for the connection
        :param ipap_message: message sent from the server.
        :return:
        """
        self.logger.debug("Starting handle_syn_ack_message")
        # verifies ack sequence number
        ack_seqno = ipap_message.get_ackseqno()

        self.logger.debug("ack_seqno:{0}".format(str(ack_seqno)))

        # verifies the connection state
        if server_connection.state == ServerConnectionState.SYN_SENT:
            try:
                server_connection.get_auction_session().confirm_message(ack_seqno)

                # send the ack message establishing the session.
                syn_ack_message = self.build_ack_message(server_connection.get_auction_session().get_next_message_id(),
                                                         ipap_message.get_seqno())
                msg = syn_ack_message.get_message()
                await self.send_message(server_connection, msg)

                # puts the connection as established.
                server_connection.set_state(ServerConnectionState.ESTABLISHED)

                self.logger.info("Connection established with server")

            except ValueError:
                self.logger.error("Invalid ack nbr from the server, the session is going to be teardown")
                await self._disconnect_socket(server_connection)
                self.auction_session_manager.del_session(server_connection.get_auction_session().get_key())

        else:
            # The server is not in syn sent state, so ignore the message
            self.logger.info("a message with syn and ack was received, but the session \
                                is not in SYN_SENT. Ignoring the message")

        self.logger.debug("Ending handle_syn_ack_message")
    async def send_ack_disconnect(self, server_connection: ServerConnection,
                                  ipap_message: IpapMessage):
        """
        Sends the ack disconnect message.

        :param server_connection: websocket and aiohttp session created for the connection
        :param ipap_message: message sent from the server.
        :return:
        """
        self.logger.debug("Starting send_ack_disconnect")
        # verifies ack sequence number
        ack_seqno = ipap_message.get_ackseqno()

        try:

            server_connection.get_auction_session().confirm_message(ack_seqno)

        except ValueError:
            # nothing was expected to be acknowledged
            pass

        # verifies the connection state
        if server_connection.state == ServerConnectionState.ESTABLISHED:

            self.logger.debug("starting to disconnect from server connection state established")
            message = self.build_ack_message(server_connection.get_auction_session().get_next_message_id(),
                                             ipap_message.get_seqno())

            print('message ack disconnect id:', message.get_seqno())
            await self.send_message(server_connection, message.get_message())

            self.logger.debug("disconnecting - before putting close_wait ")
            # server_connection.set_state(ServerConnectionState.CLOSE_WAIT)
            self.logger.debug("disconnecting - after putting close_wait ")

            from auction_client.auction_client_handler import HandleResourceRequestTeardown
            handle_tear_down = HandleResourceRequestTeardown(server_connection.get_auction_session().get_key())
            await handle_tear_down.start()

            self.logger.debug("disconnecting - after removing resource request ")

            message = self.build_fin_message(server_connection.get_auction_session().get_next_message_id(), 0)
            server_connection.get_auction_session().add_pending_message(message)
            print('message fin message id:', message.get_seqno())
            await self.send_message(server_connection, message.get_message())

            self.logger.debug("disconnecting - after sending fin message ")

            server_connection.set_state(ServerConnectionState.LAST_ACK)

            self.logger.debug("ending to disconnect from server connection state established")

        # verifies the connection state
        elif server_connection.get_state() == ServerConnectionState.FIN_WAIT_2:

            # send the ack message establishing the session.
            message = self.build_ack_message(server_connection.get_auction_session().get_next_message_id(),
                                             ipap_message.get_seqno())

            print('message fin wait ack id:', message.get_seqno())
            await self.send_message(server_connection, message.get_message())

            await self._disconnect_socket(server_connection)
            self.auction_session_manager.del_session(server_connection.get_auction_session().get_key())
            server_connection.set_state(ServerConnectionState.CLOSED)

        else:
            # The server is not in syn sent state, so ignore the message
            self.logger.info("A msg with fin state was received,but the server is not in fin_wait state, ignoring it")

        self.logger.debug("Ending  send_ack_disconnect- new state {0}".format(str(server_connection.get_state())))
Ejemplo n.º 5
0
    async def handle_fin(self, session: AuctionSession,
                         ipap_message: IpapMessage):
        """
        Handles the arrival of a fin message.

        :param session: session that is handling the connection
        :param ipap_message: message sent from the server.
        :return:
        """
        self.logger.debug("Starting  handle_fin")
        # verifies ack sequence number
        ack_seqno = ipap_message.get_ackseqno()

        try:
            session.confirm_message(ack_seqno)
        except ValueError:
            # The message ack is not the expected, so just ignore it
            pass

        # verifies the connection state
        if session.get_connection().state == ClientConnectionState.ESTABLISHED:
            message = self.build_ack_message(session.get_next_message_id(),
                                             ipap_message.get_seqno())

            await self.send_message(session.get_connection(),
                                    message.get_message())
            session.get_connection().set_state(
                ClientConnectionState.CLOSE_WAIT)

            from auction_server.auction_server_handler import HandleClientTearDown
            handle_tear_down = HandleClientTearDown(session)
            await handle_tear_down.start()

            message = self.build_fin_message(session.get_next_message_id(), 0)
            session.add_pending_message(message)
            await self.send_message(session.get_connection(),
                                    message.get_message())
            session.get_connection().set_state(ClientConnectionState.LAST_ACK)

        elif session.get_connection(
        ).state == ClientConnectionState.FIN_WAIT_2:

            self.logger.debug("receive the fin message when in fin_wait")
            # send the ack message establishing the session.
            message = self.build_ack_message(session.get_next_message_id(),
                                             ipap_message.get_seqno())

            await self.send_message(session.get_connection(),
                                    message.get_message())

            session.get_connection().set_state(ClientConnectionState.CLOSED)
            await self._disconnect_socket(session.get_connection())
            self.session_manager.del_session(session.get_key())

        else:
            # The server is not in established state, so ignore the message
            self.logger.info(
                "A msg with fin state was received,but the server \
                                is not in established state, ignoring it")

        self.logger.debug("Ending handle_fin")