/*
 * wsp_client_method_states.def
 *
 * Macro calls to generate rows of the state table. See the documentation for
 * guidance how to use and update these.
 *
 * Note that the `NULL' state has been renamed to `NULL_METHOD' because
 * NULL is reserved by C.
 */

STATE_NAME(NULL_METHOD)
STATE_NAME(REQUESTING)
STATE_NAME(WAITING)
STATE_NAME(WAITING2)
STATE_NAME(COMPLETING)


ROW(NULL_METHOD,
	S_MethodInvoke_Req,
	1,
	{
        Octstr *opdu = NULL;
        
        if (octstr_compare(current_event->u.S_MethodInvoke_Req.method, octstr_imm("GET")) == 0) {
            /* get request */
            opdu = make_get_pdu(current_event->u.S_MethodInvoke_Req.request_headers, current_event->u.S_MethodInvoke_Req.request_uri);
        }
        else if (octstr_compare(current_event->u.S_MethodInvoke_Req.method, octstr_imm("POST")) == 0) {
             /* post request */
             opdu = make_post_pdu(current_event->u.S_MethodInvoke_Req.request_headers, current_event->u.S_MethodInvoke_Req.request_uri, current_event->u.S_MethodInvoke_Req.request_body);
        }
        /* TR-Invoke.req(Class 2, Method, MoreData) */
        if (opdu != NULL) 
            send_invoke(sm, opdu, 0, PROVIDER_ACKNOWLEDGEMENT, TRANSACTION_CLASS_2);

	},
	REQUESTING)

ROW(REQUESTING,
	S_MethodAbort_Req,
	1,
	{
        /* TR-Abort.req(PEERREQ) the Method */
        abort_method(sm, current_event->u.S_MethodAbort_Req.transaction_id, WSP_ABORT_PEERREQ);

        /* S-MethodAbort.ind(USERREQ) */
        indicate_method_abort(msm, WSP_ABORT_USERREQ);
	},
	NULL_METHOD)

ROW(REQUESTING,
	Abort_Event,
	1,
	{
        /* TR-Abort.req(abort reason) the Method */
        abort_method(sm, msm->transaction_id, current_event->u.Abort_Event.reason);

        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.Abort_Event.reason);
	},
	NULL_METHOD)

ROW(REQUESTING,
	TR_Invoke_Cnf,
	1,
	{
        /* S-MethodInvoke.cnf */
        confirm_methodinvoke(msm);
	},
	WAITING)


