#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <argp.h>
#include <zeromq/zmq.h>
#include "log_api.h"
struct mconfig_args
{
char *module;
char *cmd;
char *para;
};
const char *argp_program_version = "V1.0";
const char *argp_program_bug_address = "<kaishi@jd-energy.com.cn>";
static struct argp_option mconfig_options[] = {
{"module", 'm', "MODULE", 0, "ZMQ communication module name."},
{"command", 'c', "COMMAND", 0, "Module function command.\n[log_level] \n[log_filter]"},
{"params", 'p', "PARAMS", 0, "Command line params."},
{0}
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct mconfig_args *args = state->input;
switch (key)
{
case 'm':
args->module = arg;
break;
case 'c':
args->cmd = arg;
break;
case 'p':
args->para = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp mconfig_argp = {mconfig_options, parse_opt,
"./eLinkTools -m eLink2000_power_dispatch -c log_level -p e\n \
./eLinkTools -c SUB -e \"tcp://127.0.0.1:5011\" -t \"00 strategy\"\n"};
/**
* @fn main
* @brief 命令行输入模块
* @param[in] argc - 命令行个数
* @param[in] argv - 命令行参数
* @return
* 0 - 成功
* -1 - 失败
**/
int main(int argc, char *argv[])
{
char buf[128] = {0};
struct mconfig_args cmdline = {0};
argp_parse(&mconfig_argp, argc, argv, 0, 0, &cmdline);
snprintf(buf, sizeof(buf), "ipc://%s/command", IPC_BASE_PATH);
void *context = zmq_ctx_new();
void *pub_handle = zmq_socket(context, ZMQ_PUB);
int ret = zmq_bind(pub_handle, buf);
assert(ret == 0);
usleep(200 * 1000); //延时等待订阅者连接
snprintf(buf, sizeof(buf), "%s %s %s", cmdline.module, cmdline.cmd, cmdline.para);
printf("send: %s\n", buf);
ret = zmq_send(pub_handle, buf, strlen(buf), 0);
assert(ret == strlen(buf));
zmq_close(pub_handle);
zmq_ctx_destroy(context);
return 0;
}