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
static GearmanFunctionMap _functionMap;
24
GearmanFunctionMap& GetFunctionMap(void)
29
GearmanFunctionMap::GearmanFunctionMap()
31
(void) pthread_mutex_init(&lock, NULL);
34
GearmanFunctionMap::~GearmanFunctionMap()
36
map<string, gearman_client_st>::iterator x;
38
for (x= functionMap.begin(); x != functionMap.end(); x++)
39
gearman_client_free(&((*x).second));
41
(void) pthread_mutex_destroy(&lock);
44
bool GearmanFunctionMap::add(string function, string servers)
46
map<string, gearman_client_st>::iterator x;
53
pthread_mutex_lock(&lock);
55
x= functionMap.find(function);
56
if (x == functionMap.end())
58
if (gearman_client_create(&(functionMap[function])) == NULL)
60
pthread_mutex_unlock(&lock);
67
end_pos= servers.find(',', begin_pos);
68
if (end_pos == string::npos)
69
host= servers.substr(begin_pos);
71
host= servers.substr(begin_pos, end_pos - begin_pos);
73
port_pos= host.find(':');
74
if (port_pos == string::npos)
78
port= host.substr(port_pos + 1);
82
if (gearman_client_add_server(&(functionMap[function]), host.c_str(),
84
0 : atoi(port.c_str())) != GEARMAN_SUCCESS)
86
pthread_mutex_unlock(&lock);
90
if (end_pos == string::npos)
93
begin_pos= end_pos + 1;
96
pthread_mutex_unlock(&lock);
100
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
102
map<string, gearman_client_st>::iterator x;
104
pthread_mutex_lock(&lock);
106
x= functionMap.find(function);
107
if (x == functionMap.end())
109
x= functionMap.find(string(""));
110
if (x == functionMap.end())
112
pthread_mutex_unlock(&lock);
117
if (gearman_client_clone(client, &((*x).second)) == NULL)
119
pthread_mutex_unlock(&lock);
123
pthread_mutex_unlock(&lock);