Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("PROGRAM","php dummy.php");
- class Forwarder
- {
- var $program;
- public function __construct()
- {
- echo stream_set_blocking(STDIN,1)?"STDIN is blocking\n":"STDIN is not blocking\n";
- }
- public function start()
- {
- $this->startPingPongProgram();
- while(true)
- {
- $readList=array($this->program->stdout,STDIN);
- $writeList=null;
- $errorList=null;
- $tv=null;
- $selected=stream_select($readList,$writeList,$errorList,$tv);
- if($selected===false)
- {
- echo "Error in stream_select()\n";
- }
- else
- {
- echo "We have $selected descriptor(s) ready : \n".print_r($readList,true);
- }
- foreach($readList as $readable)
- {
- if($readable==STDIN)
- {
- $line=fgets(STDIN);
- if($line===false)
- {
- echo "line is false\n";
- $this->program->dispose();
- exit();
- }
- $this->sendAllData($line);
- }
- else
- {
- echo "We got data on $readable : ".fread($readable,4096)."\n";
- }
- }
- }
- }
- protected function sendAllData($data)
- {
- $remaining=strlen($data);
- while($remaining!=0)
- {
- $written=fwrite($this->program->stdin,$data);
- $data=substr($data,$written,$remaining);
- $remaining=strlen($data);
- }
- }
- protected function startPingPongProgram()
- {
- $program=new PingPongProgram(PROGRAM);
- $this->program=$program;
- }
- }
- class PingPongProgram
- {
- var $stdin;
- var $stdout;
- var $stderr;
- var $proc;
- public function __construct($command)
- {
- $descriptorspec = array(
- 0 => array("pipe", "r"),
- 1 => array("pipe", "w"),
- 2 => array("pipe", "w")
- );
- $cwd=null;
- $env=null;
- $process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
- $this->proc=$process;
- $this->stdin=$pipes[0];
- $this->stdout=$pipes[1];
- $this->stderr=$pipes[2];
- echo stream_set_blocking($this->stdin,1)?"Program STDIN is blocking\n":"Program STDIN is not blocking\n";
- echo stream_set_blocking($this->stdout,1)?"Program STDOUT is blocking\n":"Program STDOUT is not blocking\n";
- echo stream_set_blocking($this->stderr,1)?"Program STDERR is blocking\n":"Program STDERR is not blocking\n";
- }
- public function dispose()
- {
- if(fclose($this->stdin))
- echo "Program's STDIN closed\n";
- if(fclose($this->stdout))
- echo "Program's STDOUT closed\n";
- if(fclose($this->stderr))
- echo "Program's STDERR closed\n";
- proc_close($this->proc);
- echo "Program terminated\n";
- }
- }
- $fwd=new Forwarder();
- $fwd->start();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement