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