[asterisk-commits] snuffy: branch snuffy/func_memcache r230419 - /team/snuffy/func_memcache/funcs/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Nov 17 06:02:44 CST 2009


Author: snuffy
Date: Tue Nov 17 06:02:39 2009
New Revision: 230419

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=230419
Log:
Update with some more fixes
Fix if/while spacings etc.
Fix return values so they return 1 if they actually failed
Clean some other bits n pieces

Modified:
    team/snuffy/func_memcache/funcs/func_memcache.c

Modified: team/snuffy/func_memcache/funcs/func_memcache.c
URL: http://svnview.digium.com/svn/asterisk/team/snuffy/func_memcache/funcs/func_memcache.c?view=diff&rev=230419&r1=230418&r2=230419
==============================================================================
--- team/snuffy/func_memcache/funcs/func_memcache.c (original)
+++ team/snuffy/func_memcache/funcs/func_memcache.c Tue Nov 17 06:02:39 2009
@@ -99,7 +99,8 @@
 		</see-also>
 	</function>
  ***/
-static char *config = "func_memcache.conf";
+
+static const char config[] = "func_memcache.conf";
 
 struct memcache_con {
 	char name[80];
@@ -124,53 +125,18 @@
 static AST_RWLIST_HEAD_STATIC(memcaches, memcache_con);
 
 #define MEMC_DEF_PORT 11211
-/* 
-static int fn_memcache_mget(const char *family, const char *key, char *buf, size_t len)
-{
-	struct memcache_con *memc;
-	char *value;
-
-	value = memcached_get(memc->conn, key, sizeof(key), &len, 0, &memc->rc);
-	ast_copy_string(buf, value, len);
-
-	//Free memory from libmemcached library
-	free(value);
-
-  memcached_return rc;
-  char *keys[]= {"fudge", "son", "food"};
-  size_t key_length[]= {5, 3, 4};
-  char return_key[MEMCACHED_MAX_KEY];
-  size_t return_key_length;
-  char *return_value;
-  size_t return_value_length;
-
-  rc= memcached_mget(memc, keys, key_length, 3);
-
-  x= 0;
-  while ((return_value= memcached_fetch(memc, return_key, &return_key_length, 
-                                        &return_value_length, &flags, &rc)))
-  {
-    free(return_value);
-    x++;
-  }
-
-	return 0;
-}
-*/
 
 static int fn_memcache_get(char *key, const char *server, char *buf, size_t len)
 {
-	struct memcache_con *memc;
-	char *value;
-	size_t ss,k_len;
-	uint32_t flags;
-
-	k_len = strlen(key);	
+	struct memcache_con *memc = NULL;
+	char *value = NULL;
+	size_t value_len = 0;
+	uint32_t flags = 0;
 
 	AST_RWLIST_RDLOCK(&memcaches);
 	AST_RWLIST_TRAVERSE(&memcaches, memc, list) {
 		if (!strcmp(memc->name, server)) {
-			value = memcached_get(memc->conn, key, k_len, &ss, &flags, &memc->rc);
+			value = memcached_get(memc->conn, key, strlen(key), &value_len, &flags, &memc->rc);
 			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, 
 				memcached_strerror(memc->conn, memc->rc));
 		}
@@ -178,12 +144,13 @@
 	
 	AST_RWLIST_UNLOCK(&memcaches);
 
-	if(value) {
+	if (value) {
 		ast_copy_string(buf, value, len);
 		ast_free(value);
-	}
-
-	return 0;
+		return 0;
+	}
+
+	return 1;
 }
 
 static int fn_memcache_put(const char *key, const char *server, const char *expiry, const char *value)
@@ -204,12 +171,16 @@
 						value, sizeof(value), (time_t)0, 
 						(uint32_t)0);
 			}
-			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, memcached_strerror(memc->conn,memc->rc));
+			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, memcached_strerror(memc->conn, memc->rc));
 		}
 	}
 	AST_RWLIST_UNLOCK(&memcaches);
-
-	return 0;
+	
+	if (!memc->rc) {
+		return 0;
+	}
+
+	return 1;
 }
 
 static int fn_memcache_del(const char *key, const char *server, const char *expiry)
@@ -229,12 +200,16 @@
 						(time_t)0);
 			}
 			ast_log(LOG_WARNING,"errno:%d\tstr:%s\n", memc->rc, 
-				memcached_strerror(memc->conn,memc->rc));
+				memcached_strerror(memc->conn, memc->rc));
 		}
 	}
 	AST_RWLIST_UNLOCK(&memcaches);
 
-	return 0;
+	if (!memc->rc) {
+		return 0;
+	}
+
+	return 1;
 }
 
 static int function_memcache_read(struct ast_channel *chan, const char *cmd,
@@ -266,6 +241,7 @@
 		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
 		strcpy(buf,"1");
 	}
