driver-proxy.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "config.h"
  2. #include <pthread.h>
  3. #include <uthash.h>
  4. #include "deviceapi.h"
  5. #include "driver-proxy.h"
  6. #include "miner.h"
  7. #include "util.h"
  8. struct device_drv proxy_drv;
  9. static
  10. struct proxy_client *proxy_clients;
  11. static
  12. pthread_mutex_t proxy_clients_mutex = PTHREAD_MUTEX_INITIALIZER;
  13. static
  14. void prune_worklog()
  15. {
  16. struct proxy_client *client, *tmp;
  17. struct work *work, *tmp2;
  18. struct timeval tv_now;
  19. timer_set_now(&tv_now);
  20. mutex_lock(&proxy_clients_mutex);
  21. HASH_ITER(hh, proxy_clients, client, tmp)
  22. {
  23. HASH_ITER(hh, client->work, work, tmp2)
  24. {
  25. if (timer_elapsed(&work->tv_work_start, &tv_now) <= opt_expiry)
  26. break;
  27. HASH_DEL(client->work, work);
  28. free_work(work);
  29. }
  30. }
  31. mutex_unlock(&proxy_clients_mutex);
  32. }
  33. static
  34. pthread_t prune_worklog_pth;
  35. static
  36. void *prune_worklog_thread(void *userdata)
  37. {
  38. struct cgpu_info *cgpu = userdata;
  39. pthread_detach(pthread_self());
  40. RenameThread("PXY_pruner");
  41. while (!cgpu->shutdown)
  42. {
  43. prune_worklog();
  44. sleep(60);
  45. }
  46. return NULL;
  47. }
  48. static
  49. void proxy_first_client(struct cgpu_info *cgpu)
  50. {
  51. pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, cgpu);
  52. }
  53. struct proxy_client *proxy_find_or_create_client(const char *username)
  54. {
  55. struct proxy_client *client;
  56. struct cgpu_info *cgpu;
  57. char *user;
  58. int b;
  59. if (!username)
  60. return NULL;
  61. mutex_lock(&proxy_clients_mutex);
  62. HASH_FIND_STR(proxy_clients, username, client);
  63. if (!client)
  64. {
  65. user = strdup(username);
  66. cgpu = malloc(sizeof(*cgpu));
  67. client = malloc(sizeof(*client));
  68. *cgpu = (struct cgpu_info){
  69. .drv = &proxy_drv,
  70. .threads = 0,
  71. .device_data = client,
  72. .device_path = user,
  73. };
  74. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  75. {
  76. free(client);
  77. free(cgpu);
  78. free(user);
  79. return NULL;
  80. }
  81. *client = (struct proxy_client){
  82. .username = user,
  83. .cgpu = cgpu,
  84. };
  85. b = HASH_COUNT(proxy_clients);
  86. HASH_ADD_KEYPTR(hh, proxy_clients, client->username, strlen(user), client);
  87. mutex_unlock(&proxy_clients_mutex);
  88. if (!b)
  89. proxy_first_client(cgpu);
  90. }
  91. else
  92. mutex_unlock(&proxy_clients_mutex);
  93. return client;
  94. }
  95. #ifdef HAVE_CURSES
  96. static
  97. void proxy_wlogprint_status(struct cgpu_info *cgpu)
  98. {
  99. struct proxy_client *client = cgpu->device_data;
  100. wlogprint("Username: %s\n", client->username);
  101. }
  102. #endif
  103. struct device_drv proxy_drv = {
  104. .dname = "proxy",
  105. .name = "PXY",
  106. #ifdef HAVE_CURSES
  107. .proc_wlogprint_status = proxy_wlogprint_status,
  108. #endif
  109. };