-
Notifications
You must be signed in to change notification settings - Fork 21
/
proxy_cluster.c
110 lines (97 loc) · 2.59 KB
/
proxy_cluster.c
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
#include "proxy_cluster.h"
#include "ulog/ulog.h"
#include "queue/queue.h"
#include "tokens.h"
#include "wsocket/utils/ntripcli.h"
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
struct ntrip_proxy {
char token[64]; // token: user:passwd@ipaddress
struct ntripcli str;
bool open; // if str is open
TAILQ_ENTRY(ntrip_proxy) entries;
};
// proxy list head
static TAILQ_HEAD(, ntrip_proxy) m_proxy_head = TAILQ_HEAD_INITIALIZER(m_proxy_head);
int ntripproxy_read(struct ntrip_proxy *proxy, void *buf, size_t size)
{
if (!proxy->open) {
return -1;
}
return ntripcli_read(&proxy->str, buf, size);
}
int ntripproxy_write(struct ntrip_proxy *proxy, const void *data, size_t size)
{
if (!proxy->open) {
return -1;
}
return ntripcli_write(&proxy->str, data, size);
}
const char * ntripproxy_get_path(struct ntrip_proxy *proxy)
{
if (!proxy->open) {
return NULL;
}
return proxy->str.path_cache;
}
int proxycluster_init()
{
if (TAILQ_EMPTY(&m_proxy_head)) {
return 0;
} else {
LOG_ERROR("%() already init.", __func__);
return -1;
}
}
int proxycluster_capacity_count()
{
return tokens_src_count();
}
int proxycluster_using_count()
{
return tokens_src_count_used();
}
struct ntrip_proxy * proxycluster_take_proxy(int port, const char *mnt)
{
char token[64];
if (tokens_src_take_path(token, sizeof(token)) == NULL) {
LOG_ERROR("cannot get usable source tokens");
return NULL;
}
struct ntrip_proxy *proxy = malloc(sizeof(*proxy));
if (proxy == NULL) {
LOG_ERROR("malloc failed, %s", strerror(errno));
tokens_src_release_path(token);
return NULL;
}
snprintf(proxy->token, sizeof(proxy->token), token);
ntripcli_init(&proxy->str, 5, 15, -1);
proxy->open = false;
char path[256];
snprintf(path, sizeof(path), "%s:%d/%s", token, port, mnt);
if (ntripcli_open_path(&proxy->str, path) != 0) {
LOG_ERROR("stropen failed, path='%s'", path);
tokens_src_release_path(token);
return NULL;
}
proxy->open = true;
TAILQ_INSERT_TAIL(&m_proxy_head, proxy, entries);
return proxy;
}
void proxycluster_release_proxy(struct ntrip_proxy *proxy)
{
TAILQ_REMOVE(&m_proxy_head, proxy, entries);
tokens_src_release_path(proxy->token);
ntripcli_close(&proxy->str);
proxy->open = false;
free(proxy);
}
void proxycluster_cleanup()
{
struct ntrip_proxy *pry, *tmp;
TAILQ_FOREACH_SAFE(pry, &m_proxy_head, entries, tmp) {
proxycluster_release_proxy(pry);
}
}