Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
remove cli commands registry of c api
Browse files Browse the repository at this point in the history
Summary: Ref T10562

Test Plan: N/A

Reviewers: qinzuoyan, cailiuyang

Reviewed By: cailiuyang

Subscribers: #pegasus

Maniphest Tasks: T10562

Differential Revision: https://phabricator.d.xiaomi.net/D81804
  • Loading branch information
shengofsun committed Feb 2, 2018
1 parent a55bdca commit 87194db
Show file tree
Hide file tree
Showing 27 changed files with 448 additions and 919 deletions.
83 changes: 0 additions & 83 deletions include/dsn/c/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,88 +89,6 @@ extern DSN_API const char *dsn_error_to_string(dsn_error_t err);
extern DSN_API dsn_error_t dsn_error_from_string(const char *s, dsn_error_t default_err);
/*@}*/

/*!
@defgroup app-cli Command-Line Interface (cli)
@ingroup service-api-utilities
Built-in command line interface that can be accessed via local/tcp/http consoles.
@{
*/

/*!
run a given cli command
\param command_line given command line.
\return null if it fails, else execution response
*/
extern DSN_API const char *dsn_cli_run(const char *command_line);

/*!
free memory occupied by cli response
\param command_output result from \ref dsn_cli_run
*/
extern DSN_API void dsn_cli_free(const char *command_output);

/*! cli response definition */
typedef struct dsn_cli_reply
{
const char *message; ///< zero-ended reply message
uint64_t size; ///< message_size
void *context; ///< context for free_handler
} dsn_cli_reply;

/*! cli request handler definition */
typedef void (*dsn_cli_handler)(void *context, ///< context registered by \ref dsn_cli_register
int argc, ///< argument count
const char **argv, ///< arguments
/*out*/ dsn_cli_reply *reply ///< reply message
);

/*! cli response resource gc handler definition */
typedef void (*dsn_cli_free_handler)(dsn_cli_reply reply);

/*!
register a customized cli command handler
\param command command name
\param help_one_line one line help information
\param help_long long help information
\param context context used by \ref cmd_handler
\param cmd_handler command handler
\param output_freer command result resource free handler
\return the handle of this registered command
*/
extern DSN_API dsn_handle_t dsn_cli_register(const char *command,
const char *help_one_line,
const char *help_long,
void *context,
dsn_cli_handler cmd_handler,
dsn_cli_free_handler output_freer);

/*!
same as \ref dsn_cli_register, except that the command
name is auto-augmented by rDSN as $app_full_name.$command
*/
extern DSN_API dsn_handle_t dsn_cli_app_register(const char *command,
const char *help_one_line,
const char *help_long,
void *context,
dsn_cli_handler cmd_handler,
dsn_cli_free_handler output_freer);

/*!
remove a cli handler
\param cli_handle handle of the cli returned from \ref dsn_cli_register or \ref
dsn_cli_app_register
*/
extern DSN_API void dsn_cli_deregister(dsn_handle_t cli_handle);
/*@}*/

/*!
@defgroup config Configuration Service
@ingroup service-api-utilities
Expand Down Expand Up @@ -366,7 +284,6 @@ extern DSN_API uint64_t dsn_crc64_concatenate(uint32_t xy_init,
uint64_t y_init,
uint64_t y_final,
size_t y_size);
/*@}*/

/*!
@defgroup memory Memory Management
Expand Down
58 changes: 0 additions & 58 deletions include/dsn/tool-api/command.h

This file was deleted.

105 changes: 105 additions & 0 deletions include/dsn/tool-api/command_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#pragma once

#include <dsn/utility/synchronize.h>
#include <dsn/utility/singleton.h>
#include <dsn/tool-api/rpc_message.h>
#include <map>

namespace dsn {

class command_manager : public ::dsn::utils::singleton<command_manager>
{
public:
typedef std::function<std::string(const std::vector<std::string> &)> command_handler;
command_manager();

dsn_handle_t register_command(const std::vector<std::string> &commands,
const std::string &help_one_line,
const std::string &help_long,
command_handler handler);
dsn_handle_t register_app_command(const std::vector<std::string> &commands,
const std::string &help_one_line,
const std::string &help_long,
command_handler handler);
void deregister_command(dsn_handle_t handle);

bool run_command(const std::string &cmdline, /*out*/ std::string &output);
void start_remote_cli();
void on_remote_cli(dsn_message_t req);
void set_cli_target_address(dsn_handle_t handle, dsn::rpc_address address);

private:
bool run_command(const std::string &cmd,
const std::vector<std::string> &args,
/*out*/ std::string &output);

private:
struct command_instance
{
dsn::rpc_address address;
std::vector<std::string> commands;
std::string help_short;
std::string help_long;
command_handler handler;
};

::dsn::utils::rw_lock_nr _lock;
std::map<std::string, command_instance *> _handlers;
std::vector<command_instance *> _commands;
};
}

#define UNREGISTER_VALID_HANDLER(ptr) \
do { \
if (ptr != nullptr) { \
dsn::command_manager::instance().deregister_command(ptr); \
ptr = nullptr; \
} \
} while (0)

// handle bool-flag "flag" with "args": if args are empty, then return the old flag; otherwise
// set the proper "flag" according to args
#define HANDLE_CLI_FLAGS(flag, args) \
do { \
std::string ret_msg("OK"); \
if (args.size() <= 0) { \
ret_msg = flag ? "true" : "false"; \
} else { \
if (strcmp(args[0].c_str(), "true") == 0) { \
flag = true; \
ddebug("set " #flag " to true by remote command"); \
} else if (strcmp(args[0].c_str(), "false") == 0) { \
flag = false; \
ddebug("set " #flag " to false by remote command"); \
} else { \
ret_msg = "ERR: invalid arguments"; \
} \
} \
return ret_msg; \
} while (0)
Loading

0 comments on commit 87194db

Please sign in to comment.