[go: up one dir, main page]

Menu

[r332]: / codeblue2 / gmp / prec.cpp  Maximize  Restore  History

Download this file

236 lines (187 with data), 6.2 kB

  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*****************************************************************************
Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the
above copyright notice, this list of conditions
and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Illinois
nor the names of its contributors may be used to
endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu, last updated 09/17/2009
*****************************************************************************/
#ifndef WIN32
#include <sys/time.h>
#include <time.h>
#else
#include <windows.h>
#endif
#include <common.h>
#include <dhash.h>
#include <prec.h>
using namespace std;
CPeerManagement::CPeerManagement()
{
#ifndef WIN32
pthread_mutex_init(&m_PeerRecLock, NULL);
#else
m_PeerRecLock = CreateMutex(NULL, false, NULL);
#endif
int n = 1 << m_uiHashSpace;
m_pllHashRec = new int64_t [n];
for (int i = 0; i < n; ++ i)
m_pllHashRec[i] = -1000001;
}
CPeerManagement::~CPeerManagement()
{
#ifndef WIN32
pthread_mutex_destroy(&m_PeerRecLock);
#else
CloseHandle(m_PeerRecLock);
#endif
delete [] m_pllHashRec;
}
void CPeerManagement::insert(const string& ip, const int& port, const int& session, const int32_t& id, const int& rtt, const int& fw)
{
CGuard recguard(m_PeerRecLock);
if (rtt > 0)
{
map<string, int>::iterator t = m_mRTT.find(ip);
if (t != m_mRTT.end())
t->second = (t->second * 7 + rtt) >> 3;
else
m_mRTT[ip] = rtt;
}
int key = hash(ip, port, session, id);
m_pllHashRec[key] = CTimer::getTime();
CPeerRecord* pr = new CPeerRecord;
pr->m_strIP = ip;
pr->m_iPort = port;
pr->m_iSession = session;
set<CPeerRecord*, CFPeerRec>::iterator i = m_sPeerRec.find(pr);
map<string, int>::iterator t = m_mRTT.find(ip);
if (i != m_sPeerRec.end())
{
if (id > (*i)->m_iID)
(*i)->m_iID = id;
(*i)->m_iFlowWindow = fw;
m_sPeerRecByTS.erase(*i);
(*i)->m_llTimeStamp = CTimer::getTime();
m_sPeerRecByTS.insert(*i);
delete pr;
}
else
{
if (id > 0)
pr->m_iID = id;
else
pr->m_iID = -1;
pr->m_llTimeStamp = CTimer::getTime();
pr->m_iFlowWindow = fw;
m_sPeerRec.insert(pr);
m_sPeerRecByTS.insert(pr);
if (m_sPeerRecByTS.size() > m_uiRecLimit)
{
// delete first one
set<CPeerRecord*, CFPeerRecByTS>::iterator j = m_sPeerRecByTS.begin();
CPeerRecord* t = *j;
m_sPeerRec.erase(t);
m_sPeerRecByTS.erase(j);
bool delip = true;
for (set<CPeerRecord*, CFPeerRec>::iterator k = m_sPeerRec.begin(); k != m_sPeerRec.end(); ++ k)
{
if ((*k)->m_strIP == t->m_strIP)
{
delip = false;
break;
}
}
if (delip)
m_mRTT.erase(t->m_strIP);
delete t;
}
}
}
int CPeerManagement::getRTT(const string& ip)
{
CGuard recguard(m_PeerRecLock);
map<string, int>::iterator t = m_mRTT.find(ip);
if (t != m_mRTT.end())
return t->second;
return -1;
}
int CPeerManagement::getLastID(const string& ip, const int& port, const int& session)
{
CPeerRecord pr;
pr.m_strIP = ip;
pr.m_iPort = port;
pr.m_iSession = session;
CGuard recguard(m_PeerRecLock);
set<CPeerRecord*, CFPeerRec>::iterator i = m_sPeerRec.find(&pr);
if (i != m_sPeerRec.end())
return (*i)->m_iID;
return -1;
}
void CPeerManagement::clearRTT(const string& ip)
{
CGuard recguard(m_PeerRecLock);
m_mRTT.erase(ip);
}
int CPeerManagement::flowControl(const std::string& ip, const int& port, const int& session)
{
CPeerRecord pr;
pr.m_strIP = ip;
pr.m_iPort = port;
pr.m_iSession = session;
CGuard recguard(m_PeerRecLock);
set<CPeerRecord*, CFPeerRec>::iterator i = m_sPeerRec.find(&pr);
if (i == m_sPeerRec.end())
return 0;
int thresh = (*i)->m_iFlowWindow - (CTimer::getTime() - (*i)->m_llTimeStamp) / 1000;
if (thresh > 100)
{
usleep(100000);
return 100000;
}
if (thresh > 10)
{
usleep(10000);
return 10000;
}
return 0;
}
int32_t CPeerManagement::hash(const string& ip, const int& port, const int& session, const int32_t& id)
{
char tmp[1024];
sprintf(tmp, "%s%d%d%d", ip.c_str(), port, session, id);
return DHash::hash(tmp, m_uiHashSpace);
}
bool CPeerManagement::hit(const string& ip, const int& port, const int& session, const int32_t& id)
{
int key = hash(ip, port, session, id);
int64_t diff = CTimer::getTime() - m_pllHashRec[key];
if ((diff > 1000000) || (diff < 0))
return false;
return true;
}