This repository was archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.php
More file actions
280 lines (246 loc) · 9.25 KB
/
websocket.php
File metadata and controls
280 lines (246 loc) · 9.25 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
<?php
namespace App\Services;
use PDO;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServer;
use Ratchet\MessageComponentInterface;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
require './vendor/autoload.php';
header('Content-Type: application/json');
//Generic database getter function
//
// $variable = the name of the database variable
// returns = the value in the database
function getDatabaseValue($variable)
{
try {
$connection = new PDO("sqlite:RemotePhysLab.db");
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $connection->query(sprintf("SELECT Values_ from data WHERE Variables=='%s';", $variable));
$query_result = $query->fetch();
$value = $query_result[0];
$connection = null;
return $value;
} catch (PDOException $exception) {
$connection = null;
return "Error: " . $exception->getMessage();
}
}
//Generic database setter function
//
// $variable = the name of the database variable
// $value = the value to set in the database
function setDatabaseValue($variable, $value)
{
try {
$connection = new PDO("sqlite:RemotePhysLab.db");
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $connection->prepare("UPDATE data SET Values_=:value WHERE Variables==:variable;");
$statement->bindParam(':value', $value);
$statement->bindParam(':variable', $variable);
$statement->execute();
$statement = null;
$connection = null;
} catch (PDOException $exception) {
$statement = null;
$connection = null;
return "Error: " . $exception->getMessage();
}
}
/////////// DATABASE GETTER AND SETTERS ///////////
function setDefVoltage($voltage)
{
setDatabaseValue("deflectingVoltage", $voltage);
}
function getDefVoltage()
{
return getDatabaseValue("deflectingVoltage");
}
function setAccVoltage($voltage)
{
setDatabaseValue("acceleratingVoltage", $voltage);
}
function getAccVoltage()
{
return getDatabaseValue("acceleratingVoltage");
}
function setCurrentAmperage($amperage)
{
setDatabaseValue("magnetizingCurrent", $amperage);
}
function getCurrentAmperage()
{
return getDatabaseValue("magnetizingCurrent");
}
function setMagneticArc($arc)
{
setDatabaseValue("magneticArc", $arc);
}
function getMagneticArc()
{
return getDatabaseValue("magneticArc");
}
function setDefVoltagePolarity($polarity)
{
setDatabaseValue("deflectingPolarity", $polarity);
}
function getDefVoltagePolarity()
{
return getDatabaseValue("deflectingPolarity");
}
//////////WEBSOCKET SERVER CLASS//////////
class RatchetServer implements MessageComponentInterface
{
private $controlDuration = 30000; //Milliseconds before control is returned
private $queue = []; //Clients who have requested access
private $inControl; //The client in control of the device
private $clients; //All connected clients
//Constructor
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
//Forwards a message from one client to all other connected clients
private function forwardMessage($from, $msg)
{
foreach ($this->clients as $client) {
if ($from !== $client) {
//Send message to all other connected clients
$client->send($msg);
}
}
}
//Sends the message to all connected computers
private function sendToAll($msg)
{
foreach ($this->clients as $client) {
$client->send($msg);
}
}
//Updates how many computers are before each one in the queue
private function updateComputersBefore()
{
$iii = 0;
foreach ($this->queue as $user) {
$user->send(json_encode(array('computersBefore' => ($iii++))));
}
}
//Updates Computers Connected, Computers Waiting, and the Controlling id on all computers
private function updateConnectionInfo()
{
$array = array('computersConnected' => count($this->clients),
'computersWaiting' => count($this->queue), 'controllingId' => $this->inControl->resourceId);
if ($this->inControl == null) {
$array['setControlDurationSliderDisabled'] = false;
} else {
$array['setControlDurationSliderDisabled'] = true;
}
$this->sendToAll(json_encode($array));
}
//Removes control from the current user and gives it to the next user in the queue
private function grantAccessToNext()
{
//Remove new client from queue and replace inControl with the new client
$this->inControl = array_shift($this->queue);
//Remove control and update computer info
$this->sendToAll(json_encode(array('setControlsDisabled' => true)));
$this->updateComputersBefore();
$this->updateConnectionInfo();
//Allow control to new client
if ($this->inControl != null) {
$this->inControl->send(json_encode(array('setControlsDisabled' => false)));
//Limit control duration only if more clients are in the queue
if (count($this->queue) > 0) {
$this->inControl->send(json_encode(array('setControlDuration' => $this->controlDuration)));
}
}
}
/////////////WEBSOCKET FUNCTIONS/////////////
public function onOpen(ConnectionInterface $connection)
{
$this->clients->attach($connection);
echo "New connection! ({$connection->resourceId})\n";
$json = array('defVoltage' => getDefVoltage(),
'accVoltage' => getAccVoltage(),
'currentAmperage' => getCurrentAmperage(),
'defVoltagePolarity' => getDefVoltagePolarity(),
'magneticArc' => getMagneticArc(),
'controlDuration' => $this->controlDuration,
'ownId' => $connection->resourceId);
$connection->send(json_encode($json));
$this->updateConnectionInfo();
}
public function onClose(ConnectionInterface $connection)
{
$this->clients->detach($connection); //Remove user from clients list
unset($this->queue[$connection->resourceId]); //Remove user from access queue
echo "Connection {$connection->resourceId} has disconnected\n";
//Update person in control of the device
if ($this->inControl == $connection) {
$this->grantAccessToNext();
}
//Update computer info
$this->updateComputersBefore();
$this->updateConnectionInfo();
}
public function onError(ConnectionInterface $connection, \Exception $exception)
{
echo $exception;
$connection->close();
}
public function onMessage(ConnectionInterface $from, $msg)
{
foreach (json_decode($msg, true) as $key => $value) {
//Update appropriate values in database if in control
if ($from == $this->inControl) {
switch ($key) {
case 'defVoltage':
setDefVoltage($value);
$this->forwardMessage($from, $msg);
break;
case 'accVoltage':
setAccVoltage($value);
$this->forwardMessage($from, $msg);
break;
case 'currentAmperage':
setCurrentAmperage($value);
$this->forwardMessage($from, $msg);
break;
case 'defVoltagePolarity':
setDefVoltagePolarity($value);
$this->forwardMessage($from, $msg);
break;
case 'magneticArc':
setMagneticArc($value);
$this->forwardMessage($from, $msg);
break;
case 'returnControl':
$this->grantAccessToNext();
break;
default:
$from->send(json_encode(array('error' => "Error: Unknown function")));
}
} else if ($key == 'requestAccess') {
$this->queue[$from->resourceId] = $from;
//If no one is currently controlling the device, go ahead and grant access
if ($this->inControl == null) {
$this->grantAccessToNext();
} else if (count($this->queue) == 1) {
//Only send duration setter if this is first client added to queue
$this->inControl->send(json_encode(array('setControlDuration' => $this->controlDuration)));
}
//Update info on all other computers
$this->updateComputersBefore();
$this->updateConnectionInfo();
} else if ($key == 'setControlDuration' && $this->inControl == null) { //Don't need to check queue
$this->controlDuration = $value;
$this->forwardMessage($from,json_encode(array('controlDuration' => $this->controlDuration)));
}
}
}
}
////////////MAIN SCRIPT TO INITIATE SERVER////////////
$server = IoServer::factory(new HttpServer(new WsServer(new RatchetServer())), 8000);
$server->run();
echo "WebSocket listening on port 8000";