-
Notifications
You must be signed in to change notification settings - Fork 9
/
connectionspool.cpp
149 lines (116 loc) · 3.11 KB
/
connectionspool.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
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
#include <stdio.h>
#include <algorithm>
#include "connectionspool.h"
connectionsPool::connectionsPool()
{
maxConnNr[0] = 0;
maxConnNr[1] = 0;
nrAvailable[0] = 0;
nrAvailable[1] = 0;
}
std::set<size_t> connectionsPool::getReservedButNotAssigned()
{
std::set<size_t> ret;
ret.insert(reservedButNotAssigned[0].begin(), reservedButNotAssigned[0].end());
ret.insert(reservedButNotAssigned[1].begin(), reservedButNotAssigned[1].end());
return ret;
}
std::set<size_t> connectionsPool::getAssigned()
{
std::set<size_t> ret;
ret.insert(assigned[0].begin(), assigned[0].end());
ret.insert(assigned[1].begin(), assigned[1].end());
return ret;
}
void connectionsPool::preReleaseConnection(int boxType, size_t connNr)
{
assigned[boxType].erase(connNr);
toBeAvailable[boxType].insert(connNr);
}
void connectionsPool::releaseConnection(int boxType, size_t connNr)
{
printf("3.11.2017: release conn %d\n", connNr);
if(toBeAvailable[boxType].find(connNr) == toBeAvailable[boxType].end())
{
printf("NOT IN TOBEAVAILABLE %d\n", connNr);
}
toBeAvailable[boxType].erase(connNr);
nrAvailable[boxType]++;
available.insert(available.begin(), connNr);
}
bool connectionsPool::sufficientToConsume(int boxType)
{
return reservedButNotAssigned[boxType].size() != 0;
}
size_t connectionsPool::consumeConnection(int boxType)
{
size_t ret = reservedButNotAssigned[boxType].front();
reservedButNotAssigned[boxType].erase(reservedButNotAssigned[boxType].begin());
assigned[boxType].insert(ret);
return ret;
}
bool connectionsPool::sufficientAvailable(int boxType)
{
return /*available.size() != 0*/nrAvailable[boxType] > 0;
}
bool connectionsPool::increaseAvailable(int boxType, int max)
{
if(maxConnNr[boxType] < max)
{
// available.push_back(maxConnNr[boxType]);
available.push_back(maxConnNr[0] + maxConnNr[1]);
maxConnNr[boxType]++;
nrAvailable[boxType]++;
}
else
{
return false;
}
return true;
}
/**
* Reserve a connection of a specific type
* @param boxType type of the connection
* @return the connection's number
*/
size_t connectionsPool::reserveConnection(int boxType)
{
size_t ret = available.front();
available.erase(available.begin());
/*17.02.2017*/
nrAvailable[boxType]--;
reservedButNotAssigned[boxType].push_back(ret);
// printf("reserve t%d c%d\n", boxType, ret);
return ret;
}
size_t connectionsPool::reserveConnectionPreferred(int boxType, size_t approxConnectionNumber)
{
long currentApprox = approxConnectionNumber;
std::vector<size_t>::iterator foundIterator = available.end();
bool doNotFlipDirection = false;
int step = 0;
int direction = 1;
while(foundIterator == available.end())
{
if(currentApprox == 0)
{
doNotFlipDirection = true;
direction = 1;
}
else
{
direction = direction * -1;
}
currentApprox = currentApprox + direction * step;
if(currentApprox >= 0)
{
foundIterator = std::find(available.begin(), available.end(), (size_t) currentApprox);
//printf("..... %lu %d\n", currentApprox, step);
}
step++;
}
available.erase(foundIterator);
nrAvailable[boxType]--;
reservedButNotAssigned[boxType].push_back(currentApprox);
return currentApprox;
}