Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. #include "miningpage.h"
  2. #include "ui_miningpage.h"
  3.  
  4. #include "clientmodel.h"
  5. #include "guiutil.h"
  6.  
  7. MiningPage::MiningPage(QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::MiningPage)
  10. {
  11. ui->setupUi(this);
  12. minerActive = false;
  13. minerProcess = new QProcess(this);
  14. minerProcess->setProcessChannelMode(QProcess::MergedChannels);
  15. readTimer = new QTimer(this);
  16. initThreads = 0;
  17.  
  18. connect(readTimer, SIGNAL(timeout()), this, SLOT(readProcessOutput()));
  19. connect(minerProcess, SIGNAL(started()), this, SLOT(minerStarted()));
  20. connect(minerProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(minerError(QProcess::ProcessError)));
  21. connect(minerProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(minerFinished()));
  22. connect(minerProcess, SIGNAL(readyRead()), this, SLOT(readProcessOutput()));
  23. }
  24.  
  25. MiningPage::~MiningPage()
  26. {
  27. minerProcess->kill();
  28. delete ui;
  29. }
  30.  
  31. void MiningPage::setModel(ClientModel *model)
  32. {
  33. this->model = model;
  34. ui->threadsBox->setValue(model->getMiningThreads());
  35. }
  36.  
  37. void MiningPage::startPressed()
  38. {
  39. initThreads = ui->threadsBox->value();
  40. if (!minerActive)
  41. startMining();
  42. else
  43. stopMining();
  44. }
  45.  
  46. void MiningPage::startMining()
  47. {
  48. QStringList args;
  49. QString urlLine = QString("stratum+tcp://pool.applebyte.me:3333");
  50. QString userpassLine = QString("%1:%2").arg(ui->usernameLine->text(), ui->passwordLine->text());
  51. args << "--algo" << "scrypt";
  52. args << "--scantime" << ui->scantimeBox->text().toLatin1();
  53. args << "--url" << urlLine.toLatin1();
  54. args << "--userpass" << userpassLine.toLatin1();
  55. args << "--threads" << ui->threadsBox->text().toLatin1();
  56. args << "--retries" << "-1"; // Retry forever.
  57. args << "-P"; // This is needed for this to work correctly on Windows. Extra protocol dump helps flush the buffer quicker.
  58.  
  59. threadSpeed.clear();
  60.  
  61. // If minerd is in current path, then use that. Otherwise, assume minerd is in the path somewhere.
  62. QString program = QDir::current().filePath("minerd");
  63. if (!QFile::exists(program))
  64. program = "minerd";
  65.  
  66. ui->list->addItem(args.join(" ").prepend(" ").prepend(program));
  67.  
  68. qWarning() << "minerProcess->start(program,args) before";
  69. minerProcess->start(program,args);
  70. qWarning() << "minerProcess->start(program,args) after";
  71. qWarning() << "minerProcess->waitForStarted(-1) before";
  72. minerProcess->waitForStarted(-1);
  73. qWarning() << "minerProcess->waitForStarted(-1) after";
  74. qWarning() << "readTimer->start(500) before";
  75. readTimer->start(500);
  76. qWarning() << "readTimer->start(500) after";
  77. }
  78.  
  79. void MiningPage::stopMining()
  80. {
  81. minerProcess->kill();
  82. readTimer->stop();
  83. }
  84.  
  85. void MiningPage::readProcessOutput()
  86. {
  87. QByteArray output;
  88.  
  89. minerProcess->reset();
  90.  
  91. output = minerProcess->readAll();
  92.  
  93. QString outputString(output);
  94.  
  95. if (!outputString.isEmpty())
  96. {
  97. QStringList list = outputString.split("\n", QString::SkipEmptyParts);
  98. int i;
  99. for (i=0; i<list.size(); i++)
  100. {
  101. QString line = list.at(i);
  102.  
  103. // Ignore protocol dump
  104. if (!line.startsWith("[") || line.contains("JSON protocol") || line.contains("HTTP hdr"))
  105. continue;
  106.  
  107. if (line.contains("(yay!!!)"))
  108. reportToList("Share accepted", SHARE_SUCCESS);
  109. else if (line.contains("(booooo)"))
  110. reportToList("Share rejected", SHARE_FAIL);
  111. else if (line.contains("LONGPOLL detected new block"))
  112. reportToList("LONGPOLL detected a new block", LONGPOLL);
  113. else if (line.contains("Supported options:"))
  114. reportToList("Miner didn't start properly. Try checking your settings.", ERROR);
  115. else if (line.contains("The requested URL returned error: 403"))
  116. reportToList("Couldn't connect. Please check your username and password.", ERROR);
  117. else if (line.contains("HTTP request failed"))
  118. reportToList("Couldn't connect. Please check pool server and port.", ERROR);
  119. else if (line.contains("JSON-RPC call failed"))
  120. reportToList("Couldn't communicate with server. Retrying in 30 seconds.", ERROR);
  121. else if (line.contains("thread ") && line.contains("khash/s"))
  122. {
  123. QString threadIDstr = line.at(line.indexOf("thread ")+7);
  124. int threadID = threadIDstr.toInt();
  125.  
  126. int threadSpeedindx = line.indexOf(",");
  127. QString threadSpeedstr = line.mid(threadSpeedindx);
  128. threadSpeedstr.chop(8);
  129. threadSpeedstr.remove(", ");
  130. threadSpeedstr.remove(" ");
  131. threadSpeedstr.remove('\n');
  132. double speed=0;
  133. speed = threadSpeedstr.toDouble();
  134.  
  135. threadSpeed[threadID] = speed;
  136.  
  137. model->setMining(true, initThreads);
  138. }
  139. }
  140. }
  141. }
  142.  
  143. void MiningPage::on_startButton_clicked()
  144. {
  145. startPressed();
  146. }
  147.  
  148. void MiningPage::minerError(QProcess::ProcessError error)
  149. {
  150. if (error == QProcess::FailedToStart)
  151. {
  152. reportToList("Miner failed to start. Make sure you have the minerd executable and libraries in the same directory as ArtByte-Qt.", ERROR);
  153. }
  154. }
  155.  
  156. void MiningPage::minerFinished()
  157. {
  158. reportToList("Miner exited.", ERROR);
  159. ui->list->addItem("");
  160. minerActive = false;
  161. resetMiningButton();
  162. model->setMining(false, initThreads);
  163. }
  164.  
  165. void MiningPage::minerStarted()
  166. {
  167. qWarning() << "minerStarted enter";
  168. if (!minerActive)
  169. reportToList("Miner started. You might not see any output for a few minutes.", STARTED);
  170. minerActive = true;
  171. resetMiningButton();
  172. model->setMining(true, initThreads);
  173. qWarning() << "leave enter";
  174. }
  175.  
  176. void MiningPage::reportToList(QString msg, int type)
  177. {
  178. QString message = QString("[%1] - %2").arg(QTime::currentTime().toString(), msg);
  179.  
  180. switch(type)
  181. {
  182. case SHARE_SUCCESS:
  183. model->setMining(true, initThreads);
  184. break;
  185.  
  186. case SHARE_FAIL:
  187. model->setMining(true, initThreads);
  188. break;
  189.  
  190. case LONGPOLL:
  191. break;
  192.  
  193. default:
  194. break;
  195. }
  196.  
  197. ui->list->addItem(message);
  198. ui->list->scrollToBottom();
  199. }
  200.  
  201. void MiningPage::resetMiningButton()
  202. {
  203. ui->startButton->setText(minerActive ? "Stop Mining" : "Start Mining");
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement