Browse Source

Add a json parser to the stratum receiver that will generate return messages

master
Con Kolivas 11 years ago
parent
commit
126c7964e2
  1. 28
      src/ckpool.c
  2. 60
      src/stratifier.c

28
src/ckpool.c

@ -127,7 +127,6 @@ static void launch_process(proc_instance_t *pi)
{
pid_t pid;
create_process_unixsock(pi);
pid = fork();
if (pid < 0)
quit(1, "Failed to fork %s in launch_process", pi->processname);
@ -237,7 +236,7 @@ static void test_functions(ckpool_t *ckp)
#endif
}
static void launch_generator(ckpool_t *ckp)
static void prepare_generator(ckpool_t *ckp)
{
proc_instance_t *pi = &ckp->generator;
@ -245,10 +244,10 @@ static void launch_generator(ckpool_t *ckp)
pi->processname = strdup("generator");
pi->sockname = pi->processname;
pi->process = &generator;
launch_process(pi);
create_process_unixsock(pi);
}
static void launch_stratifier(ckpool_t *ckp)
static void prepare_stratifier(ckpool_t *ckp)
{
proc_instance_t *pi = &ckp->stratifier;
@ -256,10 +255,10 @@ static void launch_stratifier(ckpool_t *ckp)
pi->processname = strdup("stratifier");
pi->sockname = pi->processname;
pi->process = &stratifier;
launch_process(pi);
create_process_unixsock(pi);
}
static void launch_connector(ckpool_t *ckp)
static void prepare_connector(ckpool_t *ckp)
{
proc_instance_t *pi = &ckp->connector;
@ -267,7 +266,7 @@ static void launch_connector(ckpool_t *ckp)
pi->processname = strdup("connector");
pi->sockname = pi->processname;
pi->process = &connector;
launch_process(pi);
create_process_unixsock(pi);
}
static void *watchdog(void *arg)
@ -281,13 +280,13 @@ static void *watchdog(void *arg)
pid = wait(&status);
if (pid == ckp->generator.pid) {
LOGERR("Generator process dead! Relaunching");
launch_generator(ckp);
launch_process(&ckp->generator);
} else if (pid == ckp->stratifier.pid) {
LOGERR("Stratifier process dead! Relaunching");
launch_stratifier(ckp);
launch_process(&ckp->stratifier);
} else if (pid == ckp->connector.pid) {
LOGERR("Connector process dead! Relaunching");
launch_connector(ckp);
launch_process(&ckp->connector);
}
}
return NULL;
@ -359,9 +358,12 @@ int main(int argc, char **argv)
create_pthread(&ckp.pth_listener, listener, &ckp.main);
/* Launch separate processes from here */
launch_generator(&ckp);
launch_stratifier(&ckp);
launch_connector(&ckp);
prepare_generator(&ckp);
prepare_stratifier(&ckp);
prepare_connector(&ckp);
launch_process(&ckp.generator);
launch_process(&ckp.stratifier);
launch_process(&ckp.connector);
test_functions(&ckp);

60
src/stratifier.c

@ -113,6 +113,9 @@ struct stratum_instance {
UT_hash_handle hh;
int id;
char enonce1[20];
double diff;
bool authorised;
char *packagever;
};
typedef struct stratum_instance stratum_instance_t;
@ -311,7 +314,10 @@ static stratum_instance_t *__stratum_add_instance(int id)
{
stratum_instance_t *instance = ckzalloc(sizeof(stratum_instance_t));
sprintf(instance->enonce1, "%016lx", enonce1_64++);
instance->id = id;
instance->diff = 1.0;
LOGDEBUG("Added instance %d with enonce1 %s", id, instance->enonce1);
HASH_ADD_INT(stratum_instances, id, instance);
return instance;
}
@ -329,12 +335,13 @@ static void stratum_add_recvd(json_t *val)
mutex_unlock(&stratum_recv_lock);
}
static void stratum_add_sending(json_t *val)
static void stratum_add_send(json_t *val, int client_id)
{
stratum_msg_t *msg;
msg = ckzalloc(sizeof(stratum_msg_t));
msg->json_msg = val;
msg->client_id = client_id;
mutex_lock(&stratum_send_lock);
LL_APPEND(stratum_sends, msg);
@ -432,6 +439,51 @@ static void *blockupdate(void *arg)
return NULL;
}
static json_t *gen_json_result(int client_id, json_t *method, json_t *params)
{
return json_string("Empty");
}
static void parse_instance_msg(int client_id, json_t *msg)
{
json_t *result_val = NULL, *err_val = NULL, *id_val = NULL;
json_t *method, *msg_id, *params;
json_t *json_msg;
json_msg = json_object();
id_val = json_object_get(msg, "id");
if (unlikely(!id_val)) {
err_val = json_string("-1:id not found");
goto out;
}
if (unlikely(!json_is_integer(id_val))) {
err_val = json_string("-1:id is not integer");
goto out;
}
method = json_object_get(msg, "method");
if (unlikely(!method)) {
err_val = json_string("-3:method not found");
goto out;
}
if (unlikely(!json_is_string(method))) {
err_val = json_string("-1:method is not string");
goto out;
}
params = json_object_get(msg, "params");
if (unlikely(!params)) {
err_val = json_string("-1:params not found");
goto out;
}
err_val = json_null();
result_val = gen_json_result(client_id, method, params);
out:
json_object_set_nocheck(json_msg, "id", id_val);
json_object_set_nocheck(json_msg, "error", err_val);
json_object_set_nocheck(json_msg, "result", result_val);
stratum_add_send(json_msg, client_id);
}
static void *stratum_receiver(void *arg)
{
ckpool_t *ckp = (ckpool_t *)arg;
@ -469,9 +521,7 @@ static void *stratum_receiver(void *arg)
}
ck_uilock(&instance_lock);
char *s = json_dumps(msg->json_msg, 0);
LOGERR("Client %d sent message %s", msg->client_id, s);
free(s);
parse_instance_msg(msg->client_id, msg->json_msg);
json_decref(msg->json_msg);
free(msg);
@ -483,11 +533,11 @@ static void *stratum_receiver(void *arg)
static void *stratum_sender(void *arg)
{
ckpool_t *ckp = (ckpool_t *)arg;
stratum_msg_t *msg;
rename_proc("sender");
while (42) {
stratum_msg_t *msg;
char *s;
mutex_lock(&stratum_send_lock);

Loading…
Cancel
Save