message-op.cpp 3.2 KB
Newer Older
1 2
/*
 * message-op.cpp
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19 20
 */

#include "sal/message-op.h"
21 22 23 24 25

using namespace std;

LINPHONE_BEGIN_NAMESPACE

Ghislain MARY's avatar
Ghislain MARY committed
26 27
void SalMessageOp::processError () {
	if (mDir == Dir::Outgoing)
28
		mRoot->mCallbacks.message_delivery_update(this, SalMessageDeliveryFailed);
Ghislain MARY's avatar
Ghislain MARY committed
29 30 31
	else
		lWarning() << "Unexpected error for incoming message on op [" << this << "]";
	mState = State::Terminated;
32 33
}

Ghislain MARY's avatar
Ghislain MARY committed
34 35 36
void SalMessageOp::processIoErrorCb (void *userCtx, const belle_sip_io_error_event_t *event) {
	auto op = static_cast<SalMessageOp *>(userCtx);
	sal_error_info_set(&op->mErrorInfo, SalReasonIOError, "SIP", 503, "IO Error", nullptr);
37
	op->processError();
38 39
}

Ghislain MARY's avatar
Ghislain MARY committed
40 41 42 43
void SalMessageOp::processResponseEventCb (void *userCtx, const belle_sip_response_event_t *event) {
	auto op = static_cast<SalMessageOp *>(userCtx);
	int statusCode = belle_sip_response_get_status_code(belle_sip_response_event_get_response(event));

44
	op->setErrorInfoFromResponse(belle_sip_response_event_get_response(event));
Ghislain MARY's avatar
Ghislain MARY committed
45 46 47 48 49 50 51 52

	SalMessageDeliveryStatus status = SalMessageDeliveryFailed;
	if ((statusCode >= 100) && (statusCode < 200))
		status = SalMessageDeliveryInProgress;
	else if ((statusCode >= 200) && (statusCode < 300))
		status = SalMessageDeliveryDone;

	op->mRoot->mCallbacks.message_delivery_update(op, status);
53 54
}

Ghislain MARY's avatar
Ghislain MARY committed
55 56 57
void SalMessageOp::processTimeoutCb (void *userCtx, const belle_sip_timeout_event_t *event) {
	auto op = static_cast<SalMessageOp *>(userCtx);
	sal_error_info_set(&op->mErrorInfo, SalReasonRequestTimeout, "SIP", 408, "Request timeout", nullptr);
58
	op->processError();
59 60
}

Ghislain MARY's avatar
Ghislain MARY committed
61 62
void SalMessageOp::processRequestEventCb (void *userCtx, const belle_sip_request_event_t *event) {
	auto op = static_cast<SalMessageOp *>(userCtx);
63
	op->processIncomingMessage(event);
64 65
}

Ghislain MARY's avatar
Ghislain MARY committed
66 67 68 69 70 71 72 73 74 75 76 77
SalMessageOp::SalMessageOp (Sal *sal) : SalOp(sal) {
	mType = Type::Message;
	fillCallbacks();
}

void SalMessageOp::fillCallbacks () {
	static belle_sip_listener_callbacks_t opMessageCallbacks = { 0 };
	if (!opMessageCallbacks.process_io_error) {
		opMessageCallbacks.process_io_error = processIoErrorCb;
		opMessageCallbacks.process_response_event = processResponseEventCb;
		opMessageCallbacks.process_timeout = processTimeoutCb;
		opMessageCallbacks.process_request_event = processRequestEventCb;
78
	}
Ghislain MARY's avatar
Ghislain MARY committed
79
	mCallbacks = &opMessageCallbacks;
80 81
}

82
int SalMessageOp::sendMessage (const Content &content) {
83
	mDir = Dir::Outgoing;
Ghislain MARY's avatar
Ghislain MARY committed
84 85 86

	auto request = buildRequest("MESSAGE");
	if (!request)
87
		return -1;
Ghislain MARY's avatar
Ghislain MARY committed
88 89 90

	prepareMessageRequest(request, content);
	return sendRequest(request);
91 92 93
}

LINPHONE_END_NAMESPACE