From cad0913b3799819bbada60fb16f079e3e4d58de0 Mon Sep 17 00:00:00 2001 From: xkonni Date: Mon, 5 Nov 2012 01:18:15 +0100 Subject: [PATCH] daemon: fixes in message handling --- daemon.cpp | 80 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/daemon.cpp b/daemon.cpp index 7520544..1f2b960 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -87,7 +87,8 @@ int main(int argc, char* argv[]) { error("ERROR reading from socket"); - if (strlen(buffer) > 8) { + printf("message: %s\n", buffer); + if (strlen(buffer) >= 8) { for (int i=0; i<5; i++) { nGroup[i] = buffer[i]; } @@ -97,48 +98,51 @@ int main(int argc, char* argv[]) { nSwitchNumber = buffer[i]-48; } nAction = buffer[7]-48; + + /** + * handle messages + */ + int nAddr = getAddr(nGroup, nSwitchNumber); + char msg[13]; + if (nAddr > nPlugs) + printf("Switch invalid: %s::%d\n", nGroup, nSwitchNumber); + else { + switch (nAction) { + /** + * off + */ + case 0: + mySwitch.switchOff(nGroup, nSwitchNumber); + nState[nAddr] = 0; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + /** + * on + */ + case 1: + mySwitch.switchOn(nGroup, nSwitchNumber); + nState[nAddr] = 1; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + /** + * status + */ + case 2: + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + } } else { printf("message corrupted or incomplete"); } - /** - * handle messages - */ - int nAddr = getAddr(nGroup, nSwitchNumber); - char msg[13]; - if (nAddr > nPlugs) - printf("Switch invalid: %s::%d\n", nGroup, nSwitchNumber); - else { - switch (nAction) { - /** - * off - */ - case 0: - mySwitch.switchOff(nGroup, nSwitchNumber); - nState[nAddr] = 0; - sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - n = write(newsockfd,msg,14); - break; - /** - * on - */ - case 1: - mySwitch.switchOn(nGroup, nSwitchNumber); - nState[nAddr] = 1; - sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - n = write(newsockfd,msg,14); - break; - /** - * status - */ - case 2: - printf("nState[%d] %d\n", nAddr, nState[nAddr]); - sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - n = write(newsockfd,msg,14); - break; - } - } if (n < 0) error("ERROR writing to socket");