store pid of process and return instead of exit into Sac_a_push::run method
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Wed, 16 Jan 2013 06:27:53 +0000 (07:27 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Wed, 16 Jan 2013 06:30:48 +0000 (07:30 +0100)
web/Obj/sac-a-push.phh
web/spush/brisk-spush.php

index ee30cea..441d2eb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /*
- *  brisk - spush/sac-a-push.phh
+ *  brisk - Obj/sac-a-push.phh
  *
  *  Copyright (C) 2012 Matteo Nastasi
  *                          mailto: nastasi@alternativeoutput.it 
@@ -72,6 +72,26 @@ function global_dump()
     fprintf(STDERR, "G_with_topbanner = [%s]\n", print_r($G_with_topbanner, TRUE));
 }
 
+function pid_save()
+{
+    $pid = getmypid();
+    $fname = LEGAL_PATH."/brisk.pid";
+
+    if (file_exists($fname)) {
+        log_crit("WARN: brisk.pid already exists");
+    }
+    file_put_contents($fname, sprintf("%d\n", $pid));
+}
+
+function pid_remove()
+{
+    $fname = LEGAL_PATH."/brisk.pid";
+
+    if (file_exists($fname)) {
+        unlink($fname);
+    }
+}
+
 function spu_process_info($stream_info, $method, &$header, &$get, &$post, &$cookie)
 {
     $check_post = FALSE;
@@ -567,11 +587,11 @@ class Sac_a_push {
                             }
                             if ($sock === $this->list) {
                                 printf("Arrivati %d bytes da list\n", strlen($buf));
-                                exit(21);
+                                return(21);
                             }
                             else if ($sock === $this->in) {
                                 printf("Arrivati %d bytes da stdin\n", strlen($buf));
-                                exit(22);
+                                return(22);
                             }
                             else {
                                 // $user_a[$s2u[intval($sock)]]->disable();
@@ -606,10 +626,10 @@ class Sac_a_push {
                                 }
                                 else if ($line == "shutdown") {
                                     if ($this->app->dump_data()) {
-                                        exit(0);
+                                        return(0);
                                     }
                                     else {
-                                        exit(1);
+                                        return(1);
                                     }
                                 }
                             }
index 8eece0c..9a65d21 100755 (executable)
@@ -41,18 +41,24 @@ require_once($G_base."briskin5/index_wr.php");
 
 function main()
 {
-    if (($room = Room::create(LEGAL_PATH."/brisk-crystal.data")) == FALSE) {
-        log_crit("room::create failed");
-        exit(1);
-    }
+    pid_save();
+    do {
+        if (($room = Room::create(LEGAL_PATH."/brisk-crystal.data")) == FALSE) {
+            log_crit("room::create failed");
+            $ret = 1;
+            break;
+        }
 
-    if (($s_a_p = Sac_a_push::create($room, USOCK_PATH, 0, 0)) === FALSE) {
-        exit(1);
-    }
+        if (($s_a_p = Sac_a_push::create($room, USOCK_PATH, 0, 0)) === FALSE) {
+            $ret = 2;
+            break;
+        }
 
-    $s_a_p->run();
+        $ret = $s_a_p->run();
+    } while (0);
 
-    exit(0);
+    pid_remove();
+    exit($ret);
 }
 
 main();