driver-proxy.c 2.8 KB

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