driver-proxy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <unistd.h>
  11. #include <pthread.h>
  12. #include <uthash.h>
  13. #include "deviceapi.h"
  14. #include "driver-proxy.h"
  15. #include "miner.h"
  16. #include "util.h"
  17. BFG_REGISTER_DRIVER(proxy_drv)
  18. static const struct bfg_set_device_definition proxy_set_device_funcs[];
  19. static
  20. struct proxy_client *proxy_clients;
  21. static
  22. pthread_mutex_t proxy_clients_mutex = PTHREAD_MUTEX_INITIALIZER;
  23. static
  24. void prune_worklog()
  25. {
  26. struct proxy_client *client, *tmp;
  27. struct work *work, *tmp2;
  28. struct timeval tv_now;
  29. timer_set_now(&tv_now);
  30. mutex_lock(&proxy_clients_mutex);
  31. HASH_ITER(hh, proxy_clients, client, tmp)
  32. {
  33. HASH_ITER(hh, client->work, work, tmp2)
  34. {
  35. if (timer_elapsed(&work->tv_work_start, &tv_now) <= opt_expiry)
  36. break;
  37. HASH_DEL(client->work, work);
  38. free_work(work);
  39. }
  40. }
  41. mutex_unlock(&proxy_clients_mutex);
  42. }
  43. static
  44. pthread_t prune_worklog_pth;
  45. static
  46. void *prune_worklog_thread(void *userdata)
  47. {
  48. struct cgpu_info *cgpu = userdata;
  49. pthread_detach(pthread_self());
  50. RenameThread("PXY_pruner");
  51. while (!cgpu->shutdown)
  52. {
  53. prune_worklog();
  54. sleep(60);
  55. }
  56. return NULL;
  57. }
  58. static
  59. void proxy_first_client(struct cgpu_info *cgpu)
  60. {
  61. pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, cgpu);
  62. }
  63. struct proxy_client *proxy_find_or_create_client(const char *username)
  64. {
  65. struct proxy_client *client;
  66. struct cgpu_info *cgpu;
  67. char *user;
  68. int b;
  69. if (!username)
  70. return NULL;
  71. mutex_lock(&proxy_clients_mutex);
  72. HASH_FIND_STR(proxy_clients, username, client);
  73. if (!client)
  74. {
  75. user = strdup(username);
  76. cgpu = malloc(sizeof(*cgpu));
  77. client = malloc(sizeof(*client));
  78. *cgpu = (struct cgpu_info){
  79. .drv = &proxy_drv,
  80. .set_device_funcs = proxy_set_device_funcs,
  81. .threads = 0,
  82. .device_data = client,
  83. .device_path = user,
  84. .min_nonce_diff = minimum_pdiff,
  85. };
  86. timer_set_now(&cgpu->cgminer_stats.start_tv);
  87. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  88. {
  89. free(client);
  90. free(cgpu);
  91. free(user);
  92. return NULL;
  93. }
  94. *client = (struct proxy_client){
  95. .username = user,
  96. .cgpu = cgpu,
  97. .desired_share_pdiff = 0.,
  98. };
  99. b = HASH_COUNT(proxy_clients);
  100. HASH_ADD_KEYPTR(hh, proxy_clients, client->username, strlen(user), client);
  101. mutex_unlock(&proxy_clients_mutex);
  102. if (!b)
  103. proxy_first_client(cgpu);
  104. cgpu_set_defaults(cgpu);
  105. }
  106. else
  107. {
  108. mutex_unlock(&proxy_clients_mutex);
  109. cgpu = client->cgpu;
  110. }
  111. thread_reportin(cgpu->thr[0]);
  112. return client;
  113. }
  114. // See also, stratumsrv_init_diff in driver-stratum.c
  115. static
  116. const char *proxy_set_diff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const success)
  117. {
  118. struct proxy_client * const client = proc->device_data;
  119. double nv = atof(newvalue);
  120. if (nv < 0)
  121. return "Invalid difficulty";
  122. if (nv <= minimum_pdiff)
  123. nv = minimum_pdiff;
  124. client->desired_share_pdiff = nv;
  125. #ifdef USE_LIBEVENT
  126. stratumsrv_client_changed_diff(client);
  127. #endif
  128. return NULL;
  129. }
  130. #ifdef HAVE_CURSES
  131. static
  132. void proxy_wlogprint_status(struct cgpu_info *cgpu)
  133. {
  134. struct proxy_client *client = cgpu->device_data;
  135. wlogprint("Username: %s\n", client->username);
  136. }
  137. #endif
  138. static const struct bfg_set_device_definition proxy_set_device_funcs[] = {
  139. {"diff", proxy_set_diff, "desired share difficulty for clients"},
  140. {NULL},
  141. };
  142. struct device_drv proxy_drv = {
  143. .dname = "proxy",
  144. .name = "PXY",
  145. #ifdef HAVE_CURSES
  146. .proc_wlogprint_status = proxy_wlogprint_status,
  147. #endif
  148. };