lots of coded stuff, the beginnings of the server/client comms bits
[ga4php.git] / gaas / gaasd / gaasd.php
1 <?php 
2
3 // get out master library for gaasd daemon
4 require_once("../lib/gaasdLib.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                         $recvd = "";
47                         $continue = true;
48                         while($continue) {
49                                 $size = socket_recv($data_socket, $recvd_a, 1024, 0);
50                                 $recvd .= $recvd_a;
51                                 if(preg_match("/.*\:EOD$/", $recvd)) {
52                                         // we have a full string... break out
53                                         $continue = false;
54                                         break;
55                                 }
56                         }
57                         
58                         $xps = explode(":", $recvd);
59                         $component =  unserialize(base64_decode($xps[1]));
60                         $msg_type = $component["type"];
61                         $msg = $component["data"];
62
63                         $data_returned = processMessage($msg_type, $msg);
64                         
65                         $d_comp["type"] = $msg_type;
66                         $d_comp["data"] = $data_returned;
67                         
68                         $realdata_returning = "AS:".base64_encode(serialize($d_comp)).":EOD";
69                         
70                         socket_send($data_socket, $realdata_returning, strlen($realdata_returning), 0);
71                         socket_close($data_socket);
72                 }
73         }
74 }
75
76 function processMessage($msg_type, $msg)
77 {
78         global $MESSAGES;
79
80         $function = $MESSAGES[$msg_type]."_server";
81         
82         if(function_exists($function)) {
83                 return $function($msg);
84         } else {
85                 error_log("Call to undefined function! $function\n");
86                 return false;
87         }
88         
89 }
90
91
92 ?>