summaryrefslogtreecommitdiff
path: root/src/dispatch.c
blob: d58517c11c83c87fc308a1e494456cb38ed62bde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011  Alan Fitton

 * 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.

 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <glib.h>
#include <glib/gprintf.h>
#include <json-glib/json-glib.h>
#include <curl/curl.h>

#include "config.h"
#include "dispatch.h"
#include "http.h"
#include "json.h"

static void dispatch_async_threadfunc(struct DispatchAsyncData *task,
                                      trg_client * client);

JsonObject *dispatch(trg_client * client, JsonNode * req, int *status)
{
    gchar *serialized;
    struct http_response *response;
    JsonObject *deserialized;
    JsonNode *result;
    GError *decode_error = NULL;

    serialized = trg_serialize(req);
    json_node_free(req);
#ifdef DEBUG
    if (g_getenv("TRG_SHOW_OUTGOING"))
        g_printf("=>(outgoing)=> %s\n", serialized);
#endif
    response = trg_http_perform(client, serialized);
    g_free(serialized);

    if (status)
        *status = response->status;

    if (response->status != CURLE_OK) {
        http_response_free(response);
        return NULL;
    }

    deserialized = trg_deserialize(response, &decode_error);
    http_response_free(response);

    if (decode_error) {
        g_printf("JSON decoding error: %s\n", decode_error->message);
        g_error_free(decode_error);
        if (status)
            *status = FAIL_JSON_DECODE;
        return NULL;
    }

    result = json_object_get_member(deserialized, "result");
    if (status
        && (!result || g_strcmp0(json_node_get_string(result), "success")))
        *status = FAIL_RESPONSE_UNSUCCESSFUL;

    return deserialized;
}

static void dispatch_async_threadfunc(struct DispatchAsyncData *task,
                                      trg_client * client)
{
    int status;
    JsonObject *result = dispatch(client, task->req, &status);
    if (task->callback)
        task->callback(result, status, task->data);
    g_free(task);
}

GThreadPool *dispatch_init_pool(trg_client * client)
{
    return g_thread_pool_new((GFunc) dispatch_async_threadfunc, client,
                             DISPATCH_POOL_SIZE, FALSE, NULL);
}

gboolean dispatch_async(trg_client * client, JsonNode * req,
                        void (*callback) (JsonObject *, int, gpointer),
                        gpointer data)
{
    GError *error = NULL;
    struct DispatchAsyncData *args = g_new(struct DispatchAsyncData, 1);

    args->callback = callback;
    args->data = data;
    args->req = req;

    g_thread_pool_push(client->pool, args, &error);
    if (error) {
        g_printf("thread creation error: %s\n", error->message);
        g_error_free(error);
        g_free(args);
        return FALSE;
    } else {
        return TRUE;
    }
}