Skip to content

Zyre binding #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c79c471
Add ZMQZyre class, a binding of the libzyre
wysman Jul 2, 2014
e3ad68f
Update api.h
wysman Jul 2, 2014
b18fec2
Add test on ZMQZyre::getSocket
wysman Jul 2, 2014
b35155c
Refactor ZMQZyre::getSocket
wysman Jul 3, 2014
994f3ca
Display version of libs
wysman Jul 3, 2014
929b108
Remove a camelcase variable
wysman Jul 3, 2014
f0ccd4f
Use real tabs
wysman Jul 3, 2014
7bd6f6c
add zend_parse_parameters_none where missing
wysman Jul 3, 2014
574d4a0
Fix returns after zend_parse_parameters
wysman Jul 3, 2014
fa37d9b
Use PHP_ZMQ_ZYRE_OBJECT to create this pointer
wysman Jul 3, 2014
0027747
Ensure peer or groupe name are not empty
wysman Jul 3, 2014
cfabf10
Check for null pointers
wysman Jul 3, 2014
5d74206
Set persistent_id to NULL
wysman Jul 3, 2014
64d6cd1
Add test of using ZMQPoll with ZMQZyre
wysman Jul 3, 2014
c4be910
Add zyre tests file in the package.xml
wysman Jul 3, 2014
7a2297f
Use correct filename... need a break ;)
wysman Jul 3, 2014
05c5c8d
Throw exception, if group name or peer id are empty
wysman Jul 8, 2014
89a3911
ecalloc, do not need null check
wysman Jul 8, 2014
0955820
Fix memory leak
wysman Jul 8, 2014
40ed1b5
Use zsys_version to find libczmq version at execution
wysman Jul 16, 2014
2d5e18c
Add gitignore
wysman Jul 16, 2014
46e7b8f
Merge remote-tracking branch 'origin/master' into zyre
wysman Jul 16, 2014
db12454
Display the CZMQ & Zyre librairy version, only if detected by configu…
wysman Jul 16, 2014
2c2e564
Fix build if zyre is not available
wysman Jul 17, 2014
104e908
Add install from bianary repo of czmq & zyre
wysman Jul 22, 2014
bb5f512
Reduce travis test matrix for test, will be revert
wysman Jul 22, 2014
b087ddb
be verbose on link creation for travis log
wysman Jul 22, 2014
0ec9791
configure PKG_CONFIG_PATH in travis bootstrap
wysman Jul 22, 2014
c4b9fc2
typo
wysman Jul 22, 2014
05f8a3f
Try direct export, configure script do not detect czmq/zyre
wysman Jul 22, 2014
1f220e1
add debugs
wysman Jul 22, 2014
cfcc299
Test autodetection of libzmq in configure script
wysman Jul 22, 2014
1a18827
Ensure the php extension build is OK, before running test
wysman Jul 22, 2014
6a96e40
Pass TSRMLS_C into zhash_foreach callback
wysman Jul 22, 2014
45190b8
Remove pre-installed version of zmq by travis
wysman Jul 22, 2014
c99745f
Be more verbose on pkg-config
wysman Jul 22, 2014
141ff43
Restore with-zmq option on configure
wysman Jul 23, 2014
af91e6a
Test build from source for czmq/zyre ibs with the same prefix as zmq …
wysman Jul 23, 2014
3ecd031
typo
wysman Jul 23, 2014
ae15fc2
typo
wysman Oct 8, 2014
0cf5fbb
Merge branch 'master' into zyre
wysman Oct 13, 2014
6ce250f
Reduce tests matrix, will be revert
wysman Oct 13, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion api.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,89 @@ public function count() {}
*/
public function clear() {}
}

class ZMQZyre {
/**
* Construct a ZMQZyre
*
* @param ZMQContext $context
* @return void
*/
public function __construct(ZMQContext $ZMQContext) {}
/**
* Set node header; these are provided to other nodes during discovery and come in each ENTER message.
*
* @param string $name
* @param string $value
* @return void
*/
public function setHeader($name, $value="") {}
/**
* Start node, after setting header values. When you start a node it begins discovery and connection.
There is no stop method; to stop a node, destroy it.
*
* @return void
*/
public function start() {}
/**
* Stop node, this signals to other peers that this node will go away.
This is polite; however you can also just destroy the node without stopping it.
*
* @return void
*/
public function stop() {}
/**
* Join a named group; after joining a group you can send messages
to the group and all Zyre nodes in that group will receive them.
*
* @param string $group
* @return void
*/
public function join($group) {}
/**
* Leave a group.
*
* @param string $group
* @return void
*/
public function leave($group) {}
/**
* Receive next message from network; the message may be a control
message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
Returns associative array, or NULL if interrupted
*
* @return void
*/
public function recv() {}
/**
* Send a message on the network to a specific peer
*
* @param string $peer
* @param string $data
* @return void
*/
public function sendPeer($peer, $data) {}
/**
* Send a message on the network for a group
*
* @param string $group
* @param string $data
* @return void
*/
public function sendGroup($group, $data) {}
/**
* Get zyre ZeroMQ socket, for polling or receiving messages
*
* @return ZMQSocket
*/
public function getSocket() {}
}

