src/Dcarrith/LxMPD/LxMPD.php
private function read() {

                // Check for a connection
                if( !$this->connection->established ) {
                        $this->connection->establish();
                }

                // Set up the array to use for storing the read in MPD response
                $response = array();

		// This will be used in case there is an empty array as the response
		$ok = false;

		// Get the stream meta-data
                $info = stream_get_meta_data( $this->connection->socket );

		//Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info));

                // Wait for output to finish or time out
                while( !feof( $this->connection->socket ) && !$info['timed_out'] ) {

			try {

                        $line = trim( fgets( $this->connection->socket ));

			//Log::info('LxMPD->read : line', array($line));

			$info = stream_get_meta_data( $this->connection->socket );

			//Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info));

                        // We get empty lines sometimes. Ignore them.
                        if( empty( $line ) ) {

                                continue;

                        } else if( strcmp( self::MPD_OK, $line ) == 0 ) {

				$ok = true;
                                break;

                        } else if( strncmp( self::MPD_ERROR, $line, strlen( self::MPD_ERROR ) ) == 0 && preg_match( '/^ACK \[(.*?)\@(.*?)\] \{(.*?)\} (.*?)$/', $line, $matches ) ) {

				// First item in matches will be the errorConstant
				// Second item in matches will be the index of the failed command
				// Third item in matches will be the original command that was run
				// Fourth item in matches will be the error message
				list( $constant, $index, $command, $error ) = $matches;

				Log::info('LxMPD write constant', array($constant));
				//Log::info('LxMPD write index', array($index));
				//Log::info('LxMPD write command', array($command));
				//Log::info('LxMPD write error', array($error));

				throw new MPDException( 'Command failed: '.$error, self::MPD_COMMAND_FAILED );

			} else {

			        $response[] = $line;
                        }

			} catch (Exception $e) {

				Log::info('LxMPD->read : Exception occurred', $e);
			}
                }

                if( $info['timed_out'] ) {

			$this->connection->close();

                        throw new MPDException( 'Command timed out', self::MPD_TIMEOUT );

                } else {

			if( !count($response) ) {

				$response = $ok;
			}

                        return $response;
                }
        }
private function read() {

                // Check for a connection
                if( !$this->connection->established ) {
                        $this->connection->establish();
                }

                // Set up the array to use for storing the read in MPD response
                $response = array();

		// This will be used in case there is an empty array as the response
		$ok = false;

		// Get the stream meta-data
                $info = stream_get_meta_data( $this->connection->socket );

		//Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info));

                // Wait for output to finish or time out
                while( !feof( $this->connection->socket ) && !$info['timed_out'] ) {

			try {

                        $line = trim( fgets( $this->connection->socket ));

			//Log::info('LxMPD->read : line', array($line));

			$info = stream_get_meta_data( $this->connection->socket );

			//Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info));

                        // We get empty lines sometimes. Ignore them.
                        if( empty( $line ) ) {

                                continue;

                        } else if( strcmp( self::MPD_OK, $line ) == 0 ) {

				$ok = true;
                                break;

                        } else if( strncmp( self::MPD_ERROR, $line, strlen( self::MPD_ERROR ) ) == 0 && preg_match( '/^ACK \[(.*?)\@(.*?)\] \{(.*?)\} (.*?)$/', $line, $matches ) ) {

				// First item in matches will be the errorConstant
				// Second item in matches will be the index of the failed command
				// Third item in matches will be the original command that was run
				// Fourth item in matches will be the error message
				list( $constant, $index, $command, $error ) = $matches;

				Log::info('LxMPD write constant', array($constant));
				//Log::info('LxMPD write index', array($index));
				//Log::info('LxMPD write command', array($command));
				//Log::info('LxMPD write error', array($error));

				throw new MPDException( 'Command failed: '.$error, self::MPD_COMMAND_FAILED );

			} else {

			        $response[] = $line;
                        }

			} catch (Exception $e) {

				Log::info('LxMPD->read : Exception occurred', $e);
			}
                }

                if( $info['timed_out'] ) {

			$this->connection->close();

                        throw new MPDException( 'Command timed out', self::MPD_TIMEOUT );

                } else {

			if( !count($response) ) {

				$response = $ok;
			}

                        return $response;
                }
        }
private function read ( ) { // Check for a connection if ( ! $ this -> connection -> established ) { $ this -> connection -> establish ( ) ; } // Set up the array to use for storing the read in MPD response $ response = array ( ) ; // This will be used in case there is an empty array as the response $ ok = false ; // Get the stream meta-data $ info = stream_get_meta_data ( $ this -> connection -> socket ) ; //Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info)); // Wait for output to finish or time out while ( ! feof ( $ this -> connection -> socket ) && ! $ info [ 'timed_out' ] ) { try { $ line = trim ( fgets ( $ this -> connection -> socket ) ) ; //Log::info('LxMPD->read : line', array($line)); $ info = stream_get_meta_data ( $ this -> connection -> socket ) ; //Log::info('LxMPD->read : stream_get_meta_data for this->connection->socket', array($info)); // We get empty lines sometimes. Ignore them. if ( empty ( $ line ) ) { continue ; } else if ( strcmp ( self :: MPD_OK , $ line ) == 0 ) { $ ok = true ; break ; } else if ( strncmp ( self :: MPD_ERROR , $ line , strlen ( self :: MPD_ERROR ) ) == 0 && preg_match ( '/^ACK \[(.*?)\@(.*?)\] \{(.*?)\} (.*?)$/' , $ line , $ matches ) ) { // First item in matches will be the errorConstant // Second item in matches will be the index of the failed command // Third item in matches will be the original command that was run // Fourth item in matches will be the error message list ( $ constant , $ index , $ command , $ error ) = $ matches ; Log :: info ( 'LxMPD write constant' , array ( $ constant ) ) ; //Log::info('LxMPD write index', array($index)); //Log::info('LxMPD write command', array($command)); //Log::info('LxMPD write error', array($error)); throw new MPDException ( 'Command failed: ' . $ error , self :: MPD_COMMAND_FAILED ) ; } else { $ response [ ] = $ line ; } } catch ( Exception $ e ) { Log :: info ( 'LxMPD->read : Exception occurred' , $ e ) ; } } if ( $ info [ 'timed_out' ] ) { $ this -> connection -> close ( ) ; throw new MPDException ( 'Command timed out' , self :: MPD_TIMEOUT ) ; } else { if ( ! count ( $ response ) ) { $ response = $ ok ; } return $ response ; } }
Reads data from the MPD socket
@return array Array of lines of data
Reads data from the MPD socket