-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsipLocalConfig.cpp
211 lines (185 loc) · 4.97 KB
/
sipLocalConfig.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
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
#include "sipLocalConfig.h"
#define CONFIGFILE_PATH "../../../../conf/SipSupService.conf"
#define LOCAL_SECTION "localserver"
#define SIP_SECTION "sipserver"
static const string keyLocalIp = "local_ip";
static const string keyLocalPort = "local_port";
static const string keySipId = "sip_id";
static const string keySipIp = "sip_ip";
static const string keySipPort = "sip_port";
static const string keySipRealm = "sip_realm";
static const string keySipUsr = "sip_usr";
static const string keySipPwd = "sip_pwd";
static const std::string keyRtpPortBegin = "rtp_port_begin";
static const std::string keyRtpPortEnd = "rtp_port_end";
static const string keySubNodeNum = "subnode_num";
static const string keySubNodeId = "sip_subnode_id";
static const string keySubNodeIp = "sip_subnode_ip";
static const string keySubNodePort = "sip_subnode_port";
static const string keySubNodePoto = "sip_subnode_poto";
static const string keySubNodeAuth = "sip_subnode_auth";
SipLocalConfig::SipLocalConfig()
:m_conf(CONFIGFILE_PATH)
{
m_localIp = "";
m_localPort = 0;
m_sipId = "";
m_sipIp = "";
m_sipPort = 0;
m_sipRealm = "";
m_subNodeIp = "";
m_subNodePort = 0;
m_subNodePoto = 0;
m_subNodeAuth = 0;
}
SipLocalConfig::~SipLocalConfig()
{
}
int SipLocalConfig::ReadConf()
{
int ret = 0;
m_conf.setSection(LOCAL_SECTION);
m_localIp = m_conf.readStr(keyLocalIp);
if(m_localIp.empty())
{
ret = -1;
LOG(ERROR)<<"localIp is wrong";
return ret;
}
m_localPort = m_conf.readInt(keyLocalPort);
if(m_localPort <= 0)
{
ret = -1;
LOG(ERROR)<<"localPort is wrong";
return ret;
}
m_conf.setSection(SIP_SECTION);
m_sipId = m_conf.readStr(keySipId);
if(m_sipId.empty())
{
ret = -1;
LOG(ERROR)<<"sipId is wrong";
return ret;
}
m_sipIp = m_conf.readStr(keySipIp);
if(m_sipIp.empty())
{
ret = -1;
LOG(ERROR)<<"sipIp is wrong";
return ret;
}
m_sipPort = m_conf.readInt(keySipPort);
if(m_sipPort <= 0)
{
ret = -1;
LOG(ERROR)<<"sipPort is wrong";
return ret;
}
m_sipRealm = m_conf.readStr(keySipRealm);
if(m_sipRealm.empty())
{
ret = -1;
LOG(ERROR)<<"sipRealm is wrong";
return ret;
}
m_usr = m_conf.readStr(keySipUsr);
if(m_usr.empty())
{
ret = -1;
LOG(ERROR)<<"usr is wrong";
return ret;
}
m_pwd = m_conf.readStr(keySipPwd);
if(m_pwd.empty())
{
ret = -1;
LOG(ERROR)<<"pwd is wrong";
return ret;
}
LOG(INFO)<<"localip:"<<m_localIp<<",localport:"<<m_localPort<<",sipid:"<<m_sipId<<",sipip:"<<m_sipIp\
<<",sipport:"<<m_sipPort<<",sipRealm"<<m_sipRealm;
m_rtpPortBegin = m_conf.readInt(keyRtpPortBegin);
if(m_rtpPortBegin<=0)
{
ret =-1;
LOG(ERROR)<<"rtpPortBegin is NULL";
return ret;
}
m_rtpPortEnd = m_conf.readInt(keyRtpPortEnd);
if(m_rtpPortEnd<=0)
{
ret =-1;
LOG(ERROR)<<"rtpPortEnd is NULL";
return ret;
}
initRandPort();
int num = m_conf.readInt(keySubNodeNum);
LOG(INFO)<<"num:"<<num;
SubNodeInfo info;
for(int i = 1;i<num+1;++i)
{
string id = keySubNodeId + to_string(i);
string ip = keySubNodeIp + to_string(i);
string port = keySubNodePort + to_string(i);
string poto = keySubNodePoto + to_string(i);
string auth = keySubNodeAuth + to_string(i);
info.id = m_conf.readStr(id);
info.ip = m_conf.readStr(ip);
info.port = m_conf.readInt(port);
info.poto = m_conf.readInt(poto);
info.auth = m_conf.readInt(auth);
ubNodeInfoList.push_back(info);
}
LOG(INFO)<<"ubNodeInfoList.SIZE:"<<ubNodeInfoList.size();
return ret;
}
void SipLocalConfig::initRandPort()
{
while(m_rtpPortBegin % 2)
{
m_rtpPortBegin++;
}
while(m_rtpPortEnd % 2 == 0)
{
m_rtpPortEnd--;
}
AutoMutexLock lck(&m_rtpPortLock);
for(int i=m_rtpPortBegin;i< (m_rtpPortEnd + 1);i++)
{
m_RandNum.push(i);
}
LOG(INFO)<<"rand size:"<<m_RandNum.size();
}
int SipLocalConfig::popOneRandNum()
{
int rtpPort = 0;
AutoMutexLock lck(&m_rtpPortLock);
if(m_RandNum.size()>0)
{
//取完一个就扔出队列,每次取两个号
rtpPort = m_RandNum.front();
m_RandNum.pop();
if(rtpPort % 2) //这里我们再做下判断
{
//如果第一个是奇数,那么重新取
rtpPort = m_RandNum.front();
m_RandNum.pop();
}
//取第二个号
m_RandNum.pop();
}
return rtpPort;
}
//回收rtp port
int SipLocalConfig::pushOneRandNum(int num)
{
if(num < m_rtpPortBegin || num > m_rtpPortEnd)
{
return -1;
}
AutoMutexLock lck(&m_rtpPortLock);
m_RandNum.push(num);
m_RandNum.push(num+1);
LOG(INFO)<<"push rtp:"<<num<<",rtcp:"<<num+1;
return 0;
}