driver-proxy.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. mutex_lock(&proxy_clients_mutex);
  59. HASH_FIND_STR(proxy_clients, username, client);
  60. if (!client)
  61. {
  62. user = strdup(username);
  63. cgpu = malloc(sizeof(*cgpu));
  64. client = malloc(sizeof(*client));
  65. *cgpu = (struct cgpu_info){
  66. .drv = &proxy_drv,
  67. .threads = 0,
  68. .device_data = client,
  69. .device_path = user,
  70. };
  71. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  72. {
  73. free(client);
  74. free(cgpu);
  75. free(user);
  76. return NULL;
  77. }
  78. *client = (struct proxy_client){
  79. .username = user,
  80. .cgpu = cgpu,
  81. };
  82. b = HASH_COUNT(proxy_clients);
  83. HASH_ADD_KEYPTR(hh, proxy_clients, client->username, strlen(user), client);
  84. mutex_unlock(&proxy_clients_mutex);
  85. if (!b)
  86. proxy_first_client(cgpu);
  87. }
  88. else
  89. mutex_unlock(&proxy_clients_mutex);
  90. return client;
  91. }
  92. #ifdef HAVE_CURSES
  93. static
  94. void proxy_wlogprint_status(struct cgpu_info *cgpu)
  95. {
  96. struct proxy_client *client = cgpu->device_data;
  97. wlogprint("Username: %s\n", client->username);
  98. }
  99. #endif
  100. struct device_drv proxy_drv = {
  101. .dname = "proxy",
  102. .name = "PXY",
  103. #ifdef HAVE_CURSES
  104. .proc_wlogprint_status = proxy_wlogprint_status,
  105. #endif
  106. };