preload_images start added to first initializiation stream's data
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index 4fb54b1..fc3a670 100644 (file)
@@ -2,7 +2,7 @@
 /*
  *  brisk - briskin5.phh
  *
- *  Copyright (C) 2006-2011 Matteo Nastasi
+ *  Copyright (C) 2006-2012 Matteo Nastasi
  *                          mailto: nastasi@alternativeoutput.it 
  *                                  matteo.nastasi@milug.org
  *                          web: http://www.alternativeoutput.it
@@ -477,13 +477,15 @@ class Bin5_user extends User {
        } 
     */
     
-    function spawn($from, &$bri, $table, $table_pos)
+    function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
     {
-        GLOBAL $CO_bin5_pref_ring_endauct;
-        
         if (($thiz = new Bin5_user()) == FALSE)
             return (FALSE);
-        
+
+        if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
+            $CO_bin5_pref_ring_endauct = "";
+        }
+
         $thiz->parentcopy($from);
         
         /* NOTE: at this moment idx and table_pos fields have the same value 
@@ -498,7 +500,7 @@ class Bin5_user extends User {
         log_wr("Bin5 constructor");
         
         $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
-        
+
         $thiz->table_orig = $table;
         $thiz->table      = 0;
         $thiz->table_pos  = $table_pos;
@@ -718,7 +720,37 @@ class Bin5_user extends User {
         return ($ret);
     }
 
-    static function blocking_error($is_unrecoverable)
+public static function stream_fini($is_unrecoverable)
+{
+    GLOBAL $G_four_rnd_string;
+
+    printf("xXx user::stream_fini\n");
+
+    // IF IFRAME THEN:
+    $body = "";
+    $body .= sprintf("<html>
+<head>
+<script type=\"text/javascript\" src=\"%scommons.js\"></script>
+<script type=\"text/javascript\" src=\"%sxynt-http-streaming-ifra.js\"></script>
+<script type=\"text/javascript\">
+var http_streaming = \"ready\";", self::base_get(), self::base_get());
+    $body .= sprintf("
+window.onload = function () { if (http_streaming != \"ready\") { http_streaming.reload(); } };
+</script>
+</head>
+<body>");
+    $body .= sprintf("<!-- \n%s -->\n", $G_four_rnd_string);
+    $body .= sprintf("<script id='hs%d' type='text/javascript'><!--
+push(\"%s\");
+// -->
+</script>", 0, escpush(self::blocking_error($is_unrecoverable)) );
+    // ELSE IF XHR THEN:
+    // return (self::blocking_error($is_unrecoverable));
+    return ($body);
+}
+
+
+    protected function blocking_error($is_unrecoverable)
     {
         GLOBAL $is_page_streaming;
         
@@ -752,23 +784,6 @@ class Bin5_user extends User {
         $ret = FALSE;
         $curtime = time();
         
-        /* if ((($curtime - $this->lacc) >  STREAM_TIMEOUT) || Room::garbage_time_is_expired($curtime)) { */
-        /*     $S_load_stat['lL_laccgarb']++; */
-        
-        /*     if (($curtime - $this->lacc) >=  STREAM_TIMEOUT) { */
-        /*         $S_load_stat['wU_lacc_upd']++; */
-        /*         $this->lacc = $curtime; */
-        /*     } */
-        
-        /*     if (Room::garbage_time_is_expired($curtime)) { */
-        /*         log_only("F"); */
-        
-        /*         $S_load_stat['wR_garbage']++; */
-        /*         log_main("pre garbage_manager TRE"); */
-        /*         $this->room->garbage_manager(FALSE); */
-        /*     } */
-        /* } */
-        
       /* Nothing changed, return. */
       if ($cur_step == $this->step) 
           return (FALSE);
