driver-proxy.c 2.4 KB

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