reload.cpp 3.71 KB

#include <lib_include.h>
#include <lib_file_ini.h>
enum E_RELOAD_CMD{
	e_reload_cmd_min = 1,
	e_reload_cmd_notice = 1,//发公告		
	e_reload_cmd_mail = 2,//发送邮件	
	e_reload_cmd_offline = 3,//T人下线
	e_reload_cmd_max,
};

static struct sockaddr_in mcast_addr;
static int mcast_fd;
static const char *opt_string = "c:hv:";

static int connect_to_broadcaster(const char* mcast_ip, int mcast_port, const char* outgoing_ip, const char* outgoing_interface)
{
	mcast_fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (mcast_fd == -1) {
		printf("Failed to Create `mcast_fd`: err=%d %s\n", errno, strerror(errno));
		return -1;
	}
	memset(&mcast_addr, 0, sizeof(mcast_addr));
	mcast_addr.sin_family = AF_INET;
	inet_pton(AF_INET, mcast_ip, &(mcast_addr.sin_addr));
	mcast_addr.sin_port = htons(mcast_port);
									
	int on = 1;
	setsockopt(mcast_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
	
	in_addr_t ipaddr;
	inet_pton(AF_INET, outgoing_ip, &ipaddr);
	if (setsockopt(mcast_fd, IPPROTO_IP, IP_MULTICAST_IF, &ipaddr, sizeof ipaddr) == -1) {
		printf("Failed to Set Outgoing Interface: err=%d %s %s\n",
			errno, strerror(errno), outgoing_ip);
		return -1;
	}

	if (bind(mcast_fd, (struct sockaddr*)&mcast_addr, sizeof mcast_addr) == -1) {
		printf("Failed to Bind `mcast_fd`: err=%d %s\n", errno, strerror(errno));
		return -1;
	}

	struct group_req req;
	req.gr_interface = if_nametoindex(outgoing_interface);
	memcpy(&req.gr_group, &mcast_addr, sizeof mcast_addr);
	if (setsockopt(mcast_fd, IPPROTO_IP, MCAST_JOIN_GROUP, &req, sizeof req) == -1) {
		printf("Failed to Join Mcast Grp: err=%d %s\n", errno, strerror(errno));
		return -1;
	}

	return 0;
}

int send_to_broadcaster(std::string cmd, std::string val)
{
	std::string send_data;
	send_data += cmd;
	if (!val.empty()){
		send_data +="," + val;
	}
	return sendto(mcast_fd, send_data.c_str(), (int)send_data.size(), 0, (sockaddr*)&mcast_addr, sizeof(mcast_addr));
}

void display_usage()
{
    printf("-c  define the command\n");
	printf("    1,  e_reload_cmd_notice = 1,发公告	\n");
	printf("    2,  e_reload_cmd_mail = 2,//发送邮件	\n");
	printf("    3,  e_reload_cmd_offline = 3,//T人下线	\n");
    printf("-v  command's value\n");
    printf("    1,xxxxxx\n");
	printf("    2,server_id[0:all],mail_id\n");
	printf("    3,server_id, uid\n");
    printf("-h  display this message\n");
}

int main(int argc, char* argv[])
{
	el::lib_file_ini_t ini;
	ini.load("./reload.ini");

	std::string mcast_ip;
	int mcast_port = 0;
	std::string outgoing_ip;
	std::string outgoing_interface;
	
	if (SUCC != ini.get_val(mcast_ip, "mcast", "ip")){
		printf("error[]");
		return 0;
	}
	if (SUCC != ini.get_val(mcast_port, "mcast", "port")){
		printf("error[]");
		return 0;
	}
	if (SUCC != ini.get_val(outgoing_ip, "mcast", "outgoing_ip")){
		printf("error[]");
		return 0;
	}
	if (SUCC != ini.get_val(outgoing_interface, "mcast", "outgoing_interface")){
		printf("error[]");
		return 0;
	}

    //int i = 0;
	std::string val;
	std::string cmd;
    int oc ;
    while( (oc = getopt(argc, argv, opt_string)) != -1) {
        switch( oc ) {
			case 'c':{
					cmd = optarg;
					printf("cmd:%s\n",cmd.c_str());
				}
			break;
            case 'v':{
					val = optarg;
					printf("value:%s\n",val.c_str());
				}
                break;
            case 'h':
                display_usage();
                break;
            default:
                break;
        }
    }

	uint32_t cmd_id = atoi(cmd.c_str());
	if (cmd_id < e_reload_cmd_min || e_reload_cmd_max <= cmd_id){
		printf("no reload cmd:%u", cmd_id);
		return 0;
	}

	int ret = connect_to_broadcaster(mcast_ip.c_str(), mcast_port, outgoing_ip.c_str(), outgoing_interface.c_str());
	if (0 != ret){
		return 0;
	}

	ret = send_to_broadcaster(cmd, val);
    printf("send ret:%d\n",ret);
	return ret;
}