-
Notifications
You must be signed in to change notification settings - Fork 7
/
impresto.php
164 lines (126 loc) · 4.65 KB
/
impresto.php
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
<?php
# Required Packages:
# PECL-json (PHP <5.2.0)
# PECL-memcached
# Memcached Configuration
define('MC_SERVER','127.0.0.1');
define('MC_PORT','11211');
# HTTP Long-Held Timeout, in seconds
define('HTTP_LONG_POLLING_TIMEOUT',30);
# Only sync with updates happened within the last 10 minutes
define('SLIDE_UPDATE_TIMEOUT', 600);
# Handles sending json-format replies
class REPLY {
private static $HTTPStatusCodes = array(100=>'Continue',101=>'Switching Protocols',
200=>'OK',201=>'Created',202=>'Accepted',203=>'Non-Authoritative Information',204=>'No Content',205=>'Reset Content',206=>'Partial Content',
400=>'Bad Request',401=>'Unauthorized',402=>'Payment Required',403=>'Forbidden',404=>'Not Found',405=>'Method Not Allowed',406=>'Not Accepted',
408=>'Request Timeout',409=>'Conflict',410=>'Gone',411=>'Length Required',412=>'Precondition Failed',413=>'Request Entry Too Large',
414=>'Request-URI Too Long',415=>'Unsupported Media Type',416=>'Request Range Not Satisfiable',417=>'Expectation Failed',
426=>'Upgrade Required',
500=>'Internal Server Error',501=>'Not Implemented',502=>'Bad Gateway',503=>'Service Unavailable',504=>'Gateway Timeout',
505=>'HTTP Version Not Supported');
public static function data($data){
exit(json_encode($data));
}
public static function ok() { self::http_code(200);}
public static function accepted() { self::http_code(202);}
public static function bad_request() { self::http_code(400);}
public static function server_error() { self::http_code(500);}
public static function timeout() { self::http_code(408);}
public static function http_code($resultCode){
self::data(array(
'result'=>$resultCode,
'resultText'=>self::$HTTPStatusCodes[$resultCode]
));
}
}
# Fetches Memcached data, stores and reads slide update info
class Mem {
private $memcache;
function __construct($id){
### Find Memcached persistent instance
$this->memcache = new \Memcached($id);
$this->memcache->addServer(MC_SERVER,MC_PORT,40);
if($this->memcache->isPristine()) {
### Set default values
$this->memcache->set('updateTime', -SLIDE_UPDATE_TIMEOUT);
$this->memcache->set('currentSlide', 0);
$this->memcache->set('currentNarrator', 0);
}
}
public function getTalkSlide(){
return $this->memcache->get('currentSlide');
}
public function setTalkSlide($slideID, $narratorID){
return $this->memcache->set('currentSlide', $slideID)
&& $this->memcache->set('currentNarrator', $narratorID)
&& $this->memcache->set('updateTime', time());
}
public function getTalkSlideUpdateTime($audienceID){
if($this->memcache->get('currentNarrator') == $audienceID) {
return -SLIDE_UPDATE_TIMEOUT;
}
return $this->memcache->get('updateTime');
}
}
# [Client] [Server]
# (slide_update,time)------------------>(POST)
# |
# (compare time to slide_updateTime)<----------
# | |
# <if slide_updateTime newer>---->(sleep)------
# | Y
# [waitinf ajax]<-----------------------(send slide_number,time)
if (isset($_POST['data'])) {
$json = json_decode($_POST['data'],true);
# error decoding json
if (is_null($json)) {
REPLY::bad_request();
}
}
else {
REPLY::bad_request();
}
# Must set talkID and command
if(empty($json['talkID']) || empty($json['command'])) {
REPLY::bad_request();
}
$command = htmlspecialchars($json['command']);
$talkID = htmlspecialchars($json['talkID']);
$userID = htmlspecialchars($json['userID']);
$mem = new Mem($talkID);
switch ($command) {
case 'update_slide': # Set current slide id
if (empty($json['slideID'])){
REPLY::bad_request();
}
$slideID = htmlspecialchars($json['slideID']);
if($mem->setTalkSlide($slideID, $userID)) {
REPLY::accepted();
}
else {
REPLY::server_error();
}
break;
case 'get_slide': # Get last slide id
if (!isset($json['slideUpdateTime'])) {
REPLY::bad_request();
}
$lastClientUpdate = htmlspecialchars($json['slideUpdateTime']);
# Start long polling
for($h=0; $h<= HTTP_LONG_POLLING_TIMEOUT; $h++){
# If the slide has been updated within the last 10 minutes and after the last check, send the current slide id to client
$lastUpdate = $mem->getTalkSlideUpdateTime($userID);
if ($lastUpdate != $lastClientUpdate
&& $lastUpdate > $lastClientUpdate - SLIDE_UPDATE_TIMEOUT ) {
REPLY::data(array(
'slideID'=>$mem->getTalkSlide(),
'slideUpdateTime'=>$lastUpdate,
'result'=>200
));
}
sleep(1);
}
REPLY::timeout();
break;
}