All pastes #432636 Raw Edit

Untitled

public php v1 · immutable
·published 2007-04-10 11:18 UTC
rendered paste body
#!/usr/bin/php-gtk<?phpfunction sig_handler($signo) {	global $id;	switch ($signo) {		case SIGTERM: // handle shutdown tasks		exit;		break;		case SIGHUP: // handle restart tasks		break;		default: // handle all other signals	}}function send_msg($msg){	global $msgqueue;	if (!msg_send($msgqueue, 1, $msg, true, true, $errmsg)) {			//echo "MSG_SEND ERROR: $errmsg \n";	}}function shutdown_function(){	global $id;	send_msg("($id): process finished");}register_shutdown_function('shutdown_function');$msgqueue = msg_get_queue(ftok(__FILE__, 'R'),0666 | IPC_CREAT);// setup signal handlerspcntl_signal(SIGTERM, "sig_handler");pcntl_signal(SIGHUP, "sig_handler");$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);	ncurses_mvaddstr(0,1,"Logistor Server");	ncurses_move(1,0);	ncurses_refresh();	# posix_kill($pid,SIGINT); // use this to kill all processes	while (true) {		$currentqueue = msg_stat_queue($msgqueue);		$n = $currentqueue['msg_qnum'];		for ($msgi = 0; $msgi < $n; $msgi++) {			if (!msg_receive ($msgqueue, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {				ncurses_addstr(" MSG_RECV ERROR: $errmsg \n");			}else{				ncurses_addstr(" $msg\n");				ncurses_refresh();			}		}	}}else{	$server = stream_socket_server('tcp://127.0.0.1:1999');	$child = false;	$i = 0;	$id = $i;	while(!$child){		$i++;		send_msg("($id): waiting for client ($i)");		$client[$i] = stream_socket_accept($server, -1);		send_msg("($id): forking");		$pid = pcntl_fork();		if ($pid == -1) {			die("could not fork");		} else if ($pid) {			send_msg("($id): closing client ($i)");			fclose($client[$i]);			sleep(2);  // wait before acepting another client		}else{			$child = ture;		}	}	if($child){		$id = $i;		send_msg("($id): adding data watch");		Gtk::io_add_watch($client[$id], Gtk::IO_IN, 'client_data_in');		send_msg("($id): starting Gtk loop");		send_msg("($id): wait for client data");		Gtk::main();	}}function client_data_in($client, $type){	global $id;	send_msg("($id): writting to client connection");	$data = fgets($client);	fwrite($client, $data);	send_msg("($id): closing client connection");	fclose($client);	Gtk::main_quit();}?>