<?php
// C = msg key
// L = locking key
-$message_key = ftok(realpath(dirname(__FILE__)), "c");
-$locking_key = ftok(realpath(dirname(__FILE__)), "l");
-$store_key = ftok(realpath(dirname(__FILE__)), "s");
+$MESSAGE_KEY = ftok(realpath(dirname(__FILE__)), "c"); // to daemon
+$LOCKING_KEY = ftok(realpath(dirname(__FILE__)), "l");
+$STORE_KEY = ftok(realpath(dirname(__FILE__)), "s");
+$CONF_STORE_SIZE = 16777216; // 16M by default
-echo "comms key: $message_key, $locking_key, $store_key\n";
+global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
+
+class Comms {
+ function __construct()
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+ $this->semres = sem_get($LOCKING_KEY, 0666);
+ $this->msgres = msg_get_queue($MESSAGE_KEY, 0666);
+ }
+
+ function getConfig($type=0)
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
+ sem_acquire($this->semres);
+ $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
+ $config = shm_get_var($shm_space, $type);
+ sem_release($this->semres);
+
+ return $config;
+ }
+
+ function lockConfigs()
+ {
+ sem_acquire($this->semres);
+ }
+
+ function unlockConfigs()
+ {
+ sem_release($this->semres);
+ }
+
+ function deleteConfig($type)
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
+ $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
+
+ shm_remove_var($shm_space, $type);
+ }
+
+ // type = 0 for the fs config
+ // type = 1 for the boot hardware config
+ // type = 2 for running config
+ // type = 3 for web config
+
+ // type = 4 for merged running config cache
+ // type = 5 for merged web config cache
+
+ // type = 6 for "messages" that appear on the pages
+ function putConfig($config, $type=0)
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
+ sem_acquire($this->semres);
+ $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
+ shm_put_var($shm_space, $type, $config);
+ sem_release($this->semres);
+ }
+
+ function waitForMessage()
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+ $queue = msg_get_queue($MESSAGE_KEY);
+ msg_receive($queue, 0, $msgtype, 1024, $msg);
+
+ return $msg;
+ }
+
+ function sendMessage($msg)
+ {
+ global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+ $queue = msg_get_queue($MESSAGE_KEY);
+ msg_send($queue, 1, $msg);
+
+ }
+
+ private $semres, $msgres;
+
+}
?>
\ No newline at end of file