aboutsummaryrefslogtreecommitdiff
path: root/alarm-tools/alarmd_reference_client.c
blob: d200e04c42f2dbfe171b984cb4da240906d0ecb8 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

#include "alarmd_proto.h"

int8_t read_string(int sock, char (*buffer)[128])
{
	uint8_t length = 0;
	if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) {
		return -1;
	}
	if (length >= 128) {
		return -1;
	}
	if (recv(sock, buffer, length, 0) < 0) {
		return -1;
	}
	(*buffer)[127] = '\0';
	return length;
}

int alarmd_dump_alarms(int sock)
{
	ssize_t i = 0;
	ssize_t nread = 0;
	size_t length = 0;
	uint32_t count = 0;
	uint32_t packet_type = 0;
	uint8_t is_raised = 0;
	char buffer[128];

	packet_type = htonl(ALARMD_PACKET_TYPE_QUERY);

	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	nread = recv(sock, &count, sizeof(count), 0);
	if (nread < 0) {
		perror("recv");
		return 1;
	} else if (nread < sizeof(count)) {
		fprintf(stderr, "Alarm count too small\n");
		return 1;
	}

	count = ntohl(count);

	printf("Alarms\n"
	       "------\n");

	for (i = 0; i < count; i++) {
		if ((length = read_string(sock, &buffer)) < 0) {
			perror("recv");
			break;
		}
		if (recv(sock, &is_raised, sizeof(is_raised), 0) != sizeof(is_raised)) {
			perror("recv");
			break;
		}
		printf("[%s] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", buffer);
	}

	if (i < count) {
		fprintf(stderr, "Not all alarms received, output must be missing some\n");
		return 1;
	}

	printf("\n");

	return 0;
}

int alarmd_register(int sock, char *desc, char (*uuid)[16])
{
	uint8_t length = 0;
	uint32_t packet_type = 0;

	packet_type = htonl(ALARMD_PACKET_TYPE_REGISTER);
	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	length = strlen(desc);
	if (send(sock, &length, sizeof(length), 0) != sizeof(length)) {
		perror("send");
		return 1;
	}

	if (send(sock, desc, strlen(desc), 0) != strlen(desc)) {
		perror("send");
		return 1;
	}
	if (recv(sock, uuid, 16, 0) != 16) {
		perror("recv");
		return 1;
	}
	return 0;
}

int alarmd_deregister(int sock, char uuid[16])
{
	uint32_t packet_type = 0;

	packet_type = htonl(ALARMD_PACKET_TYPE_DEREGISTER);
	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	/* FIXME 16 magic */
	if (send(sock, uuid, 16, 0) != 16) {
		perror("send");
		return 1;
	}
	return 0;
}

int alarmd_raise(int sock, char uuid[16])
{
	uint32_t packet_type = 0;

	packet_type = htonl(ALARMD_PACKET_TYPE_RAISE);
	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	/* FIXME 16 magic */
	if (send(sock, uuid, 16, 0) != 16) {
		perror("send");
		return 1;
	}
	return 0;
}

int alarmd_clear(int sock, char uuid[16])
{
	uint32_t packet_type = 0;

	packet_type = htonl(ALARMD_PACKET_TYPE_CLEAR);
	if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) {
		perror("send");
		return 1;
	}

	/* FIXME 16 magic */
	if (send(sock, uuid, 16, 0) != 16) {
		perror("send");
		return 1;
	}
	return 0;
}

int main(void)
{
	int sock = 0;
	char buffer[128];
	struct addrinfo hints, *s_info, *p;

	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	/* FIXME spec custom hostname on cmd line */
	if (getaddrinfo("localhost", ALARMD_PORT, &hints, &s_info) != 0) {
		perror("getaddrinfo");
		return 1;
	}

	for (p = s_info; p != NULL; p = p->ai_next) {
		if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
			perror("socket");
			continue;
		}
		if (connect(sock, p->ai_addr, p->ai_addrlen) < 0) {
			close(sock);
			perror("connect");
			continue;
		}
		break;
	}

	if (!p) {
		fprintf(stderr, "Connection to server failed\n");
		return 1;
	}


	/**/
	char *name = "Some test alarm";
	char uuid[16];
	/**/

	freeaddrinfo(s_info);

	printf(">>>Register\n");
	int i, j;
	for (i = 0; i < 10000; i++) {
		for (j = 0; j < 100; j++) {
			alarmd_register(sock, name, &uuid);
		}
		fprintf(stderr, "%d", i);
	}
	
//	alarmd_dump_alarms(sock);
//	printf(">>>Raise\n");
//	alarmd_raise(sock, uuid);
//	alarmd_dump_alarms(sock);
//	printf(">>>Clear\n");
//	alarmd_clear(sock, uuid);
//	alarmd_dump_alarms(sock);
//	printf(">>>Deregister\n");
//	alarmd_deregister(sock, uuid);
	close(sock);
}