From c38e9d2db8e01e809087d39906352d7e50aa681e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Sun, 9 Mar 2014 16:51:52 +0100 Subject: [PATCH 1/8] Changed to new stream_socket_client --- src/FuseSource/Stomp/Stomp.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index 29491e7..7a597ca 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -66,6 +66,7 @@ class Stomp protected $_socket = null; protected $_hosts = array(); protected $_params = array(); + protected $_ctx = null; protected $_subscriptions = array(); protected $_defaultPort = 61613; protected $_currentHost = - 1; @@ -82,11 +83,15 @@ class Stomp * Constructor * * @param string $brokerUri Broker URL + * @param array $opts options will be passed to stream_context_create and + * eventually be used to setup the connection. See the respective + * documentation on php.net on how to fill this array. * @throws StompException */ - public function __construct ($brokerUri) + public function __construct ($brokerUri, $opts = array()) { $this->_brokerUri = $brokerUri; + $this->_ctx = stream_context_create($opts); $this->_init(); } /** @@ -169,9 +174,17 @@ protected function _makeConnection () fclose($this->_socket); $this->_socket = null; } - $this->_socket = @fsockopen($scheme . '://' . $host, $port, $connect_errno, $connect_errstr, $this->_connect_timeout_seconds); + + $this->_socket = stream_socket_client( + $scheme.'://'.$host.':'.$port, + $connect_errno, $connect_errstr, + $this->_connect_timeout_seconds, + STREAM_CLIENT_CONNECT, $this->_ctx + ); if (!is_resource($this->_socket) && $att >= $this->_attempts && !array_key_exists($i + 1, $this->_hosts)) { - throw new StompException("Could not connect to $host:$port ($att/{$this->_attempts})"); + throw new StompException("Could not connect to $host:$port + ($att/{$this->_attempts}), ErrorCode $connect_errno, Reason: + $connect_errstr"); } else if (is_resource($this->_socket)) { $connected = true; $this->_currentHost = $i; From 259e403daaa893dc0344c29040871ac2188c6c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Sun, 9 Mar 2014 16:52:07 +0100 Subject: [PATCH 2/8] Example with certificate --- examples/first.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/first.php b/examples/first.php index 6018ada..045bf82 100644 --- a/examples/first.php +++ b/examples/first.php @@ -1,5 +1,8 @@ array( + 'local_cert' => './cachain.pem', + 'passphrase' => 'bcop' + ) +); +$con = new Stomp("ssl://localhost:61613", $opts); // connect $con->connect(); // send a message to the queue @@ -44,4 +53,4 @@ // disconnect $con->disconnect(); -?> \ No newline at end of file +?> From 55d31adebf090bcefc2fbb6bf26e5ec9c69b7afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Tue, 18 Mar 2014 11:42:32 +0100 Subject: [PATCH 3/8] suppress errors and warnings on connection creation, are handled by a later check --- src/FuseSource/Stomp/Stomp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index 7a597ca..f23aed9 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -175,7 +175,7 @@ protected function _makeConnection () $this->_socket = null; } - $this->_socket = stream_socket_client( + $this->_socket = @stream_socket_client( $scheme.'://'.$host.':'.$port, $connect_errno, $connect_errstr, $this->_connect_timeout_seconds, From e80db84ea7592a2175175ee4bb95bee98bd7982d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Thu, 27 Mar 2014 10:21:26 +0100 Subject: [PATCH 4/8] Make the connection timeout a default parameter of the constructor --- src/FuseSource/Stomp/Stomp.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index f23aed9..86e53fd 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -76,7 +76,7 @@ class Stomp protected $_sessionId; protected $_read_timeout_seconds = 60; protected $_read_timeout_milliseconds = 0; - protected $_connect_timeout_seconds = 60; + protected $_connect_timeout_seconds; protected $_waitbuf = array(); /** @@ -88,10 +88,11 @@ class Stomp * documentation on php.net on how to fill this array. * @throws StompException */ - public function __construct ($brokerUri, $opts = array()) + public function __construct ($brokerUri, $opts = array(), $connect_timeout_seconds = 60) { $this->_brokerUri = $brokerUri; $this->_ctx = stream_context_create($opts); + $this->_connect_timeout_seconds = $connect_timeout_seconds; $this->_init(); } /** From 60c9254dc04e252d16252cafe8969a22b893837c Mon Sep 17 00:00:00 2001 From: rethab Date: Fri, 28 Mar 2014 15:44:58 +0100 Subject: [PATCH 5/8] Added link to example --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 77f5962..d0ad68d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Documentation * [Web Site](http://stomp.fusesource.org/documentation/php/) +Step by Step: Certificate based Authentication +---------------------------------------------- +https://github.com/rethab/php-stomp-cert-example + Tests ----- From 71697bdfd42700929f755cc30b6047fb6548363b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Tue, 1 Apr 2014 10:39:47 +0200 Subject: [PATCH 6/8] replace flclose with stream_socket_shutdown --- src/FuseSource/Stomp/Stomp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index 86e53fd..505201f 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -172,7 +172,7 @@ protected function _makeConnection () $port = $this->_defaultPort; } if ($this->_socket != null) { - fclose($this->_socket); + stream_socket_shutdown($this->_socket); $this->_socket = null; } @@ -516,7 +516,7 @@ public function disconnect () if (is_resource($this->_socket)) { $this->_writeFrame(new Frame('DISCONNECT', $headers)); - fclose($this->_socket); + stream_socket_shutdown($this->_socket); } $this->_socket = null; $this->_sessionId = null; From c51c7938e13fc00a4d0031b5d2a8d8add0670df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Tue, 1 Apr 2014 15:32:16 +0200 Subject: [PATCH 7/8] added attempts parameter to constructor --- src/FuseSource/Stomp/Stomp.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index 505201f..b8f6c57 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -70,13 +70,13 @@ class Stomp protected $_subscriptions = array(); protected $_defaultPort = 61613; protected $_currentHost = - 1; - protected $_attempts = 10; + protected $_attempts = null; protected $_username = ''; protected $_password = ''; protected $_sessionId; protected $_read_timeout_seconds = 60; protected $_read_timeout_milliseconds = 0; - protected $_connect_timeout_seconds; + protected $_connect_timeout_seconds = null; protected $_waitbuf = array(); /** @@ -88,11 +88,12 @@ class Stomp * documentation on php.net on how to fill this array. * @throws StompException */ - public function __construct ($brokerUri, $opts = array(), $connect_timeout_seconds = 60) + public function __construct ($brokerUri, $opts = array(), $connect_timeout_seconds = 60, $attempts = 10) { $this->_brokerUri = $brokerUri; $this->_ctx = stream_context_create($opts); $this->_connect_timeout_seconds = $connect_timeout_seconds; + $this->_attempts = $attempts; $this->_init(); } /** From ffc7d2ba88559bfea8f0b5163025bcdfa40dfa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Wed, 2 Apr 2014 14:09:27 +0200 Subject: [PATCH 8/8] shutdown stream for read and write --- src/FuseSource/Stomp/Stomp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FuseSource/Stomp/Stomp.php b/src/FuseSource/Stomp/Stomp.php index b8f6c57..642cb25 100755 --- a/src/FuseSource/Stomp/Stomp.php +++ b/src/FuseSource/Stomp/Stomp.php @@ -173,7 +173,7 @@ protected function _makeConnection () $port = $this->_defaultPort; } if ($this->_socket != null) { - stream_socket_shutdown($this->_socket); + stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR); $this->_socket = null; } @@ -517,7 +517,7 @@ public function disconnect () if (is_resource($this->_socket)) { $this->_writeFrame(new Frame('DISCONNECT', $headers)); - stream_socket_shutdown($this->_socket); + stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR); } $this->_socket = null; $this->_sessionId = null;