Commit 9052ad6c authored by xHire's avatar xHire
Browse files

Migration from coincer-commons repo

Reformatted with `indent -kr -i8 -l80 -sc -fca -fc1`
Original author: Mikoláš Stuchlík <stuchlej@gmail.com>
parent ab5a6c06
/*
* Coincer
* Copyright (C) 2019 Coincer Developers
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _BSD_SOURCE
#include <string.h>
#include "jansson_wrapper.h"
parsing_error_t get_real_number(json_t *source, const char *name,
double *destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_real(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
*destination = json_real_value(property);
return PARSING_NO_ERROR;
}
parsing_error_t get_decimal_number(json_t *source, const char *name,
int64_t *destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_integer(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
*destination = json_integer_value(property);
return PARSING_NO_ERROR;
}
parsing_error_t get_string(json_t *source, const char *name,
char **destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (*destination != NULL) {
return PARSING_OUTPUT_POINTER_NOT_NULL;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_string(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
(*destination) = strdup(json_string_value(property));
return PARSING_NO_ERROR;
}
parsing_error_t get_boolean(json_t *source, const char *name,
bool *destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_boolean(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
*destination = json_boolean_value(property);
return PARSING_NO_ERROR;
}
parsing_error_t get_array(json_t *source, const char *name,
json_t **destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (*destination != NULL) {
return PARSING_OUTPUT_POINTER_NOT_NULL;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_array(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
*destination = property;
return PARSING_NO_ERROR;
}
parsing_error_t get_object(json_t *source, const char *name,
json_t **destination)
{
if (source == NULL) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (!json_is_object(source)) {
return PARSING_SOUCE_NOT_AN_OBJECT;
}
if (*destination != NULL) {
return PARSING_OUTPUT_POINTER_NOT_NULL;
}
if (destination == NULL) {
return PARSING_OUTPUT_POINTER_NULL;
}
json_t *property = json_object_get(source, name);
if (property == NULL) {
return PARSING_NO_SUCH_PROPERTY;
}
if (json_is_null(property)) {
return PARSING_PROPERTY_IS_NULL;
}
if (!json_is_object(property)) {
return PARSING_PROPERTY_TYPE_MISMATCH;
}
*destination = property;
return PARSING_NO_ERROR;
}
/*
* Coincer
* Copyright (C) 2019 Coincer Developers
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JANSSON_WRAPPER_H
#define JANSSON_WRAPPER_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <jansson.h>
typedef enum parsing_error {
PARSING_NO_ERROR,
PARSING_NO_SUCH_PROPERTY,
PARSING_PROPERTY_TYPE_MISMATCH,
PARSING_OUTPUT_POINTER_NOT_NULL,
PARSING_OUTPUT_POINTER_NULL,
PARSING_SOUCE_NOT_AN_OBJECT,
PARSING_PROPERTY_IS_NULL
} parsing_error_t;
/**
* Parses real number from from json object.
*
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to destination.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_real_number(json_t *source, const char *name,
double *destination);
/**
* Parses decimal number from from json object.
*
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to destination.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_decimal_number(json_t *source, const char *name,
int64_t *destination);
/**
* Parses string from from json object. User is responsible for freeing result.
*
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to null pointer.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_string(json_t *source, const char *name,
char **destination);
/**
* Parses boolean from from json object.
*
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to destination.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_boolean(json_t *source, const char *name,
bool *destination);
/**
* Parses array from from json object. User is responsible for freeing result.
*
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to null poiner.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_array(json_t *source, const char *name,
json_t **destination);
/**
* Parses object from from json object. User is responsible for freeing result.
* @param source nonunll input json object.
* @param name name of property.
* @param destination nonull pointer to null pointer.
* @return error code, PARSING_NO_ERROR on success.
*/
parsing_error_t get_object(json_t *source, const char *name,
json_t **destination);
#endif /* JANSSON_WRAPPER_H */
/*
* Coincer
* Copyright (C) 2019 Coincer Developers
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rpc_endpoint_template.h"
rpc_error_t encode_template_request(json_t *args_root,
struct template_args args)
{
return RPC_ERROR_UNKNOWN;
}
rpc_error_t decode_template_request(json_t *args_root,
struct template_args *args)
{
return RPC_ERROR_UNKNOWN;
}
rpc_error_t encode_template_response(json_t *rslt_root,
struct template_rslt rslt)
{
return RPC_ERROR_UNKNOWN;
}
rpc_error_t decode_template_response(json_t *rslt_root,
struct template_rslt *rslt)
{
return RPC_ERROR_UNKNOWN;
}
struct template_args *template_args_init(void)
{
return NULL;
}
struct template_rslt *template_rslt_init(void)
{
return NULL;
}
void template_args_destroy(struct template_args *instance)
{
}
void template_rslt_destroy(struct template_rslt *instance)
{
}
/*
* Coincer
* Copyright (C) 2019 Coincer Developers
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef rpc_endpoint_template_h
#define rpc_endpoint_template_h
#include <jansson.h>
#include <stdio.h>
#include "rpc_method_description.h"
/**
* Container for rpc method arguments
*/
struct template_args {
};
/**
* Container for rpc method result
*/
struct template_rslt {
};
/**
* Callback method. On client side used for resolving responses, on server side
* used for resolvig requests. Argument and result and error pointer should be
* never NULL.
*
* @param template_callback callback type for better build time type checking
* @return true on success
*/
typedef bool(*template_callback) (struct template_args *,
struct template_rslt *, enum rpc_error *);
/**
* Encodes request continer into provided json object.
*
* @param args_root NONNULL pointer to json object
* @param args arguments
* @return encoding error, RPC_ERROR_NONE on success
*/
rpc_error_t encode_template_request(json_t *args_root,
struct template_args args);
/**
* Decodes request json object into provided container.
*
* @param args_root NONNULL pointer to json object
* @param args NONNULL pointer to argument container
* @return decoding error, RPC_ERROR_NONE on success
*/
rpc_error_t decode_template_request(json_t *args_root,
struct template_args *args);
/**
* Encodes response continer into provided json object.
*
* @param rslt_root NONNULL pointer to json object
* @param rslt reponse
* @return encoding error, RPC_ERROR_NONE on success
*/
rpc_error_t encode_template_response(json_t *rslt_root,
struct template_rslt rslt);
/**
* Decodes response json object into provided container.
*
* @param rslt_root NONNULL pointer to json object
* @param rslt NONNULL pointer to result container
* @return decoding error, RPC_ERROR_NONE on success
*/
rpc_error_t decode_template_response(json_t *rslt_root,
struct template_rslt *rslt);
/**
* Create new heap instance of argument container.
*
* @return NULL on error
*/
struct template_args *template_args_init(void);
/**
* Create new heap instance of response container.
*
* @return NULL on error
*/
struct template_rslt *template_rslt_init(void);
/**
* Frees heap instance of argument container.
*
* @param template_args NONNULL pointer to argument continer
*/
void template_args_destroy(struct template_args *);
/**
* Frees heap instance of result container.
*
* @param template_rslt NONNULL pointer to result continer
*/
void template_rslt_destroy(struct template_rslt *);
/**
* This structure contains pointers to all methods used to maintain rpc
* method's lifecycle. Pointer to this sctructure must be imported into
* rpc_manager's rpc method collection.
*/
static struct rpc_method_description TEMPLATE_METHOD = { "method_name",
(generic_rpc_callback) (template_callback) &method_not_assigned,
(generic_rpc_request_encoder) &encode_template_request,
(generic_rpc_request_decoder) &decode_template_request,
(generic_rpc_response_encoder) &encode_template_response,
(generic_rpc_response_decoder) &decode_template_response,
(generic_rpc_args_cont_init) &template_args_init,
(generic_rpc_rslt_cont_init) &template_rslt_init,
(generic_rpc_args_cont_destroy) &template_args_destroy,
(generic_rpc_rslt_cont_destroy) &template_rslt_destroy
};
#endif /* rpc_endpoint_template_h */
/*
* Coincer
* Copyright (C) 2019 Coincer Developers
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "accept_order.h"
#include "jansson_wrapper.h"
#define ARG_ID_NAME "id"
#define RSLT_ID_NAME "id"
#define RSLT_STATE_NAME "state"
rpc_error_t encode_accept_order_request(json_t *args_root,
struct accept_order_args *args)
{
json_t *order_id;
/* Encode from */
if (args->order_id == NULL) {
return JSON_INTERNAL_ERROR;
}
order_id = json_string(args->order_id);
if (order_id == NULL) {
return JSON_INTERNAL_ERROR;
}
json_object_set_new(args_root, ARG_ID_NAME, order_id);
return RPC_ERROR_NONE;
}
rpc_error_t decode_accept_order_request(json_t *args_root,
struct accept_order_args *args)
{
parsing_error_t reuse_err_c = PARSING_NO_ERROR;
/* Decoding from */
reuse_err_c = get_string(args_root, ARG_ID_NAME, &(args->order_id));
if (reuse_err_c != PARSING_NO_ERROR) {
return JSON_PARSE_ERROR;
}
return RPC_ERROR_NONE;
}
rpc_error_t encode_accept_order_response(json_t *rslt_root,
struct accept_order_rslt *rslt)
{
json_t *order_id, *state;
/* Encode from */
if (rslt->order_id == NULL) {
return JSON_INTERNAL_ERROR;
}
order_id = json_string(rslt->order_id);
if (order_id == NULL) {
return JSON_INTERNAL_ERROR;
}
json_object_set_new(rslt_root, RSLT_ID_NAME, order_id);
/* Encode state */
state = json_string(ACCEPT_ORDER_STATUS_NAMES[rslt->status]);
if (state == NULL) {
return JSON_INTERNAL_ERROR;
}
json_object_set_new(rslt_root, RSLT_STATE_NAME, state);
return RPC_ERROR_NONE;
}
rpc_error_t decode_accept_order_response(json_t *rslt_root,
struct accept_order_rslt *rslt)
{
parsing_error_t reuse_err_c = PARSING_NO_ERROR;
char *status_string = NULL;
int status_lookup_counter;
/* Decoding id */
reuse_err_c = get_string(rslt_root, RSLT_ID_NAME, &(rslt->order_id));
if (reuse_err_c != PARSING_NO_ERROR) {
return JSON_PARSE_ERROR;
}
reuse_err_c = get_string(rslt_root, RSLT_STATE_NAME, &status_string);
if (reuse_err_c != PARSING_NO_ERROR) {
return JSON_PARSE_ERROR;
}