You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1KB

  1. package fr.slixe.mining.http.controller;
  2. import org.slf4j.Logger
  3. import org.slf4j.LoggerFactory
  4. import com.google.gson.Gson;
  5. import com.google.gson.JsonElement
  6. import com.google.gson.JsonObject
  7. import com.google.gson.JsonParser
  8. import com.google.inject.Inject;
  9. import fr.litarvan.paladin.http.Controller;
  10. import fr.litarvan.paladin.http.Request
  11. import fr.litarvan.paladin.http.routing.JsonBody
  12. import fr.slixe.mining.http.InvalidParameterException
  13. import fr.slixe.mining.service.DaemonService
  14. import fr.slixe.mining.service.StatsService
  15. import fr.slixe.mining.structures.FakeRPCResult
  16. import fr.slixe.mining.structures.Miner
  17. public class MainController extends Controller {
  18. private static final Logger log = LoggerFactory.getLogger("Controller");
  19. @Inject
  20. private Gson gson
  21. @Inject
  22. private DaemonService daemon
  23. @Inject
  24. private StatsService stats
  25. @JsonBody(parse = false)
  26. def jsonRpc(Request request)
  27. {
  28. String content = request.getParam("__jBdy");
  29. JsonElement jsonElement = new JsonParser().parse(content)
  30. if (jsonElement == null || !jsonElement.isJsonObject()) {
  31. throw new InvalidParameterException("Invalid body")
  32. }
  33. JsonObject json = jsonElement.getAsJsonObject()
  34. String method = json.get("method").getAsString()
  35. Optional<Miner> optMiner = stats.findMiner(request.ip)
  36. Miner miner = optMiner.isPresent() ? optMiner.get() : stats.createMiner(request.ip)
  37. miner.markAlive()
  38. switch (method) {
  39. case "get_info":
  40. return new FakeRPCResult(daemon.info)
  41. case "getblocktemplate":
  42. miner.lastJobRequest = System.currentTimeMillis()
  43. return new FakeRPCResult(daemon.blockTemplate)
  44. case "submitblock":
  45. miner.lastBlockSent = System.currentTimeMillis()
  46. def jsonArray = json.get("params").getAsJsonArray()
  47. def x = new String[2];
  48. x[0] = jsonArray.get(0).getAsString();
  49. x[1] = jsonArray.get(1).getAsString();
  50. return new FakeRPCResult(daemon.submitBlock(miner, x))
  51. }
  52. [
  53. message: "Invalid method"
  54. ]
  55. }
  56. def job()
  57. {
  58. daemon.blockTemplate
  59. }
  60. def info()
  61. {
  62. daemon.info
  63. }
  64. def miners()
  65. {
  66. stats.miners
  67. }
  68. def blocks()
  69. {
  70. stats.blocks
  71. }
  72. }