Initial coding of the gaasd new auth server.
[ga4php.git] / gaas / gaasd / gaasd.php
1 <?php 
2
3 // get out master library for gaasd daemon
4 require_once("../lib/lib.php");
5
6 // first we want to fork into the background like all good daemons should
7 //$pid = pcntl_fork();
8
9
10 // uncomment this bit and comment the fork above to stop it going into the background
11 $pid = 0;
12
13 if($pid == -1) {
14         // we failed to fork, oh woe is me
15 } else if($pid) {
16         // i am the parent, i shall leave
17         //echo "i am a parent, i leave\n";
18         exit(0);
19 } else {
20         // here is where i need to swithc to TCP network protocol stuff
21         // i must bind 127.0.0.1 though.
22         // what i want to happen is this:
23         // 1) server receives connection
24         // 2) server forks off process to process connection
25         // 3) main server continues.
26         // a forked process thingy should be fully self contained and capable of dealing
27         // with "problems", i.e. the parent doesnt want to have to clean up children
28         
29         // Here goes the tcp equivalent
30         global $TCP_PORT_NUMBER;
31         $res = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
32         socket_bind($res, "127.0.0.1", $TCP_PORT_NUMBER);
33         socket_listen($res);
34
35         while(true) {
36                 $data_socket = socket_accept($res);
37                 // now i fork
38                 $forked = pcntl_fork();
39                 
40                 // TODO: DEAL WITH THIS PROPERLY
41                 if($forked == -1) {
42                         echo "Failed to fork\n";
43                 } else if(!$forked) {
44                         // I am the child, i process the request
45                         // all the shit down below goes in here
46                 }
47         }
48 }
49
50 ?>