Beispiel #1
0
static int kr_server_try_connect_to_cluster(void)
{
    if (krserver.coordport != 0) {
        krserver.cofd = kr_net_tcp_connect(krserver.neterr, \
            krserver.coordip, krserver.coordport);
        if (krserver.cofd == KR_NET_ERR) {
            KR_LOG(KR_LOGERROR, "kr_net_tcp_connect[%s][%d] failed[%s]",
                krserver.coordip, krserver.coordport, krserver.neterr);
            return -1;
        }
    }
    
    if (krserver.cofd < 0) {
        KR_LOG(KR_LOGERROR, "Can't connect to coordinator in cluster!");
        return -1;
    }
    
    /* write svron message to the cluster */
    if (kr_server_handle_svron() != 0) {
        KR_LOG(KR_LOGERROR, "kr_server_handle_svron cluster!");
        return -1;
    }
    
    /* Register read file event */
    if (kr_event_file_create(krserver.krel, krserver.cofd, KR_EVENT_READABLE, \
        kr_server_message_read_handler, NULL) == KR_NET_ERR) {
        KR_LOG(KR_LOGERROR, "kr_event_file_create failed[%s]", krserver.neterr);
        return -1;
    }
    
    return 0;
}
Beispiel #2
0
static void kr_coordi_common_accept_handler(int fd) 
{
    char ip[32];
    int port;
    
    kr_net_peer_to_string(fd, ip, &port);
    printf("Connection From: fd:%d ip:%s port:%d\n", fd, ip, port);
    
    if (fd != -1) {
        kr_net_nonblock(NULL,fd);
        kr_net_tcp_nodelay(NULL,fd);
        if (kr_event_file_create(krcoordi.el, fd, KR_EVENT_READABLE,
            kr_coordi_read_handler, NULL) == KR_NET_ERR)
        {
            KR_LOG(KR_LOGERROR, "kr_event_file_create error!");
            close(fd);
            return;
        }
    }
}
Beispiel #3
0
int kr_server_initialize(void)
{
    int ret = 0;
    
    /* signal handling */
    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    kr_server_sigterm_setup();
    
    /* Daemonize */
    if (krserver.daemonize) {
        kr_server_daemonize();
    }
    
    /* Create event loop */
    krserver.krel = kr_event_loop_create(krserver.maxevents);

    /* Register server cron time event */
    kr_event_time_create(krserver.krel, 1, kr_server_cron, NULL, NULL);
    
    /* Start up krengine */
    krserver.krengine = kr_engine_startup(
            krserver.dbname, krserver.dbuser, krserver.dbpass,
            krserver.logpath, krserver.serverid, krserver.loglevel,
            krserver.krdbmodule, krserver.datamodule, krserver.rulemodule,
            krserver.hdicachesize, krserver.threadcnt, krserver.hwm,
            krserver.krel);
    if (krserver.krengine == NULL) {
        KR_LOG(KR_LOGERROR, "kr_engine_startup failed!\n");
        return -1;
    }
        
    /* Becoming a server */
    if (krserver.tcpport != 0) {
        krserver.ipfd = kr_net_tcp_server(krserver.neterr, \
            krserver.tcpport, krserver.tcpbindaddr);
        if (krserver.ipfd == KR_NET_ERR) {
            KR_LOG(KR_LOGERROR, "kr_net_tcp_server[%d] failed[%s]",
                krserver.tcpport, krserver.neterr);
            return -1;
        }
    }
    if (krserver.ipfd < 0) {
        KR_LOG(KR_LOGERROR, "tcpport [%d] uncorrect!", krserver.tcpport);
        return -1;
    }
    
    /* Register tcp accept file event */
    ret = kr_event_file_create(krserver.krel, krserver.ipfd, 
            KR_EVENT_READABLE,  kr_server_tcp_accept_handler, NULL);
    if (ret == KR_NET_ERR) {
        KR_LOG(KR_LOGERROR, "kr_event_file_create tcp error");
        return -1;
    }
    
    /* Serve as part of cluster */
    if (krserver.clustermode != 0) {
        if (kr_server_connect_to_cluster() != 0) {
            KR_LOG(KR_LOGERROR, "kr_server_connect_to_cluster failed");
            return -1;
        }
    }
    
    return 0;
}