note 95385 added to function.posix-mkfifo

From: Date: Wed, 30 Dec 2009 13:36:09 +0000
Subject: note 95385 added to function.posix-mkfifo
Groups: php.notes 
Request: Send a blank email to php-notes+get-164469@lists.php.net to get a copy of this message
Object Oriented FIFO Communication process: interface Communication { public function receive($bytes = 1024); public function getData(); public function clearData(); public function send($data); } class FIFOCommunication implements Communication { private $fifos; private $data; static public function stream_fifo_open($fifoPath, $mode) { if (pcntl_fork() == 0) { if (! file_exists($fifoPath)) { posix_mkfifo($fifoPath, $mode); } $fifo = fopen($fifoPath, 'w'); sleep(1); exit(0); } else { usleep(15); return fopen($fifoPath, 'r'); } } static public function stream_fifo_write($fifoPath, $mode, $data) { if (pcntl_fork() == 0) { if (! file_exists($fifoPath)) { posix_mkfifo($fifoPath, $mode); } $fifo = fopen($fifoPath, 'w'); fwrite($fifo, $data); exit(0); } } public function __construct( $fifoInputName, $fifoInputMode, $fifoOutputName, $fifoOutputMode ) { $this->fifos = array( 'input' => array( 'file' => self::stream_fifo_open($fifoInputName, $fifoInputMode), 'mode' => $fifoInputMode, 'name' => $fifoInputName, 'use' => 'r', ), 'output' => array( 'mode' => $fifoOutputMode, 'name' => $fifoOutputName, 'use' => 'w' ) ); } public function remove($type = null) { switch ($type) { case 'input': @unlink($this->fifos['input']['name']); break; case 'output': @unlink($this->fifos['output']['name']); break; default: @unlink($this->fifos['input']['name']); @unlink($this->fifos['output']['name']); } } public function receive($bytes = 1024) { $readers = array($this->fifos['input']['file']); if (stream_select($readers, $writers = null, $except = null, 0, 15) == 1) { $data = fread($this->fifos['input']['file'], $bytes); } if (! empty($data)) { $this->data .= $data; return true; } else { return false; } } public function getData() { return $this->data; } public function clearData() { $this->data = null; } public function send($data) { $fifoOutput = & $this->fifos['output']; self::stream_fifo_write($fifoOutput['name'], $fifoOutput['mode'], $data); } } $fifoCommunication = new FIFOCommunication( getmypid() . '.input', 0600, getmypid() . '.output', 0600 ); echo "COMMUNICATION STARTED\n"; while (true) { if ($fifoCommunication->receive()) { $data = $fifoCommunication->getData(); if ($data == "EXIT\n") { break; } else { $fifoCommunication->send('RECEIVED: ' . $fifoCommunication->getData()); } } sleep(1); } $fifoComunication->remove(); ---- Server IP: 69.147.83.197 Probable Submitter: 190.192.21.65 ---- Manual Page -- http://www.php.net/manual/en/function.posix-mkfifo.php Edit -- https://master.php.net/note/edit/95385 Del: integrated -- https://master.php.net/note/delete/95385/integrated Del: useless -- https://master.php.net/note/delete/95385/useless Del: bad code -- https://master.php.net/note/delete/95385/bad+code Del: spam -- https://master.php.net/note/delete/95385/spam Del: non-english -- https://master.php.net/note/delete/95385/non-english Del: in docs -- https://master.php.net/note/delete/95385/in+docs Del: other reasons-- https://master.php.net/note/delete/95385 Reject -- https://master.php.net/note/reject/95385 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164469) next »