changed how configs work
[CBFWR.git] / libcbfwr / comms.php
1 <?php
2 // C = msg key
3 // L = locking key
4 $MESSAGE_KEY = ftok(realpath(dirname(__FILE__)), "c"); // to daemon
5 $LOCKING_KEY = ftok(realpath(dirname(__FILE__)), "l");
6 $STORE_KEY = ftok(realpath(dirname(__FILE__)), "s");
7 $CONF_STORE_SIZE = 16777216; // 16M by default
8
9 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
10
11 class Comms {
12         function __construct()
13         {
14                 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
15                 $this->semres = sem_get($LOCKING_KEY, 0666);
16                 $this->msgres = msg_get_queue($MESSAGE_KEY, 0666);
17         }
18         
19         function getConfig($type=0)
20         {
21                 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
22                 sem_acquire($this->semres);
23                 $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
24                 $config = shm_get_var($shm_space, $type);
25                 sem_release($this->semres);
26                 
27                 return $config;
28         }
29         
30         function lockConfigs()
31         {
32                 sem_acquire($this->semres);
33         }
34         
35         function unlockConfigs()
36         {
37                 sem_release($this->semres);
38         }
39         
40         
41         // type = 0 for the fs config
42         // type = 1 for the boot hardware config
43         // type = 2 for running config
44         // type = 3 for web config
45         function putConfig($config, $type=0)
46         {
47                 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
48                 sem_acquire($this->semres);
49                 $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
50                 shm_put_var($shm_space, $type, $config);
51                 sem_release($this->semres);             
52         }
53         
54         function waitForMessage()
55         {
56                 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
57                 $queue = msg_get_queue($MESSAGE_KEY);
58                 msg_receive($queue, 0, $msgtype, 1024, $msg);
59                 
60                 return $msg;
61         }
62         
63         function sendMessage($msg)
64         {
65                 global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
66                 $queue = msg_get_queue($MESSAGE_KEY);
67                 msg_send($queue, 1, $msg);
68                 
69         }
70         
71         private $semres, $msgres;
72         
73 }
74
75 ?>