rendered paste body#!/usr/bin/php-gtk
<?php
define('DEMON_MONITOR',0);
define('DEMON_SERVER',1);
define('DEMON_CLIENT',2);
class logistor_monitor {
public function monitor(){
$this->type = DEMON_MONITOR;
$this->send_message("($this->id): starting monitor loop");
// monitor stuff
while (true) {
$currentqueue = msg_stat_queue($this->messagequeue);
$n = $currentqueue['msg_qnum'];
for ($msgi = 0; $msgi < $n; $msgi++) {
if (!msg_receive ($this->messagequeue, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
echo("MSG_RECV ERROR: $errmsg \n");
}else{
echo("$msg\n");
}
}
}
}
}
class logistor_server {
public function server(){
$this->type = DEMON_SERVER;
$this->id++;
$this->send_message("($this->id): starting server");
$this->server = stream_socket_server('tcp://127.0.0.1:1999');
$child = false;
$i = 1;
while (!$child) {
$this->send_message("($this->id): waiting for client (".($this->id+$i).")");
$client = stream_socket_accept($this->server, -1);
$this->send_message("($this->id): forking client (".($this->id+$i).")");
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
$this->send_message("($this->id): closing client (".($this->id+$i).")");
fclose($client);
$i++;
sleep(2); // wait before acepting another client
}else{
$this->type = DEMON_CLIENT;
$this->id = $this->id + $i;
$this->client = $client;
$child = ture;
}
}
}
}
class logistor_client {
public function client(){
$this->send_message("($this->id): adding data watch");
Gtk::io_add_watch($this->client, Gtk::IO_IN, array($this, 'data_in'));
$this->send_message("($this->id): starting Gtk loop");
$this->send_message("($this->id): wait for client data");
Gtk::main();
}
public function data_in($client, $type){
$this->send_message("($this->id): writting to client connection");
$data = fgets($client);
fwrite($client, $data);
$this->send_message("($this->id): closing client connection");
fclose($client);
Gtk::main_quit();
}
}
class logistor {
public $type;
public $messagequeue;
public $client;
public $server;
public $id = 0;
public function __construct(){
$ipckey = ftok(__FILE__, 'R'); // create System V IPC key
$this->messagequeue = msg_get_queue($ipckey,0666 | IPC_CREAT); // setup master message queue
}
public function send_message($message){
msg_send($this->messagequeue, 1, $message, true, true, $errmsg);
}
}
$demon = new logistor_demon();
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
}elseif ($pid){
runkit_class_adopt('logistor','logistor_monitor');
$demon->monitor();
}else{
runkit_class_adopt('logistor','logistor_server');
$demon->server();
}
if ($demon->type === DEMON_CLIENT) {
runkit_class_emancipate('logistor');
runkit_class_adopt('logistor','logistor_client');
$demon->client();
}
?>