1
/* Copyright (C) 2009 Sun Microsystems
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include "function_map.h"
22
/* Constructor and destructor happen during module dlopen/dlclose. */
23
static GearmanFunctionMap _functionMap;
25
GearmanFunctionMap& GetFunctionMap(void)
30
GearmanFunctionMap::GearmanFunctionMap()
32
(void) pthread_mutex_init(&lock, NULL);
35
GearmanFunctionMap::~GearmanFunctionMap()
37
map<string, gearman_client_st>::iterator x;
39
for (x= functionMap.begin(); x != functionMap.end(); x++)
40
gearman_client_free(&((*x).second));
42
(void) pthread_mutex_destroy(&lock);
45
bool GearmanFunctionMap::add(string function, string servers)
47
map<string, gearman_client_st>::iterator x;
54
pthread_mutex_lock(&lock);
56
x= functionMap.find(function);
57
if (x == functionMap.end())
59
if (gearman_client_create(&(functionMap[function])) == NULL)
61
pthread_mutex_unlock(&lock);
66
/* Parse server strings in the format "host[:port][,host[:port]]..." */
69
end_pos= servers.find(',', begin_pos);
70
if (end_pos == string::npos)
71
host= servers.substr(begin_pos);
73
host= servers.substr(begin_pos, end_pos - begin_pos);
75
port_pos= host.find(':');
76
if (port_pos == string::npos)
80
port= host.substr(port_pos + 1);
84
/* For each host:port pair, add a server to the cloning object. */
85
if (gearman_client_add_server(&(functionMap[function]), host.c_str(),
87
0 : atoi(port.c_str())) != GEARMAN_SUCCESS)
89
pthread_mutex_unlock(&lock);
93
if (end_pos == string::npos)
96
begin_pos= end_pos + 1;
99
pthread_mutex_unlock(&lock);
103
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
105
map<string, gearman_client_st>::iterator x;
107
pthread_mutex_lock(&lock);
109
x= functionMap.find(function);
110
if (x == functionMap.end())
112
x= functionMap.find(string(""));
113
if (x == functionMap.end())
115
pthread_mutex_unlock(&lock);
120
/* Clone the object, the list of host:port pairs get cloned with it. */
121
if (gearman_client_clone(client, &((*x).second)) == NULL)
123
pthread_mutex_unlock(&lock);
127
pthread_mutex_unlock(&lock);