00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "function_map.h"
00017 #include <libgearman-1.0/client.h>
00018 #include <string.h>
00019 #include <stdlib.h>
00020
00021 using namespace std;
00022
00023
00024 static GearmanFunctionMap _functionMap;
00025
00026 GearmanFunctionMap& GetFunctionMap(void)
00027 {
00028 return _functionMap;
00029 }
00030
00031 GearmanFunctionMap::GearmanFunctionMap()
00032 {
00033 (void) pthread_mutex_init(&lock, NULL);
00034 }
00035
00036 GearmanFunctionMap::~GearmanFunctionMap()
00037 {
00038 map<string, gearman_client_st>::iterator x;
00039
00040 for (x= functionMap.begin(); x != functionMap.end(); x++)
00041 gearman_client_free(&((*x).second));
00042
00043 (void) pthread_mutex_destroy(&lock);
00044 }
00045
00046 bool GearmanFunctionMap::add(string function, string servers)
00047 {
00048 map<string, gearman_client_st>::iterator x;
00049 gearman_return_t ret;
00050
00051 pthread_mutex_lock(&lock);
00052
00053 x= functionMap.find(function);
00054 if (x == functionMap.end())
00055 {
00056 if (gearman_client_create(&(functionMap[function])) == NULL)
00057 {
00058 pthread_mutex_unlock(&lock);
00059 return false;
00060 }
00061 }
00062
00063 gearman_client_remove_servers(&(functionMap[function]));
00064 ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
00065 pthread_mutex_unlock(&lock);
00066 if (ret != GEARMAN_SUCCESS)
00067 return false;
00068
00069 return true;
00070 }
00071
00072 bool GearmanFunctionMap::get(string function, gearman_client_st *client)
00073 {
00074 map<string, gearman_client_st>::iterator x;
00075
00076 pthread_mutex_lock(&lock);
00077
00078 x= functionMap.find(function);
00079 if (x == functionMap.end())
00080 {
00081 x= functionMap.find(string(""));
00082 if (x == functionMap.end())
00083 {
00084 pthread_mutex_unlock(&lock);
00085 return false;
00086 }
00087 }
00088
00089
00090 if (gearman_client_clone(client, &((*x).second)) == NULL)
00091 {
00092 pthread_mutex_unlock(&lock);
00093 return false;
00094 }
00095
00096 pthread_mutex_unlock(&lock);
00097 return true;
00098 }