diff --git a/statemon.c b/statemon.c index d648e126..60f63f32 100644 --- a/statemon.c +++ b/statemon.c @@ -346,10 +346,8 @@ int StateMonFactory(SConnection * pCon, SicsInterp * pSics, void *pData, pStateMonDummyCon = SCCreateDummyConnection(pSics); lID = RegisterCallback(pNew->pCall, STSTART, StateInterest, pStateMonDummyCon, NULL); - SCRegister(pStateMonDummyCon, pSics, pNew->pCall, lID); lID = RegisterCallback(pNew->pCall, STEND, StateInterest, pStateMonDummyCon, NULL); - SCRegister(pStateMonDummyCon, pSics, pNew->pCall, lID); pRes = VarCreate(usInternal, veInt, SICS_SUID); AddCommand(pSics, SICS_SUID, VarWrapper, (KillFunc) VarKill, pRes); /* @@ -359,6 +357,20 @@ int StateMonFactory(SConnection * pCon, SicsInterp * pSics, void *pData, return 1; } +/* + * Context test function for callback removal + */ + int CheckConMatch(const void* context, const void* pUserData) +{ + SConnection *pKon = (SConnection*) pUserData; + SConnection *pCon = (SConnection*) context; + if (VerifyConnection(pCon) && VerifyConnection(pKon)) { + if (pCon->ident == pKon->ident) + return 0; + } + return 1; +} + /*---------------------------------------------------------------------------*/ int StateMonAction(SConnection * pCon, SicsInterp * pSics, void *pData, int argc, char *argv[]) @@ -377,6 +389,7 @@ int StateMonAction(SConnection * pCon, SicsInterp * pSics, void *pData, } strtolower(argv[1]); if (strcmp(argv[1], "interest") == 0) { + RemoveCallbackUsr(self->pCall, StateInterest, CheckConMatch, pCon); /* only this one */ callCon = SCCopyConnection(pCon); if (callCon == NULL) { SCWrite(pCon, "ERROR: out of memory registering interest", eError); @@ -385,32 +398,26 @@ int StateMonAction(SConnection * pCon, SicsInterp * pSics, void *pData, lID = RegisterCallback(self->pCall, STSTART, StateInterest, callCon, NULL); - SCRegister(pCon, pSics, self->pCall, lID); lID = RegisterCallback(self->pCall, STPAUSE, StateInterest, callCon, NULL); - SCRegister(pCon, pSics, self->pCall, lID); lID = RegisterCallback(self->pCall, STCONTINUE, StateInterest, callCon, NULL); - SCRegister(pCon, pSics, self->pCall, lID); lID = RegisterCallback(self->pCall, STBUSY, StateInterest, callCon, NULL); - SCRegister(pCon, pSics, self->pCall, lID); lID = RegisterCallback(self->pCall, STEND, StateInterest, callCon, NULL); - SCRegister(pCon, pSics, self->pCall, lID); SCSendOK(pCon); return 1; } else if (strcmp(argv[1], "uninterest") == 0) { - for (i = 0; i < 2; i++) { - lID = SCgetCallbackID(pCon, self->pCall); - if (lID >= 0) { - RemoveCallback(self->pCall, lID); - SCUnregisterID(pCon, lID); - } - } + RemoveCallbackUsr(self->pCall, StateInterest, CheckConMatch, pCon); /* only this one */ + SCSendOK(pCon); + return 1; + } else if (strcmp(argv[1], "hdbuninterest") == 0) { + RemoveCallbackUsr(self->pCall, StateHdbInterest, CheckConMatch, pCon); /* only this one */ SCSendOK(pCon); return 1; } else if (strcmp(argv[1], "hdbinterest") == 0) { + RemoveCallbackUsr(self->pCall, StateHdbInterest, CheckConMatch, pCon); /* only this one */ callCon = SCCopyConnection(pCon); if (callCon == NULL) { SCWrite(pCon, "ERROR: out of memory registering interest", eError); @@ -418,10 +425,8 @@ int StateMonAction(SConnection * pCon, SicsInterp * pSics, void *pData, } lID = RegisterCallback(self->pCall, STSTART, StateHdbInterest, callCon, SCDeleteConnection); - /* TODO: why not SCRegister(pCon, pSics, self->pCall, lID); */ lID = RegisterCallback(self->pCall, STEND, StateHdbInterest, callCon, NULL); - /* TODO: why not SCRegister(pCon, pSics, self->pCall, lID); */ SCSendOK(pCon); return 1; } else if (strcmp(argv[1], "start") == 0) { @@ -467,7 +472,6 @@ int StateMonAction(SConnection * pCon, SicsInterp * pSics, void *pData, } return 0; } - SCWrite(pCon, "ERROR: keyword not recognized", eError); return 0;