Commit 0abac0b5d9ec40f6e6b0d076bcbc4bdd290a2884

Kano 2012-02-24T03:00:01

API implement addpool command

diff --git a/README b/README
index c3a28a9..e4e744b 100644
--- a/README
+++ b/README
@@ -660,6 +660,13 @@ The list of requests - a (*) means it requires privileged access - and replies a
                               stating the results of enabling pool N
                               The Msg includes the pool URL
 
+ addpool|URL,USR,PASS (*)
+               none           There is no reply section just the STATUS section
+                              stating the results of attempting to add pool N
+                              The Msg includes the pool URL
+                              Use '\\' to get a '\' and '\,' to include a comma
+                              inside URL, USR or PASS
+
  disablepool|N (*)
                none           There is no reply section just the STATUS section
                               stating the results of disabling pool N
diff --git a/api.c b/api.c
index b3aea26..9528230 100644
--- a/api.c
+++ b/api.c
@@ -264,6 +264,10 @@ static const char *JSON_PARAMETER = "parameter";
 #define MSG_ALRENAP 49
 #define MSG_ALRDISP 50
 #define MSG_DISLASTP 51
+#define MSG_MISPDP 52
+#define MSG_INVPDP 53
+#define MSG_TOOMANYP 54
+#define MSG_ADDPOOL 55
 
 enum code_severity {
 	SEVERITY_ERR,
@@ -356,6 +360,10 @@ struct CODES {
  { SEVERITY_INFO,  MSG_ALRENAP,	PARAM_POOL,	"Pool %d:'%s' already enabled" },
  { SEVERITY_INFO,  MSG_ALRDISP,	PARAM_POOL,	"Pool %d:'%s' already disabled" },
  { SEVERITY_ERR,   MSG_DISLASTP,PARAM_POOL,	"Cannot disable last active pool %d:'%s'" },
+ { SEVERITY_ERR,   MSG_MISPDP,	PARAM_NONE,	"Missing addpool details" },
+ { SEVERITY_ERR,   MSG_INVPDP,	PARAM_STR,	"Invalid addpool details '%s'" },
+ { SEVERITY_ERR,   MSG_TOOMANYP,PARAM_NONE,	"Reached maximum number of pools (%d)" },
+ { SEVERITY_SUCC,  MSG_ADDPOOL,	PARAM_STR,	"Added pool '%s'" },
  { SEVERITY_FAIL, 0, 0, NULL }
 };
 
@@ -986,6 +994,81 @@ static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 	strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
 }
 
+static void copyadvanceafter(char ch, char **param, char **buf)
+{
+#define src_p (*param)
+#define dst_b (*buf)
+
+	while (*src_p && *src_p != ch) {
+		if (*src_p == '\\' && *(src_p+1) != '\0')
+			src_p++;
+
+		*(dst_b++) = *(src_p++);
+	}
+	if (*src_p)
+		src_p++;
+
+	*(dst_b++) = '\0';
+}
+
+static bool pooldetails(char *param, char **url, char **user, char **pass)
+{
+	char *ptr, *buf;
+
+	ptr = buf = malloc(strlen(param)+1);
+	if (unlikely(!buf))
+		quit(1, "Failed to malloc pooldetails buf");
+
+	*url = buf;
+
+	// copy url
+	copyadvanceafter(',', &param, &buf);
+
+	if (!(*param)) // missing user
+		goto exitsama;
+
+	*user = buf;
+
+	// copy user
+	copyadvanceafter(',', &param, &buf);
+
+	if (!*param) // missing pass
+		goto exitsama;
+
+	*pass = buf;
+
+	// copy pass
+	copyadvanceafter(',', &param, &buf);
+
+	return true;
+
+exitsama:
+	free(ptr);
+	return false;
+}
+
+static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
+{
+	char *url, *user, *pass;
+
+	if (param == NULL || *param == '\0') {
+		strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
+		return;
+	}
+
+	if (!pooldetails(param, &url, &user, &pass)) {
+		strcpy(io_buffer, message(MSG_INVPDP, 0, param, isjson));
+		return;
+	}
+
+	if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
+		strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
+		return;
+	}
+
+	strcpy(io_buffer, message(MSG_ADDPOOL, 0, url, isjson));
+}
+
 static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 {
 	struct pool *pool;
@@ -1271,7 +1354,7 @@ struct CMDS {
 	{ "gpucount",		gpucount,	false },
 	{ "cpucount",		cpucount,	false },
 	{ "switchpool",		switchpool,	true },
-//	{ "addpool",		addpool,	true }, Not yet ...
+	{ "addpool",		addpool,	true },
 	{ "enablepool",		enablepool,	true },
 	{ "disablepool",	disablepool,	true },
 	{ "gpuintensity",	gpuintensity,	true },