Browse Source

SGW: Split proxy code out from driver-getwork into driver-proxy

Luke Dashjr 12 years ago
parent
commit
19e8fa8996
5 changed files with 156 additions and 131 deletions
  1. 4 0
      Makefile.am
  2. 2 0
      configure.ac
  3. 7 131
      driver-getwork.c
  4. 124 0
      driver-proxy.c
  5. 19 0
      driver-proxy.h

+ 4 - 0
Makefile.am

@@ -83,6 +83,10 @@ endif
 
 
 bfgminer_SOURCES	+= logging.c
 bfgminer_SOURCES	+= logging.c
 
 
+if NEED_BFG_DRIVER_PROXY
+bfgminer_SOURCES += driver-proxy.c driver-proxy.h
+endif
+
 if USE_LIBMICROHTTPD
 if USE_LIBMICROHTTPD
 bfgminer_SOURCES += httpsrv.c httpsrv.h driver-getwork.c
 bfgminer_SOURCES += httpsrv.c httpsrv.h driver-getwork.c
 bfgminer_LDADD += $(libmicrohttpd_LIBS)
 bfgminer_LDADD += $(libmicrohttpd_LIBS)

+ 2 - 0
configure.ac

@@ -350,6 +350,8 @@ if test "x$httpsrv" != "xno"; then
 fi
 fi
 AM_CONDITIONAL([USE_LIBMICROHTTPD], [test x$httpsrv = xyes])
 AM_CONDITIONAL([USE_LIBMICROHTTPD], [test x$httpsrv = xyes])
 
 
