Skip to content

Commit

Permalink
test for continuous query
Browse files Browse the repository at this point in the history
  • Loading branch information
popojargo committed May 8, 2017
1 parent 111bfa1 commit 2dae717
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 372 deletions.
229 changes: 120 additions & 109 deletions src/Adapter/AbstractCouchHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,113 +24,124 @@
*
* @author alexis
*/
abstract class AbstractCouchHttpAdapter implements CouchHttpAdapterInterface {

protected $dsn = null;

/**
* @var array database source name parsed
*/
protected $dsnParsed = null;
protected $options = null;

/**
*
* @var array allowed HTTP methods for REST dialog
*/
protected $httpMethods = [
SELF::METHOD_PUT,
SELF::METHOD_POST,
SELF::METHOD_GET,
SELF::METHOD_DELETE,
SELF::METHOD_COPY
];

/**
*
* @var string the session cookie
*/
protected $sessioncookie = null;

public function setDsn($dsn) {
$this->dsn = $dsn;
}

public function getDsn() {
return $this->dsn;
}

public function setOptions($options) {
$this->options = $options;
}

public function getOptions() {
return $this->options;
}

public function __construct($dsn, $options = []) {
$this->setOptions($options);
$this->dsn = preg_replace('@/+$@', '', $dsn);
$this->dsnParsed = parse_url($this->dsn);

if (!isset($this->dsnParsed['port'])) {
$this->dsnParsed['port'] = 80;
}
}

/**
* set the session cookie to send in the headers
*
* @param string $cookie
* the session cookie ( example : AuthSession=Y291Y2g6NENGNDgzNz )
*
* @return \PHPOnCouch\Couch
*/
public function setSessionCookie($cookie) {
$this->sessioncookie = $cookie;
return $this;
}

/**
* get the session cookie
*
* @return string cookie
*/
public function getSessionCookie() {
return $this->sessioncookie;
}

/**
* get the session cookie
*
* @return string cookie
*/
public function hasSessionCookie() {
return (bool) $this->sessioncookie;
}

/**
* return a part of the data source name
*
* if $part parameter is empty, returns dns array
*
* @param string $part part to return
* @return string DSN part
*/
protected function dsnPart($part = null) {
if (!$part) {
return $this->getDsn();
}
if (isset($this->dsnParsed[$part])) {
return $this->dsnParsed[$part];
}
}

abstract public function query($method, $url, $parameters = [], $data = null, $contentType = null);

abstract public function storeAsFile($url, $data, $contentType);

abstract public function storeFile($url, $file, $contentType);

abstract public function continuousQuery($callable, $method, $url, $parameters = [], $data = null);
abstract class AbstractCouchHttpAdapter implements CouchHttpAdapterInterface
{

protected $dsn = null;

/**
* @var array database source name parsed
*/
protected $dsnParsed = null;
protected $options = null;

/**
*
* @var array allowed HTTP methods for REST dialog
*/
protected $httpMethods = [
SELF::METHOD_PUT,
SELF::METHOD_POST,
SELF::METHOD_GET,
SELF::METHOD_DELETE,
SELF::METHOD_COPY
];

/**
*
* @var string the session cookie
*/
protected $sessioncookie = null;

public function setDsn($dsn)
{
$this->dsn = $dsn;
}

public function getDsn()
{
return $this->dsn;
}

public function setOptions($options)
{
$this->options = $options;
}

public function getOptions()
{
return $this->options;
}

public function __construct($dsn, $options = [])
{
$this->setOptions($options);
$this->dsn = preg_replace('@/+$@', '', $dsn);
if (($parsed = parse_url($this->dsn)))
$this->dsnParsed = $parsed;

if (!isset($this->dsnParsed['port'])) {
$this->dsnParsed['port'] = 80;
}
}

/**
* set the session cookie to send in the headers
*
* @param string $cookie
* the session cookie ( example : AuthSession=Y291Y2g6NENGNDgzNz )
*
* @return \PHPOnCouch\Couch
*/
public function setSessionCookie($cookie)
{
$this->sessioncookie = $cookie;
return $this;
}

/**
* get the session cookie
*
* @return string cookie
*/
public function getSessionCookie()
{
return $this->sessioncookie;
}

/**
* get the session cookie
*
* @return string cookie
*/
public function hasSessionCookie()
{
return (bool) $this->sessioncookie;
}

/**
* return a part of the data source name
*
* if $part parameter is empty, returns dns array
*
* @param string $part part to return
* @return string DSN part
*/
protected function dsnPart($part = '')
{
if (strlen($part) <= 0) {
return $this->getDsn();
}
if (isset($this->dsnParsed[$part])) {
return $this->dsnParsed[$part];
}
}

abstract public function query($method, $url, $parameters = [], $data = null, $contentType = null);

abstract public function storeAsFile($url, $data, $contentType);

abstract public function storeFile($url, $file, $contentType);

abstract public function continuousQuery($callable, $method, $url, $parameters = [], $data = null);
}
32 changes: 16 additions & 16 deletions src/Adapter/CouchHttpAdapterSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use InvalidException;
use InvalidArgumentException;
use PHPOnCouch\Exceptions\CouchException;
use Exception;

/**
Expand All @@ -35,22 +36,21 @@ class CouchHttpAdapterSocket extends AbstractCouchHttpAdapter implements CouchHt

/**
* open the connection to the CouchDB server
*
* @param bool $stream True to setup a stream client, otherwise false.
*
* This function can throw an Exception if it fails
*
* @return boolean Weither the connecti
*
*
* ooopp terminal
sudo reboot
* on is successful
* @return boolean Weither the connection is successful
*
* @throws Exception
*/
protected function connect()
{
$ssl = $this->dsnPart('scheme') == 'https' ? 'ssl://' : '';
$this->socket = @fsockopen($ssl . $this->dsnPart('host'), $this->dsnPart('port'), $errNum, $errStr);
$errNum = -1;
$errStr = '';
$this->socket = fsockopen($ssl . $this->dsnPart('host'), $this->dsnPart('port'), $errNum, $errStr);
if (!$this->socket) {
$errMsg = 'Could not open connection to ' . $this->dsnPart('host') . ':';
$errMsg .= $this->dsnPart('port') . ': ' . $errStr . ' (' . $errNum . ')';
Expand Down Expand Up @@ -210,20 +210,21 @@ public function continuousQuery($callable, $method, $url, $parameters = [], $dat
$url = $url . '?' . http_build_query($parameters);
//Send the request to the socket
$request = $this->buildRequest($method, $url, $data, null);
if (!$this->connect())
if (!$this->connect(true))
return false;

fwrite($this->socket, $request);
//Read the headers and check that the response is valid
$response = '';
$headers = false;
while (!feof($this->socket) && !$headers) {
$response .= fgets($this->socket);
$hasHeaders = false;
while (!feof($this->socket) && !$hasHeaders) {
$response .= fgets($this->socket, 128);
if ($response == "HTTP/1.1 100 Continue\r\n\r\n") {
$response = '';
continue;
} //Ignore 'continue' headers, they will be followed by the real header.
else if (preg_match("/\r\n\r\n$/", $response)) {
$headers = true;
$hasHeaders = true;
}
}
$headers = explode("\n", trim($response));
Expand All @@ -246,14 +247,13 @@ public function continuousQuery($callable, $method, $url, $parameters = [], $dat
$read = [$this->socket];
($numChangedStreams = stream_select($read, $e, $e2, 1));
if (false === $numChangedStreams) {
$this->socket = null;
$this->disconnect();
} elseif ($numChangedStreams > 0) {
$line = fgets($this->socket);
if (strlen(trim($line))) {
$break = call_user_func($callable, json_decode($line), $clone);
if ($break === false) {
fclose($this->socket);
}
if ($break === false)
$this->disconnect();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Couch.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function dsnPart($part = null)
* the body will by json_decode()d
*
* @static
* @param string $rawData data sent back by the server
* @param string|boolean $rawData data sent back by the server
* @param boolean $jsonAsArray is true, the json response will be
* decoded as an array. Is false, it's decoded as an object
* @return array CouchDB response
Expand Down
Loading

0 comments on commit 2dae717

Please sign in to comment.