Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
240 lines (211 loc) · 7.21 KB
/
Copy pathmain.cpp
File metadata and controls
240 lines (211 loc) · 7.21 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
236
237
238
239
240
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "NodeServer.h"
#include "servant/Communicator.h"
#include "util/tc_option.h"
#include "util/tc_file.h"
#include "util/tc_config.h"
#include <iostream>
using namespace std;
using namespace tars;
NodeServer g_app;
TC_Config* g_pconf;
static string outfill(const string& s, char c = ' ', int n = 29)
{
return (s + string(abs(n - (int)s.length()), c));
}
bool getConfig(const string &sLocator,const string &sRegistryObj,string &sNodeId,string &sLocalIp, string &sConfigFile, bool bCloseCout)
{
try
{
//string sLocalIp;
string sResult;
string sTemplate;
TC_Config tConf;
CommunicatorFactory::getInstance()->getCommunicator()->setProperty("locator",sLocator);
RegistryPrx pRegistryPrx = CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy<RegistryPrx>(sRegistryObj);
if( sLocalIp.empty() && pRegistryPrx->getClientIp(sLocalIp) != 0)
{
cerr<<"cannot get localip: " <<sLocalIp << endl;
return false;
}
if(sNodeId == "" )
{
sNodeId = sLocalIp;
}
pRegistryPrx->getNodeTemplate(sNodeId,sTemplate);
if(TC_Common::trim(sTemplate) == "" )
{
cerr << "cannot get node Template nodeid:"<<sNodeId <<",Template:"<<sTemplate<< endl;
return false;
}
sTemplate = TC_Common::replace(sTemplate, "${locator}",sLocator);
sTemplate = TC_Common::replace(sTemplate, "${registryObj}",sRegistryObj);
sTemplate = TC_Common::replace(sTemplate, "${localip}",sLocalIp);
//sTemplate = TC_Common::replace(sTemplate, "${nodeid}",sNodeId);
tConf.parseString(sTemplate);
if (!bCloseCout)
{
string sCloseCoutValue = tConf.get("/tars/application/server<closecout>");
if (sCloseCoutValue == "1")
{
sTemplate = TC_Common::replace(sTemplate, "closecout=1", "closecout=0");
tConf.parseString(sTemplate);
}
else if (sCloseCoutValue.empty())
{
map<string, string> m;
m["closecout"] = "0";
tConf.insertDomainParam("/tars/application/server", m,true);
}
else
{
cerr << "failed to set closeout value" << endl;
}
}
string sConfigPath = TC_File::extractFilePath(sConfigFile);
if(!TC_File::makeDirRecursive( sConfigPath ))
{
cerr<<"cannot create dir: " <<sConfigPath << endl;
exit( 0 );
}
string sFileTemp = sConfigPath + "/config.conf.tem";
ofstream configfile(sFileTemp.c_str());
if(!configfile.good())
{
cerr << "cannot create configuration file: "<<sConfigFile<< endl;
return false;
}
configfile <<TC_Common::replace(tConf.tostr(),"\\s"," ") << endl;
configfile.close();
string sFileBak = sConfigFile+"."+TC_Common::now2str() + ".bak";
if(TC_File::isFileExist(sConfigFile))
{
TC_File::copyFile(sConfigFile,sFileBak,true);
}
TC_File::copyFile(sFileTemp,sConfigFile,true);
TC_File::removeFile(sFileTemp,false);
return true;
}
catch(exception &e)
{
cerr<< "load config erro:" <<e.what()<< endl;
}
return false;
}
void monitorNode(const string &configFile)
{
TC_Config conf;
conf.parseFile(configFile);
string nodeObj = "AdminObj@"+conf["/tars/application/server<local>"];
ServantPrx prx = CommunicatorFactory::getInstance()->getCommunicator()->stringToProxy<ServantPrx>(nodeObj);
prx->tars_ping();
}
void parseConfig(int argc, char *argv[])
{
TC_Option tOp;//consider
tOp.decode(argc, argv);
if (tOp.hasParam("nodeversion"))
{
cout << "Node:" << TARS_VERSION << "_" << NODE_VERSION << endl;
exit(0);
}
if (tOp.hasParam("monitor"))
{
try
{
string configFile = tOp.getValue("config");
monitorNode(configFile);
}
catch (exception &ex)
{
cout << "failed:" << ex.what() << endl;
exit(-1);
}
exit(0);
return;
}
string sLocator = tOp.getValue("locator");
string sNodeId = tOp.getValue("nodeid");
string sConfigFile = tOp.getValue("config");
string sRegistryObj = tOp.getValue("registryObj");
string sLocalIp = tOp.getValue("localip");
if(sConfigFile == "")
{
cerr << endl;
cerr <<"start server with locator config, for example: "<<endl;
cerr << argv[0] << " --locator=tars.tarsregistry.QueryObj@tcp -h 172.25.38.67 -p 17890" " --config=config.conf [--nodeid = 172.25.38.67 --registryObj=tars.tarsregistry.RegistryObj]" << endl;
cerr << argv[0] << " --config=config.conf" << endl;
exit(0);
}
if(!TC_File::isAbsolute(sConfigFile))
{
char sCwd[PATH_MAX];
if ( getcwd( sCwd, PATH_MAX ) == NULL )
{
TLOGERROR("cannot get the current directory:\n" << endl);
exit( 0 );
}
sConfigFile = string(sCwd) +"/"+ sConfigFile;
}
sConfigFile = TC_File::simplifyDirectory(sConfigFile);
if(sLocator != "")
{
if(sRegistryObj == "")
{
sRegistryObj = "tars.tarsregistry.RegistryObj";
}
bool bCloseOut=true;
if(tOp.hasParam("closecout"))
{
if(TC_Common::lower(tOp.getValue("closecout"))=="n")
{
bCloseOut = false;
}
}
bool bRet = getConfig(sLocator,sRegistryObj,sNodeId, sLocalIp, sConfigFile, bCloseOut);
if(bRet == false)
{
cerr<<"reload config erro. use old config";
}
}
cout <<endl;
map<string,string> mOp = tOp.getMulti();
for(map<string,string>::const_iterator it = mOp.begin(); it != mOp.end(); ++it)
{
cout << outfill( it->first)<< it->second << endl;
}
}
int main( int argc, char* argv[] )
{
try
{
TC_Common::daemon();
parseConfig(argc,argv);
g_pconf = &g_app.getConfig();
g_app.main( argc, argv );
g_app.waitForShutdown();
}
catch ( exception& ex )
{
cout<< ex.what() << endl;
}
catch ( ... )
{
cout<< "main unknow exception cathed" << endl;
}
return 0;
}