celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpe...@apache.org
Subject svn commit: r1629967 - in /celix/trunk/remote_services/discovery_etcd/private: include/etcd.h src/etcd.c src/etcd_watcher.c
Date Tue, 07 Oct 2014 19:35:17 GMT
Author: bpetri
Date: Tue Oct  7 19:35:17 2014
New Revision: 1629967

URL: http://svn.apache.org/r1629967
Log:
CELIX-152: added etcd update functionality

Modified:
    celix/trunk/remote_services/discovery_etcd/private/include/etcd.h
    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/include/etcd.h
URL: http://svn.apache.org/viewvc/celix/trunk/remote_services/discovery_etcd/private/include/etcd.h?rev=1629967&r1=1629966&r2=1629967&view=diff
==============================================================================
--- celix/trunk/remote_services/discovery_etcd/private/include/etcd.h (original)
+++ celix/trunk/remote_services/discovery_etcd/private/include/etcd.h Tue Oct  7 19:35:17
2014
@@ -1,6 +1,8 @@
 #ifndef ETCD_H_
 #define ETCD_H_
 
+#include <stdbool.h>
+
 #define MAX_NODES			256
 
 #define MAX_KEY_LENGTH		256
@@ -21,7 +23,7 @@
 bool etcd_init(char* server, int port);
 bool etcd_get(char* key, char* value, char*action, int* modifiedIndex);
 bool etcd_getNodes(char* directory, char** nodeNames, int* size);
-bool etcd_set(char* key, char* value, int ttl);
+bool etcd_set(char* key, char* value, int ttl, bool prevExist);
 bool etcd_del(char* key);
 bool etcd_watch(char* key, int index, char* action, char* prevValue, char* value);
 

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=1629967&r1=1629966&r2=1629967&view=diff
==============================================================================
--- celix/trunk/remote_services/discovery_etcd/private/src/etcd.c (original)
+++ celix/trunk/remote_services/discovery_etcd/private/src/etcd.c Tue Oct  7 19:35:17 2014
@@ -174,8 +174,9 @@ bool etcd_getNodes(char* directory, char
 	return retVal;
 }
 
-//set
-bool etcd_set(char* key, char* value, int ttl) {
+
+
+bool etcd_set(char* key, char* value, int ttl, bool prevExist) {
 	json_error_t error;
 	json_t* js_root;
 	json_t* js_node;
@@ -183,6 +184,7 @@ bool etcd_set(char* key, char* value, in
 	bool retVal = false;
 	char url[MAX_URL_LENGTH];
 	char request[MAX_CONTENT_LENGTH];
+	char* cur = request;
 	int res;
 	struct MemoryStruct reply;
 
@@ -190,11 +192,13 @@ bool etcd_set(char* key, char* value, in
 	reply.size = 0; /* no data at this point */
 
 	snprintf(url, MAX_URL_LENGTH, "http://%s:%d/v2/keys/%s", etcd_server, etcd_port, key);
+	cur += snprintf(cur, MAX_CONTENT_LENGTH, "value=%s", value);
 
-	if (ttl <= 0)
-	    snprintf(request, MAX_CONTENT_LENGTH, "value=%s", value);
-	else
-	    snprintf(request, MAX_CONTENT_LENGTH, "value=%s;ttl=%d", value, ttl);
+	if (ttl > 0)
+	    cur += snprintf(cur, MAX_CONTENT_LENGTH, ";ttl=%d", ttl);
+
+	if (prevExist)
+	    cur += snprintf(cur, MAX_CONTENT_LENGTH, ";prevExist=true");
 
 	res = performRequest(url, PUT, WriteMemoryCallback, request, (void*) &reply);
 	if (res == CURLE_OPERATION_TIMEDOUT) {
@@ -218,6 +222,8 @@ bool etcd_set(char* key, char* value, in
 	return retVal;
 }
 
+
+
 //delete
 bool etcd_del(char* key) {
 	json_error_t error;

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=1629967&r1=1629966&r2=1629967&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 Tue Oct  7 19:35:17
2014
@@ -141,6 +141,9 @@ static celix_status_t etcdWatcher_addOwn
 {
     celix_status_t status = CELIX_BUNDLE_EXCEPTION;
     char localNodePath[MAX_LOCALNODE_LENGTH];
+    char value[MAX_VALUE_LENGTH];
+    char action[MAX_VALUE_LENGTH];
+    int modIndex;
     char* endpoints = NULL;
     char* ttlStr = NULL;
     int ttl;
@@ -167,12 +170,13 @@ static celix_status_t etcdWatcher_addOwn
         }
     }
 
-    if (etcd_set(localNodePath, endpoints, ttl) == false)
-    {
+	if (etcd_get(localNodePath, &value[0], &action[0], &modIndex) != true) {
+		etcd_set(localNodePath, endpoints, ttl, false);
+	}
+	else if (etcd_set(localNodePath, endpoints, ttl, true) == false)  {
         fw_log(logger, OSGI_FRAMEWORK_LOG_WARNING, "Cannot register local discovery");
     }
-    else
-    {
+    else {
         status = CELIX_SUCCESS;
     }
 



Mime
View raw message