init script installation added and version updated
[brisk.git] / web / Obj / brisk.phh
index 6249c43..804c6ee 100644 (file)
@@ -58,7 +58,7 @@ define('EXPIRE_TIME_WAG', 10);
 define('WAKEUP_TIME', 12);
 // BAN_TIME da allineare anche in commons.js
 define('BAN_TIME', 3600);
-define('GARBAGE_TIMEOUT', 10);
+define('GARBAGE_TIMEOUT', 5);
 define('NICKSERV', "<i>BriskServ</i>");
 
 define('LOCK_SHARE_MAX', 10000);
@@ -140,10 +140,10 @@ $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
 $G_lng = langtolng($G_lang);
 
 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
-$G_brisk_version = "4.5.2";
+$G_brisk_version = "4.6.0";
 
 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
-$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuova visualizzazione dello stato della connessione dati, indirizzo IP sorgente memorizzato correttamente nei log.',
+$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: aggiunti script di avvio automatico, gestione dei segnali e logging dell\' ultima connessione.',
                                        'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
                        'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
                                        'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
@@ -288,6 +288,18 @@ Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nasta
 <br><b>version '.$G_brisk_version.'</b><br><br>
 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
 
+function addrtoipv4($addr)
+{
+    $ipv4addr_arr = explode(':' , $addr);
+    if (isset($ipv4addr_arr[3])) {
+        $ipv4addr = $ipv4addr_arr[3];
+    }
+    else {
+        $ipv4addr = $addr;
+    }
+    return $ipv4addr;
+}
+
 function mop_flush()
 {
     for ($i = 0; $i < ob_get_level(); $i++)
@@ -325,42 +337,6 @@ function file_unlock($res)
     }
 }
 
-function webservers_exceeded()
-{
-    return(file_exists(PROXY_PATH."/webservers_exceded.flag"));
-}
-
-function webservers_check()
-{
-    GLOBAL $G_webserver_max;
-
-    /* FIXME: check all procs expirations */
-    return (10);
-
-    $ct = 0;
-
-    $dh = opendir('/proc');
-    while (($file = readdir($dh)) !== false) {
-        if (preg_match('/[0-9]+/', $file)) {
-            $cmdline = explode("\0", file_get_contents('/proc/'.$file.'/cmdline'));
-            // echo "xxx".$cmdline[0].$n;
-            if (strstr('/usr/sbin/apache2', $cmdline[0]) != FALSE) {
-                // echo "yyy".$cmdline[0].$n;
-                $ct++;
-            }
-        }
-    }
-    closedir($dh);
-
-    if ($ct >= $G_webserver_max) {
-        touch(PROXY_PATH."/webservers_exceded.flag");
-    }
-    else {
-        unlink(PROXY_PATH."/webservers_exceded.flag");
-    }
-    return ($ct);
-}
-
 $escpush_from = array("\\", "\"");
 $escpush_to   = array("\\\\", "\\\"");
 function escpush($s)
@@ -670,7 +646,54 @@ class Table {
 } // end class Table
 
 
+class Delay_Manager
+{
+    var $delta;
+    var $lastckeck;
+    var $triglevel;
 
+    function Delay_Manager($triglevel)
+    {
+        $this->triglevel = $triglevel;
+        $this->delta = array();
+        $this->lastcheck = 0;
+    }
+
+    function delta_get($curtime)
+    {
+        // clean too old delta items
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            if ($this->delta[$i][0] < $curtime) {
+                array_splice($this->delta, $i, 1);
+                $i--;
+            }
+        }
+
+        // add new delta items if delay exceeded $this->triglevel sec
+        if ($curtime > $this->lastcheck + $this->triglevel && $curtime < $this->lastcheck + 1200.0) {
+            $delta = $curtime - $this->lastcheck - $this->triglevel;
+            array_push($this->delta, array($curtime + $delta , $delta));
+            // fprintf(STDERR, "DELTA: add new delta [%f] [%f] [%f]\n", $this->triglevel, $curtime + $delta, $delta);
+        }
+
+        // extract the maximum valid delta
+        $delta_max = 0.0;
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            $delta_cur = $this->delta[$i][1];
+            if ($delta_max < $delta_cur)
+                $delta_max = $delta_cur;
+        }
+
+        // fprintf(STDERR, "DELTA: status %d, delta_max: %f\n", count($this->delta), $delta_max);
+
+        return ($delta_max);
+    }
+
+    function lastcheck_set($curtime)
+    {
+        $this->lastcheck = $curtime;
+    }
+}
 
 class Room
 {
@@ -685,6 +708,8 @@ class Room
     var $garbage_timeout;
     var $shm_sz;
     
+    var $delay_mgr;
+
     function Room ($crystal_filename) {
         $this->crystal_filename = $crystal_filename;
         $this->user  = array();
@@ -714,6 +739,8 @@ class Room
         }
         $this->garbage_timeout = 0;
         $this->shm_sz = SHM_DIMS_MIN;
+
+        $this->delay_mgr = new Delay_Manager(1.5);
     }
 
   function garbage_manager($force)
@@ -725,14 +752,15 @@ class Room
     log_rd2("garbage_manager START");
 
     /* Garbage collector degli utenti in timeout */
-    $curtime = time();
+    $curtime = microtime(TRUE);
+
+    $delta = $this->delay_mgr->delta_get($curtime);
 
     if (!$force && !($this->garbage_timeout < $curtime)) {
+        $this->delay_mgr->lastcheck_set($curtime);
         return ($ismod);
     }
       
-    webservers_check();
-    
     // Before all align times with table timeout
     for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
        $table_cur = $this->table[$table_idx];
@@ -753,7 +781,6 @@ class Room
                     $bri = FALSE;
                 }
 
-
                 if ($bri != FALSE) {
                     //
                     //  SPAWN: JOIN
@@ -844,9 +871,9 @@ class Room
        if ($user_cur->sess == "") 
             continue;
        
-       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
+       if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) {
             // Auto logout dell'utente
-            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
+            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime - delta ".($curtime - $delta));
             
             if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
                 log_auth($user_cur->sess, "Autologout session.");
@@ -863,7 +890,7 @@ class Room
             }
        }
 
-       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
+       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < ($curtime - $delta)) { // lo rimettiamo in piedi
             if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
                 $this->room_wakeup($user_cur);
                 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
@@ -878,6 +905,7 @@ class Room
     $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
     $ismod = TRUE;
 
+    $this->delay_mgr->lastcheck_set($curtime);
     return ($ismod);
   }
 
@@ -1887,6 +1915,7 @@ class Room
       }
       else {
           fprintf(STDERR, "ROOM FROM FILE\n");
+          rename($crystal_filename, $crystal_filename.".old");
       }
 
     return $room;
@@ -2188,8 +2217,11 @@ class Room
           do {
               if (!isset($cookie['sess'])
                   || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
-                  $content = User::stream_fini($s_a_p->rndstr, TRUE);
+                  if (($transp  = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
+                      $transp = "iframe";
                   
+                  $content = User::stream_fini($transp, $s_a_p->rndstr, TRUE);
+
                   $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
                   return TRUE;