ROW(REQUESTING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_DISCONNECT,
	{
        WAPEvent *wsp_event;

        /* Disconnect the session */
		wsp_event = wap_event_create(Disconnect_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	REQUESTING)

ROW(REQUESTING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_SUSPEND,
	{
        WAPEvent *wsp_event;

        /* suspend the session */
		wsp_event = wap_event_create(Suspend_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	REQUESTING)

ROW(REQUESTING,
	TR_Abort_Ind,
	e->abort_code != WSP_ABORT_DISCONNECT && e->abort_code != WSP_ABORT_SUSPEND,
	{
        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.TR_Abort_Ind.abort_code);
	},
	NULL_METHOD)



ROW(WAITING,
	S_MethodAbort_Req,
	1,
	{
        /* TR-Abort.req(PEERREQ) the Method */
        abort_method(sm, current_event->u.S_MethodAbort_Req.transaction_id, WSP_ABORT_PEERREQ);

        /* S-MethodAbort.ind(USERREQ) */
        indicate_method_abort(msm, WSP_ABORT_USERREQ);
	},
	NULL_METHOD)

ROW(WAITING,
	Abort_Event,
	1,
	{
        /* TR-Abort.req(abort reason) the Method */
        abort_method(sm, msm->transaction_id, current_event->u.Abort_Event.reason);

        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.Abort_Event.reason);
	},
	NULL_METHOD)


ROW(WAITING,
	TR_Result_Ind,
	pdu->type == Reply,
	{
        /* S-MethodResult.ind(headers, body, moredata) */
        indicate_method_result(msm, wsp_headers_unpack(pdu->u.Reply.headers, 1), pdu->u.Reply.data, pdu->u.Reply.status);
	},
	COMPLETING)

ROW(WAITING,
	TR_Result_Ind,
	pdu->type != Reply /* other */,
	{
        /* TR-Abort.req(PROTOERR */
        abort_method(sm, e->handle, WSP_ABORT_PROTOERR);

       /* S-MethodAbort.ind(PROTOERR reason) */
        indicate_method_abort(msm, WSP_ABORT_PROTOERR);
	},
	NULL_METHOD)


ROW(WAITING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_DISCONNECT,
	{
        WAPEvent *wsp_event;

        /* Disconnect the session */
		wsp_event = wap_event_create(Disconnect_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	WAITING)

ROW(WAITING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_SUSPEND,
	{
        WAPEvent *wsp_event;

        /* suspend the session */
		wsp_event = wap_event_create(Suspend_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	WAITING)

ROW(WAITING,
	TR_Abort_Ind,
	e->abort_code != WSP_ABORT_DISCONNECT && e->abort_code != WSP_ABORT_SUSPEND,
	{
        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.TR_Abort_Ind.abort_code);
	},
	NULL_METHOD)

/*
ROW(WAITING2,
	S_MethodAbort_Req,
	1,
	{
        // TR-Abort.req(PEERREQ) the Method //

        // S-MethodAbort.ind(USERREQ) //
	},
	NULL_METHOD)

ROW(WAITING2,
	Abort_Event,
	1,
	{
        // TR-Abort.req(abort reason) the Method //

        // S-MethodAbort.ind(abort reason) //
	},
	NULL_METHOD)

ROW(WAITING2,
	TR-Result_Ind,
	// SDU size > MRU //,
	{
        // TR-Abort.req(MRUEXCEEDED //

        // S-MethodAbort.ind(MRUEXCEEDED reason) //
	},
	NULL_METHOD)

ROW(WAITING2,
	TR-Result_Ind,
	// Response body, MoreData flag set //,
	{
        // TR-ResultData.res //

        // S-MethodResultData.ind(body, moredata) //
	},
	WAITING2)

ROW(WAITING2,
	TR-Result_Ind,
	// Frame boundary, Data Fragment PDU, MoreData flag cleared //,
	{
        // S-MethodResult.ind(headers, body, moredata) //
	},
	COMPLETING)

ROW(WAITING2,
	TR-Result_Ind,
	// No Frame boundary, Response Body, moredata flag cleared //,
	{
        // S-MethodResult.ind(body, moredata) //
	},
	COMPLETING)


ROW(WAITING2,
	TR_Abort_Ind,
	// reason == DISCONNECT //,
	{
        // Disconnect the session //
	},
	WAITING2)

ROW(WAITING2,
	TR_Abort_Ind,
	// reason == SUSPEND //,
	{
        // suspend the session //
	},
	WAITING2)

ROW(WAITING2,
	TR_Abort_Ind,
	// other //,
	{
        // S-MethodAbort.ind(abort reason) //
	},
	NULL_METHOD)
*/


ROW(COMPLETING,
	S_MethodResult_Res,
	1,
	{
        /* TR-Result.res(Exit info = Acknowledgement Headers */
        sendResultResponse(current_event->u.S_MethodResult_Res.session_handle);
	},
	NULL_METHOD)

ROW(COMPLETING,
	S_MethodAbort_Req,
	1,
	{
        /* TR-Abort.req(PEERREQ) the Method */
        abort_method(sm, current_event->u.S_MethodAbort_Req.transaction_id, WSP_ABORT_PEERREQ);

        /* S-MethodAbort.ind(USERREQ) */
        indicate_method_abort(msm, WSP_ABORT_USERREQ);
	},
	NULL_METHOD)

ROW(COMPLETING,
	Abort_Event,
	1,
	{
        /* TR-Abort.req(abort reason) the Method */
        abort_method(sm, msm->transaction_id, current_event->u.Abort_Event.reason);

        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.Abort_Event.reason);
	},
	NULL_METHOD)

ROW(COMPLETING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_DISCONNECT,
	{
        WAPEvent *wsp_event;

        /* Disconnect the session */
		wsp_event = wap_event_create(Disconnect_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	COMPLETING)

ROW(COMPLETING,
	TR_Abort_Ind,
	e->abort_code == WSP_ABORT_SUSPEND,
	{
        WAPEvent *wsp_event;

        /* suspend the session */
		wsp_event = wap_event_create(Suspend_Event);
		wsp_event->u.Disconnect_Event.session_handle = msm->session_handle;
        wsp_event->priority = 1; /* top of the queue */
        wsp_client_session_dispatch_event(wsp_event);
	},
	COMPLETING)

ROW(COMPLETING,
	TR_Abort_Ind,
	e->abort_code != WSP_ABORT_DISCONNECT && e->abort_code != WSP_ABORT_SUSPEND,
	{
        /* S-MethodAbort.ind(abort reason) */
        indicate_method_abort(msm, current_event->u.TR_Abort_Ind.abort_code);
	},
	NULL_METHOD)





#undef ROW
#undef STATE_NAME
