| 1 | /* |
| 2 | * File: StatusRunner.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "StatusRunner.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "NamedPipe.h" |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | |
| 31 | StatusRunner::StatusRunner(SynchronizedQueue& statusQueue, HerculesExecutor * herculesExecutor) |
| 32 | : Runner(statusQueue), mHerculesExecutor(herculesExecutor) |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | StatusRunner::~StatusRunner() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void StatusRunner::run() |
| 42 | { |
| 43 | #ifdef hFramework |
| 44 | mProcess = mHerculesExecutor->getQProcess(); |
| 45 | connect(mHerculesExecutor->getQProcess(), |
| 46 | SIGNAL(readyReadStandardError()), |
| 47 | this, |
| 48 | SLOT(readStandardError())); |
| 49 | return; |
| 50 | #else |
| 51 | QFile& statusFile = NamedPipe::getInstance().getHerculesStatus(); |
| 52 | QByteArray buff; |
| 53 | buff.resize(512); |
| 54 | mRunning = true; |
| 55 | bool lineRead = false; |
| 56 | int startupCounter = 0; |
| 57 | |
| 58 | while(mRunning) |
| 59 | { |
| 60 | int size; |
| 61 | if ((size = statusFile.readLine(buff.data(),512)) <= 0) |
| 62 | { |
| 63 | // we wait up to 2 seconds for data to appear in the log |
| 64 | if (!lineRead) |
| 65 | { |
| 66 | if (startupCounter++ < 200) |
| 67 | { |
| 68 | QThread::msleep(10); |
| 69 | continue; // retry reading by returning to main loop |
| 70 | } |
| 71 | } |
| 72 | emit newData(); |
| 73 | break; |
| 74 | } |
| 75 | lineRead = true; |
| 76 | buff[size-1] = '\0'; |
| 77 | mQueue.push_back(buff); |
| 78 | emit newData(); |
| 79 | } |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void StatusRunner::readStandardError() |
| 85 | { |
| 86 | mProcess->setReadChannel(QProcess::StandardError); |
| 87 | while (true) |
| 88 | { |
| 89 | QByteArray output = mProcess->readLine(); |
| 90 | if (output.length() == 0) break; |
| 91 | output[output.length()-1]='\0'; |
| 92 | mQueue.push_back(output.data()); |
| 93 | emit newData(); |
| 94 | } |
| 95 | } |
| 96 |