Procházet zdrojové kódy

Made set/clear methods static, made entry point methods private.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@40 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz před 10 roky
rodič
revize
5cb7f3b39a
2 změnil soubory, kde provedl 96 přidání a 75 odebrání
  1. 22
    18
      service.cpp
  2. 74
    57
      service.hpp

+ 22
- 18
service.cpp Zobrazit soubor

@@ -74,13 +74,13 @@ VOID WINAPI ServiceCtrlHandler(DWORD message) {
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
logStream << "ServiceCtrlHandler. message: " << message
<< ". Calling processMessages" << std::endl;
<< ". Calling processCtrlMessage" << std::endl;
logStream.close();
#endif
CodeDweller::Service &service = CodeDweller::Service::getInstance();
service.processMessages(message);
service.processCtrlMessage(message);
#ifdef DEBUG_LOG_FILE
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
@@ -107,10 +107,21 @@ namespace CodeDweller {
std::mutex Service::objectMutex;
Service::Service() :
pauseReceived(false),
resumeReceived(false),
stopReceived(false) {
std::vector<std::string> Service::cmdLineArgs;
bool Service::pauseReceived = false;
bool Service::resumeReceived = false;
bool Service::stopReceived = false;
std::vector<Service::Callback *> Service::pauseCallbacks;
std::vector<Service::Callback *> Service::resumeCallbacks;
std::vector<Service::Callback *> Service::stopCallbacks;
Service::Service() {
}
Service &Service::getInstance() {
@@ -138,13 +149,6 @@ namespace CodeDweller {
#ifdef WIN32
serviceName = argv[0];
std::string::size_type indx = serviceName.find_last_of("/\\");
if (std::string::npos != indx) {
serviceName = serviceName.substr(indx + 1);
}
SERVICE_TABLE_ENTRY ServiceTable[] = {
{"", (LPSERVICE_MAIN_FUNCTION) ServiceMain},
{NULL, NULL}
@@ -399,12 +403,12 @@ namespace CodeDweller {
#ifdef WIN32
void Service::processMessages(DWORD message) {
void Service::processCtrlMessage(DWORD message) {
#ifdef DEBUG_LOG_FILE
std::ofstream logStream;
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
logStream << "processMessages. message: " << message << std::endl;
logStream << "processCtrlMessage. message: " << message << std::endl;
logStream.close();
#endif
@@ -414,7 +418,7 @@ namespace CodeDweller {
#ifdef DEBUG_LOG_FILE
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
logStream << "processMessages. Received Pause" << std::endl;
logStream << "processCtrlMessage. Received Pause" << std::endl;
logStream.close();
#endif
@@ -445,7 +449,7 @@ namespace CodeDweller {
#ifdef DEBUG_LOG_FILE
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
logStream << "processMessages. Received Resume" << std::endl;
logStream << "processCtrlMessage. Received Resume" << std::endl;
logStream.close();
#endif
@@ -476,7 +480,7 @@ namespace CodeDweller {
#ifdef DEBUG_LOG_FILE
logStream.open(DEBUG_LOG_FILE, std::fstream::app);
logStream << "processMessages. Received STOP" << std::endl;
logStream << "processCtrlMessage. Received STOP" << std::endl;
logStream.close();
#endif

+ 74
- 57
service.hpp Zobrazit soubor

@@ -37,6 +37,20 @@
#include <vector>
#include <mutex>

#ifdef WIN32

int _tmain(int argc, TCHAR *argv[]);

VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

VOID WINAPI ServiceCtrlHandler(DWORD message);

#else

int main(int argc, char *argv[]);

#endif

namespace CodeDweller {

/** Singleton class that implements a daemon (*nix) or service
@@ -88,12 +102,6 @@ namespace CodeDweller {
{
public:

/// Get the instance of the singleton.
//
// \returns a reference to the singleton.
//
static Service& getInstance();

/// Callback functor interface.
class Callback {

@@ -104,79 +112,54 @@ namespace CodeDweller {

};

/// Main entry point.
//
// \param[in] argc is the number of arguments.
//
// \param[in] argv is an array of strings containing the
// command-line arguments. The end of the array is indicated by a
// null pointer.
//
// \returns exit code of the service.
//
int main(int argc, char *argv[]);

#ifdef WIN32

/// Service entry point for Windows.
// \param[in] argc is the number of arguments.
//
// \param[in] argv is an array of strings containing the
// command-line arguments. The end of the array is indicated by a
// null pointer.
//
void serviceMain(DWORD argc, LPTSTR *argv);

#endif

/// Register a callback for receipt of Pause.
//
// \param[in] pauseFunctor is the function object to invoke when
// Pause is received.
//
void onPauseCall(Callback &pauseFunctor);
static void onPauseCall(Callback &pauseFunctor);

/// Register a callback for receipt of Resume.
//
// \param[in] resumeFunctor is the function object to invoke when
// Resume is received.
//
void onResumeCall(Callback &resumeFunctor);
static void onResumeCall(Callback &resumeFunctor);

/// Register a callback for receipt of Stop.
//
// \param[in] stopFunctor is the function object to invoke when
// Stop is received.
//
void onStopCall(Callback &stopFunctor);
static void onStopCall(Callback &stopFunctor);

/// Check whether Pause was received.
//
// \returns if the Pause message was received, false otherwise.
//
bool receivedPause();
static bool receivedPause();

/// Check whether Resume was received.
//
// \returns if the Resume message was received, false otherwise.
//
bool receivedResume();
static bool receivedResume();

/// Check whether the last message received was Stop.
//
// \returns true if Stop was the most recent message received,
// false otherwise.
//
bool receivedStop();
static bool receivedStop();

/// Clear receiving the Pause message.
void clearReceivedPause();
static void clearReceivedPause();

/// Clear receiving the Resume message.
void clearReceivedResume();
static void clearReceivedResume();

/// Clear receiving the Stop message.
void clearReceivedStop();
static void clearReceivedStop();

/// Get a reference to the command-line arguments.
//
@@ -184,7 +167,7 @@ namespace CodeDweller {
// the application. Index i corresponds to command-line argument
// i.
//
const std::vector<std::string> &arguments();
static const std::vector<std::string> &arguments();

private:

@@ -197,7 +180,37 @@ namespace CodeDweller {
/// Prevent assignment.
void operator=(Service const&) {}

public:
/// Get the instance of the singleton.
//
// \returns a reference to the singleton.
//
static Service& getInstance();

/// Main entry point.
//
// \param[in] argc is the number of arguments.
//
// \param[in] argv is an array of strings containing the
// command-line arguments. The end of the array is indicated by a
// null pointer.
//
// \returns exit code of the service.
//
int main(int argc, char *argv[]);

#ifdef WIN32

/// Service entry point for Windows.
// \param[in] argc is the number of arguments.
//
// \param[in] argv is an array of strings containing the
// command-line arguments. The end of the array is indicated by a
// null pointer.
//
void serviceMain(DWORD argc, LPTSTR *argv);

#endif

/// Load the command-line arguments.
//
// This method loads the object with the command-line parameters.
@@ -216,20 +229,16 @@ namespace CodeDweller {
//
int run();

private:
/// Mutex to serialize access to the object.
static std::mutex objectMutex;

#ifdef WIN32

public:
/// Process messages.
/// Process a control message.
//
// \param[in] message is the message to process.
//
void processMessages(DWORD message);

private:
void processCtrlMessage(DWORD message);

#else
/// Thread start function to receive and process messages.
@@ -237,29 +246,27 @@ namespace CodeDweller {
#endif

/// Command-line arguments.
std::vector<std::string> cmdLineArgs;
static std::vector<std::string> cmdLineArgs;

/// True if Pause message was received.
bool pauseReceived;
static bool pauseReceived;

/// True if Resume message was received.
bool resumeReceived;
static bool resumeReceived;

/// True if Stop message was received.
bool stopReceived;
static bool stopReceived;

/// Functions to invoke when the Pause is received.
std::vector<Callback *> pauseCallbacks;
static std::vector<Callback *> pauseCallbacks;

/// Functions to invoke when the Resume is received.
std::vector<Callback *> resumeCallbacks;
static std::vector<Callback *> resumeCallbacks;

/// Functions to invoke when the Stop is received.
std::vector<Callback *> stopCallbacks;
static std::vector<Callback *> stopCallbacks;

#ifdef WIN32
/// Name of service.
std::string serviceName;

/// Status of the service.
SERVICE_STATUS serviceStatus;
@@ -267,9 +274,19 @@ namespace CodeDweller {
/// Handle for accessing service status on the OS.
SERVICE_STATUS_HANDLE serviceStatusHandle = NULL;

friend int ::_tmain(int argc, TCHAR *argv[]);

friend VOID WINAPI ::ServiceMain(DWORD argc, LPTSTR *argv);

friend VOID WINAPI ::ServiceCtrlHandler(DWORD message);

#else

/// Set of signals to wait for.
sigset_t signalSet;

friend int ::main(int argc, char *argv[]);

#endif

};

Načítá se…
Zrušit
Uložit