Two new examples by Michael
authorStefan de Konink <stefan@konink.de>
Sun, 28 Feb 2010 03:17:52 +0000 (28 04:17 +0100)
committerStefan de Konink <stefan@konink.de>
Sun, 28 Feb 2010 03:17:52 +0000 (28 04:17 +0100)
examples/hello_world_forks.php [new file with mode: 0644]
examples/mistral.php [new file with mode: 0644]

diff --git a/examples/hello_world_forks.php b/examples/hello_world_forks.php
new file mode 100644 (file)
index 0000000..8e09683
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+class HelloWorldApp extends Mistral
+{
+    public static function main($http_request) {
+                #print_r($http_request);
+
+                #xhprof_enable();
+                self::parse_headers($http_request);
+                #$xhprof_data = xhprof_disable();
+                #print_r($xhprof_data);
+
+                $output = "Hello World!";
+        $answer = array('status_code' => '200 OK',
+                        'connection' => $_SERVER['HTTP_CONNECTION'],
+                        'content-type' => 'text/html',
+                        'content-length' => strlen($output),
+                        'body' => $output);
+                return $answer;
+        }
+}
+
+
+$maxChildren = 4;
+$startport = 8080;
+while( $cntChildren < $maxChildren ) {
+    $cntChildren++;
+        $startport++;
+
+        $pid = pcntl_fork();
+        if ($pid == -1) {
+                echo "Warning: Cannot spawn child process!!!\n";
+        }
+        elseif($pid) {
+        // Parent process
+        }
+        else {
+                echo 'Spawn Child [' . getMyPid() . '] Port ' . $startport . "\n";
+                $app = new HelloWorldApp('0.0.0.0', $startport, 3);
+                $app->register_callback('HelloWorldApp::main');
+                $app->run();
+        }
+}
+
+while(true) {
+        sleep(1);
+}
+?>
diff --git a/examples/mistral.php b/examples/mistral.php
new file mode 100644 (file)
index 0000000..2cc026c
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+class Mistral
+{
+        public function __construct($ipaddress = '127.0.0.1', $port=8081, $keepalive=3) {
+                mistral_init($ipaddress, $port, $keepalive);
+        }
+
+        public function register_callback($callback_func) {
+                mistral_register_callback($callback_func);
+        }
+
+        public function run() {
+                mistral_start();
+        }
+
+        public function parse_headers($header) {
+                $lines = explode("\n", $header);
+                $first_line = explode(' ', array_shift($lines));
+
+                $_SERVER['REQUEST_METHOD']   = strtoupper($first_line[0]);
+                $_SERVER['REQUEST_URI']      = $first_line[1];
+                $_SERVER['SERVER_PROTOCOL']  = strtoupper($first_line[2]);
+
+                foreach( $lines as $line ) {
+                        $line = trim($line);
+                        if( $line == '' ) continue;
+                        $parts = explode(': ', $line);
+                        if( isset($parts[0]) ) {
+                                $parts[0] = strtoupper($parts[0]);
+                                if( $parts[0] == 'COOKIE' ) {
+                                        $cookies = explode('; ', $parts[1]);
+                                        foreach( $cookies as $cookie ) {
+                                                $cookie_parts = explode('=', $cookie);
+                                                $_COOKIE[$cookie_parts[0]] = $cookie_parts[1];
+                                        }
+                                } else {
+                                        $_SERVER['HTTP_'.str_replace('-', '_', $parts[0])] = $parts[1];
+                                }
+                        }
+                }
+
+                $parsed_uri = parse_url($_SERVER['REQUEST_URI']);
+                parse_str($parsed_uri['query'], $_GET);
+
+                $_SERVER['argv'] = array(0 => $parsed_uri['query']);
+                $_SERVER['argc'] = count($_SERVER['argv']);
+                $_SERVER['GATEWAY_INTERFACE'] = 'CGI/1.1';
+                $_SERVER['SCRIPT_NAME'] = $parsed_uri['path'];
+                $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
+                $_SERVER['PATH_TRANSLATED'] = __PATH__;
+                $_SERVER['QUERY_STRING'] = $parsed_uri['query'];
+                $_SERVER['DOCUMENT_ROOT'] = '/';
+                if( !isset($_SERVER['HTTP_CONNECTION']) ) {
+                        $_SERVER['HTTP_CONNECTION'] = $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1' ? 'keep-alive' : 'close';
+                }
+        }
+}
+?>