Browse Source

Create a stratifier thread that updates the data from the gbt template regularly and receives other requests

master
Con Kolivas 11 years ago
parent
commit
6f1838536c
  1. 3
      src/Makefile.am
  2. 12
      src/ckpool.c
  3. 1
      src/libckpool.h
  4. 98
      src/stratifier.c
  5. 15
      src/stratifier.h

3
src/Makefile.am

@ -5,5 +5,6 @@ libckpool_la_SOURCES = libckpool.c libckpool.h sha2.c sha2.h
libckpool_la_LIBADD = @PTHREAD_LIBS@ @MATH_LIBS@ @RT_LIBS@ @JANSSON_LIBS@ libckpool_la_LIBADD = @PTHREAD_LIBS@ @MATH_LIBS@ @RT_LIBS@ @JANSSON_LIBS@
bin_PROGRAMS = ckpool bin_PROGRAMS = ckpool
ckpool_SOURCES = ckpool.c ckpool.h generator.c generator.h bitcoin.c bitcoin.h ckpool_SOURCES = ckpool.c ckpool.h generator.c generator.h bitcoin.c bitcoin.h \
stratifier.c stratifier.h
ckpool_LDADD = libckpool.la ckpool_LDADD = libckpool.la

12
src/ckpool.c

@ -20,6 +20,7 @@
#include "ckpool.h" #include "ckpool.h"
#include "libckpool.h" #include "libckpool.h"
#include "generator.h" #include "generator.h"
#include "stratifier.h"
/* Only global variable, to be used only by sighandler */ /* Only global variable, to be used only by sighandler */
static ckpool_t *global_ckp; static ckpool_t *global_ckp;
@ -149,6 +150,7 @@ static void clean_up(ckpool_t *ckp)
static void shutdown_children(ckpool_t *ckp, int sig) static void shutdown_children(ckpool_t *ckp, int sig)
{ {
kill(ckp->generator.pid, sig); kill(ckp->generator.pid, sig);
kill(ckp->stratifier.pid, sig);
} }
static void sighandler(int sig) static void sighandler(int sig)
@ -206,15 +208,15 @@ static void test_functions(ckpool_t *ckp)
} }
send_unix_msg(genfd, "getbase"); send_unix_msg(genfd, "getbase");
buf = recv_unix_msg(genfd); buf = recv_unix_msg(genfd);
LOGWARNING("getbase response: %s", buf);
dealloc(buf); dealloc(buf);
#if 0
genfd = open_unix_client(ckp->generator.us.path); genfd = open_unix_client(ckp->generator.us.path);
if (genfd < 0) { if (genfd < 0) {
LOGWARNING("Failed to open generator socket %s", path); LOGWARNING("Failed to open generator socket %s", path);
return; return;
} }
send_unix_msg(genfd, "shutdown"); send_unix_msg(genfd, "shutdown");
#endif
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -276,6 +278,12 @@ int main(int argc, char **argv)
ckp.generator.process = &generator; ckp.generator.process = &generator;
launch_process(&ckp.generator); launch_process(&ckp.generator);
ckp.stratifier.ckp = &ckp;
ckp.stratifier.processname = strdup("stratifier");
ckp.stratifier.sockname = ckp.stratifier.processname;
ckp.stratifier.process = &stratifier;
launch_process(&ckp.stratifier);
/* Install signal handlers only for the master process to be able to /* Install signal handlers only for the master process to be able to
* shut down all child processes */ * shut down all child processes */
handler.sa_handler = &sighandler; handler.sa_handler = &sighandler;

1
src/libckpool.h

@ -148,6 +148,7 @@ struct ckpool_instance {
/* Process instance data of parent/child processes */ /* Process instance data of parent/child processes */
proc_instance_t main; proc_instance_t main;
proc_instance_t generator; proc_instance_t generator;
proc_instance_t stratifier;
/* Bitcoind data */ /* Bitcoind data */
char *btcdurl; char *btcdurl;

98
src/stratifier.c

@ -0,0 +1,98 @@
/*
* Copyright 2014 Con Kolivas
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include "ckpool.h"
#include "libckpool.h"
#include "bitcoin.h"
static void update_base(ckpool_t *ckp)
{
char *path = ckp->generator.us.path, *buf;
int genfd;
genfd = open_unix_client(ckp->generator.us.path);
if (genfd < 0) {
LOGWARNING("Failed to open generator socket %s", path);
return;
}
send_unix_msg(genfd, "getbase");
buf = recv_unix_msg(genfd);
/* Do something with this buffer here */
dealloc(buf);
}
static int strat_loop(ckpool_t *ckp, proc_instance_t *pi)
{
int sockd, ret = 0, selret;
unixsock_t *us = &pi->us;
char *buf = NULL;
fd_set readfds;
tv_t timeout;
reset:
timeout.tv_sec = 60;
retry:
FD_ZERO(&readfds);
FD_SET(us->sockd, &readfds);
selret = select(us->sockd + 1, &readfds, NULL, NULL, &timeout);
if (selret < 0) {
if (interrupted())
goto retry;
LOGERR("Select failed in strat_loop");
ret = 1;
goto out;
}
if (!selret) {
LOGDEBUG("60s elapsed in strat_loop, updating gbt base");
update_base(ckp);
goto reset;
}
sockd = accept(us->sockd, NULL, NULL);
if (sockd < 0) {
if (interrupted())
goto retry;
LOGERR("Failed to accept on stratifier socket");
ret = 1;
goto out;
}
dealloc(buf);
buf = recv_unix_msg(sockd);
if (!buf) {
LOGWARNING("Failed to get message in strat_loop");
close(sockd);
goto retry;
}
LOGDEBUG("Stratifier received request: %s", buf);
if (!strncasecmp(buf, "shutdown", 8))
goto out;
if (!strncasecmp(buf, "update", 6)) {
update_base(ckp);
goto reset;
}
out:
dealloc(buf);
return ret;
}
int stratifier(proc_instance_t *pi)
{
ckpool_t *ckp = pi->ckp;
int ret = 0;
strat_loop(ckp, pi);
return ret;
}

15
src/stratifier.h

@ -0,0 +1,15 @@
/*
* Copyright 2014 Con Kolivas
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#ifndef STRATIFIER_H
#define STRATIFIER_H
int stratifier(proc_instance_t *pi);
#endif /* STRATIFIER_H */
Loading…
Cancel
Save