summary refs log tree commit diff
path: root/ring.c
blob: 0fec6d02db7ebb583cc7755eaef2c108efc6ad9c (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
/* 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 Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include "bounce.h"

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

void ringAlloc(size_t len) {
	if (len & (len - 1)) errx(EX_CONFIG, "ring length must be power of two");
	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]);
	ring.times[i] = time(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);
		// FIXME: Keep old pointer around for saving when exiting for error.
		consumers.ptr = realloc(
			consumers.ptr, sizeof(*consumers.ptr) * consumers.cap
		);
		if (!consumers.ptr) err(EX_OSERR, "realloc");
	}

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

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

const char *ringPeek(time_t *time, size_t consumer) {
	if (!ringDiff(consumer)) return NULL;
	if (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(time_t *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 FileVersion = 0x0165636E756F70;

static int writeSize(FILE *file, size_t value) {
	return (fwrite(&value, sizeof(value), 1, file) ? 0 : -1);
}
static int writeTime(FILE *file, time_t 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, FileVersion)) 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, 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 version;
	fread(&version, sizeof(version), 1, file);
	if (ferror(file)) err(EX_IOERR, "fread");
	if (feof(file)) return;

	if (version != FileVersion) errx(EX_DATAERR, "unknown file version");
	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 < ring.len; ++i) {
		readTime(file, &ring.times[i]);
	}
	for (size_t i = 0; i < ring.len; ++i) {
		readString(file, &buf, &cap);
		if (feof(file)) break;
		ring.lines[i] = strdup(buf);
		if (!ring.lines[i]) err(EX_OSERR, "strdup");
	}

	free(buf);
}