Skip to content
This repository
tree: be64a9f034
Fetching contributors…

Cannot retrieve contributors at this time

file 206 lines (155 sloc) 5.834 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/* Copyright (C) 2009 Piotr Pokora <piotrek.pokora@gmail.com>
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "php_midgard_handle.h"

/* Get configuration name declared in vhost configuration.
midgard.configuration = "midgard"; */
static const gchar *__get_configuration_name(TSRMLS_D)
{
const gchar *name = MGDG(midgard_configuration);

if (!name || *name == '\0')
return NULL;

return (const gchar *) name;
}

/* Get configuration file path. Shoul dbe declaored in vhost configuration
midgard.configuration_file = "/home/user/config/midgard.conf" */
static const gchar *__get_configuration_filepath(TSRMLS_D)
{
const gchar *path = MGDG(midgard_configuration_file);

if (!path || *path == '\0')
return NULL;

return (const gchar *) path;
}

struct _MgdGHolder {
GHashTable *names;
GHashTable *files;
};

/* Initialize global midgard handle holder */
void php_midgard_handle_holder_init(MgdGHolder **mgh)
{
if (*mgh != NULL)
return;

*mgh = g_new(MgdGHolder, 1);

(*mgh)->names = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
(*mgh)->files = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);

return;
}

/* Free handle holder */
void php_midgard_handle_holder_free(MgdGHolder **mgh)
{
if ((*mgh)->names != NULL)
g_hash_table_destroy((*mgh)->names);

if ((*mgh)->files != NULL)
g_hash_table_destroy((*mgh)->files);

g_free(*mgh);

*mgh = NULL;

return;
}

/* Initialize per request copy. Early sets loghandler so we can log messages. */
MidgardConnection *__handle_set(MidgardConnection *mgd TSRMLS_DC)
{
g_assert(mgd != NULL);

MidgardConnection *copy = midgard_connection_copy(mgd);

guint loghandler = g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, php_midgard_log_errors, (gpointer)copy);
midgard_connection_set_loghandler(copy, loghandler);

if (MGDG(midgard_memory_debug)) {
php_printf("---> midgard_connection_set_loghandler(%d)\n", loghandler);
}

return copy;
}

static MidgardConnection *__handle_from_global_config(MgdGHolder *mgh, GHashTable *global_cfgs, const gchar *config_name TSRMLS_DC)
{
if (global_cfgs == NULL)
return NULL;

/* This is per process connection handler */
MidgardConnection *mgd = NULL;
/* This is per request connection handler */
MidgardConnection *mgd_copy = NULL;

/* First, let's check if given configuration name already eixts in global holder.
If it exists, get a pointer */
mgd = g_hash_table_lookup(mgh->names, config_name);

if (mgd != NULL) {
if (!midgard_connection_reopen(mgd)) {
php_error(E_WARNING, "Failed to reopen lost connection");
return NULL;
}

mgd_copy = __handle_set(mgd TSRMLS_CC);
return mgd_copy;
}

/* Check if configuration exists in system config directory. */
MidgardConfig *config = g_hash_table_lookup(global_cfgs, config_name);

if (config == NULL)
return NULL;

mgd = midgard_connection_new();

if (!midgard_connection_open_config(mgd, config)) {
php_error(E_WARNING, "Failed to open connection using given '%s' configuration", config_name);
g_object_unref(mgd);
return NULL;
}

mgd_copy = __handle_set(mgd TSRMLS_CC);

/* Insert connection in process' pool */
g_hash_table_insert(mgh->names, g_strdup(config_name), mgd);

return mgd_copy;
}

static MidgardConnection *__handle_from_filepath(MgdGHolder *mgh, const gchar *config_path TSRMLS_DC)
{
/* This is per process connection handler */
MidgardConnection *mgd = NULL;
/* This is per request connection handler */
MidgardConnection *mgd_copy = NULL;

/* First, let's check if given configuration path already eixts in global holder.
If it exists, get a pointer */
mgd = g_hash_table_lookup(mgh->files, config_path);

if (mgd != NULL) {
if (!midgard_connection_reopen(mgd)) {
php_error(E_WARNING, "Failed to reopen lost connection");
return NULL;
}

mgd_copy = __handle_set(mgd TSRMLS_CC);
return mgd_copy;
}

/* Open configuration from given filepath */
mgd = midgard_connection_new();
GError *err = NULL;

if (!midgard_connection_open_from_file(mgd, config_path, &err)) {
php_error(E_WARNING, "Failed to open connection using given '%s' configuration file: %s",
config_path,
err && err->message ? err->message : "Unknown reason");
g_error_free(err);
g_object_unref(mgd);
return NULL;
}

mgd_copy = __handle_set(mgd TSRMLS_CC);

/* Insert connection in process' pool */
g_hash_table_insert(mgh->files, g_strdup(config_path), mgd);

return mgd_copy;
}

MidgardConnection *php_midgard_handle_lookup(MgdGHolder **mgh, GHashTable *global_cfgs TSRMLS_DC)
{
if (*mgh == NULL)
php_midgard_handle_holder_init(mgh);

const gchar *config_path = __get_configuration_filepath(TSRMLS_C);

if (config_path != NULL) {
MidgardConnection *handle = __handle_from_filepath(*mgh, config_path TSRMLS_CC);

if (handle != NULL) {
return handle;
}

// handle is NULL. falling back to file-based search of config
}

const gchar *config_name = __get_configuration_name(TSRMLS_C);

if (config_name == NULL)
return NULL;

return __handle_from_global_config(*mgh, global_cfgs, config_name TSRMLS_CC);
}
Something went wrong with that request. Please try again.