File: Getter.cpp

package info (click to toggle)
linssid 3.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,424 kB
  • sloc: cpp: 1,667; xml: 51; sh: 17; makefile: 6
file content (123 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * File:   Getter.cpp
 * Author: warren
 *
 * Created on October 31, 2012, 8:59 AM
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <random>
#include <QtCore>
#include <QString>
#include <QThread>
#include <QtWidgets>
#include <stdio.h>
#include "Custom.h"
#include "Getter.h"
#include "MainForm.h"
#include "ui_MainForm.h"

extern string pipeName;
extern qint64 startTime;
extern string beginBlockString;
extern string endBlockString;
extern string pipeName;
// extern ofstream linssidLog;
extern runStates runState;

using namespace std;

Getter::Getter() {
};

Getter::~Getter() {
};

void Getter::run() {
    // do stuff here
    exec();
}

MainForm* Getter::pMainForm;

const QEvent::Type Getter::DATA_WANTED_EVENT = static_cast<QEvent::Type> (DATAWANTED);
// Define the custom event subclass

class Getter::DataWantedEvent : public QEvent {
public:

    DataWantedEvent(const int customData1) :
    QEvent(Getter::DATA_WANTED_EVENT), wantedBlockNo(customData1) {
    }

    int getWantedBlockNo() const {
        return wantedBlockNo;
    }
private:
    int wantedBlockNo;
};

void Getter::postDataWantedEvent(const int customData1) {
    // This method (postDataWantedEvent) can be called from any thread
    QApplication::postEvent(this, new DataWantedEvent(customData1));
}

void Getter::customEvent(QEvent * event) {
    // When we get here, we've crossed the thread boundary and are now
    // executing in the Qt object's thread
    if (event->type() == Getter::DATA_WANTED_EVENT) {
        handleDataWantedEvent(static_cast<DataWantedEvent *> (event));
    }
    // use more else ifs to handle other custom events
}

void Getter::handleDataWantedEvent(const DataWantedEvent *event) {
    string endCommand;
    static fstream thePipe(pipeName);
    thePipe.flush();
    int blockNo = event->getWantedBlockNo();
    int rcSysCall = 0;
    ostringstream cppIsStupid;
    while (true) {
        cppIsStupid.str("");
        cppIsStupid << " " << blockNo;
        string lineCommand = 
                "echo \"" + beginBlockString + cppIsStupid.str() + "\" >> " + pipeName;
//                "echo \"" + beginBlockString + cppIsStupid.str() + "\\n\" >> " + pipeName;
        lineCommand += " ; iw ";
        lineCommand += Getter::pMainForm->getCurrentInterface() + " scan >> " + pipeName;
        for (int retries = 0; retries < 10; retries++) {
            rcSysCall = system(lineCommand.c_str());
            if ((rcSysCall == 0) || (runState == STOPPING)) break;
            else QThread::msleep(500); // wait 500 msec and try again
        }
        if (rcSysCall != 0) { // retries exhausted on multiple failures
            endCommand = "echo \"" + endBlockString + " -1\\n\" >> " + pipeName;
            // linssidLog << "Getter: command failed block " << blockNo << endl;
        } else { // success!
            endCommand = "echo \"" + endBlockString + cppIsStupid.str() + "\\n\" >> " + pipeName;
            // linssidLog << "Getter: block returned " << blockNo << endl;
        }

        waste(system(endCommand.c_str())); // put the end block tag in the stream

        Getter::pMainForm->postDataReadyEvent(blockNo);
        blockNo+=1;
        
        if (runState == RUNNING) QThread::msleep(500);
        for (int wink=0; (wink < Getter::pMainForm->getNapTime()) && (runState == RUNNING); wink++) {
            QThread::msleep(1000);        
        }
        if (runState != RUNNING) break; // exit the infinite while loop
    }
    runState = STOPPED;
}

inline void Getter::waste(int) {
    // This silliness is to ignore an argument function's return code without
    // having the compiler whine about it.
}