-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.c
More file actions
344 lines (292 loc) · 10.4 KB
/
client.c
File metadata and controls
344 lines (292 loc) · 10.4 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "client.h"
static ot_err append_op(ot_client* client, ot_op** op) {
ot_doc* doc = client->doc;
char* op_enc = ot_encode(*op);
char* doc_enc = NULL;
if (doc->composed == NULL) {
fprintf(stderr, "[INFO] Appending operation to empty document.\n"
"\tOperation: %s\n",
op_enc);
} else {
doc_enc = ot_encode(doc->composed);
fprintf(stderr, "[INFO] Appending operation to document.\n"
"\tDocument: %s\n"
"\tOperation: %s\n",
doc_enc, op_enc);
}
ot_err err = ot_doc_append(doc, op);
if (err != OT_ERR_NONE) {
if (doc->composed == NULL) {
fprintf(stderr, "[ERROR %d] Appending operation to empty document "
"failed.\n"
"\tOperation: %s\n",
err, op_enc);
} else {
fprintf(stderr, "[ERROR %d] Appending operation to document failed."
"\n"
"\tDocument: %s\n"
"\tOperation: %s\n",
err, doc_enc, op_enc);
free(doc_enc);
}
ot_free_op(*op);
free(op_enc);
return err;
}
if (doc_enc != NULL) {
free(doc_enc);
}
free(op_enc);
return OT_ERR_NONE;
}
static ot_err buffer_op(ot_client* client, ot_op* op) {
if (client->buffer == NULL) {
client->buffer = ot_dup_op(op);
return OT_ERR_NONE;
}
char* buffer_enc = ot_encode(client->buffer);
char* op_enc = ot_encode(op);
fprintf(stderr, "[INFO] Composing buffer with applied operation.\n"
"\tBuffer: %s\n"
"\tApplied Operation: %s\n",
buffer_enc, op_enc);
ot_op* composed = ot_compose(client->buffer, op);
if (composed == NULL) {
fprintf(stderr, "[ERROR %d] Composition of buffer with applied "
"operation failed.\n"
"\tBuffer: %s\n"
"\tApplied Operation: %s\n",
OT_ERR_COMPOSE_FAILED, buffer_enc, op_enc);
free(buffer_enc);
free(op_enc);
return OT_ERR_BUFFER_FAILED;
}
free(buffer_enc);
free(op_enc);
char* composed_enc = ot_encode(composed);
fprintf(stderr, "[INFO] Composition of buffer with applied operation "
"succeeded.\n"
"\tComposed Buffer: %s\n",
composed_enc);
free(composed_enc);
ot_free_op(client->buffer);
client->buffer = NULL;
client->buffer = composed;
return 0;
}
static void send_buffer(ot_client* client, const char* received_hash) {
if (client->buffer == NULL) {
if (client->anticipated != NULL) {
ot_free_op(client->anticipated);
client->anticipated = NULL;
}
return;
}
if (received_hash != NULL) {
memcpy(client->buffer->parent, received_hash, 20);
}
char* enc_buf = ot_encode(client->buffer);
client->send(enc_buf, client->context);
fprintf(stderr, "[INFO] Sent message.\n\tJSON: %s\n", enc_buf);
free(enc_buf);
if (client->anticipated != NULL) {
ot_free_op(client->anticipated);
client->anticipated = NULL;
}
client->anticipated = ot_dup_op(client->buffer);
ot_free_op(client->buffer);
client->buffer = NULL;
client->ack_required = true;
}
static void fire_op_event(ot_client* client, ot_event_type type, ot_op* op) {
client->event(type, op, client->context);
}
// xform_anticipated calculates a new anticipated op by transforming the current
// anticipated op against an incoming op. It outputs an intermediate operation,
// inter, which can be transformed against the current buffer. This function
// will free the received op, provided that no error is returned. The outputted
// intermediate op must be freed by the caller.
static ot_err xform_anticipated(ot_client* client, ot_op* received,
ot_op** inter) {
// We aren't anticipating any acknowledgment, so the buffer can be directly
// transformed against the received op.
if (client->anticipated == NULL) {
*inter = received;
return OT_ERR_NONE;
}
char* received_enc = ot_encode(received);
char* anticipated_enc = ot_encode(client->anticipated);
fprintf(stderr, "[INFO] Transforming received operation against anticipated"
" operation.\n"
"\tReceived Operation: %s\n"
"\tAnticipated Operation: %s\n",
received_enc, anticipated_enc);
ot_xform_pair p = ot_xform(received, client->anticipated);
if (p.op1_prime == NULL || p.op2_prime == NULL) {
fprintf(stderr, "[ERROR %d] Transformation of received operation "
"against anticipated operation failed.\n"
"\tReceived Operation: %s\n"
"\tAnticipated Operation: %s\n",
OT_ERR_XFORM_FAILED, received_enc, anticipated_enc);
free(received_enc);
free(anticipated_enc);
return OT_ERR_XFORM_FAILED;
}
free(received_enc);
free(anticipated_enc);
char* op1_prime_enc = ot_encode(p.op1_prime);
char* op2_prime_enc = ot_encode(p.op2_prime);
fprintf(stderr, "[INFO] Transformation of received operation against "
"anticipated operation succeeded.\n"
"\tReceived Operation': %s\n"
"\tAnticipated Operation': %s\n",
op1_prime_enc, op2_prime_enc);
free(op1_prime_enc);
free(op2_prime_enc);
*inter = p.op1_prime;
ot_free_op(client->anticipated);
client->anticipated = NULL;
client->anticipated = p.op2_prime;
ot_free_op(received);
return OT_ERR_NONE;
}
static ot_err xform_buffer(ot_client* client, ot_op* inter, ot_op** apply) {
// We haven't buffered any new ops, so the intermediate op can be directly
// applied to the document.
if (client->buffer == NULL) {
*apply = inter;
return OT_ERR_NONE;
}
char* buffer_enc = ot_encode(client->buffer);
char* inter_enc = ot_encode(inter);
fprintf(stderr, "[INFO] Transforming buffer against intermediate operation."
"\n"
"\tBuffer: %s\n"
"\tIntermediate Operation: %s\n",
buffer_enc, inter_enc);
ot_xform_pair p = ot_xform(client->buffer, inter);
if (p.op1_prime == NULL || p.op2_prime == NULL) {
fprintf(stderr, "[ERROR %d] Transformation of buffer against "
"intermediate operation failed.\n"
"\tBuffer: %s\n"
"\tIntermediate Operation: %s\n",
OT_ERR_XFORM_FAILED, buffer_enc, inter_enc);
free(buffer_enc);
free(inter_enc);
return OT_ERR_XFORM_FAILED;
}
free(buffer_enc);
free(inter_enc);
char* op1_prime_enc = ot_encode(p.op1_prime);
char* op2_prime_enc = ot_encode(p.op2_prime);
fprintf(stderr, "[INFO] Transformation of buffer against intermediate "
"operation succeeded.\n"
"\tBuffer': %s\n"
"\tIntermediate Operation': %s\n",
op1_prime_enc, op2_prime_enc);
free(op1_prime_enc);
free(op2_prime_enc);
*apply = p.op2_prime;
ot_free_op(client->buffer);
client->buffer = NULL;
ot_free_op(inter);
client->buffer = p.op1_prime;
return OT_ERR_NONE;
}
ot_client* ot_new_client(send_func send, ot_event_func event) {
ot_client* client = malloc(sizeof(ot_client));
client->buffer = NULL;
client->anticipated = NULL;
client->send = send;
client->event = event;
client->doc = NULL;
client->client_id = 0;
client->ack_required = false;
client->context = NULL;
return client;
}
void ot_free_client(ot_client* client) {
ot_doc* doc = client->doc;
if (doc != NULL) {
ot_free_doc(client->doc);
}
if (client->anticipated != NULL) {
ot_free_op(client->anticipated);
}
if (client->buffer != NULL) {
ot_free_op(client->buffer);
}
free(client);
}
void ot_client_open(ot_client* client, ot_doc* doc) {
client->doc = doc;
}
void ot_client_receive(ot_client* client, const char* op) {
fprintf(stderr, "[INFO] Received message.\n\tJSON: %s\n", op);
ot_op* dec = ot_new_op();
ot_err err = ot_decode(dec, op);
if (err != OT_ERR_NONE) {
fprintf(stderr, "[ERROR %d] The decoded operation returned an error.\n"
"\tJSON: %s\n",
err, op);
ot_free_op(dec);
fire_op_event(client, OT_ERROR, NULL);
return;
}
if (dec->client_id == client->client_id) {
char hex[41] = {0};
atohex(hex, dec->hash, 20);
fprintf(stderr, "[INFO] Operation was acknowledged.\n"
"\tHash: %s\n",
hex);
client->ack_required = false;
send_buffer(client, dec->hash);
ot_free_op(dec);
return;
}
fire_op_event(client, OT_OP_INCOMING, NULL);
ot_op* inter;
err = xform_anticipated(client, dec, &inter);
if (err != OT_ERR_NONE) {
ot_free_op(dec);
fire_op_event(client, OT_ERROR, NULL);
return;
}
ot_op* apply;
err = xform_buffer(client, inter, &apply);
if (err != OT_ERR_NONE) {
ot_free_op(inter);
fire_op_event(client, OT_ERROR, NULL);
return;
}
if (client->doc == NULL) {
fputs("[INFO] Creating a new document.\n", stderr);
client->doc = ot_new_doc();
}
append_op(client, &apply);
fire_op_event(client, OT_OP_APPLIED, apply);
}
ot_err ot_client_apply(ot_client* client, ot_op** op) {
char* op_enc = ot_encode(*op);
fprintf(stderr, "[INFO] Editor applying operation.\n"
"\tOperation: %s\n",
op_enc);
free(op_enc);
(*op)->client_id = client->client_id;
if (client->doc == NULL) {
fputs("[INFO] Creating a new document.\n", stderr);
client->doc = ot_new_doc();
}
ot_err append_err = append_op(client, op);
if (append_err != OT_ERR_NONE) {
return append_err;
}
ot_err buf_err = buffer_op(client, *op);
if (buf_err != OT_ERR_NONE) {
return buf_err;
}
if (!client->ack_required) {
send_buffer(client, NULL);
}
return OT_ERR_NONE;
}