Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added session handler

  • Loading branch information...
commit c77394fd33b3eba143eb08de7d3a16dec1779fc5 1 parent a204da8
Mikko Koppanen authored
View
10 LICENSE
@@ -1,6 +1,6 @@
--------------------------------------------------------------------
- The PHP License, Version 3.0
-Copyright (c) 1999 - 2003 The PHP Group. All rights reserved.
+ The PHP License, version 3.01
+Copyright (c) 1999 - 2010 The PHP Group. All rights reserved.
--------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
@@ -38,8 +38,8 @@ are met:
6. Redistributions of any form whatsoever must retain the following
acknowledgment:
- "This product includes PHP, freely available from
- <http://www.php.net/>".
+ "This product includes PHP software, freely available from
+ <http://www.php.net/software/>".
THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND
ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
@@ -64,5 +64,5 @@ The PHP Group can be contacted via Email at group@php.net.
For more information on the PHP Group and the PHP project,
please see <http://www.php.net>.
-This product includes the Zend Engine, freely available at
+PHP includes the Zend Engine, freely available at
<http://www.zend.com>.
View
2  config.m4
@@ -53,7 +53,7 @@ if test "$PHP_ZOOKEEPER" != "no"; then
PHP_ADD_LIBRARY_WITH_PATH(zookeeper_mt, $PHP_LIBZOOKEEPER_DIR/lib, ZOOKEEPER_SHARED_LIBADD)
PHP_SUBST(ZOOKEEPER_SHARED_LIBADD)
- PHP_NEW_EXTENSION(zookeeper, php_zookeeper.c , $ext_shared,,)
+ PHP_NEW_EXTENSION(zookeeper, php_zookeeper.c php_zookeeper_session.c, $ext_shared,,)
fi
View
39 php_zookeeper.c
@@ -1,11 +1,11 @@
/*
+----------------------------------------------------------------------+
- | Copyright (c) 2009 The PHP Group |
+ | Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
- | This source file is subject to version 3.0 of the PHP license, |
+ | This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_0.txt. |
+ | http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
@@ -38,8 +38,8 @@
#include <ext/standard/php_smart_str.h>
#include "php_zookeeper.h"
-
-#define DEFAULT_RECV_TIMEOUT 10000
+#include "php_zookeeper_private.h"
+#include "php_zookeeper_session.h"
/****************************************
Helper macros
@@ -73,6 +73,8 @@ typedef struct {
static zend_class_entry *zookeeper_ce = NULL;
+static int le_zookeeper_connection;
+
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3)
const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 };
#undef ZEND_BEGIN_ARG_INFO_EX
@@ -141,7 +143,7 @@ static PHP_METHOD(Zookeeper, connect)
char *host;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
- long recv_timeout = DEFAULT_RECV_TIMEOUT;
+ long recv_timeout = ZK_G(recv_timeout);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!l", &host, &host_len, &fci, &fcc, &recv_timeout) == FAILURE) {
return;
@@ -161,7 +163,7 @@ static PHP_METHOD(Zookeeper, __construct)
char *host = NULL;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
- long recv_timeout = DEFAULT_RECV_TIMEOUT;
+ long recv_timeout = ZK_G(recv_timeout);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sf!l", &host, &host_len, &fci, &fcc, &recv_timeout) == FAILURE) {
ZVAL_NULL(object);
@@ -881,6 +883,7 @@ static void php_aclv_to_array(const struct ACL_vector *aclv, zval *array)
static void php_zk_init_globals(zend_php_zookeeper_globals *php_zookeeper_globals_p TSRMLS_DC)
{
zend_hash_init_ex(&ZK_G(callbacks), 5, NULL, (dtor_func_t)php_cb_data_destroy, 1, 0);
+ php_zookeeper_globals_p->recv_timeout = 10000;
}
static void php_zk_destroy_globals(zend_php_zookeeper_globals *php_zookeeper_globals_p TSRMLS_DC)
@@ -1110,10 +1113,31 @@ static void php_zk_register_constants(INIT_FUNC_ARGS)
}
/* }}} */
+ZEND_RSRC_DTOR_FUNC(php_zookeeper_connection_dtor)
+{
+ if (rsrc->ptr) {
+ php_zookeeper_session *zk_sess = (php_zookeeper_session *)rsrc->ptr;
+ zookeeper_close(zk_sess->zk);
+ pefree(zk_sess, 1);
+ rsrc->ptr = NULL;
+ }
+}
+
+int php_zookeeper_get_connection_le()
+{
+ return le_zookeeper_connection;
+}
+
+PHP_INI_BEGIN()
+ STD_PHP_INI_ENTRY("zookeper.recv_timeout", "10000", PHP_INI_SYSTEM, OnUpdateLong, recv_timeout, zend_php_zookeeper_globals, php_zookeeper_globals)
+PHP_INI_END()
+
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(zookeeper)
{
zend_class_entry ce;
+
+ le_zookeeper_connection = zend_register_list_destructors_ex(NULL, php_zookeeper_connection_dtor, "Zookeeper persistent connection (sessions)", module_number);
INIT_CLASS_ENTRY(ce, "Zookeeper", zookeeper_class_methods);
zookeeper_ce = zend_register_internal_class(&ce TSRMLS_CC);
@@ -1131,6 +1155,7 @@ PHP_MINIT_FUNCTION(zookeeper)
php_zk_init_globals(&php_zookeeper_globals TSRMLS_CC);
#endif
+ php_session_register_module(ps_zookeeper_ptr);
return SUCCESS;
}
/* }}} */
View
17 php_zookeeper.h
@@ -1,11 +1,11 @@
/*
+----------------------------------------------------------------------+
- | Copyright (c) 2009 The PHP Group |
+ | Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
- | This source file is subject to version 3.0 of the PHP license, |
+ | This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_0.txt. |
+ | http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
@@ -33,6 +33,7 @@ extern zend_module_entry zookeeper_module_entry;
ZEND_BEGIN_MODULE_GLOBALS(php_zookeeper)
HashTable callbacks;
+ long recv_timeout;
ZEND_END_MODULE_GLOBALS(php_zookeeper)
PHP_MINIT_FUNCTION(zookeeper);
@@ -48,6 +49,16 @@ PHP_MINFO_FUNCTION(zookeeper);
#define ZK_G(v) (php_zookeeper_globals.v)
#endif
+
+#include "ext/session/php_session.h"
+
+extern ps_module ps_mod_zookeeper;
+#define ps_zookeeper_ptr &ps_mod_zookeeper
+
+PS_FUNCS(zookeeper);
+
+
+
#endif /* PHP_ZOOKEEPER_H */
View
23 php_zookeeper_private.h
@@ -0,0 +1,23 @@
+/*
+ +----------------------------------------------------------------------+
+ | Copyright (c) 2010 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Andrei Zmievski <andrei@php.net> |
+ | Mikko Koppanen <mkoppanen@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+#ifndef _PHP_ZOOKEEPER_PRIVATE_H_
+# define _PHP_ZOOKEEPER_PRIVATE_H_
+
+int php_zookeeper_get_connection_le();
+
+#endif /* _PHP_ZOOKEEPER_PRIVATE_H_ */
View
202 php_zookeeper_session.c
@@ -0,0 +1,202 @@
+/*
+ +----------------------------------------------------------------------+
+ | Copyright (c) 2010 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Andrei Zmievski <andrei@php.net> |
+ | Mikko Koppanen <mkoppanen@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+#include "php_zookeeper.h"
+#include "php_zookeeper_private.h"
+#include "php_zookeeper_session.h"
+
+#define ZK_SESS_DATA php_zookeeper_session *session = PS_GET_MOD_DATA();
+
+#define PHP_ZK_PARENT_NODE "/php-sessid"
+
+ZEND_DECLARE_MODULE_GLOBALS(php_zookeeper)
+
+ps_module ps_mod_zookeeper = {
+ PS_MOD(zookeeper)
+};
+
+/* {{{ static php_zookeeper_session *php_zookeeper_session_init(char *save_path TSRMLS_DC)
+ Initialize the session
+*/
+static php_zookeeper_session *php_zookeeper_session_init(char *save_path TSRMLS_DC)
+{
+ struct Stat stat;
+
+ int status, recv_timeout = ZK_G(recv_timeout);
+ php_zookeeper_session *session;
+
+ session = pecalloc(1, sizeof(php_zookeeper_session), 1);
+ session->zk = zookeeper_init(save_path, NULL, recv_timeout, 0, NULL, 0);
+
+ if (!session->zk) {
+ efree(session);
+ return NULL;
+ }
+
+ /* Create parent node if it does not exist */
+ if (zoo_exists(session->zk, PHP_ZK_PARENT_NODE, 1, &stat) == ZNONODE) {
+ status = zoo_create(session->zk, PHP_ZK_PARENT_NODE, 0, 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
+
+ if (status != ZOK) {
+ zookeeper_close(session->zk);
+ efree(session);
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to create parent node for sessions");
+ }
+ }
+ return session;
+}
+/* }}} */
+
+/* {{{ static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_DC)
+ Get a connection. If connection does not exist in persistent list allocates a new one
+*/
+static php_zookeeper_session *php_zookeeper_session_get(char *save_path TSRMLS_DC)
+{
+ php_zookeeper_session *session;
+
+ char *plist_key;
+ int plist_key_len;
+ zend_rsrc_list_entry le, *le_p = NULL;
+
+ plist_key_len = spprintf(&plist_key, 0, "zk-conn:[%s]", save_path);
+ plist_key_len += 1;
+
+ if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le_p) == SUCCESS) {
+ if (le_p->type == php_zookeeper_get_connection_le()) {
+ efree(plist_key);
+ return (php_zookeeper_session *) le_p->ptr;
+ }
+ }
+
+ session = php_zookeeper_session_init(save_path TSRMLS_CC);
+ le.type = php_zookeeper_get_connection_le();
+ le.ptr = session;
+
+ if (zend_hash_update(&EG(persistent_list), (char *)plist_key, plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not register persistent entry for the zk handle");
+ }
+ efree(plist_key);
+ return session;
+}
+/* }}} */
+
+/* {{{ PS_OPEN_FUNC(zookeeper)
+*/
+PS_OPEN_FUNC(zookeeper)
+{
+ struct Stat stat;
+ php_zookeeper_session *session = php_zookeeper_session_get(PS(save_path) TSRMLS_C);
+
+ if (!session) {
+ PS_SET_MOD_DATA(NULL);
+ return FAILURE;
+ }
+
+ PS_SET_MOD_DATA(session);
+ return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PS_READ_FUNC(zookeeper)
+*/
+PS_READ_FUNC(zookeeper)
+{
+ ZK_SESS_DATA;
+ int status, path_len;
+ char path[512];
+ struct Stat stat;
+
+ char *buffer;
+ int buffer_len;
+
+ path_len = snprintf(path, 512, "%s/%s", PHP_ZK_PARENT_NODE, key);
+ status = zoo_exists(session->zk, path, 1, &stat);
+
+ if (status != ZOK) {
+ *val = estrdup("");
+ *vallen = 0;
+ return SUCCESS;
+ }
+
+ *val = emalloc(stat.dataLength);
+ *vallen = stat.dataLength;
+
+ status = zoo_get(session->zk, path, 0, *val, vallen, &stat);
+
+ if (status != ZOK) {
+ efree(*val);
+ *val = estrdup("");
+ *vallen = 0;
+ return FAILURE;
+ }
+ return SUCCESS;
+}
+/* }}} */
+
+/* {{{ PS_WRITE_FUNC(zookeeper)
+*/
+PS_WRITE_FUNC(zookeeper)
+{
+ ZK_SESS_DATA;
+ int status, path_len;
+ char path[512];
+ struct Stat stat;
+
+ path_len = snprintf(path, 512, "%s/%s", PHP_ZK_PARENT_NODE, key);
+ status = zoo_exists(session->zk, path, 1, &stat);
+
+ if (status != ZOK) {
+ status = zoo_create(session->zk, path, val, vallen, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
+ } else {
+ status = zoo_set(session->zk, path, val, vallen, -1);
+ }
+
+ return (status == ZOK) ? SUCCESS : FAILURE;
+}
+/* }}} */
+
+PS_DESTROY_FUNC(zookeeper)
+{
+ ZK_SESS_DATA;
+ int path_len, status;
+ char path[512];
+
+ path_len = snprintf(path, 512, "%s/%s", PHP_ZK_PARENT_NODE, key);
+ status = zoo_delete(session->zk, path, -1);
+
+ return (status == ZOK) ? SUCCESS : FAILURE;
+}
+
+PS_GC_FUNC(zookeeper)
+{
+ /* TODO: add garbage collection */
+}
+
+PS_CLOSE_FUNC(zookeeper)
+{
+ ZK_SESS_DATA;
+ PS_SET_MOD_DATA(NULL);
+ return SUCCESS;
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim: noet sw=4 ts=4 fdm=marker:
+ */
View
29 php_zookeeper_session.h
@@ -0,0 +1,29 @@
+/*
+ +----------------------------------------------------------------------+
+ | Copyright (c) 2010 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Andrei Zmievski <andrei@php.net> |
+ | Mikko Koppanen <mkoppanen@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+#ifndef _PHP_ZOOKEEPER_SESSION_H_
+# define _PHP_ZOOKEEPER_SESSION_H_
+
+/* {{{ typedef struct _php_zookeeper_session
+*/
+typedef struct _php_zookeeper_session {
+ /* Connection to zookeeper */
+ zhandle_t *zk;
+} php_zookeeper_session;
+/* }}} */
+
+#endif /* _PHP_ZOOKEEPER_SESSION_H_ */
Please sign in to comment.
Something went wrong with that request. Please try again.