void* Listener(void * args) { char buf[BUFSIZE]; SDMSubreqst sub; SDMDeletesub del; MessageManager mm; mm.Async_Init(my_port); // Send one hearbeat, let the app fail SendHeartbeat(); while(1) { if(mm.IsReady()) { //SendHeartbeat(); #ifdef WIN32 switch(mm.GetMsg(buf)) #else switch(mm.GetMessage(buf)) #endif { case SDM_Subreqst: sub.Unmarshal(buf); printf("Subscription Rec'd for %d\n",sub.msg_id.getInterfaceMessagePair()); fflush(NULL); pthread_mutex_lock(&subscription_mutex); subscriptions.AddSubscription(sub); pthread_mutex_unlock(&subscription_mutex); break; case SDM_Deletesub: printf("Cancel Rec'd\n"); del.Unmarshal(buf); pthread_mutex_lock(&subscription_mutex); subscriptions.RemoveSubscription(del); pthread_mutex_unlock(&subscription_mutex); break; default: printf("Invalid Message found!\n"); fflush(NULL); break; } } else { usleep(100000); } } return NULL; }
int main(int argc, char ** argv) { unsigned long result = 0; int length = 0; int miss_count = 0; int num_toget = 0; char buf[BUFSIZE]; SDMReady heartbeat; MessageManager mm; SDMComponent_ID tm; //Set monitor process address heartbeat.source.setSensorID(0); heartbeat.source.setAddress(inet_addr("127.0.0.1")); heartbeat.source.setPort(PORT_TM_MONITOR); //Set TM address tm.setAddress(inet_addr("127.0.0.1")); tm.setPort(PORT_TM); tm.setSensorID(0); signal(SIGINT, SigIntHandler); tm_handle = StartTM(argv); mm.Async_Init_Both(PORT_TM_MONITOR); /* If spawn was successful */ if (tm_handle != NULL) { /* Allow the TM to get started up */ sleep(HEARTBEAT_INTERVAL); while (1) { //Send heartbeats via UDP heartbeat.SendTo(tm); length = heartbeat.Marshal(buf); sleep(HEARTBEAT_INTERVAL); //If Task Manager quit if (!GetExitCodeProcess(tm_handle,&result)) printf(" Monitor: Invalid handle for retrieving exit code.\n"); /* If the TaskManager has quit for some reason */ if (result != STILL_ACTIVE) { printf(" Monitor: TM failed, restarting...\n"); tm_handle = StartTM(argv); if (tm_handle != NULL) { /* Allow the TM to restart */ sleep(HEARTBEAT_INTERVAL); /* Start over at while */ continue; } /* Error re-starting TM */ else { printf(" Monitor: Could not restart the TM.\n"); return -1; } } /* The TM is still running, check for responses to heartbeat messages */ else if (mm.IsReady()) { //Should respond with one message num_toget = 1; while (mm.IsReady()) { num_toget--; /* Message is discarded, we only care that one was received */ mm.GetMsg(buf); } if (num_toget > 0) miss_count++; else miss_count = 0; } /* The TM is still running, but no responses were received */ else { //If no messages were received miss_count++; if (miss_count == NUM_HEARTBEAT_TRIES) { printf(" Monitor: TM unresponsive, restarting...\n"); /* If the Terminate fails and the TM is already dead */ if (!TerminateProcess(tm_handle,0)) { /* Get its return value */ GetExitCodeProcess(tm_handle,&result); } /* Restart the TM */ tm_handle = StartTM(argv); /* Success restarting */ if (tm_handle != NULL) { /* Allow TM to get started */ sleep(HEARTBEAT_INTERVAL); /* Start over at while loop */ continue; } /* Error restarting */ else { printf(" Monitor: Could not restart the TM.\n"); return -1; } } } } } else { printf(" Monitor: Error starting the TM.\n"); return -1; } return 0; }
void* Listener(void * args) { char buf[BUFSIZE]; SDMSubreqst sub; SDMDeletesub del; MessageManager mm; mm.Async_Init(myPort); while(1) { pthread_testcancel(); if(mm.IsReady()) { SendHeartbeat(); #ifdef WIN32 switch(mm.GetMsg(buf)) #else switch(mm.GetMessage(buf)) #endif { case SDM_ACK: printf("SDMAck received\n"); helloReply = true; break; case SDM_Register: printf("SDMRegister received\n"); waitForReg = false; break; case SDM_ID: printf("SDMID received\n"); myID.Unmarshal(buf); printf("CompID: \n"); printf(" SensorID: %li\n", myID.destination.getSensorID()); printf(" Address: %lx\n", myID.destination.getAddress()); printf(" Port: %i\n", myID.destination.getPort()); waitForID = false; break; case SDM_Subreqst: sub.Unmarshal(buf); printf("Subscription Rec'd for %d\n",sub.msg_id.getInterfaceMessagePair()); pthread_mutex_lock(&subscription_mutex); subscriptions.AddSubscription(sub); pthread_mutex_unlock(&subscription_mutex); break; case SDM_Deletesub: printf("Cancel Rec'd\n"); del.Unmarshal(buf); pthread_mutex_lock(&subscription_mutex); subscriptions.RemoveSubscription(del); pthread_mutex_unlock(&subscription_mutex); break; default: printf("Invalid Message found!\n"); break; } } else { usleep(100000); } } return NULL; }