Merged with Windows_service_fixes

This commit is contained in:
Stefan Persson 2012-12-04 15:01:46 +01:00
commit 9d39fe8d47
6 changed files with 77 additions and 16 deletions

View file

@ -60,6 +60,8 @@ int CallbackMainDispatcher::registerCallback(CallbackStruct::CallbackType type,
callback->id = id; callback->id = id;
callback->context = context; callback->context = context;
d->callbackList.push_back(callback); d->callbackList.push_back(callback);
//logga callbackprenumeration lades till
debuglog(id, "Callback added");
return id; return id;
} }
@ -68,9 +70,11 @@ int CallbackMainDispatcher::unregisterCallback(int callbackId) {
{ {
TelldusCore::MutexLocker locker(&d->mutex); TelldusCore::MutexLocker locker(&d->mutex);
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) { for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
//logga, avregistrering av callback
if ( (*callback_it)->id != callbackId ) { if ( (*callback_it)->id != callbackId ) {
continue; continue;
} }
debuglog(callbackId, "Callback unregistered");
newEventList.splice(newEventList.begin(), d->callbackList, callback_it); newEventList.splice(newEventList.begin(), d->callbackList, callback_it);
break; break;
} }
@ -100,9 +104,16 @@ void CallbackMainDispatcher::run() {
if (!cbd) { if (!cbd) {
continue; continue;
} }
//logga här, att den fortfarande körs, ev till fil bara för att det kan bli så mkt...
//om för mkt, kolla att viss tid gått sedan förra ggn eller ngt...
//debuglog(333, "Callbackevent, signalled");
TelldusCore::MutexLocker locker(&d->mutex); TelldusCore::MutexLocker locker(&d->mutex);
//logga, har låst
//debuglog(333, "Callbackevent, locked");
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) { for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
if ( (*callback_it)->type == cbd->type ) { if ( (*callback_it)->type == cbd->type ) {
//ev logga här också, att det finns ngnstans att skicka till
//debuglog((*callback_it)->id, "Callbackevent, sending");
std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> ptr(new TelldusCore::TDEventDispatcher(eventData, *callback_it, d->janitor)); std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> ptr(new TelldusCore::TDEventDispatcher(eventData, *callback_it, d->janitor));
d->eventThreadList.push_back(ptr); d->eventThreadList.push_back(ptr);
} }

View file

@ -78,6 +78,7 @@ std::wstring Client::getWStringFromService(const Message &msg) {
} }
int Client::registerEvent( CallbackStruct::CallbackType type, void *eventFunction, void *context ) { int Client::registerEvent( CallbackStruct::CallbackType type, void *eventFunction, void *context ) {
debuglog(555, "Client, Registering callback");
return d->callbackMainDispatcher.registerCallback(type, eventFunction, context ); return d->callbackMainDispatcher.registerCallback(type, eventFunction, context );
} }
@ -86,10 +87,11 @@ void Client::run() {
d->eventSocket.connect(L"TelldusEvents"); d->eventSocket.connect(L"TelldusEvents");
while(d->running) { while(d->running) {
if(!d->eventSocket.isConnected()) { if(!d->eventSocket.isConnected()){
d->eventSocket.connect(L"TelldusEvents"); // try to reconnect to service debuglog(555, "Client, Trying to (re)connect to TelldusEvents");
if(!d->eventSocket.isConnected()) { d->eventSocket.connect(L"TelldusEvents"); //try to reconnect to service
// reconnect didn't succeed, wait a while and try again if(!d->eventSocket.isConnected()){
//reconnect didn't succeed, wait a while and try again
msleep(2000); msleep(2000);
continue; continue;
} }
@ -153,21 +155,24 @@ std::wstring Client::sendToService(const Message &msg) {
if(tries == 20) { if(tries == 20) {
TelldusCore::Message msg; TelldusCore::Message msg;
msg.addArgument(TELLSTICK_ERROR_CONNECTING_SERVICE); msg.addArgument(TELLSTICK_ERROR_CONNECTING_SERVICE);
debuglog(555, "Connection failed, 20 retries, giving up.");
return msg; return msg;
} }
Socket s; Socket s;
s.connect(L"TelldusClient"); s.connect(L"TelldusClient");
if (!s.isConnected()) { // Connection failed if (!s.isConnected()) { //Connection failed
debuglog(555, "Connection failed");
msleep(500); msleep(500);
continue; // retry continue; // retry
} }
s.write(msg.data()); s.write(msg.data());
if (!s.isConnected()) { // Connection failed sometime during operation... (better check here, instead of 5 seconds timeout later) if (!s.isConnected()) { // Connection failed sometime during operation... (better check here, instead of 5 seconds timeout later)
msleep(500); msleep(500);
continue; // retry debuglog(555, "Error in write, should retry");
continue; //retry
} }
readData = s.read(8000); // TODO(stefan) changed to 10000 from 5000, how much does this do...? readData = s.read(1000); //TODO changed to 10000 from 5000, how much does this do...?
if(readData == L"") { if(readData == L""){
msleep(500); msleep(500);
continue; // TODO(stefan): can we be really sure it SHOULD be anything? continue; // TODO(stefan): can we be really sure it SHOULD be anything?
// TODO(stefan): perhaps break here instead? // TODO(stefan): perhaps break here instead?
@ -189,6 +194,7 @@ void Client::stopThread() {
} }
int Client::unregisterCallback( int callbackId ) { int Client::unregisterCallback( int callbackId ) {
debuglog(555, "Client, correctly unregistering callback");
return d->callbackMainDispatcher.unregisterCallback(callbackId); return d->callbackMainDispatcher.unregisterCallback(callbackId);
} }

View file

@ -1,5 +1,5 @@
#include "Socket.h" #include "Socket.h"
#include "common.h"
#include <windows.h> #include <windows.h>
#include <AccCtrl.h> #include <AccCtrl.h>
#include <Aclapi.h> #include <Aclapi.h>
@ -120,6 +120,10 @@ std::wstring Socket::read(int timeout){
if (!fSuccess) { if (!fSuccess) {
DWORD err = GetLastError(); DWORD err = GetLastError();
debuglog((int)err, "Something read error");
if(err != ERROR_OPERATION_ABORTED){ //gets this "error" always when nothing was reads
debuglog((int)err, "Socket read error");
}
if(err == ERROR_MORE_DATA){ if(err == ERROR_MORE_DATA){
moreData = true; moreData = true;
@ -127,8 +131,10 @@ std::wstring Socket::read(int timeout){
else{ else{
buf[0] = 0; buf[0] = 0;
} }
if (err == ERROR_BROKEN_PIPE) { if (err == ERROR_BROKEN_PIPE){
debuglog((int)err, "Got an error, close this socket");
d->connected = false; d->connected = false;
break; //TODO is this correct?
} }
} }
returnString.append(buf); returnString.append(buf);
@ -153,7 +159,7 @@ void Socket::write(const std::wstring &msg){
BOOL writeSuccess = WriteFile(d->hPipe, msg.data(), (DWORD)msg.length()*sizeof(wchar_t), &bytesWritten, &oOverlap); BOOL writeSuccess = WriteFile(d->hPipe, msg.data(), (DWORD)msg.length()*sizeof(wchar_t), &bytesWritten, &oOverlap);
result = GetLastError(); result = GetLastError();
if (writeSuccess || result == ERROR_IO_PENDING) { if (writeSuccess || result == ERROR_IO_PENDING) {
result = WaitForSingleObject(writeEvent, 500); result = WaitForSingleObject(writeEvent, 30000);
if (result == WAIT_TIMEOUT) { if (result == WAIT_TIMEOUT) {
CancelIo(d->hPipe); CancelIo(d->hPipe);
WaitForSingleObject(oOverlap.hEvent, INFINITE); WaitForSingleObject(oOverlap.hEvent, INFINITE);
@ -164,12 +170,18 @@ void Socket::write(const std::wstring &msg){
return; return;
} }
fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &bytesWritten, TRUE); fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &bytesWritten, TRUE);
if (!fSuccess){
debuglog(result, "Error in GetOverlappedResult");
result = GetLastError();
debuglog(result, "Error in GetOverlappedResult, this message");
}
} }
CloseHandle(writeEvent); CloseHandle(writeEvent);
if (!fSuccess) { if (!fSuccess) {
CloseHandle(d->hPipe); CloseHandle(d->hPipe);
d->hPipe = 0; d->hPipe = 0;
debuglog(result, "Error in write event, closing socket");
d->connected = false; d->connected = false;
return; return;
} }

View file

@ -11,6 +11,7 @@
#ifdef _WINDOWS #ifdef _WINDOWS
#include <windows.h> #include <windows.h>
#include <ole2.h> #include <ole2.h>
#include <fstream>
#define strcasecmp _stricmp #define strcasecmp _stricmp
#define strncasecmp _strnicmp #define strncasecmp _strnicmp
#else #else
@ -18,11 +19,13 @@
#endif #endif
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> //#include <stdlib.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include "common/Strings.h" #include "common/Strings.h"
#include <time.h>
inline void msleep( const int msec) { inline void msleep( const int msec) {
#ifdef _WINDOWS #ifdef _WINDOWS
Sleep(msec); Sleep(msec);
@ -40,11 +43,12 @@ inline void dlog(const char *fmt, ...) {
fflush(stdout); fflush(stdout);
} }
inline void debuglog(const int intMessage, const std::string strMessage) { inline void debuglogfilename(const int intMessage, const std::string strMessage, const std::string filename){
#ifdef _WINDOWS #ifdef _WINDOWS
static bool firstRun = true; static bool firstRun = true;
std::ofstream file; std::ofstream file;
std::string filename("C:/log_locks.txt");
if (firstRun) { if (firstRun) {
file.open(filename.c_str(), std::ios::out); file.open(filename.c_str(), std::ios::out);
firstRun = false; firstRun = false;
@ -52,7 +56,14 @@ inline void debuglog(const int intMessage, const std::string strMessage) {
file.open(filename.c_str(), std::ios::out | std::ios::app); file.open(filename.c_str(), std::ios::out | std::ios::app);
} }
file << "[" << GetCurrentThreadId() << "] " << intMessage << " - " << strMessage << "\n"; time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
char* thetime = asctime(localtm);
thetime[strlen(thetime)-1] = '\0';
file << thetime << " [" << GetCurrentThreadId() << "] " << intMessage << " - " << strMessage << "\n";
file.flush(); file.flush();
file.close(); file.close();
@ -65,6 +76,16 @@ inline void debuglog(const int intMessage, const std::string strMessage) {
#endif #endif
} }
inline void debuglogservice(const int intMessage, const std::string strMessage){
std::string filename("C:/telldus_service_debug.txt");
debuglogfilename(intMessage, strMessage, filename);
}
inline void debuglog(const int intMessage, const std::string strMessage){
std::string filename("C:/telldus_client_debug.txt");
debuglogfilename(intMessage, strMessage, filename);
}
inline char *wrapStdString( const std::string &string) { inline char *wrapStdString( const std::string &string) {
#ifdef _WINDOWS #ifdef _WINDOWS
return reinterpret_cast<char *>(SysAllocStringByteLen(string.c_str(), (unsigned int)string.size())); return reinterpret_cast<char *>(SysAllocStringByteLen(string.c_str(), (unsigned int)string.size()));

View file

@ -15,6 +15,10 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#endif // _LINUX #endif // _LINUX
#include "common/common.h" //debug
#include <iostream> //debug
#include <sstream> //debug
#include <list> #include <list>
#include <map> #include <map>
#include <memory> #include <memory>
@ -162,11 +166,16 @@ void EventUpdateManager::sendMessageToClients(EventUpdateData *data) {
it++; it++;
} else { } else {
// connection is dead, remove it //connection is dead, remove it
debuglogservice(0, "Lost connection, removing it");
delete *it; delete *it;
it = d->clients.erase(it); it = d->clients.erase(it);
} }
} }
//printf("Sent message to %d connected clients", connected)
//std::stringstream strMessage;
//strMessage << "Sent message to " << connected << " clients" << std::endl;
//debuglogservice(0, strMessage.str());
} }
void EventUpdateManager::executeScripts(EventUpdateData *data) { void EventUpdateManager::executeScripts(EventUpdateData *data) {

View file

@ -5,6 +5,7 @@
// //
// //
#include "service/TelldusMain.h" #include "service/TelldusMain.h"
#include "common/common.h" //debug
#include <stdio.h> #include <stdio.h>
#include <list> #include <list>
@ -64,6 +65,7 @@ void TelldusMain::suspend() {
} }
void TelldusMain::start(void) { void TelldusMain::start(void) {
debuglogservice(0, "Starting service");
TelldusCore::EventRef clientEvent = d->eventHandler.addEvent(); TelldusCore::EventRef clientEvent = d->eventHandler.addEvent();
TelldusCore::EventRef dataEvent = d->eventHandler.addEvent(); TelldusCore::EventRef dataEvent = d->eventHandler.addEvent();
TelldusCore::EventRef executeActionEvent = d->eventHandler.addEvent(); TelldusCore::EventRef executeActionEvent = d->eventHandler.addEvent();