All pastes #425408 Raw Edit

Untitled

public text v1 · immutable
·published 2007-04-05 13:55 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');

Gtk::io_add_watch($server, Gtk::IO_IN, 'client_connect');

Gtk::main();

function client_connect($server, $type){
	$pid = pcntl_fork();
	if ($pid == -1) {
		die("could not fork");
	} else if ($pid) {
		return true;
		// parent do nothing
	}else{
		echo "acepting client\n";
		$client = stream_socket_accept($server);
		echo "adding watch\n";
		Gtk::io_add_watch($client, Gtk::IO_IN, 'client_data_in');
		return false;
	}
}

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

?>