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->context = context;
d->callbackList.push_back(callback);
//logga callbackprenumeration lades till
debuglog(id, "Callback added");
return id;
}
@ -68,9 +70,11 @@ int CallbackMainDispatcher::unregisterCallback(int callbackId) {
{
TelldusCore::MutexLocker locker(&d->mutex);
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
//logga, avregistrering av callback
if ( (*callback_it)->id != callbackId ) {
continue;
}
debuglog(callbackId, "Callback unregistered");
newEventList.splice(newEventList.begin(), d->callbackList, callback_it);
break;
}
@ -100,9 +104,16 @@ void CallbackMainDispatcher::run() {
if (!cbd) {
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);
//logga, har låst
//debuglog(333, "Callbackevent, locked");
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
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));
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 ) {
debuglog(555, "Client, Registering callback");
return d->callbackMainDispatcher.registerCallback(type, eventFunction, context );
}
@ -86,10 +87,11 @@ void Client::run() {
d->eventSocket.connect(L"TelldusEvents");
while(d->running) {
if(!d->eventSocket.isConnected()) {
d->eventSocket.connect(L"TelldusEvents"); // try to reconnect to service
if(!d->eventSocket.isConnected()) {
// reconnect didn't succeed, wait a while and try again
if(!d->eventSocket.isConnected()){
debuglog(555, "Client, Trying to (re)connect to TelldusEvents");
d->eventSocket.connect(L"TelldusEvents"); //try to reconnect to service
if(!d->eventSocket.isConnected()){
//reconnect didn't succeed, wait a while and try again
msleep(2000);
continue;
}
@ -153,21 +155,24 @@ std::wstring Client::sendToService(const Message &msg) {
if(tries == 20) {
TelldusCore::Message msg;
msg.addArgument(TELLSTICK_ERROR_CONNECTING_SERVICE);
debuglog(555, "Connection failed, 20 retries, giving up.");
return msg;
}
Socket s;
s.connect(L"TelldusClient");
if (!s.isConnected()) { // Connection failed
if (!s.isConnected()) { //Connection failed
debuglog(555, "Connection failed");
msleep(500);
continue; // retry
}
s.write(msg.data());
if (!s.isConnected()) { // Connection failed sometime during operation... (better check here, instead of 5 seconds timeout later)
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...?
if(readData == L"") {
readData = s.read(1000); //TODO changed to 10000 from 5000, how much does this do...?
if(readData == L""){
msleep(500);
continue; // TODO(stefan): can we be really sure it SHOULD be anything?
// TODO(stefan): perhaps break here instead?
@ -189,6 +194,7 @@ void Client::stopThread() {
}
int Client::unregisterCallback( int callbackId ) {
debuglog(555, "Client, correctly unregistering callback");
return d->callbackMainDispatcher.unregisterCallback(callbackId);
}

View file

@ -1,5 +1,5 @@
#include "Socket.h"
#include "common.h"
#include <windows.h>
#include <AccCtrl.h>
#include <Aclapi.h>
@ -120,6 +120,10 @@ std::wstring Socket::read(int timeout){
if (!fSuccess) {
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){
moreData = true;
@ -127,8 +131,10 @@ std::wstring Socket::read(int timeout){
else{
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;
break; //TODO is this correct?
}
}
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);
result = GetLastError();
if (writeSuccess || result == ERROR_IO_PENDING) {
result = WaitForSingleObject(writeEvent, 500);
result = WaitForSingleObject(writeEvent, 30000);
if (result == WAIT_TIMEOUT) {
CancelIo(d->hPipe);
WaitForSingleObject(oOverlap.hEvent, INFINITE);
@ -164,12 +170,18 @@ void Socket::write(const std::wstring &msg){
return;
}
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);
if (!fSuccess) {
CloseHandle(d->hPipe);
d->hPipe = 0;
debuglog(result, "Error in write event, closing socket");
d->connected = false;
return;
}

View file

@ -11,6 +11,7 @@
#ifdef _WINDOWS
#include <windows.h>
#include <ole2.h>
#include <fstream>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
@ -18,11 +19,13 @@
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
//#include <stdlib.h>
#include <string.h>
#include <string>
#include "common/Strings.h"
#include <time.h>
inline void msleep( const int msec) {
#ifdef _WINDOWS
Sleep(msec);
@ -40,11 +43,12 @@ inline void dlog(const char *fmt, ...) {
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
static bool firstRun = true;
std::ofstream file;
std::string filename("C:/log_locks.txt");
if (firstRun) {
file.open(filename.c_str(), std::ios::out);
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 << "[" << 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.close();
@ -65,6 +76,16 @@ inline void debuglog(const int intMessage, const std::string strMessage) {
#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) {
#ifdef _WINDOWS
return reinterpret_cast<char *>(SysAllocStringByteLen(string.c_str(), (unsigned int)string.size()));

View file

@ -15,6 +15,10 @@
#include <sys/types.h>
#include <sys/wait.h>
#endif // _LINUX
#include "common/common.h" //debug
#include <iostream> //debug
#include <sstream> //debug
#include <list>
#include <map>
#include <memory>
@ -162,11 +166,16 @@ void EventUpdateManager::sendMessageToClients(EventUpdateData *data) {
it++;
} else {
// connection is dead, remove it
//connection is dead, remove it
debuglogservice(0, "Lost connection, removing it");
delete *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) {

View file

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