summaryrefslogtreecommitdiffstats
path: root/src/aclk/mqtt_websockets/test.c
blob: 59a9f3474822cb874bb2b90bff469c50114ece5f (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
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2020 Timotej Šiškovič

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mqtt_wss_client.h"

int test_exit = 0;
int port = 0;

void mqtt_wss_log_cb(mqtt_wss_log_type_t log_type, const char* str)
{
    (void)log_type;
    puts(str);
}

#define TEST_MSGLEN_MAX 512
void msg_callback(const char *topic, const void *msg, size_t msglen, int qos)
{
    char cmsg[TEST_MSGLEN_MAX];
    size_t len = (msglen < TEST_MSGLEN_MAX - 1) ? msglen : (TEST_MSGLEN_MAX - 1);
    memcpy(cmsg,
           msg,
           len);
    cmsg[len] = 0;

    if (!strcmp(cmsg, "shutdown"))
        test_exit = 1;

    printf("Got Message From Broker Topic \"%s\" QOS %d MSG: \"%s\"\n", topic, qos, cmsg);
}

#define TESTMSG "Hello World!"
int client_handle(mqtt_wss_client client)
{
    struct mqtt_connect_params params = {
        .clientid = "test",
        .username = "anon",
        .password = "anon",
        .keep_alive = 10
    };

/*    struct mqtt_wss_proxy proxy = {
        .host = "127.0.0.1",
        .port = 3128,
        .type = MQTT_WSS_PROXY_HTTP
    };*/

    while (mqtt_wss_connect(client, "127.0.0.1", port, &params, MQTT_WSS_SSL_ALLOW_SELF_SIGNED, NULL /*&proxy*/)) {
        printf("Connect failed\n");
        sleep(1);
        printf("Attempting Reconnect\n");
    }
    printf("Connection succeeded\n");

    mqtt_wss_subscribe(client, "test", 1);

    while (!test_exit) {
        if(mqtt_wss_service(client, -1) < 0)
            return 1;
    }
    return 0;
}

int main(int argc, char **argv)
{
    if (argc >= 2)
        port = atoi(argv[1]);
    if (!port)
        port = 9002;
    printf("Using port %d\n", port);

    mqtt_wss_client client = mqtt_wss_new("main", mqtt_wss_log_cb, msg_callback, NULL);
    if (!client) {
        printf("Couldn't initialize mqtt_wss\n");
        return 1;
    }
    while (!test_exit) {
        printf("client_handle = %d\n", client_handle(client));
    }
    if (test_exit) {
        mqtt_wss_disconnect(client, 2000);
    }

    mqtt_wss_destroy(client);
    return 0;
}