-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathot.php
More file actions
178 lines (151 loc) · 5.42 KB
/
ot.php
File metadata and controls
178 lines (151 loc) · 5.42 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
<?php
class ot {
const OT_NOP = 0;
const OT_INSERT = 1;
const OT_DELETE = 2;
protected $db;
public function __construct() {
$this->db = new mysqli('127.0.0.1', 'test', 'test', 'test');
}
public function join() {
// return unique identifier
return rand(0, 9999);
}
public function send($index, $client_id, $client_op_id, $op) {
$res = $this->db->query('SELECT * FROM ot WHERE id > ' . $index . ' ORDER BY id');
if($res === false) throw new Exception('Query failed', 500);
while($row = $res->fetch_assoc()) {
// sometimes we will be sent the same client operation
// multiple times, if we see it, we can return immediately
if($row['client_id'] == $client_id && $row['client_op_id'] == $client_op_id) {
return true;
}
$row['op'] = json_decode($row['op']);
$this->xform($row['op'], $op);
// we throw away $row['op'] since server is authoritative
$index = $row['id'];
}
$stmt = $this->db->prepare('INSERT INTO ot SET id=?, client_id=?, client_op_id=?, op=?');
if($stmt === false) throw new Exception('Prepare failed', 500);
$id = $index + 1;
$op_json = json_encode($op);
$ret = $stmt->bind_param("iiis", $id, $client_id, $client_op_id, $op_json);
if($ret === false) throw new Exception('Bind failed', 500);
$ret = $stmt->execute();
if($ret === false) throw new Exception('Execute failed', 500);
return true;
}
public function recv($index) {
// retrieve operations after index
$res = $this->db->query('SELECT * FROM ot WHERE id > ' . $index . ' ORDER BY id');
$rows = array();
while($row = $res->fetch_assoc()) {
$row['id'] = (int)$row['id']; // for js comparison?
$row['op'] = json_decode($row['op']);
$rows[] = $row;
}
return $rows;
}
public function xform($a, $b) {
if($a->type == self::OT_NOP && $b->type == self::OT_NOP) {
// do nothing
} elseif($a->type == self::OT_NOP && $b->type == self::OT_INSERT) {
// do nothing
} elseif($a->type == self::OT_NOP && $b->type == self::OT_DELETE) {
// do nothing
} elseif($a->type == self::OT_INSERT && $b->type == self::OT_NOP) {
// do nothing
} elseif($a->type == self::OT_INSERT && $b->type == self::OT_INSERT) {
if($a->start < $b->start) {
$b->start++;
} elseif($a->start > $b->start) {
$a->start++;
} else {
// tie goes to server
$b->start++;
}
} elseif($a->type == self::OT_INSERT && $b->type == self::OT_DELETE) {
if($a->start < $b->start) {
$b->start++;
} elseif($a->start > $b->start) {
$a->start--;
} else {
// tie goes to insert
$b->start++;
}
} elseif($a->type == self::OT_DELETE && $b->type == self::OT_NOP) {
// do nothing
} elseif($a->type == self::OT_DELETE && $b->type == self::OT_INSERT) {
if($a->start < $b->start) {
$b->start--;
} elseif($a->start > $b->start) {
$a->start++;
} else {
// tie goes to insert
$a->start++;
}
} elseif($a->type == self::OT_DELETE && $b->type == self::OT_DELETE) {
if($a->start < $b->start) {
$b->start--;
} elseif($a->start > $b->start) {
$a->start--;
} else {
$a->type = self::OT_NOP;
unset($a->start);
$b->type = self::OT_NOP;
unset($b->start);
}
} else {
throw new Exception('cannot xform ' . $a->type . ', ' . $b->type, 500);
}
}
}
try {
$ot = new ot();
// dispatch
if(!isset($_REQUEST['action'])) {
throw new Exception('Action required.', 400);
}
switch($_REQUEST['action']) {
case 'join':
$response = $ot->join();
break;
case 'send':
if(!isset($_REQUEST['index'])) { throw new Exception('Index required.', 400); }
if(!is_numeric($_REQUEST['index'])) { throw new Exception('Numeric index required.', 400); }
if(!is_finite($_REQUEST['index'])) { throw new Exception('Finite index required.', 400); }
$index = $_REQUEST['index'];
if(!isset($_REQUEST['client_id'])) { throw new Exception('Client ID required.', 400); }
if(!is_numeric($_REQUEST['client_id'])) { throw new Exception('Numeric Client ID required.', 400); }
if(!is_finite($_REQUEST['client_id'])) { throw new Exception('Finite Client ID required.', 400); }
$client_id = $_REQUEST['client_id'];
if(!isset($_REQUEST['client_op_id'])) { throw new Exception('Client OP ID required.', 400); }
if(!is_numeric($_REQUEST['client_op_id'])) { throw new Exception('Numeric Client OP ID required.', 400); }
if(!is_finite($_REQUEST['client_op_id'])) { throw new Exception('Finite Client OP ID required.', 400); }
$client_op_id = $_REQUEST['client_op_id'];
if(!isset($_REQUEST['op'])) { throw new Exception('Operation required.', 400); }
$op = json_decode($_REQUEST['op']);
if(is_null($op)) { throw new Exception('OP required.', 400); }
$response = $ot->send($index, $client_id, $client_op_id, $op);
break;
case 'recv':
if(!isset($_REQUEST['index'])) { throw new Exception('Index required.', 400); }
if(!is_numeric($_REQUEST['index'])) { throw new Exception('Numeric index required.', 400); }
if(!is_finite($_REQUEST['index'])) { throw new Exception('Finite index required.', 400); }
$response = $ot->recv($_REQUEST['index']);
break;
default:
throw new Exception('Action not found.', 400);
}
$result = array();
$result['type'] = 'response';
$result['data'] = $response;
} catch(Exception $e) {
header('HTTP/1.0 ' . $e->getCode());
$result = array();
$result['type'] = 'exception';
$result['message'] = $e->getMessage();
}
header('Content-type: application/json');
echo json_encode($result);
?>