Skip to content

Commit

Permalink
add tag for client
Browse files Browse the repository at this point in the history
tag is infomation of clients' pid/ipv4addr
use gtest
  • Loading branch information
张晨 authored and 张晨 committed May 23, 2019
1 parent 17cce22 commit 899ec8b
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 94 deletions.
41 changes: 12 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endif()
include (${CUSTOM_CMAKE_MODULES}/common.mk)

option(BUILD_DEBUG "debug or release" OFF)
option(BUILD_TEST "build unit-tests" OFF)
option(BUILD_TEST "build tests" OFF)

function(parseLogLevel varName)
if (NOT ${varName})
Expand Down Expand Up @@ -184,37 +184,20 @@ install(FILES ${flora_headers} DESTINATION include)

# unit-tests
if (BUILD_TEST)
file(GLOB flora_unit_test_SOURCES
unit-tests/*.h
unit-tests/*.cc
findPackage(gtest REQUIRED
HINTS ${gtestPrefix}
HEADERS gtest/gtest.h
STATIC_LIBS gtest
)
add_executable(flora-test
${flora_unit_test_SOURCES}
)
target_include_directories(flora-test
PRIVATE include ${mutils_INCLUDE_DIRS}
)
target_link_libraries(flora-test
flora-cli
flora-svc
${mutils_LIBRARIES}
)
set_target_properties(flora-test PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
install(TARGETS flora-test RUNTIME DESTINATION bin)

file(GLOB flora_demo_SOURCES
examples/*.h
examples/*.cc
)
add_executable(flora-demo
${flora_demo_SOURCES}
)
target_include_directories(flora-demo
PRIVATE include ${mutils_INCLUDE_DIRS}
add_executable(test test/main.cc test/simple.cc test/svc.h)
target_include_directories(test PRIVATE
include
${gtest_INCLUDE_DIRS}
${mutils_INCLUDE_DIRS}
)
target_link_libraries(flora-demo
target_link_libraries(test
flora-cli
flora-svc
${mutils_LIBRARIES}
${gtest_LIBRARIES}
)
endif(BUILD_TEST)
4 changes: 4 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Configuration:
Dependencies:
--mutils=DIR specify mutils install dir
--ncurses=DIR specify ncurses install dir
--gtest=DIR specify gtest install dir
Cross Compile:
--toolchain=DIR toolchain install dir
Expand Down Expand Up @@ -73,6 +74,9 @@ do
--ncurses=*)
CMAKE_ARGS=(${CMAKE_ARGS[@]} -DncursesPrefix=$conf_optarg)
;;
--gtest=*)
CMAKE_ARGS=(${CMAKE_ARGS[@]} -DgtestPrefix=$conf_optarg)
;;
--toolchain=*)
CMAKE_ARGS=(${CMAKE_ARGS[@]} -DTOOLCHAIN_HOME=$conf_optarg)
CROSS_COMPILE=yes
Expand Down
33 changes: 29 additions & 4 deletions include/flora-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ class ClientCallback {
class MonitorListItem {
public:
uint32_t id;
int32_t pid;
std::string name;
uint32_t flags;
uint64_t tag;
std::string name;
};

class MonitorSubscriptionItem {
Expand Down Expand Up @@ -194,6 +194,31 @@ class MonitorCallback {
virtual void disconnected() {}
};

class MsgSender {
public:
// return: 0 unix domain socket connection
// 1 tcp socket connection
static uint32_t connection_type();

// pid of msg sender, if connection type is unix domain socket connection
static pid_t pid();

// identify of msg sender
static uint64_t tag();

// ipv4 addr string, if connection type is tcp socket connection
static const char* ipaddr();

// ipv4 port, if connection type is tcp socket connection
static uint16_t port();

static const char* name();

// pid string, if connection type is unix domain socket connection
// ipaddr:port, if connection type is tcp socket connection
static void to_string(std::string& str);
};

} // namespace flora

extern "C" {
Expand Down Expand Up @@ -229,9 +254,9 @@ typedef void (*flora_call_callback_t)(int32_t rescode,
flora_call_result *result, void *arg);

typedef struct {
uint32_t id;
int32_t pid;
uint64_t tag;
const char *name;
uint32_t id;
} flora_cli_monitor_list_item;
typedef struct {
const char *name;
Expand Down
5 changes: 1 addition & 4 deletions monitor/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ static bool parseCmdline(int argc, char **argv, CmdlineArgs &res) {
class FloraClientInfo {
public:
uint32_t id = 0;
int32_t pid = 0;
string name;
uint32_t flags = 0;
};
Expand All @@ -45,7 +44,6 @@ class MonitorView : public flora::MonitorCallback {
while (it != items.end()) {
auto fcit = floraClients.emplace(floraClients.end());
fcit->id = it->id;
fcit->pid = it->pid;
fcit->name = it->name;
fcit->flags = it->flags;
++it;
Expand All @@ -56,7 +54,6 @@ class MonitorView : public flora::MonitorCallback {
void list_add(MonitorListItem &item) {
auto fcit = floraClients.emplace(floraClients.end());
fcit->id = item.id;
fcit->pid = item.pid;
fcit->name = item.name;
fcit->flags = item.flags;
updateMonitorScreen();
Expand Down Expand Up @@ -98,7 +95,7 @@ class MonitorView : public flora::MonitorCallback {

void printTabLine(FloraClientInfo &info) {
char buf[16];
snprintf(buf, sizeof(buf), "%d", info.pid);
// snprintf(buf, sizeof(buf), "%d", info.pid);
printTabCell(0, buf);
printTabCell(1, info.name.c_str());
printw("\n");
Expand Down
6 changes: 5 additions & 1 deletion src/adap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class AdapterInfo {
AdapterInfo() { id = ++idseq; }

uint32_t id;
int32_t pid = 0;
std::string name;
std::set<std::string> declared_methods;
uint32_t flags = 0;
Expand Down Expand Up @@ -53,6 +52,11 @@ class Adapter {
public:
uint32_t serialize_flags;
AdapterInfo *info = nullptr;
// unix socket adapter: pid
// tcp socket adapter:
// high 32 bits: 0x80000000 | ipv4port
// low 32 bits: ipv4addr
uint64_t tag = 0;
#ifdef FLORA_DEBUG
uint32_t recv_times = 0;
uint32_t recv_bytes = 0;
Expand Down
52 changes: 49 additions & 3 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ using namespace std;
using namespace std::chrono;
using rokid::Uri;

thread_local uint64_t flora::internal::Client::tag = 0;
thread_local string flora::internal::Client::sender_name;

static bool ignore_sigpipe = false;
int32_t flora::Client::connect(const char *uri, flora::ClientCallback *ccb,
flora::MonitorCallback *mcb,
Expand Down Expand Up @@ -229,7 +232,8 @@ bool Client::handle_cmd_after_auth(int32_t cmd, shared_ptr<Caps> &resp) {
string name;
shared_ptr<Caps> args;

if (ResponseParser::parse_post(resp, name, msgtype, args) != 0) {
if (ResponseParser::parse_post(resp, name, msgtype, args, tag,
sender_name) != 0) {
return false;
}
if (cli_callback) {
Expand All @@ -241,7 +245,8 @@ bool Client::handle_cmd_after_auth(int32_t cmd, shared_ptr<Caps> &resp) {
string name;
int32_t msgid;
shared_ptr<Caps> args;
if (ResponseParser::parse_call(resp, name, args, msgid) != 0) {
if (ResponseParser::parse_call(resp, name, args, msgid, tag,
sender_name) != 0) {
return false;
}
if (cli_callback) {
Expand All @@ -258,7 +263,7 @@ bool Client::handle_cmd_after_auth(int32_t cmd, shared_ptr<Caps> &resp) {
PendingRequestList::iterator it;
Response response;

if (ResponseParser::parse_reply(resp, msgid, rescode, response) != 0) {
if (ResponseParser::parse_reply(resp, msgid, rescode, response, tag) != 0) {
KLOGW(TAG, "parse reply failed");
return false;
}
Expand Down Expand Up @@ -733,6 +738,47 @@ void ReplyImpl::end(int32_t code, std::shared_ptr<Caps> &data) {
} // namespace internal
} // namespace flora

namespace flora {
uint32_t MsgSender::connection_type() {
return flora::internal::TagHelper::type(flora::internal::Client::tag);
}

pid_t MsgSender::pid() {
return flora::internal::TagHelper::pid(flora::internal::Client::tag);
}

uint64_t MsgSender::tag() {
return flora::internal::Client::tag;
}

const char* MsgSender::ipaddr() {
return flora::internal::TagHelper::ipaddr(flora::internal::Client::tag);
}

uint16_t MsgSender::port() {
return flora::internal::TagHelper::port(flora::internal::Client::tag);
}

const char* MsgSender::name() {
return flora::internal::Client::sender_name.c_str();
}

void MsgSender::to_string(string& str) {
str = "[";
if (MsgSender::connection_type() == 0) {
char buf[16];
snprintf(buf, sizeof(buf), "%d", MsgSender::pid());
str += buf;
} else {
string tmp;
flora::internal::TagHelper::to_addr_string(flora::internal::Client::tag, tmp);
str += tmp;
}
str += "]";
str += MsgSender::name();
}
} // namespace flora

using flora::Reply;
using flora::Response;
using flora::internal::Client;
Expand Down
2 changes: 2 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class Client : public flora::Client {
public:
std::string auth_extra;
ClientCallback *cli_callback = nullptr;
static thread_local uint64_t tag;
static thread_local std::string sender_name;
#ifdef FLORA_DEBUG
uint32_t post_times = 0;
uint32_t post_bytes = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#define FLORA_VERSION 3
#define FLORA_VERSION 4

// client --> server
#define CMD_AUTH_REQ 0
Expand Down
Loading

0 comments on commit 899ec8b

Please sign in to comment.