celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1632910 - in /celix/trunk/remote_services/discovery_etcd/private/src: etcd.c etcd_watcher.c
Date Sun, 19 Oct 2014 14:18:32 GMT
Author: bpetri
Date: Sun Oct 19 14:18:32 2014
New Revision: 1632910

URL: http://svn.apache.org/r1632910
Log:
CELIX-152: code refactoring

Modified:
    celix/trunk/remote_services/discovery_etcd/private/src/etcd.c
    celix/trunk/remote_services/discovery_etcd/private/src/etcd_watcher.c

Modified: celix/trunk/remote_services/discovery_etcd/private/src/etcd.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_services/discovery_etcd/private/src/etcd.c?rev=1632910&r1=1632909&r2=1632910&view=diff
==============================================================================
--- celix/trunk/remote_services/discovery_etcd/private/src/etcd.c (original)
+++ celix/trunk/remote_services/discovery_etcd/private/src/etcd.c Sun Oct 19 14:18:32 2014
@@ -79,10 +79,10 @@ bool etcd_init(char* server, int port) {
 
 // get
 bool etcd_get(char* key, char* value, char* action, int* modifiedIndex) {
-	json_t* js_root;
-	json_t* js_node;
-	json_t* js_value;
-	json_t* js_modifiedIndex;
+	json_t* js_root = NULL;
+	json_t* js_node = NULL;
+	json_t* js_value = NULL;
+	json_t* js_modifiedIndex = NULL;
 	json_error_t error;
 	int res;
 	struct MemoryStruct reply;
@@ -96,35 +96,38 @@ bool etcd_get(char* key, char* value, ch
 
 	res = performRequest(url, GET, WriteMemoryCallback, NULL, (void*) &reply);
 
-	if (res == CURLE_OPERATION_TIMEDOUT) {
-		//printf("error while performing curl w/ %s\n", url);
-	} else if (res != CURLE_OK) {
-		printf("error while performing curl\n");
-	} else if ((js_root = json_loads(reply.memory, 0, &error)) == NULL) {
-		printf("error while parsing json data\n");
-	} else if ((js_node = json_object_get(js_root, ETCD_JSON_NODE)) == NULL) {
-		printf("error while retrieving expected node object %s\n", json_dumps(js_root, 0));
-	} else if (((js_value = json_object_get(js_node, ETCD_JSON_VALUE)) == NULL) || ((js_value
= json_object_get(js_node, ETCD_JSON_VALUE)) == NULL) || ((js_modifiedIndex = json_object_get(js_node,
ETCD_JSON_MODIFIEDINDEX)) == NULL)) {
-		printf("error while retrieving expected objects\n");
-	}
-	else {
-		*modifiedIndex = json_integer_value(js_modifiedIndex);
-		strncpy(value, json_string_value(js_value), MAX_VALUE_LENGTH);
-		retVal = true;
+	if (res == CURLE_OK) {
+		js_root = json_loads(reply.memory, 0, &error);
+
+		if (js_root != NULL) {
+			js_node = json_object_get(js_root, ETCD_JSON_NODE);
+		}
+		if (js_node != NULL) {
+			js_value = json_object_get(js_node, ETCD_JSON_VALUE);
+			js_modifiedIndex = json_object_get(js_node, ETCD_JSON_MODIFIEDINDEX);
+
+			*modifiedIndex = json_integer_value(js_modifiedIndex);
+			strncpy(value, json_string_value(js_value), MAX_VALUE_LENGTH);
+			retVal = true;
+		}
+		if (js_root != NULL) {
+			json_decref(js_root);
+		}
 	}
 
 	if (reply.memory) {
 		free(reply.memory);
 	}
 
+
 	return retVal;
 }
 
 // getNodes
 bool etcd_getNodes(char* directory, char** nodeNames, int* size) {
-	json_t* js_root;
-	json_t* js_node;
-	json_t* js_nodes;
+	json_t* js_root = NULL;
+	json_t* js_node = NULL;
+	json_t* js_nodes = NULL;
 	json_error_t error;
 	int res;
 	struct MemoryStruct reply;
@@ -137,34 +140,37 @@ bool etcd_getNodes(char* directory, char
 	snprintf(url, MAX_URL_LENGTH, "http://%s:%d/v2/keys/%s", etcd_server, etcd_port, directory);
 
 	res = performRequest(url, GET, WriteMemoryCallback, NULL, (void*) &reply);
-	if (res == CURLE_OPERATION_TIMEDOUT) {
-		//printf("error while performing curl w/ %s\n", url);
-	} else if (res != CURLE_OK) {
-		printf("error while performing curl\n");
-	} else if ((js_root = json_loads(reply.memory, 0, &error)) == NULL) {
-		printf("error while parsing json data\n");
-	} else if ((js_node = json_object_get(js_root, ETCD_JSON_NODE)) == NULL) {
-		printf("error while retrieving expected nodes object\n");
-	} else if ((js_nodes = json_object_get(js_node, ETCD_JSON_NODES)) == NULL) {
-		printf("error while retrieving expected nodes object\n");
-	} else if (json_is_array(js_nodes)) {
-
-		int i = 0;
-
-		retVal = true;
-
-		for (i = 0; i < json_array_size(js_nodes) && i < MAX_NODES; i++) {
-			json_t* js_node = json_array_get(js_nodes, i);
-
-			if (!json_is_object(js_node)) {
-				retVal = false;
-			} else {
-				json_t* js_key = json_object_get(js_node, ETCD_JSON_KEY);
-				strncpy(nodeNames[i], json_string_value(js_key), MAX_KEY_LENGTH);
-			}
+
+	if (res == CURLE_OK) {
+		js_root = json_loads(reply.memory, 0, &error);
+
+		if (js_root != NULL) {
+			js_node = json_object_get(js_root, ETCD_JSON_NODE);
 		}
-		*size = i;
+		if (js_root != NULL) {
+			js_nodes = json_object_get(js_node, ETCD_JSON_NODES);
+		}
+
+
+		if (js_nodes != NULL && json_is_array(js_nodes)) {
+			int i = 0;
+			retVal = true;
+
+			for (i = 0; i < json_array_size(js_nodes) && i < MAX_NODES; i++) {
+				json_t* js_node = json_array_get(js_nodes, i);
 
+				if (!json_is_object(js_node)) {
+					retVal = false;
+				} else {
+					json_t* js_key = json_object_get(js_node, ETCD_JSON_KEY);
+					strncpy(nodeNames[i], json_string_value(js_key), MAX_KEY_LENGTH);
+				}
+			}
+			*size = i;
+		}
+		if (js_root != NULL) {
+			json_decref(js_root);
+		}
 	}
 
 	if (reply.memory) {
@@ -178,9 +184,9 @@ bool etcd_getNodes(char* directory, char
 
 bool etcd_set(char* key, char* value, int ttl, bool prevExist) {
 	json_error_t error;
-	json_t* js_root;
-	json_t* js_node;
-	json_t* js_value;
+	json_t* js_root = NULL;
+	json_t* js_node = NULL;
+	json_t* js_value = NULL;
 	bool retVal = false;
 	char url[MAX_URL_LENGTH];
 	char request[MAX_CONTENT_LENGTH];
@@ -201,18 +207,22 @@ bool etcd_set(char* key, char* value, in
 	    cur += snprintf(cur, MAX_CONTENT_LENGTH, ";prevExist=true");
 
 	res = performRequest(url, PUT, WriteMemoryCallback, request, (void*) &reply);
-	if (res == CURLE_OPERATION_TIMEDOUT) {
-		//printf("error while performing curl w/ %s\n", url);
-	} else if (res != CURLE_OK) {
-		printf("error while performing curl\n");
-	} else if ((js_root = json_loads(reply.memory, 0, &error)) == NULL) {
-		printf("error while parsing json data\n");
-	} else if ((js_node = json_object_get(js_root, ETCD_JSON_NODE)) == NULL) {
-		printf("error while retrieving expected node object %s\n", json_dumps(js_root, 0));
-	} else if ((js_value = json_object_get(js_node, ETCD_JSON_VALUE)) == NULL) {
-		printf("error while retrieving expected value object\n");
-	} else if (json_is_string(js_value)) {
-		retVal = (strcmp(json_string_value(js_value), value) == 0);
+
+	if (res == CURLE_OK) {
+		js_root = json_loads(reply.memory, 0, &error);
+
+		if (js_root != NULL) {
+			js_node = json_object_get(js_root, ETCD_JSON_NODE);
+		}
+		if (js_node != NULL) {
+			js_value = json_object_get(js_node, ETCD_JSON_VALUE);
+		}
+		if (js_value != NULL && json_is_string(js_value)) {
+			retVal = (strcmp(json_string_value(js_value), value) == 0);
+		}
+		if (js_root != NULL) {
+			json_decref(js_root);
+		}
 	}
 
 	if (reply.memory) {
@@ -227,8 +237,8 @@ bool etcd_set(char* key, char* value, in
 //delete
 bool etcd_del(char* key) {
 	json_error_t error;
-	json_t* js_root;
-	json_t* js_node;
+	json_t* js_root = NULL;
+	json_t* js_node = NULL;
 	bool retVal = false;
 	char url[MAX_URL_LENGTH];
 	char request[MAX_CONTENT_LENGTH];
@@ -240,34 +250,38 @@ bool etcd_del(char* key) {
 
 	snprintf(url, MAX_URL_LENGTH, "http://%s:%d/v2/keys/%s", etcd_server, etcd_port, key);
 	res = performRequest(url, DELETE, WriteMemoryCallback, request, (void*) &reply);
-	if (res == CURLE_OPERATION_TIMEDOUT) {
-		//printf("error while performing curl w/ %s\n", url);
-	} else if (res != CURLE_OK) {
-		printf("error while performing curl\n");
-	} else if ((js_root = json_loads(reply.memory, 0, &error)) == NULL) {
-		printf("error while parsing json data\n");
-	} else if ((js_node = json_object_get(js_root, ETCD_JSON_NODE)) == NULL) {
-		printf("error while retrieving expected node object %s\n", json_dumps(js_root, 0));
-	} else {
-		retVal = true;
+
+	if (res == CURLE_OK) {
+		js_root = json_loads(reply.memory, 0, &error);
+
+		if (js_root != NULL) {
+			js_node = json_object_get(js_root, ETCD_JSON_NODE);
+		}
+
+		retVal = (js_node != NULL);
+
+		if (js_root != NULL) {
+			json_decref(js_root);
+		}
 	}
 
 	if (reply.memory) {
 		free(reply.memory);
 	}
 
+
 	return retVal;
 }
 
 ///watch
 bool etcd_watch(char* key, int index, char* action, char* prevValue, char* value) {
 	json_error_t error;
-	json_t* js_root;
-	json_t* js_node;
-	json_t* js_prevNode;
-	json_t* js_action;
-	json_t* js_value;
-	json_t* js_prevValue;
+	json_t* js_root = NULL;
+	json_t* js_node = NULL;
+	json_t* js_prevNode = NULL;
+	json_t* js_action = NULL;
+	json_t* js_value = NULL;
+	json_t* js_prevValue = NULL;
 	bool retVal = false;
 	char url[MAX_URL_LENGTH];
 	int res;
@@ -284,30 +298,35 @@ bool etcd_watch(char* key, int index, ch
 
 	res = performRequest(url, GET, WriteMemoryCallback, NULL, (void*) &reply);
 
-	if (res == CURLE_OPERATION_TIMEDOUT) {
-		//printf("error while performing curl w/ %s\n", url);
-	} else if (res != CURLE_OK) {
-		printf("error while performing curl w/ %s\n", url);
-	} else if ((js_root = json_loads(reply.memory, 0, &error)) == NULL) {
-		printf("error while parsing json data\n");
-	} else {
-		js_action = json_object_get(js_root, ETCD_JSON_ACTION);
-		js_node = json_object_get(js_root, ETCD_JSON_NODE);
-		js_prevNode = json_object_get(js_root, ETCD_JSON_PREVNODE);
-	
-		if (js_action == NULL || js_node == NULL) {
-			printf("error while retrieving expected node object %s\n", json_dumps(js_root, 0));
-		} else if ((js_value = json_object_get(js_node, ETCD_JSON_VALUE)) == NULL) {
-			printf("error while retrieving expected value objects\n");
-		}
-		else if (json_is_string(js_value) && json_is_string(js_action)) {
-			if ((js_prevNode != NULL) && ((js_prevValue = json_object_get(js_prevNode, ETCD_JSON_VALUE))
!= NULL) && (json_is_string(js_prevValue))) {
-				strncpy(prevValue, json_string_value(js_prevValue), MAX_VALUE_LENGTH);
-			}
+	if (res == CURLE_OK) {
+		js_root = json_loads(reply.memory, 0, &error);
+
+		if (js_root != NULL) {
+			js_action = json_object_get(js_root, ETCD_JSON_ACTION);
+			js_node = json_object_get(js_root, ETCD_JSON_NODE);
+			js_prevNode = json_object_get(js_root, ETCD_JSON_PREVNODE);
+		}
+		if (js_prevNode != NULL) {
+			js_prevValue = json_object_get(js_prevNode, ETCD_JSON_VALUE);
+		}
+		if (js_node != NULL) {
+			js_value = json_object_get(js_node, ETCD_JSON_VALUE);
+		}
+		if (js_prevNode != NULL) {
+			js_prevValue = json_object_get(js_prevNode, ETCD_JSON_VALUE);
+		}
+		if ((js_prevValue != NULL) && (json_is_string(js_prevValue))) {
+			strncpy(prevValue, json_string_value(js_prevValue), MAX_VALUE_LENGTH);
+		}
+		if ((js_value != NULL) && (js_action != NULL) && (json_is_string(js_value))
&& (json_is_string(js_action))) {
 			strncpy(value, json_string_value(js_value), MAX_VALUE_LENGTH);
 			strncpy(action, json_string_value(js_action), MAX_ACTION_LENGTH);
+
 			retVal = true;
 		}
+		if (js_root != NULL) {
+			json_decref(js_root);
+		}
 	}
 
 	if (reply.memory) {

Modified: celix/trunk/remote_services/discovery_etcd/private/src/etcd_watcher.c
URL: http://svn.apache.org/viewvc/celix/trunk/remote_services/discovery_etcd/private/src/etcd_watcher.c?rev=1632910&r1=1632909&r2=1632910&view=diff
==============================================================================
--- celix/trunk/remote_services/discovery_etcd/private/src/etcd_watcher.c (original)
+++ celix/trunk/remote_services/discovery_etcd/private/src/etcd_watcher.c Sun Oct 19 14:18:32
2014
@@ -125,7 +125,7 @@ static celix_status_t etcdWatcher_addAlr
 			int modIndex;
 
 			if (etcd_get(key, &value[0], &action[0], &modIndex) == true) {
-				// check that this is not equals to the local endpoint
+				// TODO: check that this is not equals to the local endpoint
 				endpointDiscoveryPoller_addDiscoveryEndpoint(discovery->poller, strdup(&value[0]));
 
 				if (modIndex > *highestModified) {
@@ -218,10 +218,12 @@ static void* etcdWatcher_run(void* data)
 		char preValue[MAX_VALUE_LENGTH];
 		char action[MAX_ACTION_LENGTH];
 
+		time_t timeBeforeWatch = time(NULL);
+
 		if (etcd_watch(rootPath, 0, &action[0], &preValue[0], &value[0]) == true) {
 
 			if (strcmp(action, "set") == 0) {
-				endpointDiscoveryPoller_addDiscoveryEndpoint(poller, &value[0]);
+				endpointDiscoveryPoller_addDiscoveryEndpoint(poller, strdup(&value[0]));
 			} else if (strcmp(action, "delete") == 0) {
 				endpointDiscoveryPoller_removeDiscoveryEndpoint(poller, &preValue[0]);
 			} else if (strcmp(action, "update") == 0) {
@@ -231,8 +233,10 @@ static void* etcdWatcher_run(void* data)
 			}
 		}
 
-		// update own framework uuid in any case;
-	    etcdWatcher_addOwnFramework(watcher);
+		// update own framework uuid
+		if (time(NULL) - timeBeforeWatch > (DEFAULT_ETCD_TTL/2)) {
+			etcdWatcher_addOwnFramework(watcher);
+		}
 	}
 
 	return NULL;



Mime
View raw message