static void on_message_receiver_state_changed()

in src/amqp_management.c [507:584]


static void on_message_receiver_state_changed(const void* context, MESSAGE_RECEIVER_STATE new_state, MESSAGE_RECEIVER_STATE previous_state)
{
    if (context == NULL)
    {
        /* Codes_SRS_AMQP_MANAGEMENT_01_149: [ When `on_message_receiver_state_changed` is called with NULL `context`, it shall do nothing. ]*/
        LogError("on_message_receiver_state_changed called with NULL context");
    }
    else
    {
        /* Codes_SRS_AMQP_MANAGEMENT_01_150: [ When `on_message_receiver_state_changed` is called and the `new_state` is different than `previous_state`, the following actions shall be taken: ]*/
        /* Codes_SRS_AMQP_MANAGEMENT_01_160: [ When no state change is detected, `on_message_receiver_state_changed` shall do nothing. ]*/
        if (new_state != previous_state)
        {
            AMQP_MANAGEMENT_INSTANCE* amqp_management_instance = (AMQP_MANAGEMENT_INSTANCE*)context;
            switch (amqp_management_instance->amqp_management_state)
            {
            default:
                break;

            /* Codes_SRS_AMQP_MANAGEMENT_01_151: [ For the current state of AMQP management being `OPENING`: ]*/
            case AMQP_MANAGEMENT_STATE_OPENING:
            {
                switch (new_state)
                {
                case MESSAGE_RECEIVER_STATE_OPENING:
                    /* Codes_SRS_AMQP_MANAGEMENT_01_164: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_OPEING` the transition shall be ignored. ]*/
                    break;

                default:
                /* Codes_SRS_AMQP_MANAGEMENT_01_152: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_IDLE`, `MESSAGE_RECEIVER_STATE_CLOSING` or `MESSAGE_RECEIVER_STATE_ERROR`, the `on_amqp_management_open_complete` callback shall be called with `AMQP_MANAGEMENT_OPEN_ERROR`, while also passing the context passed in `amqp_management_open_async`. ]*/
                case MESSAGE_RECEIVER_STATE_IDLE:
                case MESSAGE_RECEIVER_STATE_CLOSING:
                case MESSAGE_RECEIVER_STATE_ERROR:
                    amqp_management_instance->amqp_management_state = AMQP_MANAGEMENT_STATE_IDLE;
                    amqp_management_instance->on_amqp_management_open_complete(amqp_management_instance->on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_ERROR);
                    break;

                case MESSAGE_RECEIVER_STATE_OPEN:
                    amqp_management_instance->receiver_connected = 1;
                    /* Codes_SRS_AMQP_MANAGEMENT_01_154: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_OPEN` and the message sender did not yet indicate its state as `MESSAGE_RECEIVER_STATE_OPEN`, the `on_amqp_management_open_complete` callback shall not be called. ]*/
                    if (amqp_management_instance->sender_connected != 0)
                    {
                        /* Codes_SRS_AMQP_MANAGEMENT_01_153: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_OPEN` and the message sender already indicated its state as `MESSAGE_RECEIVER_STATE_OPEN`, the `on_amqp_management_open_complete` callback shall be called with `AMQP_MANAGEMENT_OPEN_OK`, while also passing the context passed in `amqp_management_open_async`. ]*/
                        amqp_management_instance->amqp_management_state = AMQP_MANAGEMENT_STATE_OPEN;
                        amqp_management_instance->on_amqp_management_open_complete(amqp_management_instance->on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
                    }
                    break;
                }
                break;
            }
            /* Codes_SRS_AMQP_MANAGEMENT_01_155: [ For the current state of AMQP management being `OPEN`: ]*/
            case AMQP_MANAGEMENT_STATE_OPEN:
            {
                switch (new_state)
                {
                default:
                /* Codes_SRS_AMQP_MANAGEMENT_01_156: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_IDLE`, `MESSAGE_RECEIVER_STATE_OPENING`, `MESSAGE_RECEIVER_STATE_CLOSING` or `MESSAGE_RECEIVER_STATE_ERROR` the `on_amqp_management_error` callback shall be invoked while passing the `on_amqp_management_error_context` as argument. ]*/
                case MESSAGE_RECEIVER_STATE_IDLE:
                case MESSAGE_RECEIVER_STATE_CLOSING:
                case MESSAGE_RECEIVER_STATE_ERROR:
                    amqp_management_instance->amqp_management_state = AMQP_MANAGEMENT_STATE_ERROR;
                    amqp_management_instance->on_amqp_management_error(amqp_management_instance->on_amqp_management_error_context);
                    break;

                case MESSAGE_RECEIVER_STATE_OPEN:
                    /* Codes_SRS_AMQP_MANAGEMENT_01_157: [ - If `new_state` is `MESSAGE_RECEIVER_STATE_OPEN`, `on_message_receiver_state_changed` shall do nothing. ]*/
                    break;
                }
                break;
            }
            /* Codes_SRS_AMQP_MANAGEMENT_01_158: [ For the current state of AMQP management being `ERROR`: ]*/
            case AMQP_MANAGEMENT_STATE_ERROR:
                /* Codes_SRS_AMQP_MANAGEMENT_01_159: [ - All state transitions shall be ignored. ]*/
                break;
            }
        }
    }
}