📄 client_lib.c
字号:
struct ha_msg* reply; int rc; const char * result; char csignal[32]; llc_private_t* pi; ClearLog(); if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "hb_api_setsignal: bad cinfo"); return HA_FAIL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return HA_FAIL; } if ((request = hb_api_boilerplate(API_SETSIGNAL)) == NULL) { ha_api_log(LOG_ERR, "hb_api_setsignal: can't create msg"); return HA_FAIL; } snprintf(csignal, sizeof(csignal), "%d", nsig); if (ha_msg_add(request, F_SIGNAL, csignal) != HA_OK) { ha_api_log(LOG_ERR, "hb_api_setsignal: cannot add field/2"); ZAPMSG(request); return HA_FAIL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ha_api_perror("can't send message to IPC Channel"); ZAPMSG(request); return HA_FAIL; } ZAPMSG(request); /* Read reply... */ if ((reply=read_api_msg(pi)) == NULL) { return HA_FAIL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0) { rc = HA_OK; }else{ rc = HA_FAIL; } ZAPMSG(reply); return rc;}/* * Retrieve the list of nodes in the cluster. */static intget_nodelist(llc_private_t* pi){ struct ha_msg* request; struct ha_msg* reply; const char * result = NULL; struct stringlist* sl; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return HA_FAIL; } if ((request = hb_api_boilerplate(API_NODELIST)) == NULL) { ha_api_log(LOG_ERR, "get_nodelist: can't create msg"); return HA_FAIL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("can't send message to IPC Channel"); return HA_FAIL; } ZAPMSG(request); /* Loop as long as we get an API_MORE result */ /* The final node will (hopefully) have an API_OK result type */ while ((reply=read_api_msg(pi)) != NULL && (result = ha_msg_value(reply, F_APIRESULT)) != NULL && (strcmp(result, API_MORE) == 0 || strcmp(result, API_OK) == 0) && (sl = new_stringlist(ha_msg_value(reply, F_NODENAME))) != NULL){ sl->next = pi->nodelist; pi->nodelist = sl; if (strcmp(result, API_OK) == 0) { pi->nextnode = pi->nodelist; ZAPMSG(reply); return(HA_OK); } ZAPMSG(reply); } if (reply != NULL) { zap_nodelist(pi); ZAPMSG(reply); } if (reply == NULL) { ha_api_log(LOG_ERR, "General read_api_msg() failure"); }else if (result == NULL) { ha_api_log(LOG_ERR, "API reply missing " F_APIRESULT " field."); }else if (strcmp(result, API_MORE) != 0 && strcmp(result, API_OK) != 0) { ha_api_log(LOG_ERR, "Unexpected API result value: [%s]", result); }else if (ha_msg_value(reply, F_NODENAME) == NULL) { ha_api_log(LOG_ERR, "No nodename in API reply"); }else{ ha_api_log(LOG_ERR, "new_stringlist() failure."); } return HA_FAIL;}/* * Retrieve the list of interfaces for the given host. */static intget_iflist(llc_private_t* pi, const char *host){ struct ha_msg* request; struct ha_msg* reply; const char * result; struct stringlist* sl; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return HA_FAIL; } if ((request = hb_api_boilerplate(API_IFLIST)) == NULL) { ha_api_log(LOG_ERR, "get_iflist: can't create msg"); return HA_FAIL; } if (ha_msg_add(request, F_NODENAME, host) != HA_OK) { ha_api_log(LOG_ERR, "get_iflist: cannot add field"); ZAPMSG(request); return HA_FAIL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return HA_FAIL; } ZAPMSG(request); /* Loop as long as we get an API_MORE result */ /* The final interface will (hopefully) have an API_OK result type */ while ((reply=read_api_msg(pi)) != NULL && (result = ha_msg_value(reply, F_APIRESULT)) != NULL && (strcmp(result, API_MORE) == 0 || strcmp(result, API_OK) == 0) && (sl = new_stringlist(ha_msg_value(reply, F_IFNAME))) != NULL){ sl->next = pi->iflist; pi->iflist = sl; if (strcmp(result, API_OK) == 0) { pi->nextif = pi->iflist; ZAPMSG(reply); return(HA_OK); } ZAPMSG(reply); } if (reply != NULL) { zap_iflist(pi); ZAPMSG(reply); } return HA_FAIL;}/* * Return the status of the given node. */static const char *get_nodestatus(ll_cluster_t* lcl, const char *host){ struct ha_msg* request; struct ha_msg* reply; const char * result; const char * status; static char statbuf[128]; const char * ret; llc_private_t* pi; ClearLog(); if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_nodestatus: bad cinfo"); return NULL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return NULL; } if ((request = hb_api_boilerplate(API_NODESTATUS)) == NULL) { return NULL; } if (ha_msg_add(request, F_NODENAME, host) != HA_OK) { ha_api_log(LOG_ERR, "get_nodestatus: cannot add field"); ZAPMSG(request); return NULL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return NULL; } ZAPMSG(request); /* Read reply... */ if ((reply=read_api_msg(pi)) == NULL) { return NULL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0 && (status = ha_msg_value(reply, F_STATUS)) != NULL) { memset(statbuf, 0, sizeof(statbuf)); strncpy(statbuf, status, sizeof(statbuf) - 1); ret = statbuf; }else{ ret = NULL; } ZAPMSG(reply); return ret;}/** Return the status of the given client.*/static const char *get_clientstatus(ll_cluster_t* lcl, const char *host, const char *clientid, int timeout){ struct ha_msg* request; struct ha_msg* reply; const char * result; llc_private_t* pi; static char statbuf[128]; const char * clientname; const char * ret; ClearLog(); if (!ISOURS(lcl)){ ha_api_log(LOG_ERR,"get_clientstatus: bad cinfo"); return NULL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn){ ha_api_log(LOG_ERR,"not signed on"); return NULL; } clientname = (clientid == NULL) ? OurClientID : clientid; /* If host is NULL, user choose the callback method to * get the result. This also implies timeout is useless */ if (host == NULL) { struct ha_msg * m = NULL; if ((m = ha_msg_new(0)) == NULL || ha_msg_add(m, F_TYPE, T_QCSTATUS) != HA_OK || ha_msg_add(m, F_CLIENTNAME, clientname) != HA_OK || ha_msg_add(m, F_FROMID, OurClientID) != HA_OK) { if (m){ ha_msg_del(m); } ha_log(LOG_ERR, "%s: cannot add field", __FUNCTION__); return NULL; } if (sendclustermsg(lcl, m) != HA_OK) { ha_log(LOG_ERR, "%s: sendclustermsg fail",__FUNCTION__); } ha_msg_del(m); return NULL; } if (*host == EOS) { ha_api_log(LOG_ERR, "client status : bad nodename"); return NULL; } if ((request = hb_api_boilerplate(API_CLIENTSTATUS)) == NULL) { ha_api_log(LOG_ERR, "hb_api_boilerplate failed"); return NULL; } if (ha_msg_add(request, F_NODENAME, host)!= HA_OK || ha_msg_add(request, F_CLIENTNAME, clientname)!= HA_OK) { ha_api_log(LOG_ERR, "get_clientstatus: cannot add message field"); ZAPMSG(request); return NULL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return NULL; } ZAPMSG(request); /* Read reply... */ if ((reply = read_cstatus_respond_msg(pi, timeout)) == NULL) { return NULL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0 && (result = ha_msg_value(reply, F_CLIENTSTATUS)) != NULL) { memset(statbuf, 0, sizeof(statbuf)); strncpy(statbuf, result, sizeof(statbuf) - 1); ret = statbuf; } else { ha_api_perror("received wrong type of msg"); ret = NULL; } ZAPMSG(reply); return ret;}/* * Return the type of the given node. */static const char *get_nodetype(ll_cluster_t* lcl, const char *host){ struct ha_msg* request; struct ha_msg* reply; const char * result; const char * status; static char statbuf[128]; const char * ret; llc_private_t* pi; ClearLog(); if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_nodetype: bad cinfo"); return NULL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return NULL; } if ((request = hb_api_boilerplate(API_NODETYPE)) == NULL) { return NULL; } if (ha_msg_add(request, F_NODENAME, host) != HA_OK) { ha_api_log(LOG_ERR, "get_nodetype: cannot add field"); ZAPMSG(request); return NULL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return NULL; } ZAPMSG(request); /* Read reply... */ if ((reply=read_api_msg(pi)) == NULL) { return NULL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0 && (status = ha_msg_value(reply, F_NODETYPE)) != NULL) { memset(statbuf, 0, sizeof(statbuf)); strncpy(statbuf, status, sizeof(statbuf) - 1); ret = statbuf; }else{ ret = NULL; } ZAPMSG(reply); return ret;}static char * get_parameter(ll_cluster_t* lcl, const char* pname){ struct ha_msg* request; struct ha_msg* reply; const char * result; const char * pvalue; char * ret; llc_private_t* pi; ClearLog(); if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_parameter: bad cinfo"); return NULL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return NULL; } if ((request = hb_api_boilerplate(API_GETPARM)) == NULL) { return NULL; } if (ha_msg_add(request, F_PNAME, pname) != HA_OK) { ha_api_log(LOG_ERR, "get_parameter: cannot add field"); ZAPMSG(request); return NULL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return NULL; } ZAPMSG(request); /* Read reply... */ if ((reply=read_api_msg(pi)) == NULL) { return NULL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0 && (pvalue = ha_msg_value(reply, F_PVALUE)) != NULL) { ret = ha_strdup(pvalue); }else{ ret = NULL; } ZAPMSG(reply); return ret;}static const char * get_resources(ll_cluster_t* lcl){ struct ha_msg* request; struct ha_msg* reply; const char * result; const char * rvalue; char * ret; llc_private_t* pi; ClearLog(); if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_resources: bad cinfo"); return NULL; } pi = (llc_private_t*)lcl->ll_cluster_private; if (!pi->SignedOn) { ha_api_log(LOG_ERR, "not signed on"); return NULL; } if ((request = hb_api_boilerplate(API_GETRESOURCES)) == NULL) { return NULL; } /* Send message */ if (msg2ipcchan(request, pi->chan) != HA_OK) { ZAPMSG(request); ha_api_perror("Can't send message to IPC Channel"); return NULL; } ZAPMSG(request); /* Read reply... */ if ((reply=read_api_msg(pi)) == NULL) { return NULL; } if ((result = ha_msg_value(reply, F_APIRESULT)) != NULL && strcmp(result, API_OK) == 0 && (rvalue = ha_msg_value(reply, F_RESOURCES)) != NULL) { static char retvalue[64]; strncpy(retvalue, rvalue, sizeof(retvalue)-1); retvalue[DIMOF(retvalue)-1] = EOS; ret = retvalue; }else{ ret = NULL; } ZAPMSG(reply); return ret;}/* * Return heartbeat's keepalive time */static longget_keepalive(ll_cluster_t* lcl){ llc_private_t* pi; if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_keepalive: bad cinfo"); return HA_FAIL; } pi = (llc_private_t*)lcl->ll_cluster_private; return (pi->keepalive_ms);}/* * Return heartbeat's dead time */static longget_deadtime(ll_cluster_t* lcl){ llc_private_t* pi; if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_deadtime: bad cinfo"); return HA_FAIL; } pi = (llc_private_t*)lcl->ll_cluster_private; return (pi->deadtime_ms);}/* * Return suggested logging facility */static intget_logfacility(ll_cluster_t* lcl){ llc_private_t* pi; if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_logfacility: bad cinfo"); return -1; } pi = (llc_private_t*)lcl->ll_cluster_private; return (pi->logfacility);}/* * Return my nodeid. */static const char *get_mynodeid(ll_cluster_t* lcl){ if (!ISOURS(lcl)) { ha_api_log(LOG_ERR, "get_mynodeid: bad cinfo"); return NULL; } return (OurNode);}/* * Return the status of the given interface for the given machine. */static const char *get_ifstatus(ll_cluster_t* lcl, const char *host, const char * ifname){ struct ha_msg* request; struct ha_msg* reply; const char * result; const char * status; static char statbuf[128]; const char * ret; llc_private_t* pi;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -