-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingpong.php
More file actions
424 lines (372 loc) · 16.4 KB
/
Copy pathpingpong.php
File metadata and controls
424 lines (372 loc) · 16.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/* pingpong.php
run it from the commandline with php pingpong.php. It will register itself with a dcerouter
using the defined Orbiter ID. Make sure the Orbiter ID exists. After registering, it receives
all messages for the Orbiter and displays these messages on the screen.
Copyright 2010, Peer Oliver Schmidt
Licence: GPL v3
*/
global $link, $mediaLink, $currentScreen, $currentEntertainArea, $UI, $SKIN, $debug, $outputFile;
// The following variables have to amended to the local
// installation.
$possyDeviceFromID = 214; // an Orbiter device id, which only exists to receive messages.
$possyRoomForOrbiter = "20"; // The room the orbiter should be send to.
$possyEAForOrbiter = "12"; // The entertainment area that we take care off.
$runonce = 0;
$debug = false;
$outputFile = "";
$cmdline=getopt("d:r:e:h",array("help","device:","room:","ea:","debug","runonce","output:"));
foreach($cmdline as $key => $option) {
switch($key) {
case "output":
$outputFile = $option;
break;
case "debug":
$debug = true;
break;
case "d":
case "device":
$possyDeviceFromID = $option;
break;
case "r":
case "room":
$possyRoomForOrbiter = $option;
break;
case "e":
case "ea":
$possyEAForOrbiter = $option;
break;
case "runonce":
$runonce=1;
break;
case "h":
case "help":
default:
print "-d device-ID of Orbiter device\n-r room number to start in\n-e Entertainment Area to start in\n";
die();
}
}
if ($debug) print "Starting";
connectdb();
flush();
// commStart, commEnd and myMessageSend are in libMessageSend.php
$socketCommand = commStart("dcerouter",3450,$possyDeviceFromID,"COMMAND",$debug);
$socketEvent = commStart("dcerouter",3450,$possyDeviceFromID,"EVENT",$debug);
// Register Message Receiptor
$destination = -1000; // DCE Router Device
$messageType = 8; // MessageType Register Interceptor
$paramType = 1; //
$paramValue = 1;
//$paramType = 1; // PARM_FROM=1, PARM_TO=2, PARM_TEMPLATE=3, PARM_CATEGORY=4, PARM_MESSAGE_TYPE=5, PARM_MESSAGE_ID=6
//$paramValue = 10 ; // Media Plug-Ins (8=MDs, 11=MediaPlayer Plugins,13=MediaPlayers,64=VirtualMediaDriector)
// myMessageSend($socket,$deviceFromID,$deviceToID,$messageType = 1,$messageID,$parameter1ID=0,$parameter1Content="",$parameter2ID=0,$parameter2Content="") {
$parameterMessageType = 5;
$messageTypeRegister = 8;
$messageTypeEvent = 2;
$messageTypeCommand = 1;
$messageTypeClearCriteria = 13;
$criteriaTypeFrom = 1;
$criteriaTypeTo = 2;
$criteriaTypeCategory = 4;
$criteriaTypeMessageID = 6;
$deviceMediaPlugin = 10;
$deviceDCERouter = 1;
$deviceOrbiterPlugin = 9;
$messageSetNowPlaying = 242;
$messageOrbiterRegistered = 255;
// Clear all registrations - does NOT return anything on the socketEvent
myMessageSend($socketEvent,$possyDeviceFromID,$destination,$messageTypeClearCriteria,0,0,"");
// Setup regstration to receive all Events from DCERouter (device 1)
// myMessageSend($socketEvent,$possyDeviceFromID,$destination,$messageTypeRegister,0,5,$messageTypeEvent,$criteriaTypeFrom,1);
// Setup regstration to receive all Events from DCERouter (device 1)
// myMessageSend($socketEvent,$possyDeviceFromID,$destination,$messageTypeRegister,0,$parameterMessageType,$messageTypeCommand,$criteriaTypeMessageID,$messageSetNowPlaying);
// Register an interceptor to receive a message whenever playback starts.
/* myMessageSend($socketEvent,$possyDeviceFromID,$destination,$messageTypeRegister,0,$parameterMessageType,$messageTypeCommand,$criteriaTypeMessageID,242);
$input = socket_read($socketEvent, 1024);
print "input $input\n";
*/
// For now, we are happy with PLAIN_TEXT messages
if ($debug) print "PLAIN_TEXT\n";
flush();
socket_write($socketEvent, "PLAIN_TEXT\n",strlen("PLAIN_TEXT\n"));
if ($debug) print "socket_write completed";
// die();
$input = socket_read($socketEvent, 1024);
if ($debug) print "second return: $input \n";
// Tell Orbiter plugin we are alive - does NOT return anything on the socketEvent
$messageSendParameter = array(array(8,"1")); // Register ON
array_push($messageSendParameter, array(17,"")); // Current User
array_push($messageSendParameter, array(45,$possyEAForOrbiter)); // Entertainment Area
array_push($messageSendParameter, array(57,$possyRoomForOrbiter)); // Current Room
myMessageSend($socketEvent,$possyDeviceFromID,$deviceOrbiterPlugin,$messageTypeCommand,$messageOrbiterRegistered,$messageSendParameter);
// After registering this devie with Orbiter-plugin, we automatically receive messages destined for an Orbiter. No need to intercept
// any more messages per se
while ($input = socket_read($socketCommand, 1024, PHP_NORMAL_READ)) {
$arrayReturn = explode(" ",$input);
$length = intval($arrayReturn[1]) + 1;
if ($debug) print date('Y-m-d H:i') . " - Reading $length bytes\n";
$input = readSocketForDataLength($socketCommand,$length);
while ($input != "") {
if ($debug) print $input . "\n";
$input = digestMessage($input);
if ($input != "") {
if ($debug) print "--------\n";
}
}
socket_write($socketCommand,"OK\n",3);
if ($length == 1) {
die();
}
if ($runonce == 1) {
die();
}
}
// myMessageSend($socket,$possyDeviceFromID,10,1,43,13,'"' . $filePath . '"', 45, $currentEntertainArea)
commEnd($socketEvent);
commEnd($socketCommand);
function digestMessage($input) {
// Return: 4 214 1 741 9 "Shutting down" 137 "" 159 "53" 163 "" 181 "" 182 "0" 183 "1" 251 "0" 252 "0" 253 "0"
// From / To / MsgType / MsgID (if MsgType=1, MsgID=PK_Command / lots of pairs of parameter and parameter value
global $link, $debug;
$counter = 0;
$arrayReturn = explode(" ",$input);
$from = $arrayReturn[0];
$counter += strlen($from)+1;
$fromDescription = getMyValue($link,"SELECT Description FROM Device WHERE PK_Device = " . $from . ";");
$to = $arrayReturn[1];
$counter += strlen($from)+1;
$toDescription = getMyValue($link,"SELECT Description FROM Device WHERE PK_Device = " . $to . ";");
$msgType = $arrayReturn[2];
$counter += strlen($msgType)+1;
$msgId = $arrayReturn[3];
$counter += strlen($msgId)+1;
$msgIdDescription = getMyValue($link,"SELECT Description FROM Command WHERE PK_Command = " . $msgId . ";");
if ($debug) print "From $fromDescription($from) - To $toDescription($to) - MsgType $msgType - MsgID $msgIdDescription($msgId)\n";
$commandArray = array($msgId, $msgIdDescription);
for($i=4; $i < count($arrayReturn)-1 ; $i = $i+1) {
$parameter = $arrayReturn[$i];
$counter += strlen($parameter) + 1;
if (strlen($parameter) == 0) {
$i++;
$parameter = $arrayReturn[$i];
$counter += strlen($parameter);
}
if ($parameter == '"&"') {
// We have another message coming in. Sometimes, multiple commands
// are send in one blob.
// TODO: Basically the input string needs to be cut from here,
// and this function needs to be called with the remaining
// string.
$input = substr($input,$counter+2);
while (substr($input,0,1) == " ") {
$input = substr($input,1);
}
outputCommand($commandArray);
return $input;
}
if ((strlen($parameter) == 0) or ((count($arrayReturn)-1) == $i)) {
outputCommand($commandArray);
return "";
}
$parameterDesc = getMyValue($link,"SELECT Description FROM CommandParameter Where PK_CommandParameter = " . $parameter . ";");
if ($debug) print "CommandParameter: $parameterDesc($parameter) - ";
$i++;
$value = $arrayReturn[$i];
// Let's see if we have a regular parameter / value pair.
if (substr($value,0,1) == '"') {
while (substr($arrayReturn[$i],-1,1) != '"') {
$i++;
$additionalValue = $arrayReturn[$i];
$value .= " " . $additionalValue;
if ($i > 60) {
die();
}
}
}
if ($debug) print "$value\n";
$counter += strlen($value)+1;
$cleanValue=substr($value,1);
$cleanValue=substr($cleanValue,0,strlen($cleanValue)-1);
$parameterArray=array('"' . $parameter . '"' => $cleanValue);
$commandArray = array_merge($commandArray,$parameterArray);
}
outputCommand($commandArray);
if ($debug) print "\n";
/* if (strlen($input) > 500) {
$input = "only the first 200byte: " . substr($input,0,500);
}
*/ if ($debug) print "Return: --EOL--\n";
return "";
}
function outputCommand($commandArray) {
// This function displays the last received command
// in a more usable fashion.
// Now we have the MessageID (command), the current $parameter
// as well as the full $value for the parameter.
// We should now start doing special things based upon
// the commands and parameters received.
//
// Now Playing is an example of what to do. In theory, one could
// fill out a new array with the data of multiple parameters
// to send out the data in one batch. If one were to do that,
// special care must be taken for commands grouped in batches.
// TODO: Take care of more commands.
//
global $debug, $outputFile;
if ($debug) print_r($commandArray);
if ($commandArray[0] == 242) { // Now Playing
createOutput(242); // Open output
$artistAlbum=explode("\n",$commandArray['"5"'],2);
if ($commandArray['"29"']=='4') { // Audio
createOutput("Artist",$artistAlbum[0]);
createOutput("Album",$artistAlbum[1]);
createOutput("Title",$commandArray['"9"']);
}
createOutput(); // Close output
}
}
function createOutput($header = false,$details = false) {
// Formatting of the output.
// We use this function to either output the ordered data
// to the screen, or to a file atm.
//
global $debug,$outputFile,$outputFileHandle;
if ($outputFile == "") {
if ($header and $details) {
print $header . " " . $details . "\n";
}
if ( ! $header and ! $details ) {
flush();
}
} else {
if ( ! $header ) {
fclose($outputFileHandle);
} else if ( ! $details ) {
$outputFileHandle = fopen($outputFile,'w');
} else {
fwrite($outputFileHandle,$header . " " . $details . "\n");
}
}
}
function readSocketForDataLength ($socket, $len)
{
// This function was copied from the php.net site
$offset = 0;
$socketData = '';
while ($offset < $len) {
if (($data = @socket_read ($socket, $len-$offset, PHP_BINARY_READ)) === false) {
return false;
}
$dataLen = strlen ($data);
$offset += $dataLen;
$socketData .= $data;
if ($dataLen == 0) { break; }
}
return $socketData;
}
// included from libMessageSend.php
function getSocket($server, $port) {
// Get a socket connection to the router
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ( ! $socket) {
print "Error creating socket";
}
if (! socket_connect($socket,$server,$port)) {
print "Error connecting to server";
}
return $socket;
}
function sendSocket($socket, $message) {
global $debug;
// Send message to the socket
if ($debug) print $message;
$result = socket_write($socket, $message, strlen($message));
if ( ! $result) {
$errCode = socket_last_error();
socket_clear_error();
print "Error writing to socket: " . $errCode . " - " . socket_strerror($errCode) . "\n";
}
return $result;
}
function commStart($server = "localhost",$port = 3450, $deviceFromID, $command = "EVENT") {
global $debug;
$socket = getSocket($server, $port);
$result = sendSocket($socket,$command . " " . $deviceFromID . "\n");
$result = socket_read($socket,1024, PHP_NORMAL_READ);
if ($debug) {
print "Result:: " . htmlspecialchars($result) . " \n";
flush();
}
return $socket;
}
function myMessageSend($socket,$deviceFromID,$deviceToID,$messageType = 1,$messageID,$parameter1ID=0,$parameter1Content="",$parameter2ID=0,$parameter2Content="") {
// Careful:
// parameter1 und parameter2 content must include " if it is a long string!
$messageToSend = $deviceFromID . " " . $deviceToID . " " . $messageType . " " . $messageID;
if (is_array($parameter1ID)) {
foreach ($parameter1ID as $parameterPair) {
if (substr($parameterPair[1],0,1) != '"') {
$parameterPair[1] = '"' . $parameterPair[1] . '"';
}
$messageToSend .= " " . $parameterPair[0] . " " . $parameterPair[1];
}
unset($parameter1ID);
}
if (isset($parameter1ID)) {
if ($parameter1ID <> 0) {
$messageToSend .= " " . $parameter1ID . " " . $parameter1Content;
} else {
// print "No parameter 1\n";
}
if ($parameter2ID <> 0) {
$messageToSend .= " " . $parameter2ID . " " . $parameter2Content;
} else {
// print "No parameter 2\n";
}
}
$messageLength = strlen($messageToSend);
$result = sendSocket($socket, "MESSAGET " . $messageLength . "\n");
$result = sendSocket($socket, $messageToSend . "\n");
}
function commEnd($socket) {
socket_close($socket);
}
function connectdb() {
global $link, $mediaLink, $currentScreen, $currentEntertainArea, $UI, $SKIN;
$user = "root";
$password = "";
$link = mysql_connect("127.0.0.1",$user,$password);
mysql_set_charset('utf8',$link);
mysql_select_db("pluto_main",$link);
$mediaLink = mysql_connect("127.0.0.1",$user,$password,true);
mysql_set_charset('utf8',$mediaLink);
mysql_select_db("pluto_media",$mediaLink);
$currentRoom = 1;
$currentEntertainArea = 1;
$currentScreen = 1;
$UI = "FK_UI IS NULL";
$SKIN = "FK_SKIN is NULL";
// Check if we got called with some command to be executed
}
function getMyValue($link,$query) {
$returnValue = array(1);
// Expects a query which returns a single field, and returns such field as a single variable
if (!($result = mysql_query($query,$link))) {
print "<p>Query without result: " . $query . "</p>\n";
} else {
$returnValue = mysql_fetch_array($result);
}
return $returnValue[0];
}
function getMyArray($link,$query) {
// Returns the result set as an array
$returnValue = array();
if (!($result = mysql_query($query,$link))) {
print "<p>Query without result: $query</p>\n";
}
while ($row = mysql_fetch_array($result)) {
array_push($returnValue,$row);
}
return $returnValue;
}
?>