Skip to content

Commit

Permalink
Cleanup; using smart pointers to store our serviceGUI threads, now th…
Browse files Browse the repository at this point in the history
…ey are destroyed correctly
  • Loading branch information
HostedDinner committed Oct 11, 2017
1 parent f354852 commit c8a479f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void ConfigParser::parse() {

if(file != NULL){

tinyxml2::XMLDocument doc;
XMLDocument doc;
doc.LoadFile(file);

XMLElement *list = doc.FirstChildElement("servicestarter")->FirstChildElement("servicelist");
Expand Down
2 changes: 1 addition & 1 deletion src/ScrollBarController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int ScrollBarController::handleScroll(WORD type) {
int oldPos = this->scrollInfo.nPos;
int max = this->scrollInfo.nMax;
int min = this->scrollInfo.nMin;
unsigned int pageSize = this->scrollInfo.nPage;
int pageSize = (int) this->scrollInfo.nPage;

int newPos = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/ServiceGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ServiceGUI::ServiceGUI(Window *pWindow, std::wstring serviceName, std::wstring b

//This should be launched in a new thread, because otherwise it will block the current thread
//start receiving updates on service changes
this->pChangeThread = new std::thread(&WinService::registerServiceChange, this->pWinservice, std::bind(&ServiceGUI::receiceStatus, this, std::placeholders::_1), false);
this->pChangeThread = new std::thread(&WinService::registerServiceChange, this->pWinservice, std::bind(&ServiceGUI::receiveStatus, this, std::placeholders::_1), false);
}

ServiceGUI::~ServiceGUI() {
Expand All @@ -62,7 +62,7 @@ ServiceGUI::~ServiceGUI() {
delete this->pChangeThread;
}

void ServiceGUI::receiceStatus(PSERVICE_NOTIFY pSN) {
void ServiceGUI::receiveStatus(PSERVICE_NOTIFY pSN) {
SERVICE_STATUS_PROCESS ssStatus = pSN->ServiceStatus;
this->changeStatus(ssStatus.dwCurrentState);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ServiceGUI {
* It will pass the status to the changeStatus function.
* @param pSN receices struct, where the Service Information is inside
*/
void receiceStatus(PSERVICE_NOTIFY pSN);
void receiveStatus(PSERVICE_NOTIFY pSN);

private:
/**
Expand Down
19 changes: 7 additions & 12 deletions src/WinService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ WinService::WinService(std::wstring name) {
this->doServiceObservation = true;
this->serviceChange = NULL;
this->pChangeConnection = NULL;
this->pSN = NULL;

this->sn = SERVICE_NOTIFY();
this->sn.dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
this->sn.pContext = this;
this->sn.pfnNotifyCallback = &WinService::serviceChangeDispatcher;
}

WinService::~WinService() {
delete this->pChangeConnection;
delete this->pSN;
}

void WinService::start() {
Expand Down Expand Up @@ -57,7 +60,7 @@ DWORD WinService::status() {



void WinService::serviceChangeDispatcher(PVOID pParameter) {
/*static*/ void WinService::serviceChangeDispatcher(PVOID pParameter) {
PSERVICE_NOTIFY pInfo = (PSERVICE_NOTIFY) pParameter;
((WinService *) pInfo->pContext)->callServiceChange(pInfo);
}
Expand All @@ -71,18 +74,10 @@ void WinService::registerServiceChange(std::function<void(PSERVICE_NOTIFY)> call
}
if(!resubscribe){ //first call
this->pChangeConnection = new ServiceConnection(this->name, SERVICE_QUERY_STATUS, SC_MANAGER_ENUMERATE_SERVICE);
this->pSN = new SERVICE_NOTIFY;

this->pSN->dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
this->pSN->pContext = this;
this->pSN->pfnNotifyCallback = &WinService::serviceChangeDispatcher;

this->serviceChange = callback;
}

DWORD notifyMask = SERVICE_NOTIFY_RUNNING | SERVICE_NOTIFY_STOPPED | SERVICE_NOTIFY_START_PENDING | SERVICE_NOTIFY_STOP_PENDING;

NotifyServiceStatusChange(this->pChangeConnection->getSCService(), notifyMask, this->pSN);
NotifyServiceStatusChange(this->pChangeConnection->getSCService(), this->notifyMask, &this->sn);

//SleepEx will wait till timeout or if a notification (above) will be received.
//will wake up around every 2 seconds, so that the thread gets the chance to terminate (if doServiceObservation is false)
Expand Down
4 changes: 3 additions & 1 deletion src/WinService.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class WinService {
volatile bool doServiceObservation;
std::function<void(PSERVICE_NOTIFY)> serviceChange;
ServiceConnection *pChangeConnection;
PSERVICE_NOTIFY pSN;
SERVICE_NOTIFY sn;

const DWORD notifyMask = SERVICE_NOTIFY_RUNNING | SERVICE_NOTIFY_STOPPED | SERVICE_NOTIFY_START_PENDING | SERVICE_NOTIFY_STOP_PENDING;

public:
/**
Expand Down
13 changes: 9 additions & 4 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ bool Window::updateWindow() {

WPARAM Window::handleMessages() {
MSG msg;
BOOL bRet;

while(GetMessage(&msg, NULL, 0, 0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0){
if(bRet == -1){
break;
}else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
return bRet == 0 ? 0 : 1;
}

bool Window::isInitSuccess() {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@



#define VER_PRODUCTVERSION 1,0,1,0
#define VER_PRODUCTVERSION_STR "1.0.1\0"
#define VER_PRODUCTVERSION 1,0,2,0
#define VER_PRODUCTVERSION_STR "1.0.2\0"

#ifndef DEBUG
#define VER_DEBUG 0
Expand Down
19 changes: 6 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,25 @@ int main(int argc, char* argv[]){
auto *confs = configP.pGetServiceConfigs();
auto length = confs->size();

ServiceGUI *pServices[length];
std::vector<std::unique_ptr<ServiceGUI>> services;
services.reserve(length);

int i = 0;
for (auto &elem : *confs) {
pServices[i] = new ServiceGUI(&window, elem.first, elem.second);
i++;
std::unique_ptr<ServiceGUI> p(new ServiceGUI(&window, elem.first, elem.second));
services.push_back(std::move(p));
}


//Adjust the Scrollbar (height of 1 element is 30, top margin is 10)
ScrollBarController *scrBC = window.getpScrollBarController();
scrBC->setRange(1, (i*30+10) / 10);
scrBC->setRange(1, (length*30+10) / 10);



//ServiceGUI serviceGUIApache(&window, L"Apache2.4ServerDev", L"Apache");
//ServiceGUI serviceGUIMySQL(&window, L"MySQLDev", L"MySQL");

window.showWindow();

WPARAM exitCode = window.handleMessages();

//actually someone once told me, that calling delete is alway safe, but this is not true!
if(length > 0 && pServices[0] != NULL){
delete [] pServices;
}
//cleaning the serviceGUI/Services is done by our smart pointer

return exitCode;
}
2 changes: 2 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
#ifndef MAIN_H
#define MAIN_H

#include <vector>

#endif /* MAIN_H */

0 comments on commit c8a479f

Please sign in to comment.