@@ -894,7 +909,7 @@ class Bin5 {
     var $table;
     var $comm; // commands for many people
     var $step; // current step of the comm array
-    // externalized var $garbage_timeout;
+    var $garbage_timeout;
     var $shm_sz;
     
     var $table_idx;
@@ -903,7 +918,30 @@ class Bin5 {
     var $the_end;
     var $tok;
 
-    function Bin5 ($room, $table_idx, $table_token) {
+    static function page_manager($room, $header_out, $path, $method, $addr, $get, $post, $cookie) 
+    {
+        switch ($path) {
+        case "":
+        case "index.php":
+            ob_start();
+            bin5_index_main($room, $header_out, $addr, $get, $post, $cookie);
+            $content = ob_get_contents();
+            ob_end_clean();
+            
+            force_no_cache($header_out);
+
+            $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
+            
+            if ($pgflush->try_flush($curtime) == FALSE) {
+                // Add $pgflush to the pgflush array
+                array_push($pages_flush, $pgflush);
+            }
+            break;
+        }
+        return TRUE;
+    }
+
+    function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
         $this->user = array();
         $this->table = array();
         
@@ -921,7 +959,7 @@ class Bin5 {
         
         for ($i = 0 ; $i < $table->player_n ; $i++) {
             $user[$table->player[$i]]->table_token = $table_token;
-            $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i);
+            $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
         }
         $this->table[0] = Bin5_table::spawn(&$table);
         
@@ -929,7 +967,7 @@ class Bin5 {
         
         $this->table_idx = $table_idx;
         $this->table_token = $table_token;
-        Bin5::garbage_time_expire_set($table_idx, 0);
+        $this->garbage_timeout = 0;
         
         log_wr("Bin5 constructor end");
     }
@@ -967,8 +1005,7 @@ class Bin5 {
         /* Garbage collector degli utenti in timeout */
         $ismod = FALSE;
         $curtime = time();
-        // externalized if ($force || $this->garbage_timeout < $curtime) {
-        if ($force || Bin5::garbage_time_is_expired($this->table_idx, $curtime)) {
+        if ($force || $this->garbage_timeout < $curtime) {
             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
                 $user_cur = $this->user[$i];
                 if ($user_cur->sess == "" || 
@@ -1006,8 +1043,7 @@ class Bin5 {
             }
             log_rd2($user_cur->sess." GARBAGE UPDATED!");
             
-            // externalized $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
-            Bin5::garbage_time_expire_set($this->table_idx, $curtime + GARBAGE_TIMEOUT);
+            $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
             
             $ismod = TRUE;
         }
@@ -1353,49 +1389,6 @@ class Bin5 {
         return ($is_ab);
     }
 
-
-    static function garbage_time_is_expired($tab_id, $tm) 
-    {
-        $ret = TRUE;
-        $fp = FALSE;
-        do {
-            if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
-                mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'rb')) == FALSE)
-                break;
-            if (($s = fread($fp, 4)) == FALSE)
-                break;
-            if (mb_strlen($s, "ASCII") != 4)
-                break;
-            $arr = unpack('Le', $s);
-            if ($arr['e'] > $tm)
-                $ret = FALSE;
-        } while (0);
-      
-        if ($fp != FALSE)
-            fclose($fp);
-      
-        log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
-      
-        return ($ret);
-    }
-
-    static function garbage_time_expire_set($tab_id, $tm) 
-    {
-        do {
-            if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
-                mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'wb')) == FALSE)
-                break;
-            fwrite($fp, pack("L",$tm));
-            fclose($fp);
-          
-            return (TRUE);
-        } while (0);
-      
-        return (FALSE);
-    }
-
     static function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
     {
         printf("NEW_SOCKET (root): %d\n", intval($new_socket));
@@ -1406,6 +1399,8 @@ class Bin5 {
         if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE) 
             unset($table_token);
 
+        force_no_cache($header_out);
+
         switch ($path) {
         case "":
         case "index.php":
@@ -1645,6 +1640,7 @@ function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
     /* NOTIFY FOR THE CARD MAKER */
     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
         $ret .= show_table_info(&$bri, &$table, $table_pos);
+        $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
     }
     if (!$is_again) 
         $ret .= table_wellcome($user);