Skip to content

Commit d684600

Browse files
committed
add tars-client
1 parent 3d5072c commit d684600

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

tars-client/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
include_directories(${CMAKE_SOURCE_DIR}/tarscpp/util/include)
3+
include_directories(${CMAKE_SOURCE_DIR}/tarscpp/util/src/epoll_windows)
4+
include_directories(${CMAKE_SOURCE_DIR}/tarscpp/servant/protocol)
5+
6+
set(MODULE tars-client)
7+
8+
aux_source_directory(. DIR_SRC)
9+
10+
add_executable(${MODULE} ${DIR_SRC})
11+
12+
add_dependencies(${MODULE} tarsservant tarsutil)
13+
14+
target_link_libraries(${MODULE} tarsservant tarsutil)
15+
16+
if(TARS_SSL)
17+
target_link_libraries(${MODULE} tarsservant tarsutil ${LIB_SSL} ${LIB_CRYPTO})
18+
19+
if(WIN32)
20+
target_link_libraries(${MODULE} Crypt32)
21+
endif()
22+
endif()
23+
24+
if(TARS_HTTP2)
25+
target_link_libraries(${MODULE} ${LIB_HTTP2})
26+
endif()
27+
28+
target_link_libraries(tars-client ${LIB_MYSQL})
29+
30+
install(TARGETS tars-client DESTINATION deploy)

tars-client/TarsClient.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#include "TarsClient.h"
3+
#include "util/tc_port.h"
4+
5+
TarsClient::TarsClient(TC_Option &option) : _option(option)
6+
{
7+
_comm = new Communicator();
8+
9+
string locator;
10+
11+
if(option.hasParam("TARS_REGISTRY_HOST"))
12+
{
13+
locator = option.getValue("TARS_REGISTRY_HOST");
14+
}
15+
else
16+
{
17+
locator = TC_Port::getEnv("TARS_REGISTRY_HOST");
18+
}
19+
20+
if(locator.empty())
21+
{
22+
locator = "tars.tarsregistry.QueryObj@tcp -h 127.0.0.1 -p 17890";
23+
}
24+
else
25+
{
26+
locator = "tars.tarsregistry.QueryObj@tcp -h " + locator + " -p 17890";
27+
}
28+
_comm->setProperty("locator", locator);
29+
}
30+
31+
void TarsClient::call(const string &command)
32+
{
33+
cout << "call:" << command << endl;
34+
_commands[command](this, _option);
35+
}
36+
37+
TarsClient::~TarsClient()
38+
{
39+
delete _comm;
40+
_comm = NULL;
41+
}

tars-client/TarsClient.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef TARS_CLIENT
2+
#define TARS_CLIENT
3+
4+
#include "servant/Application.h"
5+
#include "framework/AdminReg.h"
6+
#include <functional>
7+
8+
9+
class TarsClient
10+
{
11+
public:
12+
typedef std::function<void(TarsClient*, TC_Option&)> command_function;
13+
14+
TarsClient(TC_Option &option);
15+
16+
~TarsClient();
17+
18+
void add(const string &command, command_function func)
19+
{
20+
_commands[command] = func;
21+
}
22+
23+
Communicator *getCommunicator() { return _comm; }
24+
25+
void call(const string &command);
26+
27+
protected:
28+
29+
TC_Option _option;
30+
31+
map<string, command_function> _commands;
32+
33+
Communicator *_comm;
34+
};
35+
36+
37+
#endif

tars-client/command/ping.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef CMD_PING
2+
#define CMD_PING
3+
4+
#include "util/tc_option.h"
5+
#include "TarsClient.h"
6+
7+
using namespace tars;
8+
9+
void ping(TarsClient* tarsClient, TC_Option& option)
10+
{
11+
string obj = option.getValue("obj");
12+
13+
cout << "ping:" << obj << endl;
14+
15+
try
16+
{
17+
ServantPrx prx = tarsClient->getCommunicator()->stringToProxy<ServantPrx>(obj);
18+
prx->tars_ping();
19+
cout << "ping succ" << endl;
20+
}
21+
catch(exception &ex)
22+
{
23+
cout << "ping err:" << ex.what() << endl;
24+
}
25+
}
26+
27+
#endif

tars-client/command/start.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef CMD_PING
2+
#define CMD_PING
3+
4+
#include "util/tc_option.h"
5+
#include "TarsClient.h"
6+
7+
using namespace tars;
8+
9+
void start(TarsClient* tarsClient, TC_Option& option)
10+
{
11+
string app = option.getValue("app");
12+
string serverName = option.getValue("server-name");
13+
string nodeName = option.getValue("node-name");
14+
15+
cout << "start:" << app << "." << serverName << endl;
16+
17+
AdminRegPrx adminPrx = tarsClient->getCommunicator()->stringToProxy<AdminRegPrx>("tars.tarsAdminRegistry.AdminRegObj");
18+
19+
try
20+
{
21+
// if(nodeName.empty())
22+
// {
23+
// adminPrx->getAllServerIds();
24+
// }
25+
26+
// ServantPrx prx = tarsClient->getCommunicator()->stringToProxy<ServantPrx>(obj);
27+
// prx->tars_ping();
28+
cout << "start succ" << endl;
29+
}
30+
catch(exception &ex)
31+
{
32+
cout << "start err:" << ex.what() << endl;
33+
}
34+
}
35+
36+
#endif

tars-client/main.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Tencent is pleased to support the open source community by making Tars available.
3+
*
4+
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
7+
* in compliance with the License. You may obtain a copy of the License at
8+
*
9+
* https://opensource.org/licenses/BSD-3-Clause
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed
12+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
14+
* specific language governing permissions and limitations under the License.
15+
*/
16+
17+
#include "util/tc_mysql.h"
18+
#include "util/tc_option.h"
19+
#include "util/tc_file.h"
20+
#include "util/tc_common.h"
21+
#include "util/tc_config.h"
22+
#include <iostream>
23+
#include <string>
24+
#include "TarsClient.h"
25+
#include "command/ping.h"
26+
27+
using namespace tars;
28+
using namespace std;
29+
30+
int main(int argc, char *argv[])
31+
{
32+
TC_Option option;
33+
34+
try
35+
{
36+
option.decode(argc, argv);
37+
38+
TarsClient tarsClient(option);
39+
40+
tarsClient.add("ping", ping);
41+
tarsClient.add("start", TarsClient::command_function());
42+
tarsClient.add("stop", TarsClient::command_function());
43+
tarsClient.add("restart", TarsClient::command_function());
44+
tarsClient.add("patch", TarsClient::command_function());
45+
46+
tarsClient.call("ping");
47+
}
48+
catch(exception &ex)
49+
{
50+
cout << "error: " << ex.what() << endl;
51+
exit(-1);
52+
}
53+
54+
return 0;
55+
}
56+
57+

0 commit comments

Comments
 (0)