+
 	return 0;
 }
 
@@ -302,6 +278,7 @@
 		ast_log(LOG_WARNING, "MEMCACHE: Error writing %s to server.\n", value);
 		return -1;
 	}
+
 	return 0;
 }
 
@@ -334,6 +311,7 @@
 		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
 		strcpy(buf,"1");
 	}
+
 	return 0;
 }
 
@@ -349,14 +327,14 @@
 	buf[0] = '\0';
 
 	if (ast_strlen_zero(parse)) {
-		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>)\n");
+		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>[,<expiry>])\n");
 		return -1;
 	}
 
 	AST_STANDARD_APP_ARGS(args, parse);
 
 	if (args.argc < 1) {
-		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>)\n");
+		ast_log(LOG_WARNING, "MEMCACHE_DELETE requires an argument, MEMCACHE_DELETE(<server>,<key>[,<expiry>])\n");
 		return -1;
 	}
 	if (args.expiry && args.expiry < 0) {
@@ -367,13 +345,16 @@
 	if (fn_memcache_get(args.key, args.server, buf, len)) {
 		ast_debug(1, "MEMCACHE_DELETE: %s/%s not found in database.\n", 
 				args.server, args.key);
+		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", "");
+		strcpy(buf, "0");
 	} else {
 		if (fn_memcache_del(args.key, args.server, args.expiry)) {
 			ast_debug(1, "MEMCACHE_DELETE: %s/%s could not be deleted from the database\n", args.server, args.key);
-		}
-	}
-
-	pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
+			strcpy(buf, "0");
+		}
+		pbx_builtin_setvar_helper(chan, "MEMCACHE_RESULT", buf);
+		strcpy(buf, "1");
+	}
 
 	return 0;
 }
@@ -396,7 +377,7 @@
 	.read = function_memcache_delete,
 };
 
-static int memc_create_client(char *cat, struct ast_variable *var, int debug)
+static int memc_create_client(char *cat, struct ast_variable *var)
 {
 	struct memcache_con *client = NULL;
 		
@@ -424,7 +405,7 @@
 			}
 			
 			if (ast_strlen_zero(cport)) {
-				ast_log(LOG_WARNING,"Port was not greater than 0, using default port\n");
+				ast_log(LOG_WARNING, "Port was not greater than 0, using default port\n");
 			} else {
 				serv->port = atoi(cport);
 			}
@@ -437,7 +418,7 @@
 				client->transport = 1;
 			}
 		} else if (!strcasecmp(var->name, "binaryprotocol")) {
-			if(!strcasecmp(var->value, "yes")) {
+			if (!strcasecmp(var->value, "yes")) {
 				client->protocol = 1;
 			}
 		} else if (!strcasecmp(var->name, "distributehash")) {
@@ -456,7 +437,6 @@
 
 	client->conn = memcached_create(NULL);
 
-
 	if (client->transport) {
 		memcached_behavior_set(client->conn, MEMCACHED_BEHAVIOR_USE_UDP, 1);
 		ast_debug(3, "UDP Transport Active\n");
@@ -513,7 +493,7 @@
 	client->rc = memcached_server_push(client->conn, client->servers);
 
 	if (client->rc == 1) {
-		ast_log(LOG_ERROR,"Push Error: %s\n", memcached_strerror(client->conn, client->rc));
+		ast_log(LOG_ERROR, "Push Error: %s\n", memcached_strerror(client->conn, client->rc));
 	}
 
 	AST_RWLIST_INSERT_HEAD(&memcaches, client, list);
@@ -559,7 +539,6 @@
 	struct ast_config *cfg = NULL;
 	struct ast_variable *var = NULL;
 	char *cat;
-	int debug;
 	struct ast_flags config_flags = { 0 };
 
 	res |= ast_custom_function_register(&memcache_function);
@@ -580,7 +559,7 @@
 	while (cat) {
 		if (strcasecmp(cat, "general")) {
 			var = ast_variable_browse(cfg, cat);
-			memc_create_client(cat, var, debug);
+			memc_create_client(cat, var);
 		}
 		cat = ast_category_browse(cfg, cat);
 	}
@@ -625,7 +604,6 @@
 	struct ast_variable *var = NULL;
 	struct memcache_con *oldmem;
 	char *cat;
-	int debug = 0;
 	struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
 
 	cfg = ast_config_load(config, config_flags);
@@ -649,7 +627,7 @@
 	while (cat) {
 		if (strcasecmp(cat, "general")) {
 			var = ast_variable_browse(cfg, cat);
-			memc_create_client(cat, var, debug);
+			memc_create_client(cat, var);
 		}
 		cat = ast_category_browse(cfg, cat);
 	}




More information about the asterisk-commits mailing list