summaryrefslogtreecommitdiff
path: root/src/dispatch.c
blob: 5620c6a5fff3ae4b95dd721951b9d4c0c20e3359 (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
/*
 * 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 "dispatch.h"
#include "http.h"
#include "json.h"

static gpointer dispatch_async_threadfunc(gpointer ptr);

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);

    g_printf("=>(outgoing)=> %s\n", serialized);
    response = trg_http_perform(client, serialized);
    g_free(serialized);

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

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

    return deserialized;
}

static gpointer dispatch_async_threadfunc(gpointer ptr)
{
    struct dispatch_async_args *args = (struct dispatch_async_args *) ptr;
    int status;
    JsonObject *result = dispatch(args->client, args->req, &status);
    if (args->callback != NULL)
	args->callback(result, status, args->data);
    g_free(args);
    return NULL;
}

GThread *dispatch_async(trg_client * client, JsonNode * req,
			void (*callback) (JsonObject *, int, gpointer),
			gpointer data)
{
    GError *error = NULL;
    GThread *thread;
    struct dispatch_async_args *args;

    args = g_new(struct dispatch_async_args, 1);
    args->callback = callback;
    args->data = data;
    args->req = req;
    args->client = client;

    thread =
	g_thread_create(dispatch_async_threadfunc, args, FALSE, &error);
    if (error != NULL) {
	g_printf("thread creation error: %s\n", error->message);
	g_error_free(error);
	g_free(args);
	return NULL;
    } else {
	return thread;
    }
}