aboutsummaryrefslogtreecommitdiffstats
path: root/toj/php/event_exec.cpp
blob: 25d7944c8eef58f35806ee64bb119760e8f0b448 (plain) (blame)
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
#include "event_exec.h"

#define EVENT_BUFLEN 1005
#define EVENT_URL "http://10.8.0.2/toj/php/event.php"

struct event_handle
{
    int init, plen, blen;
    char *post, *buf;
    pthread_mutex_t mutex;
    CURL *handle;
};

static struct event_handle conn;

size_t event_write_cb(void *in, size_t siz, size_t nb, void *buf)
{
    int len;
    len = siz*nb+1;
    if(len > conn.blen)
    {
        len = conn.blen-1;
    }
    memcpy(buf, in, len);
    ((char*)buf)[len] = 0;
    return siz*nb;
}

int event_init()
{
    if(conn.init)
    {
        return 0;
    }
    conn.plen = conn.blen = EVENT_BUFLEN;
    conn.post = (char*)malloc(EVENT_BUFLEN);
    conn.buf = (char*)malloc(EVENT_BUFLEN);
    curl_global_init(CURL_GLOBAL_ALL);
    conn.handle = curl_easy_init();
    curl_easy_setopt(conn.handle, CURLOPT_URL, EVENT_URL);
    curl_easy_setopt(conn.handle, CURLOPT_WRITEFUNCTION, event_write_cb);
    curl_easy_setopt(conn.handle, CURLOPT_WRITEDATA, conn.buf);
    pthread_mutex_init(&conn.mutex, NULL);
    conn.init = 1;
    return 1;
}

int event_exec(const char *fname, const char *name, const char *arg)
{
    int res, len;
    char *t;
    event_init();
    if(pthread_mutex_lock(&conn.mutex) == 0)
    {
        len = 0;
        len += strlen(fname)+6;
        len += strlen(name)+6;
        len += strlen(arg)+6;
        if(len > conn.plen)
        {
            t = (char*)malloc(len);
            if(!t)
            {
                pthread_mutex_unlock(&conn.mutex);
                return 0;
            }
            free(conn.post);
            conn.post = t;
            conn.plen = len;
        }
        sprintf(conn.post, "fname=%s&name=%s&arg=%s", fname, name, arg);
        curl_easy_setopt(conn.handle, CURLOPT_POSTFIELDS, conn.post);
        res = curl_easy_perform(conn.handle);
        if(res)
        {
            pthread_mutex_unlock(&conn.mutex);
            return 0;
        }
        fprintf(stderr, "%s\n", conn.buf);
        if(!strcmp(conn.buf, "true"))
        {
            pthread_mutex_unlock(&conn.mutex);
            return 1;
        }

        pthread_mutex_unlock(&conn.mutex);
    }
    return 0;
}