class ZMQException extends Exception {}
class ZMQContextException extends ZMQException {}
class ZMQSocketException extends ZMQException {}
class ZMQPollException extends ZMQException {}
class ZMQDeviceException extends ZMQException {}
class ZMQZyreException extends ZMQException {}
?>
</code></pre>
</code></pre>
34 changes: 34 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@ if test "$PHP_ZMQ" != "no"; then
AC_MSG_ERROR(Unable to find libzmq installation)
fi

AC_MSG_CHECKING(for CZMQ)
if $PKG_CONFIG --exists libczmq; then
PHP_CZMQ_VERSION=`$PKG_CONFIG libczmq --modversion`
PHP_CZMQ_PREFIX=`$PKG_CONFIG libczmq --variable=prefix`
AC_MSG_RESULT([found version $PHP_CZMQ_VERSION in $PHP_CZMQ_PREFIX])

PHP_CZMQ_LIBS=`$PKG_CONFIG libczmq --libs`
PHP_CZMQ_INCS=`$PKG_CONFIG libczmq --cflags`

PHP_EVAL_LIBLINE($PHP_CZMQ_LIBS, ZMQ_SHARED_LIBADD)
PHP_EVAL_INCLINE($PHP_CZMQ_INCS)

AC_DEFINE([HAVE_CZMQ], [], [CZMQ was found])
else
AC_MSG_RESULT([no])
fi

AC_MSG_CHECKING(for Zyre)
if $PKG_CONFIG --exists libzyre; then
PHP_ZYRE_VERSION=`$PKG_CONFIG libzyre --modversion`
PHP_ZYRE_PREFIX=`$PKG_CONFIG libzyre --variable=prefix`
AC_MSG_RESULT([found version $PHP_ZYRE_VERSION in $PHP_ZYRE_PREFIX])

PHP_ZYRE_LIBS=`$PKG_CONFIG libzyre --libs`
PHP_ZYRE_INCS=`$PKG_CONFIG libzyre --cflags`

PHP_EVAL_LIBLINE($PHP_ZYRE_LIBS, ZMQ_SHARED_LIBADD)
PHP_EVAL_INCLINE($PHP_ZYRE_INCS)

AC_DEFINE([HAVE_ZYRE], [], [ZYRE was found])
else
AC_MSG_RESULT([no])
fi

AC_CHECK_HEADERS([stdint.h],[php_zmq_have_stdint=yes; break;])
if test $php_zmq_have_stdint != "yes"; then
AC_MSG_ERROR(Unable to find stdint.h)
Expand Down
12 changes: 12 additions & 0 deletions php_zmq.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@
extern zend_module_entry zmq_module_entry;
#define phpext_zmq_ptr &zmq_module_entry

/* PHP 5.4 */
#if PHP_VERSION_ID < 50399
# define object_properties_init(zo, class_type) { \
zval *tmp; \
zend_hash_copy((*zo).properties, \
&class_type->default_properties, \
(copy_ctor_func_t) zval_add_ref, \
(void *) &tmp, \
sizeof(zval *)); \
}
#endif

#endif /* _PHP_ZMQ_H_ */
18 changes: 18 additions & 0 deletions php_zmq_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@

#include <zmq.h>

#ifdef HAVE_CZMQ && HAVE_ZYRE
# include <czmq.h>
# include <zyre.h>
# if ZYRE_VERSION_MAJOR == 1
# define HAVE_ZYRE_1
# endif
#endif

#ifdef PHP_WIN32
# include "win32/php_stdint.h"
#else
Expand Down Expand Up @@ -263,5 +271,15 @@ ZEND_BEGIN_MODULE_GLOBALS(php_zmq)
php_zmq_clock_ctx_t *clock_ctx;
ZEND_END_MODULE_GLOBALS(php_zmq)

#ifdef HAVE_ZYRE_1
#define PHP_ZMQ_ZYRE_OBJECT php_zmq_zyre *this = (php_zmq_zyre *)zend_object_store_get_object(getThis() TSRMLS_CC)

