forked from dase/CLAIMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortManager.cpp
executable file
·54 lines (51 loc) · 1.09 KB
/
PortManager.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
/*
* PortManager.cpp
*
* Created on: Aug 11, 2013
* Author: wangli
*/
#include "PortManager.h"
#include <libconfig.h++>
#include <iostream>
#include "Logging.h"
#include "Debug.h"
PortManager* PortManager::_instance=0;
PortManager::PortManager() {
getConfigure();
generateFreePorts();
Logging_PortManager("PortManager Created, the Ports range is [%u,%u]",start,end);
}
PortManager::~PortManager() {
// TODO Auto-generated destructor stub
}
PortManager* PortManager::getInstance(){
if(_instance==0){
_instance=new PortManager();
}
return _instance;
}
void PortManager::getConfigure(){
libconfig::Config cfg;
cfg.readFile(CONFIG);
start=(unsigned)cfg.lookup("PortManager.start");
end=(unsigned)cfg.lookup("PortManager.end");
}
void PortManager::generateFreePorts(){
for(int i=end;i>=start;i--){
free_ports.push_back(i);
}
}
int PortManager::applyPort(){
l.acquire();
if(free_ports.size()==0)
return -1;
int ret=free_ports.back();
free_ports.pop_back();
l.release();
return ret;
}
void PortManager::returnPort(unsigned port){
l.acquire();
free_ports.push_back(port);
l.release();
}