Browse Source

SSM: Allow configuring stratum port via --stratum-port option and RPC setconfig

Luke Dashjr 12 years ago
parent
commit
28a9b3ca53
5 changed files with 74 additions and 23 deletions
  1. 1 0
      README
  2. 9 0
      api.c
  3. 40 10
      driver-stratum.c
  4. 23 13
      miner.c
  5. 1 0
      miner.h

+ 1 - 0
README

@@ -233,6 +233,7 @@ Options for both config file and command line:
 --show-processors   Show per processor statistics in summary
 --skip-security-checks <arg> Skip security checks sometimes to save bandwidth; only check 1/<arg>th of the time (default: never skip)
 --socks-proxy <arg> Set socks4 proxy (host:port) for all pools without a proxy specified
+--stratum-port <arg> Port number to listen on for stratum miners (-1 means disabled) (default: -1)
 --submit-threads    Minimum number of concurrent share submissions (default: 64)
 --syslog            Use system log for output messages (default: standard error)
 --temp-cutoff <arg> Maximum temperature devices will be allowed to reach before being disabled, one value or comma separated list

+ 9 - 0
api.c

@@ -3066,6 +3066,8 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 		io_close(io_data);
 }
 
+extern void stratumsrv_change_port();
+
 static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 	char *comma;
@@ -3113,6 +3115,13 @@ static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 		if (httpsrv_port != -1)
 			httpsrv_start(httpsrv_port);
 	}
