All pastes #425510 Raw Edit

Stuff

public text v1 · immutable
·published 2007-04-05 15:11 UTC
rendered paste body
#!/usr/bin/php-gtk
<?php

function sig_handler($signo) {
	switch ($signo) {
		case SIGTERM: // handle shutdown tasks
		exit;
		break;
		case SIGHUP: // handle restart tasks
		break;
		default: // handle all other signals
	}
}

// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");

/* currently no used as it make it a pain to kill the process
$pid = pcntl_fork();
if ($pid == -1) {
	die("could not fork");
} else if ($pid) {
	echo "closing parent process\n";
	exit(); // we are the parent
}
*/

$server = stream_socket_server('tcp://127.0.0.1:1999');

$child = false;

$i = 0;
while(!$child){
	$i++;
	$client[$i] = stream_socket_accept($server, -1);
	echo "parent: forking\n";
	$pid = pcntl_fork();
	if ($pid == -1) {
		die("could not fork");
	} else if ($pid) {
		echo "parent: closing client connection\n";
		fclose($client[$i]);
		sleep(2);
	}else{
		echo "child: adding data watch\n";
		Gtk::io_add_watch($client[$i], Gtk::IO_IN, 'client_data_in');
		echo "child: wait for client data\n";
		$child = ture;
	}
}

if($child){
	echo "child: starting Gtk loop\n";
	Gtk::main();
}

function client_data_in($client, $type){
	echo "child: writting to client connection\n";
	fwrite($client, fgets($client));
	echo "child: closing client connection\n";
	fclose($client);
	Gtk::main_quit();
	echo "child: dies\n";
}

?>