+AM_CONDITIONAL([NEED_BFG_DRIVER_PROXY], [test x$httpsrv != xno])
+
 AC_ARG_ENABLE([modminer],
 AC_ARG_ENABLE([modminer],
 	[AC_HELP_STRING([--disable-modminer],[Compile support for ModMiner (default enabled)])],
 	[AC_HELP_STRING([--disable-modminer],[Compile support for ModMiner (default enabled)])],
 	[modminer=$enableval],
 	[modminer=$enableval],

+ 7 - 131
driver-getwork.c

@@ -26,79 +26,10 @@
 #include <uthash.h>
 #include <uthash.h>
 
 
 #include "deviceapi.h"
 #include "deviceapi.h"
+#include "driver-proxy.h"
 #include "httpsrv.h"
 #include "httpsrv.h"
 #include "miner.h"
 #include "miner.h"
 
 
-struct device_drv getwork_drv;
-
-struct getwork_client {
-	char *username;
-	struct cgpu_info *cgpu;
-	struct work *work;
-	struct timeval tv_hashes_done;
-	
-	UT_hash_handle hh;
-};
-
-static
-struct getwork_client *getwork_clients;
-static
-pthread_mutex_t getwork_clients_mutex;
-
-static
-void prune_worklog()
-{
-	struct getwork_client *client, *tmp;
-	struct work *work, *tmp2;
-	struct timeval tv_now;
-	
-	timer_set_now(&tv_now);
-	
-	mutex_lock(&getwork_clients_mutex);
-	HASH_ITER(hh, getwork_clients, client, tmp)
-	{
-		HASH_ITER(hh, client->work, work, tmp2)
-		{
-			if (timer_elapsed(&work->tv_work_start, &tv_now) <= opt_expiry)
-				break;
-			HASH_DEL(client->work, work);
-			free_work(work);
-		}
-	}
-	mutex_unlock(&getwork_clients_mutex);
-}
-
-static
-pthread_t prune_worklog_pth;
-
-static
-void *prune_worklog_thread(void *userdata)
-{
-	struct cgpu_info *cgpu = userdata;
-	
-	pthread_detach(pthread_self());
-	RenameThread("SGW_pruner");
-	
-	while (!cgpu->shutdown)
-	{
-		prune_worklog();
-		sleep(60);
-	}
-	return NULL;
-}
-
-static
-void getwork_init()
-{
-	mutex_init(&getwork_clients_mutex);
-}
-
-static
-void getwork_first_client(struct cgpu_info *cgpu)
-{
-	pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, cgpu);
-}
-
 static
 static
 void getwork_prepare_resp(struct MHD_Response *resp)
 void getwork_prepare_resp(struct MHD_Response *resp)
 {
 {
@@ -129,8 +60,7 @@ int getwork_error(struct MHD_Connection *conn, int16_t errcode, const char *errm
 
 
 int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
 int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
 {
 {
-	static bool _init = false, b;
-	struct getwork_client *client;
+	struct proxy_client *client;
 	struct MHD_Response *resp;
 	struct MHD_Response *resp;
 	char *user, *idstr = NULL;
 	char *user, *idstr = NULL;
 	const char *submit = NULL;
 	const char *submit = NULL;
@@ -144,12 +74,6 @@ int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
 	const char *hashesdone = NULL;
 	const char *hashesdone = NULL;
 	int ret;
 	int ret;
 	
 	
-	if (unlikely(!_init))
-	{
-		_init = true;
-		getwork_init();
-	}
-	
 	if (bytes_len(upbuf))
 	if (bytes_len(upbuf))
 	{
 	{
 		bytes_nullterminate(upbuf);
 		bytes_nullterminate(upbuf);
@@ -182,44 +106,14 @@ int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
 		goto out;
 		goto out;
 	}
 	}
 	
 	
-	mutex_lock(&getwork_clients_mutex);
-	HASH_FIND_STR(getwork_clients, user, client);
+	client = proxy_find_or_create_client(user);
+	free(user);
 	if (!client)
 	if (!client)
 	{
 	{
-		cgpu = malloc(sizeof(*cgpu));
-		client = malloc(sizeof(*client));
-		*cgpu = (struct cgpu_info){
-			.drv = &getwork_drv,
-			.threads = 0,
-			.device_data = client,
-			.device_path = user,
-		};
-		if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
-		{
-			free(client);
-			free(cgpu);
-			ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
-			goto out;
-		}
-		*client = (struct getwork_client){
-			.username = user,
-			.cgpu = cgpu,
-		};
-		
-		b = HASH_COUNT(getwork_clients);
-		HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
-		mutex_unlock(&getwork_clients_mutex);
-		
-		if (!b)
-			getwork_first_client(cgpu);
-	}
-	else
-	{
-		mutex_unlock(&getwork_clients_mutex);
-		free(user);
-		cgpu = client->cgpu;
+		ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
+		goto out;
 	}
 	}
-	user = NULL;
+	cgpu = client->cgpu;
 	thr = cgpu->thr[0];
 	thr = cgpu->thr[0];
 	
 	
 	hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
 	hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
@@ -310,26 +204,8 @@ out:
 		hashes_done(thr, lld, &tv_delta, NULL);
 		hashes_done(thr, lld, &tv_delta, NULL);
 	}
 	}
 	
 	
-	free(user);
 	free(idstr);
 	free(idstr);
 	if (json)
 	if (json)
 		json_decref(json);
 		json_decref(json);
 	return ret;
 	return ret;
 }
 }
-
-#ifdef HAVE_CURSES
-static
-void getwork_wlogprint_status(struct cgpu_info *cgpu)
-{
-	struct getwork_client *client = cgpu->device_data;
-	wlogprint("Username: %s\n", client->username);
-}
-#endif
-
-struct device_drv getwork_drv = {
-	.dname = "getwork",
-	.name = "SGW",
-#ifdef HAVE_CURSES
-	.proc_wlogprint_status = getwork_wlogprint_status,
-#endif
-};

+ 124 - 0
driver-proxy.c

@@ -0,0 +1,124 @@
+#include <pthread.h>
+
+#include <uthash.h>
+
+#include "deviceapi.h"
+#include "driver-proxy.h"
+#include "miner.h"
+#include "util.h"
+
+struct device_drv proxy_drv;
+
+static
+struct proxy_client *proxy_clients;
+static
+pthread_mutex_t proxy_clients_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static
+void prune_worklog()
+{
+	struct proxy_client *client, *tmp;
+	struct work *work, *tmp2;
+	struct timeval tv_now;
+	
+	timer_set_now(&tv_now);
+	
+	mutex_lock(&proxy_clients_mutex);
+	HASH_ITER(hh, proxy_clients, client, tmp)
+	{
+		HASH_ITER(hh, client->work, work, tmp2)
+		{
+			if (timer_elapsed(&work->tv_work_start, &tv_now) <= opt_expiry)
+				break;
+			HASH_DEL(client->work, work);
+			free_work(work);
+		}
+	}
+	mutex_unlock(&proxy_clients_mutex);
+}
+
+static
+pthread_t prune_worklog_pth;
+
+static
+void *prune_worklog_thread(void *userdata)
+{
+	struct cgpu_info *cgpu = userdata;
+	
+	pthread_detach(pthread_self());
+	RenameThread("PXY_pruner");
+	
+	while (!cgpu->shutdown)
+	{
+		prune_worklog();
+		sleep(60);
+	}
+	return NULL;
+}
+
+static
+void proxy_first_client(struct cgpu_info *cgpu)
+{
+	pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, cgpu);
+}
+
+struct proxy_client *proxy_find_or_create_client(const char *username)
+{
+	struct proxy_client *client;
+	struct cgpu_info *cgpu;
+	char *user;
+	int b;
+	
+	mutex_lock(&proxy_clients_mutex);
+	HASH_FIND_STR(proxy_clients, username, client);
+	if (!client)
+	{
+		user = strdup(username);
+		cgpu = malloc(sizeof(*cgpu));
+		client = malloc(sizeof(*client));
+		*cgpu = (struct cgpu_info){
+			.drv = &proxy_drv,
+			.threads = 0,
+			.device_data = client,
+			.device_path = user,
+		};
+		if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
+		{
+			free(client);
+			free(cgpu);
+			free(user);
+			return NULL;
+		}
+		*client = (struct proxy_client){
+			.username = user,
+			.cgpu = cgpu,
+		};
+		
+		b = HASH_COUNT(proxy_clients);
+		HASH_ADD_KEYPTR(hh, proxy_clients, client->username, strlen(user), client);
+		mutex_unlock(&proxy_clients_mutex);
+		
+		if (!b)
+			proxy_first_client(cgpu);
+	}
+	else
+		mutex_unlock(&proxy_clients_mutex);
+	return client;
+}
+
+#ifdef HAVE_CURSES
+static
+void proxy_wlogprint_status(struct cgpu_info *cgpu)
+{
+	struct proxy_client *client = cgpu->device_data;
+	wlogprint("Username: %s\n", client->username);
+}
+#endif
+
+struct device_drv proxy_drv = {
+	.dname = "proxy",
+	.name = "PXY",
+#ifdef HAVE_CURSES
+	.proc_wlogprint_status = proxy_wlogprint_status,
+#endif
+};

+ 19 - 0
driver-proxy.h

@@ -0,0 +1,19 @@
+#ifndef BFG_DRIVER_PROXY_H
+#define BFG_DRIVER_PROXY_H
+
+#include <uthash.h>
+
+#include "miner.h"
+
+struct proxy_client {
+	char *username;
+	struct cgpu_info *cgpu;
+	struct work *work;
+	struct timeval tv_hashes_done;
+	
+	UT_hash_handle hh;
+};
+
+extern struct proxy_client *proxy_find_or_create_client(const char *user);
+
+#endif