-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
118 lines (106 loc) · 3.06 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <signal.h>
#include "main.h"
void *general_timer(void *dummy)
{
while(!globalShutdownToggle)
{
sleep(1);
if(globalShutdownToggle)
{
//writeLog("//Global shutdown toggle true encountered in timer thread\n");
printf("Global shutdown toggle true encountered in timer thread\n");
break;
}
if(!joinNetworkPhaseFlag)
{
//Auto-shutdown check
if(metadata->autoShutDown > 0)
{
metadata->autoShutDown--;
//printf("Time left to be killed--%d\n",metadata->autoShutDown);
}
if(metadata->autoShutDown == 0)
{
//writeLog("//The auto-shutdown timer count is zero KABOOM....\nSIGTERM called");
printf("//The auto-shutdown timer count is zero.\nSIGTERM called");
kill(nodeProcessId,SIGTERM);
}
//screw up the message cache
pthread_mutex_lock(&msgCacheLock);
map<string, struct CachePacket>::iterator temp;
for(map<string,struct CachePacket>::iterator it=MessageCache.begin();it!=MessageCache.end();it++)
{
if((*it).second.msgLifetimeInCache > 0)
{
(*it).second.msgLifetimeInCache--;
if((*it).second.msgLifetimeInCache == 0)
{
MessageCache.erase(it);
}
}
}
pthread_mutex_unlock(&msgCacheLock);
//printf("[Timer] ... statusFloodTimeout = %d\n" , metadata->statusFloodTimeout);
fflush(stdout);
if(statusTimerFlag!=0)
{
if(metadata->statusFloodTimeout > 0) {
metadata->statusFloodTimeout--;
metadata->statusFloodTimeout--;
//printf("[Timer] set statusFloodTimeout = %d\n" , metadata->statusFloodTimeout);
fflush(stdout);
}
if(metadata->statusFloodTimeout <= 0)
{
printf("[Timer]\t status flood timed out\n");
fflush(stdout);
pthread_mutex_lock(&statusMsgLock);
printf("[Timer]\t **** Signal the status' condition variable\n");
fflush(stdout);
pthread_cond_signal(&statusMsgCV);
statusTimerFlag=0;
pthread_mutex_unlock(&statusMsgLock);
}
}
//keepalivetimer tracking
pthread_mutex_lock(¤tNeighboursLock);
if(!currentNeighbours.empty())
{
for(map< struct NodeInfo, int >::iterator i=currentNeighbours.begin();i!=currentNeighbours.end();i++)
{
pthread_mutex_lock(&connectionMapLock);
if(ConnectionMap[(*i).second].keepAliveTimeOut > 0)
ConnectionMap[(*i).second].keepAliveTimeOut--;
if(ConnectionMap[(*i).second].keepAliveTimeOut==0)
{
pthread_mutex_unlock(&connectionMapLock);
destroyConnectionAndCleanup((*i).second);
currentNeighbours.erase(i);
pthread_mutex_lock(&connectionMapLock);
}
pthread_mutex_unlock(&connectionMapLock);
}
}
pthread_mutex_unlock(¤tNeighboursLock);
}
else
{
if(!metadata->iAmBeacon)
{
if(metadata->joinTimeOut > 0)
metadata->joinTimeOut--;
if(metadata->joinTimeOut == 0)
{
kill(acceptProcessId,SIGUSR1);
printf("Join time out,sigusr1 called\n");
//writeLog("//Join time out occured\n");
break;
}
}
}
//printf("Going back up---2\n");
}
printf("[Timer]\t********** Leaving timer thread \n");
pthread_exit(0);
return 0;
}