typedef struct _php_zmq_zyre {
zend_object zend_object;
zctx_t *shadow_context;
zyre_t *zyre;
zval *internalSocket;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this member camelcase and the rest are underscore?

} php_zmq_zyre;
#endif

#endif /* _PHP_ZMQ_PRIVATE_H_ */
6 changes: 6 additions & 0 deletions tests/skipzyre.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

if (!extension_loaded("zmq")) die("skip");
if (!class_exists('ZMQZyre')) die('skip');

?>
15 changes: 15 additions & 0 deletions tests/zyre-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Test [Zyre] Object can be instancied multiple time
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipzyre.inc'); ?>
--FILE--
<?php

$ctx = new ZMQContext;
$z1 = new ZMQZyre($ctx);
$z2 = new ZMQZyre($ctx);
$z3 = new ZMQZyre($ctx);
echo "OK";

--EXPECTF--
OK
42 changes: 42 additions & 0 deletions tests/zyre-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Test [Zyre] Headers are received on ENTER event
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipzyre.inc'); ?>
--FILE--
<?php

$ctx = new ZMQContext;

$z1 = new ZMQZyre($ctx);
$z1->setHeader('X-TEST', 'Z1');
$z1->start();

$z2 = new ZMQZyre($ctx);
$z2->setHeader('X-TEST', 'Z2');
$z2->start();

usleep(100000);

$obj = $z1->recv();
var_dump($obj->command);
var_dump($obj->headers->{'X-TEST'});

$obj = $z2->recv();
var_dump($obj->command);
var_dump($obj->headers->{'X-TEST'});

$z1->stop();

usleep(100000);

$obj = $z2->recv();
var_dump($obj->command);

$z2->stop();

--EXPECTF--
string(5) "ENTER"
string(2) "Z2"
string(5) "ENTER"
string(2) "Z1"
string(4) "EXIT"
79 changes: 79 additions & 0 deletions tests/zyre-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
Test [Zyre] Zyre objects can exchange data in a group of peer
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipzyre.inc'); ?>
--FILE--
<?php

$ctx = new ZMQContext;

$z1 = new ZMQZyre($ctx);
$z1->start();
$z1->join('ZMQZyreTestGroup');


$z2 = new ZMQZyre($ctx);
$z2->start();
$z2->join('ZMQZyreTestGroup');


usleep(100000);

// Enter
$obj = $z1->recv();
var_dump($obj->command);

// Join
$obj = $z1->recv();
var_dump($obj->command);
var_dump($obj->group);

// Enter
$obj = $z2->recv();
var_dump($obj->command);

// Join
$obj = $z2->recv();
var_dump($obj->command);
var_dump($obj->group);

$z2->sendGroup('ZMQZyreTestGroup', 'Some usefull data');

usleep(100000);

// SHOUT
$obj = $z1->recv();
var_dump($obj->group);
var_dump($obj->data);

$z1->leave('ZMQZyreTestGroup');

usleep(100000);

// Leave
$obj = $z2->recv();
var_dump($obj->command);
var_dump($obj->group);

$z1->stop();

usleep(100000);

// Exit
$obj = $z2->recv();
var_dump($obj->command);

$z2->stop();

--EXPECTF--
string(5) "ENTER"
string(4) "JOIN"
string(16) "ZMQZyreTestGroup"
string(5) "ENTER"
string(4) "JOIN"
string(16) "ZMQZyreTestGroup"
string(16) "ZMQZyreTestGroup"
string(17) "Some usefull data"
string(5) "LEAVE"
string(16) "ZMQZyreTestGroup"
string(4) "EXIT"
49 changes: 49 additions & 0 deletions tests/zyre-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
Test [Zyre] Zyre objects can exchange data with a peer
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipzyre.inc'); ?>
--FILE--
<?php

$ctx = new ZMQContext;

$z1 = new ZMQZyre($ctx);
$z1->start();

$z2 = new ZMQZyre($ctx);
$z2->start();

usleep(100000);

// Enter
$obj = $z1->recv();
var_dump($obj->command);

// Enter
$obj = $z2->recv();
var_dump($obj->command);
$peer = $obj->peer;

$z2->sendPeer($peer, 'Some very usefull data');

usleep(100000);

// SHOUT
$obj = $z1->recv();
var_dump($obj->data);

$z1->stop();

usleep(100000);

// Exit
$obj = $z2->recv();
var_dump($obj->command);

$z2->stop();

--EXPECTF--
string(5) "ENTER"
string(5) "ENTER"
string(22) "Some very usefull data"
string(4) "EXIT"
Loading