Skip to content

Commit

Permalink
Prep 2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Jan 19, 2018
1 parent 23b5cc4 commit 9353aac
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 49 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.5.0 (2018-01-19)

## Added

- Added `use_persistent_connection` in adapter connection. By default to `false` but can be enabled
if connection times are problematic (not recommended if using basic auth).

## 2.4.3 (2018-01-18)

### Updated
Expand All @@ -28,7 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Bugfix

- Regression with https connection introduced in 2.4.0
- Regression with `https` connection introduced in 2.4.0

## 2.4.0 (2017-10-09)

Expand Down
1 change: 1 addition & 0 deletions doc/bridge_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The `Soluble\Japha\Bridge\Adapter` constructor requires `$options`, an associati
| Parameter | Description |
|------------------|------------------------------------------|
|`servlet_address` | In the form: `http(s)://<host>:<port>/<context_uri>/servlet.phpjavabridge` |
|`use_persistent_connection` | Since @2.5.0. By default `false`, set `true` for better connection times if needed. |

!!! tip
Since v2.4.0, you can also provide basic auth in the `servlet_address`, i.e.
Expand Down
1 change: 1 addition & 0 deletions src/Soluble/Japha/Bridge/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Adapter
* $ba = new Adapter([
* 'driver' => 'Pjb62',
* 'servlet_address' => 'http://127.0.0.1:8080/javabridge-bundle/java/servlet.phpjavabridge'
* //'use_persistent_connection' => false
* //'java_default_timezone' => null,
* //'java_prefer_values' => true,
* //'force_simple_xml_parser' => false
Expand Down
1 change: 1 addition & 0 deletions src/Soluble/Japha/Bridge/Adapter/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function getTimeZoneId(): string
* Set system default timezone.
*
* @throws UnsupportedTzException
* @throws Bridge\Exception\JavaException
*
* @param string|Interfaces\JavaObject|\DateTimeZone $timezone timezone id, Java(java.util.Timezone) or php DateTimeZone
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Soluble/Japha/Bridge/Driver/Pjb62/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Client
public const PARAM_JAVA_LOG_LEVEL = 'JAVA_LOG_LEVEL';
public const PARAM_JAVA_INTERNAL_ENCODING = 'JAVA_INTERNAL_ENCODING';
public const PARAM_XML_PARSER_FORCE_SIMPLE_PARSER = 'XML_PARSER_FORCE_SIMPLE_PARSER';
public const PARAM_USE_PERSISTENT_CONNECTION = 'USE_PERSISTENT_CONNECTION';

public const DEFAULT_PARAMS = [
self::PARAM_JAVA_HOSTS => 'localhost',
Expand All @@ -70,7 +71,8 @@ class Client
self::PARAM_JAVA_RECV_SIZE => 8192,
self::PARAM_JAVA_LOG_LEVEL => null,
self::PARAM_JAVA_INTERNAL_ENCODING => 'UTF-8',
self::PARAM_XML_PARSER_FORCE_SIMPLE_PARSER => false
self::PARAM_XML_PARSER_FORCE_SIMPLE_PARSER => false,
self::PARAM_USE_PERSISTENT_CONNECTION => false
];

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/NativeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ public function parse(): void
$this->event = false;
$this->buf = $this->client->read($this->java_recv_size);
$len = strlen($this->buf);
if (!xml_parse($this->parser, $this->buf, $len == 0)) {
if (!xml_parse($this->parser, $this->buf, $len === 0)) {
$this->client->protocol->handler->shutdownBrokenConnection(
sprintf('protocol error: %s,%s at col %d. Check the back end log for OutOfMemoryErrors.', $this->buf, xml_error_string(xml_get_error_code($this->parser)), xml_get_current_column_number($this->parser))
sprintf(
'protocol error: "buf: {%s}", %s at col %d. Check the back end log for OutOfMemoryErrors.',
$this->buf,
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_column_number($this->parser)
)
);
}
} while (!$this->event || $this->level > 0);
Expand Down
1 change: 1 addition & 0 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/Pjb62Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Pjb62Driver extends AbstractDriver
*
* $ba = new Pjb62Driver([
* 'servlet_address' => 'http://127.0.0.1:8080/javabridge-bundle/servlet.phpjavabridge'
* //'use_persistent_connection' => false,
* //'java_default_timezone' => null,
* //'java_prefer_values' => true,
* //'java_log_level' => null,
Expand Down
9 changes: 7 additions & 2 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/PjbProxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PjbProxyClient implements ClientInterface
'java_log_level' => null,
'java_send_size' => 8192,
'java_recv_size' => 8192,
// By default do not use persistent connection
'use_persistent_connection' => false,
// java_prefer_values=true is the default working mode
// of the soluble-japha client... It may be less efficient,
// because it casts java String, Boolean, Integer... objects
Expand All @@ -48,7 +50,7 @@ class PjbProxyClient implements ClientInterface
'java_prefer_values' => true,
// use SimpleParser (pure PHP code) even if NativeParser (based on xml_* php functions) may be used
// should only be used to workaround bugs or limitations regarding the xml extension
'force_simple_xml_parser' => false
'force_simple_xml_parser' => false,
];

/**
Expand Down Expand Up @@ -121,14 +123,16 @@ protected function __construct(array $options, LoggerInterface $logger)
* $options can be :
* "java_send_size" => 8192,
* "java_recv_size" => 8192,
* "use_persistent_connection" => false
* "java_log_level' => null,
* "java_prefer_values" => true (see note)
*
* <code>
* $options = [
* 'servlet_address' => 'http://127.0.0.1:8080/javabridge-bundle/servlet.phpjavabridge'
* "servlet_address" => 'http://127.0.0.1:8080/javabridge-bundle/servlet.phpjavabridge'
* "java_send_size" => 8192,
* "java_recv_size" => 8192,
* "use_persistent_connection" => false,
* "internal_encoding" => 'UTF-8'
* ];
* $pjb = PjbProxyClient::getInstance($options, $logger);
Expand Down Expand Up @@ -215,6 +219,7 @@ protected function loadClient(): void
Client::PARAM_JAVA_RECV_SIZE => $options['java_recv_size'],
Client::PARAM_JAVA_LOG_LEVEL => $options['java_log_level'],
Client::PARAM_XML_PARSER_FORCE_SIMPLE_PARSER => $options['force_simple_xml_parser'],
Client::PARAM_USE_PERSISTENT_CONNECTION => $options['use_persistent_connection']
]);

self::$client = new Client($params, $this->logger);
Expand Down
38 changes: 24 additions & 14 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public function setSocketHandler(SocketHandler $socketHandler): void
$this->socketHandler = $socketHandler;
}

/**
* @throws BrokenConnectionException
*/
public function getSocketHandler(): SocketHandler
{
if ($this->socketHandler === null) {
Expand Down Expand Up @@ -236,30 +239,38 @@ public function createHttpHandler()
}

/**
* @param string $channelName With format <host:port>. If host is omitted, '127.0.0.1' by default
*
* @throws ConnectionException
* @throws Exception\IOException
*/
public function createSimpleHandler(?string $name = ''): SocketHandler
public function createSimpleHandler(string $channelName): SocketHandler
{
$channelName = $name;
$errno = null;
$errstr = null;
if (is_numeric($channelName)) {
$peer = @pfsockopen($host = '127.0.0.1', $channelName, $errno, $errstr, 5);
$host = '127.0.0.1';
$port = $channelName;
} else {
$type = $channelName[0];
list($host, $channelName) = explode(':', $channelName);
$peer = pfsockopen($host, $channelName, $errno, $errstr, 20);
if (!$peer) {
throw new ConnectionException("No Java server at $host:$channelName. Error message: $errstr ($errno)");
}
list($host, $port) = explode(':', $channelName);
}
$timeout = in_array($host, ['localhost', '127.0.0.1']) ? 5 : 20;
$peer = pfsockopen($host, $port, $errno, $errstr, $timeout);
if (!$peer) {
throw new ConnectionException(
sprintf(
'No Java server at %s:%s. Error message: %s (errno: %s)',
$host,
$port,
$errstr,
$errno
)
);
}

stream_set_timeout($peer, -1);
$handler = new SocketHandler($this, new SocketChannelP($peer, $host, $this->java_recv_size, $this->java_send_size));
//$compatibility = java_getCompatibilityOption($this->client);
$compatibility = PjbProxyClient::getInstance()->getCompatibilityOption($this->client);
$this->write("\177$compatibility");
$this->serverName = "127.0.0.1:$channelName";
$this->serverName = "$host:$port";

return $handler;
}
Expand All @@ -270,7 +281,6 @@ public function java_get_simple_channel(): ?string
$java_servlet = $this->java_servlet;

return ($java_hosts && (!$java_servlet || ($java_servlet === 'Off'))) ? $java_hosts : null;
//return (JAVA_HOSTS && (!JAVA_SERVLET || (JAVA_SERVLET == "Off"))) ? JAVA_HOSTS : null;
}

public function createHandler()
Expand Down
17 changes: 10 additions & 7 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/SimpleHttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@

class SimpleHttpHandler extends SocketHandler
{
const DEFAULT_CONNECT_TIMEOUT = 20.0;

public $headers;

/**
Expand Down Expand Up @@ -124,7 +122,8 @@ public function __construct(Protocol $protocol, $ssl, $host, $port, $java_servle
}

/**
* @throws Exception\IllegalStateException on channel creation error
* @throws Exception\IllegalStateException on channel creation error
* @throws Exception\BrokenConnectionException if all goes wrong
*/
protected function createChannel()
{
Expand All @@ -147,7 +146,7 @@ protected function createChannel()
$this->protocol->handler->shutdownBrokenConnection('Broken local connection handle');
}
$this->protocol->client->sendBuffer = null;
$ret2 = $this->protocol->handler->read(1);
$this->protocol->handler->read(1);
}
}

Expand Down Expand Up @@ -193,6 +192,9 @@ public function getWebApp(): string
return $context;
}

/**
* @throws Exception\BrokenConnectionException
*/
public function write(string $data): ?int
{
return $this->protocol->getSocketHandler()->write($data);
Expand All @@ -209,7 +211,7 @@ public function doSetCookie($key, $val, $path)
}

/**
* @param int $size
* @throws Exception\BrokenConnectionException
*/
public function read(int $size): string
{
Expand All @@ -225,13 +227,14 @@ public function read(int $size): string
*/
public function getChannel(string $channelName): SocketChannelP
{
$persistent = $this->protocol->client->getParam(Client::PARAM_USE_PERSISTENT_CONNECTION);
try {
$streamSocket = new StreamSocket(
$this->ssl === 'ssl://' ? StreamSocket::TRANSPORT_SSL : StreamSocket::TRANSPORT_TCP,
$this->host.':'.$channelName,
self::DEFAULT_CONNECT_TIMEOUT,
null,
[],
true
$persistent
);
$socket = $streamSocket->getSocket();
} catch (\Throwable $e) {
Expand Down
23 changes: 20 additions & 3 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/SimpleHttpTunnelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class SimpleHttpTunnelHandler extends SimpleHttpHandler
*/
protected $isRedirect;

/**
* @var string
*/
protected $httpHeadersPayload;

/**
Expand Down Expand Up @@ -91,6 +94,7 @@ public function createChannel()
public function shutdownBrokenConnection(string $msg = '', int $code = null): void
{
if (is_resource($this->socket)) {
fflush($this->socket);
fclose($this->socket);
}
PjbProxyClient::unregisterAndThrowBrokenConnectionException($msg, $code);
Expand All @@ -102,10 +106,13 @@ public function shutdownBrokenConnection(string $msg = '', int $code = null): vo
protected function open()
{
try {
$persistent = $this->protocol->client->getParam(Client::PARAM_USE_PERSISTENT_CONNECTION);
$streamSocket = new StreamSocket(
$this->ssl === 'ssl://' ? StreamSocket::TRANSPORT_SSL : StreamSocket::TRANSPORT_TCP,
$this->host.':'.$this->port,
self::DEFAULT_CONNECT_TIMEOUT
null,
StreamSocket::DEFAULT_CONTEXT,
$persistent
);
$socket = $streamSocket->getSocket();
} catch (\Throwable $e) {
Expand Down Expand Up @@ -240,11 +247,21 @@ public function write(string $data): ?int

$count = @fwrite($this->socket, $request);
if ($count === false) {
$this->shutdownBrokenConnection('Cannot write to socket, broken connection handle');
$this->shutdownBrokenConnection(
sprintf(
'Cannot write to socket, broken connection handle: %s',
error_get_last()
)
);
}
$flushed = @fflush($this->socket);
if ($flushed === false) {
$this->shutdownBrokenConnection('Cannot flush to socket, broken connection handle');
$this->shutdownBrokenConnection(
sprintf(
'Cannot flush to socket, broken connection handle: %s',
error_get_last()
)
);
}

return (int) $count;
Expand Down
1 change: 1 addition & 0 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/SocketChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function fread(int $size): ?string
public function shutdownBrokenConnection(?string $msg = ''): void
{
if (is_resource($this->peer)) {
fflush($this->peer);
fclose($this->peer);
}
}
Expand Down
Loading

0 comments on commit 9353aac

Please sign in to comment.