Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. Backend::Backend(const QString &executable, QObject *parent) :
  2.     QThread(parent),
  3.     _executable(executable),
  4.     _process(new QProcess(this)),
  5.     _requestType(Backend::NoRequest),
  6.     _running(false)
  7. {
  8.     connect(_process, SIGNAL(readyReadStandardOutput()),
  9.             SLOT(handleOutput()));
  10. }
  11.  
  12. Backend::~Backend()
  13. {
  14.     delete _process;
  15. }
  16.  
  17. void Backend::startStimulation()
  18. {
  19.     _process->start(_executable, argsForStimulation(), QProcess::ReadOnly);
  20.     _running = true;
  21. }
  22.  
  23. void Backend::endStimulation()
  24. {
  25.     _requestType = Backend::NoRequest;
  26.     _running = false;
  27.     emit stimulationEnd();
  28. }
  29.  
  30. void Backend::handleOutput()
  31. {
  32.     QString output = _process->readAll();
  33.     switch (_requestType) {
  34.     case Backend::StimulateRequest:
  35.         if (output.split('\n').first().simplified() == "end") {
  36.             endStimulation();
  37.         }
  38.     }
  39. }
  40.  
  41. void Backend::run()
  42. {
  43.     for (;;) {
  44.         if (!_running) {
  45.             switch (_requestType)
  46.             {
  47.             case Backend::StimulateRequest:
  48.                 startStimulation();
  49.                 break;
  50.             default:
  51.                 QCoreApplication::instance()->processEvents();
  52.             }
  53.         } else {
  54.             QCoreApplication::instance()->processEvents();
  55.         }
  56.     }
  57. }
  58.  
  59. Backend::ErrorType Backend::stimulate(const QHash<QString, QVariant> &params)
  60. {
  61.     if (_running || _requestType != Backend::NoRequest) {
  62.         return Backend::AlreadyRunningError;
  63.     }
  64.  
  65.     if (!supportedFeatures().contains("stimulation")) {
  66.         return Backend::NotSupportedFeatureError;
  67.     }
  68.  
  69.     _params = params;
  70.     _requestType = Backend::StimulateRequest;
  71.  
  72.     return Backend::Success;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement