more configuarbility of xynt test page
[brisk.git] / web / xynt_test01.php
1 <?php
2
3 $desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia (status sempre verde).",
4                "Continuo: da 1 a N ogni secondo, ricomincia ogni 9 (status sempre verde).",
5                "Restart: da 1 a 8 ogni secondo, pausa 16 secondi (status passa ad arancione e poi a rosso), poi ricomincia (e status torna a verde).",
6                "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia (status sempre verde).",
7                "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9, (status sempre verde).",
8                "Reload limiter: da 1 a 8 ogni secondo e chiude, 9 setta e chiude subito,<br>il client aspetta 3 secondi, e poi da 10 a N ogni secondo, (status sempre verde).");
9
10
11 // trim(mb_convert_case($split[0], MB_CASE_TITLE, 'UTF-8'))
12
13 function headers_render($header, $len)
14 {
15     $cookies = "";
16
17     if (isset($header['Cookies'])) {
18         $cookies = $header['Cookies']->render();
19         unset($header['Cookies']);
20     }
21     if (isset($header['Location'])) {
22         header(sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s", $cookies, $header['Location']));
23     }
24     else if (isset($header['HTTP-Response'])) {
25         header(sprintf("HTTP/1.1 %s", $header['HTTP-Response']));
26         foreach($header as $key => $value) {
27             if (strtolower($key) == "http-response")
28                 continue;
29             header(sprintf("%s: %s", $key, $value));
30         }
31         if ($len >= 0) {
32             header(sprintf("Content-Length: %ld", $len));
33         }
34     }
35     else {
36         header("HTTP/1.1 200 OK\r\n");
37
38         if (!isset($header['Date']))
39             header(sprintf("Date: %s", date(DATE_RFC822)));
40         if (!isset($header['Connection']))
41             header("Connection: close");
42         if (!isset($header['Content-Type']))
43             header("Content-Type: text/html");
44         foreach($header as $key => $value) {
45             header(sprintf("%s: %s", $key, $value));
46         }
47         if ($len >= 0) {
48             header(sprintf("Content-Length: %d", $len));
49         }
50         else {
51             header("Cache-Control: no-cache, must-revalidate");
52             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
53             if (!isset($header['Content-Encoding'])) {
54                 header("Content-Encoding: chunked");
55             }
56             header("Transfer-Encoding: chunked");
57         }
58         header($cookies);
59     }
60
61     return (TRUE);
62 }
63
64 $transs = array( "iframe", "websocket", "xhr", "htmlfile" );
65 if (!isset($f_trans))
66     $f_trans = $transs[0];
67
68 if (!isset($f_test))
69     $f_test = 1;
70
71 if (!isset($f_port))
72     $f_port = 80;
73
74 if (!isset($f_fback))
75     $f_fback = 0;
76
77
78 function mop_flush()
79 {
80     for ($i = 0; $i < ob_get_level(); $i++)
81         ob_end_flush();
82     ob_implicit_flush(1);
83     flush();
84 }
85
86 $escpush_from = array("\\", "\"");
87 $escpush_to   = array("\\\\", "\\\"");
88 function escpush($s)
89 {
90     GLOBAL $escpush_from, $escpush_to;
91
92     return str_replace($escpush_from, $escpush_to, $s);
93 }
94
95 function xcape($s)
96 {
97   $from = array (   '\\',     '@',        '|' );
98   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
99
100   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
101 }
102
103 if (isset($isstream) && $isstream == "true") {
104
105     require_once("Obj/transports.phh");
106
107     if (isset($transp) && $transp == "websocket") {
108         $trobj = new Transport_websocket();
109     }
110     else if (isset($transp) && $transp == "xhr") {
111         $trobj = new Transport_xhr();
112     }
113     else if (isset($transp) && $transp == "htmlfile") {
114         $trobj = new Transport_htmlfile();
115     }
116     else {
117         $trobj = new Transport_iframe();
118     }
119     $headers_out = array();
120
121     $init_string = "";
122     for ($i = 0 ; $i < 4096 ; $i++) {
123         if (($i % 128) == 0)
124             $init_string .= " ";
125         else
126             $init_string .= chr(mt_rand(65, 90));
127     }
128     $headers_in = getallheaders();
129     $headers = array();
130     foreach ($headers_in as $header_in => $value) {
131         $headers[mb_convert_case($header_in, MB_CASE_TITLE, 'UTF-8')] = $value;
132     }
133     $fp = fopen("/tmp/xynt.log", "a+");
134     fprintf($fp, "here we are\n");
135     fclose($fp);
136
137     $body = $trobj->init("plain", $headers, $headers_out, $init_string, "", "0");
138
139     if ($body === FALSE) {
140         $fp = fopen("/tmp/xynt.log", "a+");
141         fprintf($fp, "init failed\n");
142         fclose($fp);
143     }
144     else {
145         $fp = fopen("/tmp/xynt.log", "a+");
146         fprintf($fp, "after_init [%s] [%s]\n", $transp, print_r($headers_out, TRUE));
147         fprintf($fp, "body [%s][%d]\n", $body, mb_strlen($body, "ASCII"));
148         fclose($fp);
149     }
150
151     if (isset($transp) && $transp == "websocket") {
152         header_remove('Connection');
153         header_remove('Content-Encoding');
154         header_remove('Content-Type');
155         header_remove('Date');
156         header_remove('Keep-Alive');
157         header_remove('Server');
158         header_remove('Transfer-Encoding');
159         header_remove('Vary');
160         header_remove('X-Powered-By');
161
162         headers_render($headers_out, 100);
163     }
164     $lnz = 0;
165
166     print($body);
167     $lnz += mb_strlen($body, "ASCII");
168     mop_flush();
169
170     switch ($f_test) {
171     case 1:
172         // from 1 to 9 into the innerHTML and than close
173         for ($i = 1 ; $i < 10 ; $i++) {
174             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
175             print($chunk);
176             mop_flush();
177             sleep(1);
178         }
179
180         break;
181     case 2:
182         // from 1 to 9 into the innerHTML and than close
183         for ($i = 1 ; $i < 10 ; $i++) {
184             $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
185             print($chunk);
186             $lnz += mb_strlen($chunk, "ASCII");
187             mop_flush();
188             sleep(1);
189         }
190         break;
191     case 3:
192         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
193         for ($i = 1 ; $i < 10 ; $i++) {
194             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
195             print($chunk);
196             mop_flush();
197             sleep(1);
198             if ($i == 8)
199                 sleep(60);
200         }
201         break;
202     case 4:
203         // from 1 to 9 into the innerHTML and than close
204         for ($i = 1 ; $i < 10 ; $i++) {
205             if ($i != 5) {
206                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
207             }
208             else {
209                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';|sleep(gst,3000);", $i));
210             }
211             print($chunk);
212             mop_flush();
213             sleep(1);
214         }
215         break;
216     case 5:
217         // from 1 to 9 into the innerHTML and than close
218         $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
219         for ($i = 1 ; $i < 10 ; $i++) {
220             switch($i) {
221             case 6:
222             case 7:
223             case 8:
224                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
225                 break;
226             default:
227                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
228                 break;
229             }
230             print($chunk);
231             mop_flush();
232             if ($i < 9)
233                 sleep(1);
234         }
235         break;
236     case 6:
237         // from 1 to 9 into the innerHTML and than close
238         if ($step == 8) {
239             $chunk = $trobj->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
240             print($chunk);
241             // without this usleep the delay is doubled in iframe stream because 
242             // no transp.xynt_streaming back-set is performed
243             usleep(250000);
244             mop_flush();
245         }
246         else {
247             for ($i = 1 ; $i < 10 ; $i++) {
248                 $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
249                 print($chunk);
250                 mop_flush();
251                 if ($i < 9)
252                     sleep(1);
253             }
254         }
255         break;
256     }
257
258     print($trobj->close());
259     mop_flush();
260
261     exit;
262 }
263 ?>
264 <html>
265 <head>
266 <title>XYNT TEST01</title>
267 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
268 <script type="text/javascript" src="xynt-streaming.js"></script>
269 <script type="text/javascript" src="commons.js"></script>
270 <script type="text/javascript" src="heartbit.js"></script>
271
272 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
273
274 <script type="text/javascript"><!--
275      var sess = "for_test";
276      var stat = "";
277      var subst = "";
278      var gst = new globst();
279      window.onload = function() {
280
281          xstm = new xynt_streaming(window, "<?php echo "$f_trans";?>", <?php echo "$f_port";?>, <?php echo "$f_fback";?>, console, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=<?php echo "$f_test";?>', function(com){eval(com);});
282      xstm.hbit_set(heartbit);
283      xstm.start();
284  }
285  //-->
286 </script>
287 </head>
288 <body>
289 <div>
290 <?php
291
292
293
294 printf("<table>");
295 for ($test = 1 ; $test <= count($desc) ; $test++) {
296     printf("<tr>");
297     foreach ($transs as $trans) {
298         printf("<td style=\"padding: 8px; border: 1px solid black;\"><a href=\"?f_trans=%s&f_test=%d&f_port=%d&f_fback=%d\">Test %s %02d (port %d (fb %d))</a></td>", $trans, $test, $f_port, $f_fback, $trans, $test, $f_port, $f_fback);
299     }
300     printf("</tr>\n");
301 }
302 printf("<tr><td style=\"padding: 8px; border: 1px solid black; text-align: center;\" colspan='%d'><a href='#' onclick=\"xstm.abort(); \">STOP</a></td></tr>", count($transs));
303 printf("</table>");
304 printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
305 ?>
306 </div>
307 <div>
308 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
309 </div>
310
311 <div>
312 <b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
313
314 </div>
315 <div>
316 <b>Counter</b>: <span id="container">
317 BEGIN
318 </span>
319 </div>
320 </body>
321 </html>