+#endif
+#ifdef USE_LIBEVENT
+	else if (strcasecmp(param, "stratum-port") == 0)
+	{
+		stratumsrv_port = value;
+		stratumsrv_change_port();
+	}
 #endif
 	else {
 		message(io_data, MSG_UNKCON, 0, param, isjson);

+ 40 - 10
driver-stratum.c

@@ -45,6 +45,10 @@ static struct stratumsrv_job *_ssm_jobs;
 static struct work _ssm_cur_job_work;
 static uint64_t _ssm_jobid;
 
+static struct event_base *_smm_evbase;
+static bool _smm_running;
+static struct evconnlistener *_smm_listener;
+
 struct stratumsrv_conn {
 	struct bufferevent *bev;
 	uint32_t xnonce1_le;
@@ -570,6 +574,32 @@ void stratumlistener(struct evconnlistener *listener, evutil_socket_t sock, stru
 	bufferevent_enable(bev, EV_READ | EV_WRITE);
 }
 
+void stratumsrv_start();
+
+void stratumsrv_change_port()
+{
+	struct event_base * const evbase = _smm_evbase;
+	
+	if (_smm_listener)
+		evconnlistener_free(_smm_listener);
+	
+	if (!_smm_running)
+	{
+		stratumsrv_start();
+		return;
+	}
+	
+	struct sockaddr_in sin = {
+		.sin_family = AF_INET,
+		.sin_addr.s_addr = INADDR_ANY,
+		.sin_port = htons(stratumsrv_port),
+	};
+	_smm_listener = evconnlistener_new_bind(evbase, stratumlistener, NULL, (
+		LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
+	), 0x10, (void*)&sin, sizeof(sin));
+}
+
+static
 void *stratumsrv_thread(__maybe_unused void *p)
 {
 	pthread_detach(pthread_self());
@@ -580,6 +610,7 @@ void *stratumsrv_thread(__maybe_unused void *p)
 	_ssm_client_xnonce2sz = 2;
 	
 	struct event_base *evbase = event_base_new();
+	_smm_evbase = evbase;
 	{
 		ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
 		_stratumsrv_update_notify(-1, 0, NULL);
@@ -589,17 +620,16 @@ void *stratumsrv_thread(__maybe_unused void *p)
 		struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
 		event_add(ev_update_notifier, NULL);
 	}
-	{
-		struct sockaddr_in sin = {
-			.sin_family = AF_INET,
-			.sin_addr.s_addr = INADDR_ANY,
-			.sin_port = htons(3334),
-		};
-		evconnlistener_new_bind(evbase, stratumlistener, NULL, (
-			LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
-		), 0x10, (void*)&sin, sizeof(sin));
-	}
+	stratumsrv_change_port();
 	event_base_dispatch(evbase);
 	
 	return NULL;
 }
+
+void stratumsrv_start()
+{
+	_smm_running = true;
+	pthread_t pth;
+	if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
+		quit(1, "stratumsrv thread create failed");
+}

+ 23 - 13
miner.c

@@ -163,6 +163,9 @@ static bool opt_nogpu;
 #include "httpsrv.h"
 int httpsrv_port = -1;
 #endif
+#ifdef USE_LIBEVENT
+int stratumsrv_port = -1;
+#endif
 
 struct string_elist *scan_devices;
 bool opt_force_dev_init;
@@ -1792,6 +1795,11 @@ static struct opt_table opt_config_table[] = {
 	OPT_WITH_ARG("--socks-proxy",
 		     opt_set_charp, NULL, &opt_socks_proxy,
 		     "Set socks4 proxy (host:port)"),
+#ifdef USE_LIBEVENT
+	OPT_WITH_ARG("--stratum-port",
+	             opt_set_intval, opt_show_intval, &stratumsrv_port,
+	             "Port number to listen on for stratum miners (-1 means disabled)"),
+#endif
 	OPT_WITHOUT_ARG("--submit-stale",
 			opt_set_bool, &opt_submit_stale,
 	                opt_hidden),
@@ -9905,7 +9913,7 @@ static void raise_fd_limits(void)
 }
 
 extern void bfg_init_threadlocal();
-extern void *stratumsrv_thread(void *);
+extern void stratumsrv_start();
 
 int main(int argc, char *argv[])
 {
@@ -10193,17 +10201,22 @@ int main(int argc, char *argv[])
 	}
 
 	if (!total_devices) {
-#ifndef USE_LIBMICROHTTPD
-		const int httpsrv_port = -1;
+		const bool netdev_support =
+#ifdef USE_LIBMICROHTTPD
+			(httpsrv_port != -1) ||
+#endif
+#ifdef USE_LIBEVENT
+			(stratumsrv_port != -1) ||
 #endif
-		if (httpsrv_port == -1 && (!use_curses) && !opt_api_listen)
+			false;
+		if ((!netdev_support) && (!use_curses) && !opt_api_listen)
 			quit(1, "All devices disabled, cannot mine!");
 		applog(LOG_WARNING, "No devices detected!");
 		if (use_curses)
 			applog(LOG_WARNING, "Waiting for devices; press 'M+' to add, or 'Q' to quit");
 		else
 			applog(LOG_WARNING, "Waiting for %s or press Ctrl-C to quit",
-		       (httpsrv_port == -1) ? "RPC commands" : "network devices");
+		       netdev_support ? "network devices" : "RPC commands");
 	}
 
 	load_temp_config();
@@ -10396,14 +10409,6 @@ begin_bench:
 		if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, NULL)))
 			quit(1, "submit_work thread create failed");
 	}
-	
-#ifdef USE_LIBEVENT
-	{
-		pthread_t pth;
-		if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
-			quit(1, "stratumsrv thread create failed");
-	}
-#endif
 
 	watchpool_thr_id = 2;
 	thr = &control_thr[watchpool_thr_id];
@@ -10441,6 +10446,11 @@ begin_bench:
 		httpsrv_start(httpsrv_port);
 #endif
 
+#ifdef USE_LIBEVENT
+	if (stratumsrv_port != -1)
+		stratumsrv_start();
+#endif
+
 #ifdef HAVE_CURSES
 	/* Create curses input thread for keyboard input. Create this last so
 	 * that we know all threads are created since this can call kill_work

+ 1 - 0
miner.h

@@ -891,6 +891,7 @@ extern bool opt_autofan;
 extern bool opt_autoengine;
 extern bool use_curses;
 extern int httpsrv_port;
+extern int stratumsrv_port;
 extern char *opt_api_allow;
 extern bool opt_api_mcast;
 extern char *opt_api_mcast_addr;