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
|
/*
* $Id$
*
* BDB Database Driver for SER
*
* Copyright (C) 2008 iptelorg GmbH
*
* This file is part of SIP-router, a free SIP server.
*
* SIP-router is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* SIP-router 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/** \addtogroup bdb
* @{
*/
/*! \file
* Functions related to connections to BDB.
*
* \ingroup database
*/
#include <stdlib.h>
#include <string.h>
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "bdb_con.h"
#include "bdb_uri.h"
#include "bdb_lib.h"
/** Free all memory allocated for a bdb_con structure.
* This function function frees all memory that is in use by
* a bdb_con structure.
* @param con A generic db_con connection structure.
* @param payload BDB specific payload to be freed.
*/
static void bdb_con_free(db_con_t* con, bdb_con_t *payload)
{
bdb_uri_t *buri;
if (!payload)
return;
buri = DB_GET_PAYLOAD(con->uri);
/* Delete the structure only if there are no more references
* to it in the connection pool
*/
if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
db_pool_entry_free(&payload->gen);
/* destroy and free BDB env */
pkg_free(payload);
}
int bdb_con(db_con_t* con)
{
bdb_con_t* bcon;
bdb_uri_t* buri;
buri = DB_GET_PAYLOAD(con->uri);
/* First try to lookup the connection in the connection pool and
* re-use it if a match is found
*/
bcon = (bdb_con_t*)db_pool_get(con->uri);
if (bcon) {
DBG("bdb: Connection to %s found in connection pool\n",
buri->uri);
goto found;
}
bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t));
if (!bcon) {
ERR("bdb: No memory left\n");
goto error;
}
memset(bcon, '\0', sizeof(bdb_con_t));
if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error;
DBG("bdb: Preparing new connection to %s\n", buri->uri);
if(bdb_is_database(buri->path.s)!=0)
{
ERR("bdb: database [%.*s] does not exists!\n",
buri->path.len, buri->path.s);
goto error;
}
/* Put the newly created BDB connection into the pool */
db_pool_put((struct db_pool_entry*)bcon);
DBG("bdb: Connection stored in connection pool\n");
found:
/* Attach driver payload to the db_con structure and set connect and
* disconnect functions
*/
DB_SET_PAYLOAD(con, bcon);
con->connect = bdb_con_connect;
con->disconnect = bdb_con_disconnect;
return 0;
error:
if (bcon) {
db_pool_entry_free(&bcon->gen);
pkg_free(bcon);
}
return -1;
}
int bdb_con_connect(db_con_t* con)
{
bdb_con_t *bcon;
bdb_uri_t *buri;
bcon = DB_GET_PAYLOAD(con);
buri = DB_GET_PAYLOAD(con->uri);
/* Do not reconnect already connected connections */
if (bcon->flags & BDB_CONNECTED) return 0;
DBG("bdb: Connecting to %s\n", buri->uri);
/* create BDB environment */
bcon->dbp = bdblib_get_db(&buri->path);
if(bcon->dbp == NULL)
{
ERR("bdb: error binding to DB %s\n", buri->uri);
return -1;
}
DBG("bdb: Successfully bound to %s\n", buri->uri);
bcon->flags |= BDB_CONNECTED;
return 0;
}
void bdb_con_disconnect(db_con_t* con)
{
bdb_con_t *bcon;
bdb_uri_t *buri;
bcon = DB_GET_PAYLOAD(con);
buri = DB_GET_PAYLOAD(con->uri);
if ((bcon->flags & BDB_CONNECTED) == 0) return;
DBG("bdb: Unbinding from %s\n", buri->uri);
if(bcon->dbp==NULL)
{
bcon->flags &= ~BDB_CONNECTED;
return;
}
bdblib_close(bcon->dbp, &buri->path);
bcon->dbp = 0;
bcon->flags &= ~BDB_CONNECTED;
}
/** @} */
|