Ferreteria/v0.6/clade/IO/Aspect/Connx/Plug/Shell/@removed

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | Plug‎ | Shell
Revision as of 14:17, 31 October 2025 by Woozle (talk | contribs) (Created page with "{{fmt/title|Removed Code}} =={{fmt/date|2025|03|22}}== Blocked out yesterday: {{fmt/php/block|1= // 2025-03-21 old version moved to Process::Run() public function DoCommand(string{{!}}array $sCmd, BufferIface $oBuffData) : CommOpIface { $this->OpenCmd($sCmd); #self::GotToHere(); $oAct = $this->oAct; $arPipes = $this->arPipes; // Set the output streams to non-blocking mode: stream_set_blocking($arPipes[1], 0);...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Removed Code

2025-03-22

Blocked out yesterday:

// 2025-03-21 old version moved to Process::Run()
    public function DoCommand(string|array $sCmd, BufferIface $oBuffData) : CommOpIface {
        $this->OpenCmd($sCmd);
        #self::GotToHere();
        $oAct = $this->oAct;
        $arPipes = $this->arPipes;

        // Set the output streams to non-blocking mode:
        stream_set_blocking($arPipes[1], 0);
        stream_set_blocking($arPipes[2], 0);
        // NOTE That there are TWO different arrays here, $arIO and $arPipes.

        // create a buffer for error messages
        $oBuffErrs = new MemBuffClass;
        $oBuffErrs->Open();
        $oBuffData->Open();

        #self::GotToHere('DATA BUFF ID='.$oBuffData->ObjectID());
        #self::GotToHere('ERRS BUFF ID='.$oBuffErrs->ObjectID());

        #$this->AmHere();
        $this->ReadStream($arPipes[1],$oBuffData,TRUE);     // expecting data here
        $this->ReadStream($arPipes[2],$oBuffErrs,FALSE);    // just checking for messages

        if ($oBuffErrs->IsMore()) {
            $sMsg = $oBuffErrs->RemoveBytes();

            $oAct->SetOkay(FALSE);
            $oAct->AddMsgObject(new MsgClass($sMsg));
            $oAct->QResponseErr()->SetIt($sMsg);
        } else {
            if (!$oBuffData->IsMore()) {
                $oAct->AddMsgString('No error messages, but also no regular output received.');
            }
        }

        $this->ShutCmd();
        $oBuffData->Shut();
        $oBuffErrs->Shut();

        return $oAct;
    }

    // PUBLIC alias for OpenCmd(), just while experimenting
    public function OpenCommand(string|array $sCmd) : CommOpIface {
        $this->OpenCmd($sCmd);
        return $this->oAct;
    }

    private $arPipes;
    private $rProc;
    private $oAct;
    protected function OpenCmd(string|array $sCmd) {
        $oAct = $this->NewAction();
        $arConf = array(  // configuration for proc_open()
          0 => array("pipe", "r"),  // stdin
          1 => array("pipe", "w"),  // stdout
          2 => array("pipe", "w")   // stderr
        );
        $rProc = proc_open($sCmd, $arConf, $arPipes);
        $ok = is_resource($rProc);
        $oAct->SetOkay($ok);
        #$this->arIO = $arIO;
        $this->arPipes = $arPipes;
        $this->rProc = $rProc;
        $this->oAct = $oAct;
    }
    // PUBLIC while experimenting
    public function ShutCmd() {
        $arPipes = $this->arPipes;
        $rProc = $this->rProc;
        // close the pipes:
        fclose($arPipes[0]);
        fclose($arPipes[1]);
        fclose($arPipes[2]);
        proc_close($rProc);
    }

    public function SendFromBuffer(BufferIface $oSendBuff, BufferIface $oRecvBuff) {
        $this->WriteStream($oSendBuff,$this->arPipes[0]);
        $this->ReadStream($this->arPipes[1],$oRecvBuff,FALSE);  // ...I *think*.
    }

    // 2025-03-17 set_time_limit() and stream_set_timeout() are unnecessary in non-blocking mode, and make things complicated.
    protected function ReadStream($rPipe, BufferIface $oBuff,bool $doWait) {
        $nBufSize = 1024 * 1024;  // 1M buffer
        // Process the stream in a loop
        #self::GotToHere();
        stream_set_blocking($rPipe, 0);
        $nStart = time();
        $didWait = $doWait;
        while (!feof($rPipe) && $doWait) {
            $sPiece = fread($rPipe, $nBufSize);
            if (empty($sPiece))  {
                // Time-out if I/O hangs for more than 5 seconds:
                $doWait = (time() <= ($nStart+10));    // triggers after n+1 seconds of no data
            } else {
                $oBuff->AppendBytes($sPiece);
                $nStart = time();   // got data, so reset timer
            }
        }
        if ($didWait && !$doWait) echo 'Note: timed out waiting for response.'.CRLF;    // TODO: return this in an ItWent object.
    }
    // 2025-03-17 set_time_limit() and stream_set_timeout() are unnecessary in non-blocking mode, and make things complicated.
    protected function WriteStream(BufferIface $oData, $rPipe) {
        // Process the stream in a loop
        #echo $oData->ReflectThis()->Report();
        $oData->Open();
        while ($oData->IsMore()) {
            // Time-out if I/O hangs for more than 5 seconds:
            #$ok = set_time_limit(5); // NOTE: seems to always return FALSE
            $sPiece = $oData->RemoveBytes();
            // WORKING HERE
            $ok = fwrite($rPipe,$sPiece);   // TODO: this should check $ok to make sure it wrote the entire buffer -- remaining part needs to be sent again
            if ($ok === FALSE) {
                $this->AmHere('TODO: handle write error');
            } elseif($ok !== strlen($sPiece)) {
                $this->AmHere('TODO: handle incomplete write');
            } else {
                $ok = file_put_contents('/home/woozle/Downloads/scratch/dba.dump.sql',$sPiece,FILE_APPEND);
                self::HardAssert(is_int($ok),'Problem with dump file');
            }
        }
        $oData->Shut();
    }

2024-12-25

Apparently this turned out to be unnecessary, or it didn't work or something:

protected function CheckErrorPipe($rPipe, BufferIface $oBuff) {
        $nBufSize = 1024 * 1024;  // 1M buffer
        while (!feof($rPipe)) {
            $ok = set_time_limit(5); // NOTE: seems to always return FALSE
            $sPiece = fread($rPipe, $nBufSize);
            if (($sPiece !== FALSE) && (strlen($sPiece) > 0)) {
                $oBuff->PutBytes($sPiece);
            }
        }
    }