All pastes #432613 Raw Edit

Something

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

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

function shutdown_function(){
	global $id;
	//echo "($id): process finished\n";
}

register_shutdown_function('shutdown_function');

// 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) {
	$id = -1;
ncurses_init();
ncurses_border(0,0, 0,0, 0,0, 0,0); // Do a lovely border
ncurses_mvaddstr(0,1,"Logistor Server"); 
ncurses_move(1,1); // Start top left(inside border)
ncurses_addstr("Build Logistor server Interface");
ncurses_refresh(); // Send buffer to screen
# posix_kill($pid,SIGINT); // use this to kill all processes
while (true) {}
}else{
$server = stream_socket_server('tcp://127.0.0.1:1999');

$child = false;
$i = 0;
$id = $i;

while(!$child){
	$i++;
	//echo "($id): waiting for client ($i)\n";
	$client[$i] = stream_socket_accept($server, -1);
	//echo "($id): forking\n";
	$pid = pcntl_fork();
	if ($pid == -1) {
		die("could not fork");
	} else if ($pid) {
		//echo "($id): closing client ($i)\n";
		fclose($client[$i]);
		sleep(2);  // wait before acepting another client
	}else{
		$child = ture;
	}
}

if($child){
	$id = $i;
	//echo "($id): adding data watch\n";
	Gtk::io_add_watch($client[$id], Gtk::IO_IN, 'client_data_in');
	//echo "($id): starting Gtk loop\n";
	//echo "($id): wait for client data\n";
	Gtk::main();
}

}

function client_data_in($client, $type){
	global $id;
	//echo "($id): writting to client connection\n";
	$data = fgets($client);
	fwrite($client, $data);
	//echo "($id): closing client connection\n";
	fclose($client);
	Gtk::main_quit();
}

?>