forked from aavegmit/p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cc
171 lines (149 loc) · 4.09 KB
/
timer.cc
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include "iniParser.h"
#include "main.h"
// Timer thread, decrements the value of timer for different timers
void *timer_thread(void *arg){
while(!shutDown){
sleep(1) ;
if(shutDown)
{
//printf("Timer thread halted\n");
break;
}
//JoinTimeOut, only accessible when node is Joining
if(!myInfo->isBeacon && inJoinNetwork)
{
if(myInfo->joinTimeOut > 0)
myInfo->joinTimeOut--;
else
{
if(myInfo->joinTimeOut > 0)
myInfo->joinTimeOut--;
else
{
if(myInfo->joinTimeOut==0)
{
//printf("Timer out..: %d\n", (int)pthread_self()) ;
kill(accept_pid, SIGUSR1);
break;
}
}
}
}
//KeepAliveTimeOut, monitors the value and then close the connection if the timeout becomes 0
pthread_mutex_lock(&nodeConnectionMapLock) ;
if(!nodeConnectionMap.empty() && !inJoinNetwork)
{
for (map<struct node, int >::iterator it = nodeConnectionMap.begin(); it != nodeConnectionMap.end(); ){
//printf("Values in my mAP are: %s, %d\n", (*it).first.hostname, (*it).first.portNo);
pthread_mutex_lock(&connectionMapLock) ;
if(connectionMap[(*it).second].keepAliveTimeOut > 0)
{
connectionMap[(*it).second].keepAliveTimeOut--;
it++;
}
else
{
if(connectionMap[(*it).second].keepAliveTimeOut == 0)
{
connectionMap[(*it).second].keepAliveTimeOut=-1;
pthread_mutex_unlock(&connectionMapLock) ;
closeConnection((*it).second);
//nodeConnectionMap.erase((*it).first);
nodeConnectionMap.erase(it++);
pthread_mutex_lock(&connectionMapLock) ;
}
else
it++;
}
pthread_mutex_unlock(&connectionMapLock) ;
}
}
pthread_mutex_unlock(&nodeConnectionMapLock) ;
//Auto-ShutDown Timer
if(!inJoinNetwork)
{
if(myInfo->autoShutDown > 0)
myInfo->autoShutDown--;
else
{
if(!myInfo->autoShutDown)
{
//sends the SIGTERM when needs to autoshutdown
kill(node_pid, SIGTERM);
}
}
}
// Status timer flag
if (statusTimerFlag && !inJoinNetwork){
// printf("value is now: %d\n", myInfo->statusResponseTimeout);
if(myInfo->statusResponseTimeout > 0)
--myInfo->statusResponseTimeout ;
else //if (myInfo->statusResponseTimeout == 0){
{
// Write all the status responses in the output file
//writeToStatusFile() ;
// Reset the status timer flag
pthread_mutex_lock(&statusMsgLock) ;
statusTimerFlag = 0 ;
pthread_cond_signal(&statusMsgCV);
pthread_mutex_unlock(&statusMsgLock) ;
}
}
// CHECK timer flag
if (checkTimerFlag && !inJoinNetwork){
--myInfo->checkResponseTimeout ;
//printf("Timer deducted %d\n", myInfo->checkResponseTimeout) ;
if (myInfo->checkResponseTimeout <= 0){
// Reset the status timer flag
checkTimerFlag = 0 ;
softRestartFlag = 1 ;
// Write all the status responses in the output file
//printf("No response from any beacon node. You are disconnected from the network. Connect again\n") ;
pthread_mutex_lock(&nodeConnectionMapLock) ;
for (map<struct node, int>::iterator it = nodeConnectionMap.begin(); it != nodeConnectionMap.end(); ++it){
notifyMessageSend((*it).second, 3);
}
pthread_mutex_unlock(&nodeConnectionMapLock) ;
sleep(1);
kill(node_pid, SIGTERM);
//break;
// writeToStatusFile() ;
}
}
//MsgLifetime timer, erases the message if the timer expires
if(!inJoinNetwork)
{
pthread_mutex_lock(&MessageDBLock) ;
//map<string, struct Packet>::iterator it_temp;
map<string, struct Packet>::iterator tempIt ;
for(map<string, struct Packet>::iterator it_temp = MessageDB.begin(); it_temp != MessageDB.end(); )
{
if((*it_temp).second.msgLifeTime > 0)
{
(*it_temp).second.msgLifeTime--;
++it_temp;
continue;
}
else
{
if((*it_temp).second.msgLifeTime == 0)
{
tempIt = it_temp ;
++it_temp ;
MessageDB.erase(tempIt);
}
else
++it_temp;
}
}
pthread_mutex_unlock(&MessageDBLock) ;
}
}
pthread_exit(0);
return 0;
}