about summary refs log tree commit diff
path: root/ring.c
blob: 254c2850c4207c6e15f7eb97d5e712544a9a127a (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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sysexits.h>

#include "bounce.h"

static struct {
	size_t len;
	char **lines;
	struct timeval *times;
} ring;

void ringAlloc(size_t len) {
	if (len & (len - 1)) {
		errx(EX_CONFIG, "ring length must be power of two: %zu", len);
	}
	ring.lines = calloc(len, sizeof(*ring.lines));
	if (!ring.lines) err(EX_OSERR, "calloc");
	ring.times = calloc(len, sizeof(*ring.times));
	if (!ring.times) err(EX_OSERR, "calloc");
	ring.len = len;
}

size_t producer;

void ringProduce(const char *line) {
	size_t i = producer++ & (ring.len - 1);
	if (ring.lines[i]) free(ring.lines[i]);
	gettimeofday(&ring.times[i], NULL);
	ring.lines[i] = strdup(line);
	if (!ring.lines[i]) err(EX_OSERR, "strdup");
}

struct Consumer {
	char *name;
	size_t pos;
};

static struct {
	struct Consumer *ptr;
	size_t cap, len;
} consumers;

size_t ringConsumer(const char *name) {
	for (size_t i = 0; i < consumers.len; ++i) {
		if (!strcmp(consumers.ptr[i].name, name)) return i;
	}

	if (consumers.len == consumers.cap) {
		consumers.cap = (consumers.cap ? consumers.cap * 2 : 8);
		void *ptr = realloc(
			consumers.ptr, sizeof(*consumers.ptr) * consumers.cap
		);
		if (!ptr) err(EX_OSERR, "realloc");
		consumers.ptr = ptr;
	}

	struct Consumer *consumer = &consumers.ptr[consumers.len];
	consumer->pos = 0;
	consumer->name = strdup(name);
	if (!consumer->name) err(EX_OSERR, "strdup");
	return consumers.len++;
}

void ringSet(size_t consumer, size_t pos) {
	assert(consumer < consumers.len);
	if (pos <= producer) consumers.ptr[consumer].pos = pos;
}

size_t ringPos(size_t consumer) {
	assert(consumer < consumers.len);
	return consumers.ptr[consumer].pos;
}

size_t ringDiff(size_t consumer) {
	assert(consumer < consumers.len);
	return producer - consumers.ptr[consumer].pos;
}

const char *ringPeek(struct timeval *time, size_t consumer) {
	if (!ringDiff(consumer)) return NULL;
	if (ringDiff(consumer) > ring.len) {
		warnx(
			"consumer %s dropped %zu messages",
			consumers.ptr[consumer].name, ringDiff(consumer) - ring.len
		);
		consumers.ptr[consumer].pos = producer - ring.len;
	}
	size_t i = consumers.ptr[consumer].pos & (ring.len - 1);
	if (time) *time = ring.times[i];
	assert(ring.lines[i]);
	return ring.lines[i];
}

const char *ringConsume(struct timeval *time, size_t consumer) {
	const char *line = ringPeek(time, consumer);
	if (line) consumers.ptr[consumer].pos++;
	return line;
}

void ringInfo(void) {
	fprintf(stderr, "producer: %zu\n", producer);
	for (size_t i = 0; i < consumers.len; ++i) {
		fprintf(
			stderr, "consumer %s: %zu (%zu)\n",
			consumers.ptr[i].name,
			consumers.ptr[i].pos, producer - consumers.ptr[i].pos
		);
	}
}

static const size_t Signatures[] = {
	0x0165636E756F70, // no ring size
	0x0265636E756F70, // time_t only
	0x0365636E756F70,
};

static size_t signatureVersion(size_t signature) {
	for (size_t i = 0; i < ARRAY_LEN(Signatures); ++i) {
		if (signature == Signatures[i]) return i;
	}
	errx(EX_DATAERR, "unknown file signature %zX", signature);
}

static int writeSize(FILE *file, size_t value) {
	return (fwrite(&value, sizeof(value), 1, file) ? 0 : -1);
}
static int writeTime(FILE *file, struct timeval time) {
	return (fwrite(&time, sizeof(time), 1, file) ? 0 : -1);
}
static int writeString(FILE *file, const char *str) {
	return (fwrite(str, strlen(str) + 1, 1, file) ? 0 : -1);
}

int ringSave(FILE *file) {
	if (writeSize(file, Signatures[2])) return -1;
	if (writeSize(file, ring.len)) return -1;
	if (writeSize(file, producer)) return -1;
	if (writeSize(file, consumers.len)) return -1;
	for (size_t i = 0; i < consumers.len; ++i) {
		if (writeString(file, consumers.ptr[i].name)) return -1;
		if (writeSize(file, consumers.ptr[i].pos)) return -1;
	}
	for (size_t i = 0; i < ring.len; ++i) {
		if (writeTime(file, ring.times[i])) return -1;
	}
	for (size_t i = 0; i < ring.len; ++i) {
		if (!ring.lines[i]) break;
		if (writeString(file, ring.lines[i])) return -1;
	}
	return 0;
}

static void readSize(FILE *file, size_t *value) {
	fread(value, sizeof(*value), 1, file);
	if (ferror(file)) err(EX_IOERR, "fread");
	if (feof(file)) err(EX_DATAERR, "unexpected eof");
}
static void readTime(FILE *file, struct timeval *time) {
	fread(time, sizeof(*time), 1, file);
	if (ferror(file)) err(EX_IOERR, "fread");
	if (feof(file)) err(EX_DATAERR, "unexpected eof");
}
static void readTimeT(FILE *file, time_t *time) {
	fread(time, sizeof(*time), 1, file);
	if (ferror(file)) err(EX_IOERR, "fread");
	if (feof(file)) err(EX_DATAERR, "unexpected eof");
}
static void readString(FILE *file, char **buf, size_t *cap) {
	ssize_t len = getdelim(buf, cap, '\0', file);
	if (len < 0 && !feof(file)) err(EX_IOERR, "getdelim");
}

void ringLoad(FILE *file) {
	size_t signature;
	fread(&signature, sizeof(signature), 1, file);
	if (ferror(file)) err(EX_IOERR, "fread");
	if (feof(file)) return;
	size_t version = signatureVersion(signature);

	size_t saveLen = 4096;
	if (version > 0) readSize(file, &saveLen);
	if (saveLen > ring.len) {
		errx(EX_DATAERR, "cannot load save with larger ring");
	}

	readSize(file, &producer);

	char *buf = NULL;
	size_t cap = 0;

	size_t len;
	readSize(file, &len);
	for (size_t i = 0; i < len; ++i) {
		readString(file, &buf, &cap);
		size_t consumer = ringConsumer(buf);
		readSize(file, &consumers.ptr[consumer].pos);
	}

	for (size_t i = 0; i < saveLen; ++i) {
		if (version < 2) {
			readTimeT(file, &ring.times[i].tv_sec);
		} else {
			readTime(file, &ring.times[i]);
		}
	}
	for (size_t i = 0; i < saveLen; ++i) {
		readString(file, &buf, &cap);
		if (feof(file)) break;
		ring.lines[i] = strdup(buf);
		if (!ring.lines[i]) err(EX_OSERR, "strdup");
	}
	free(buf);

	if (ring.len > saveLen) {
		producer %= saveLen;
		for (size_t i = 0; i < consumers.len; ++i) {
			struct Consumer *consumer = &consumers.ptr[i];
			consumer->pos %= saveLen;
			if (consumer->pos > producer) consumer->pos = 0;
		}
	}
}