2019-08-29 19:54:39 +00:00
|
|
|
/* Osmocom CBC (Cell Broacast Centre) */
|
|
|
|
|
|
|
|
/* (C) 2019 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0+
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2021-01-06 10:15:57 +00:00
|
|
|
#include <osmocom/core/linuxlist.h>
|
2019-08-29 19:54:39 +00:00
|
|
|
#include <osmocom/core/it_q.h>
|
|
|
|
|
2022-07-04 11:13:22 +00:00
|
|
|
#include <osmocom/cbc/rest_it_op.h>
|
|
|
|
#include <osmocom/cbc/internal.h>
|
2019-08-29 19:54:39 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HTTP THREAD
|
|
|
|
***********************************************************************/
|
|
|
|
|
2020-12-31 00:08:19 +00:00
|
|
|
/* allocate an inter-thread operation */
|
2019-08-29 19:54:39 +00:00
|
|
|
struct rest_it_op *rest_it_op_alloc(void *ctx)
|
|
|
|
{
|
|
|
|
struct rest_it_op *op = talloc_zero(ctx, struct rest_it_op);
|
|
|
|
if (!op)
|
|
|
|
return NULL;
|
|
|
|
pthread_mutex_init(&op->mutex, NULL);
|
|
|
|
pthread_cond_init(&op->cond, NULL);
|
2021-01-02 11:03:56 +00:00
|
|
|
/* set a 'safe' default for all kinds of error situations */
|
|
|
|
rest_it_op_set_http_result(op, 500, "Internal Server Error");
|
2019-08-29 19:54:39 +00:00
|
|
|
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enqueue an inter-thread operation in REST->main direction and wait for its completion */
|
2021-01-02 10:42:49 +00:00
|
|
|
int rest_it_op_send_and_wait(struct rest_it_op *op)
|
2019-08-29 19:54:39 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
2021-01-06 11:02:55 +00:00
|
|
|
LOGP(DREST, LOGL_DEBUG, "rest_it_op enqueue from %lu\n", pthread_self());
|
2020-12-31 16:32:44 +00:00
|
|
|
|
2019-08-29 19:54:39 +00:00
|
|
|
rc = osmo_it_q_enqueue(g_cbc->it_q.rest2main, op, list);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
2021-01-02 10:42:49 +00:00
|
|
|
/* grab mutex before pthread_cond_wait() */
|
2019-08-29 19:54:39 +00:00
|
|
|
pthread_mutex_lock(&op->mutex);
|
|
|
|
|
2020-12-31 16:32:44 +00:00
|
|
|
LOGP(DREST, LOGL_DEBUG, "rest_it_op wait....\n");
|
|
|
|
|
2021-01-02 10:42:49 +00:00
|
|
|
rc = pthread_cond_wait(&op->cond, &op->mutex);
|
2019-08-29 19:54:39 +00:00
|
|
|
|
2021-01-02 10:42:49 +00:00
|
|
|
LOGP(DREST, LOGL_DEBUG, "rest_it_op completed with %d (HTTP %u)\n",
|
|
|
|
rc, op->http_result.response_code);
|
2019-08-29 19:54:39 +00:00
|
|
|
|
2021-01-02 10:42:49 +00:00
|
|
|
pthread_mutex_unlock(&op->mutex);
|
2020-12-31 16:32:44 +00:00
|
|
|
|
2019-08-29 19:54:39 +00:00
|
|
|
/* 'op' is implicitly owned by the caller again now, who needs to take care
|
|
|
|
* of releasing its memory */
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* MAIN THREAD
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
|
2021-01-06 10:15:57 +00:00
|
|
|
void rest2main_read_cb(struct osmo_it_q *q, struct llist_head *item)
|
2019-08-29 19:54:39 +00:00
|
|
|
{
|
2021-01-06 10:15:57 +00:00
|
|
|
struct rest_it_op *op = container_of(item, struct rest_it_op, list);
|
2019-08-29 19:54:39 +00:00
|
|
|
struct cbc_message *cbc_msg;
|
2020-12-31 00:08:19 +00:00
|
|
|
|
2021-01-06 11:02:55 +00:00
|
|
|
LOGP(DREST, LOGL_DEBUG, "%s(op=%p) from %lu\n", __func__, op, pthread_self());
|
2020-12-31 18:07:41 +00:00
|
|
|
|
2019-08-29 19:54:39 +00:00
|
|
|
/* FIXME: look up related message and dispatch to message FSM,
|
|
|
|
* which will eventually call pthread_cond_signal(&op->cond) */
|
2020-12-31 00:08:19 +00:00
|
|
|
|
2019-08-29 19:54:39 +00:00
|
|
|
switch (op->operation) {
|
|
|
|
case REST_IT_OP_MSG_CREATE:
|
2021-01-02 10:42:49 +00:00
|
|
|
cbc_message_new(&op->u.create.cbc_msg, op);
|
2019-08-29 19:54:39 +00:00
|
|
|
break;
|
|
|
|
case REST_IT_OP_MSG_DELETE:
|
|
|
|
cbc_msg = cbc_message_by_id(op->u.del.msg_id);
|
|
|
|
if (cbc_msg) {
|
2021-01-02 10:42:49 +00:00
|
|
|
cbc_message_delete(cbc_msg, op);
|
2020-12-31 00:08:19 +00:00
|
|
|
} else {
|
2021-01-02 11:03:56 +00:00
|
|
|
rest_it_op_set_http_result(op, 404, "Unknown message ID");
|
2021-01-02 10:42:49 +00:00
|
|
|
rest_it_op_complete(op);
|
2019-08-29 19:54:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-12-31 00:08:19 +00:00
|
|
|
/* TODO: REPLACE */
|
|
|
|
/* TODO: STATUS */
|
2019-08-29 19:54:39 +00:00
|
|
|
default:
|
2021-01-02 11:03:56 +00:00
|
|
|
rest_it_op_set_http_result(op, 501, "Not Implemented");
|
2021-01-02 10:42:49 +00:00
|
|
|
rest_it_op_complete(op);
|
2019-08-29 19:54:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-02 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rest_it_op_set_http_result(struct rest_it_op *op, uint32_t code, const char *body)
|
|
|
|
{
|
|
|
|
if (!op)
|
|
|
|
return;
|
|
|
|
op->http_result.response_code = code;
|
|
|
|
op->http_result.message = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signal completion of rest_it_op to whoever is waiting for it */
|
|
|
|
void rest_it_op_complete(struct rest_it_op *op)
|
|
|
|
{
|
|
|
|
LOGP(DREST, LOGL_DEBUG, "%s(op=%p) complete\n", __func__, op);
|
|
|
|
if (!op)
|
|
|
|
return;
|
|
|
|
pthread_cond_signal(&op->cond);
|
2019-08-29 19:54:39 +00:00
|
|
|
}
|