📄 clientlib.c
字号:
msg = create_lrm_rsc_msg(rsc->id,GETRSCSTATE); if ( NULL == msg) { client_log(LOG_ERR, -1,"rsc_get_cur_state: can not create msg"); return NULL; } /*send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, -1, "rsc_get_cur_state: can not send msg to lrmd"); return NULL; } ha_msg_del(msg); /*get the return msg */ ret = msgfromIPC_noauth(ch_cmd); if (NULL == ret) { client_log(LOG_ERR, -1, "rsc_get_cur_state: can not recieve ret msg"); return NULL; }/* ha_msg_print(ret); */ /*get the state of the resource from the message */ if (HA_FAIL == ha_msg_value_int(ret, F_LRM_STATE, &state)) { ha_msg_del(ret); client_log(LOG_ERR, -1, "rsc_get_cur_state: can not get state from msg"); return NULL; } *cur_state = (state_flag_t)state; if (LRM_RSC_IDLE == *cur_state) { /*if the state is idle, the last finsihed op returned.*/ /* the op is stored in the same msg, just get it out*/ lrm_op_t* op = msg_to_op(ret); if (NULL != op) { op->rsc = rsc; pending_op_list = g_list_append(pending_op_list, op); } client_log(LOG_INFO, -1, "rsc_get_cur_state: end."); ha_msg_del(ret); return pending_op_list; } if (LRM_RSC_BUSY == *cur_state) { /*if the state is busy, the whole pending op list would be return */ int op_count, i; /*the first msg includes the count of pending ops.*/ if (HA_FAIL == ha_msg_value_int(ret, F_LRM_OPCNT, &op_count)) { client_log(LOG_ERR, -1, "rsc_get_cur_state: can not get op count"); ha_msg_del(ret); return NULL; } for (i = 0; i < op_count; i++) { /*one msg for one pending op */ struct ha_msg* op_msg = msgfromIPC_noauth(ch_cmd); if (NULL == op_msg) { client_log(LOG_ERR, 0, "rsc_get_cur_state: can not recieve ret msg"); continue; } op = msg_to_op(op_msg); /*add msg to the return list*/ if (NULL != op) { op->rsc = rsc; pending_op_list = g_list_append(pending_op_list, op); } ha_msg_del(op_msg); } ha_msg_del(ret); client_log(LOG_INFO, -1, "rsc_get_cur_state: end."); return pending_op_list; } client_log(LOG_ERR, -1,"rsc_get_cur_state: can not get state from msg"); return NULL;}/* * following are the implements of the utility functions *//*when an operation is done, a message will send back to client library. *//*this function will be call. */inton_op_done (int call_id, lrm_op_t* op, struct ha_msg* msg){ lrm_op_t* to_del_op = NULL; size_t data_len = 0; const char* data = NULL; const char* app_name = NULL; client_log(LOG_INFO, 1, "on_op_done: start.");/* ha_msg_print(msg);*/ /*get the status of the operation's excuation*/ if (HA_FAIL == ha_msg_value_int(msg, F_LRM_OPSTATUS, (int*)&op->status)) { client_log(LOG_ERR, -1, "on_op_done: can not get op status from msg."); return HA_FAIL; } /*if it finished successfully, get the return code of the operation*/ if (LRM_OP_DONE == op->status ) { if (HA_FAIL == ha_msg_value_int(msg, F_LRM_RC, &op->rc)) { client_log(LOG_ERR, -1, "on_op_done: can not get op rc from msg."); return HA_FAIL; } } app_name = ha_msg_value(msg, F_LRM_APP); if (NULL != app_name) { op->app_name = g_strdup(app_name); } /*if it has data (for example, metadata operation), get the data*/ data = cl_get_binary(msg, F_LRM_DATA,&data_len); if (NULL != data){ op->data = strndup(data, data_len); } else { op->data = NULL; } /*call the callback function*/ if (NULL != op_done_callback) { op->call_id = call_id; (*op_done_callback)(copy_op(op)); } /*remove the op from the op_list.*/ to_del_op = lookup_op(call_id); if (NULL != to_del_op) { op_list = g_list_remove(op_list, to_del_op); free_op(op); } client_log(LOG_INFO, -1, "on_op_done: end."); return HA_OK;}/* *when the condition of a monitor is satisfied, the monitor will send *back a msg,this function will be called to process the msg */inton_monitor ( int call_id, lrm_mon_t* mon, struct ha_msg* msg){ client_log(LOG_INFO, 1, "on_monitor: start."); /*get the status of the monitor*/ if (HA_FAIL == ha_msg_value_int(msg, F_LRM_OPSTATUS, (int*)&mon->status)) { client_log(LOG_ERR, -1, "on_monitor: can not get op status from msg."); return HA_FAIL; } /*if it is ok, get the rc of RA*/ if (LRM_OP_DONE == mon->status ) { if (HA_FAIL == ha_msg_value_int(msg, F_LRM_RC, &mon->rc)) { client_log(LOG_ERR, -1, "on_monitor: can not get op rc from msg."); return HA_FAIL; } } /*call the callback function*/ if (NULL != monitor_callback) { mon->call_id = call_id; (*monitor_callback)(copy_mon(mon)); } client_log(LOG_INFO, -1, "on_monitor: end."); return HA_OK;}lrm_op_t*msg_to_op(struct ha_msg* msg){ lrm_op_t* save_op = NULL; size_t data_len = 0; const char* data = NULL; lrm_op_t* op = NULL; const char* temp_params = NULL; const char* op_temp = NULL; client_log(LOG_INFO, 1, "msg_to_op: start."); op = g_new(lrm_op_t, 1); /*op->op_type*/ op_temp = ha_msg_value(msg, F_LRM_OP); if (NULL == op_temp) { client_log(LOG_ERR, -1, "msg_to_op: can not get op_type."); return NULL; } op->op_type = g_strdup(op_temp); /*op->params*/ temp_params = ha_msg_value(msg, F_LRM_PARAM); if (NULL != temp_params) { char* params = g_strdup(temp_params); op->params = string_to_hash_table(params); g_free(params); } else { op->params = NULL; } /*op->timeout*/ if (HA_FAIL == ha_msg_value_int(msg,F_LRM_TIMEOUT, &op->timeout)) { client_log(LOG_ERR, -1, "msg_to_op: can not get op_type."); return NULL; } /*op->call_id*/ if (HA_FAIL == ha_msg_value_int(msg,F_LRM_CALLID, &op->call_id)) { client_log(LOG_ERR, -1, "msg_to_op: can not get call_id."); return NULL; } /*op->user_data*/ save_op = lookup_op(op->call_id); if (NULL != save_op) { op->user_data = save_op->user_data; } else { op->user_data = NULL; } /*op->status*/ if (HA_FAIL==ha_msg_value_int(msg,F_LRM_OPSTATUS, (int*)&op->status)) { client_log(LOG_INFO, 0, "msg_to_op: can not get status."); } /*op->data*/ data = cl_get_binary(msg, F_LRM_DATA,&data_len); if (NULL != data){ op->data = strndup(data, data_len); } else { op->data = NULL; } /*op->rc*/ if (HA_FAIL == ha_msg_value_int(msg,F_LRM_RC, &op->rc)) { client_log(LOG_INFO, 0, "msg_to_op: can not get rc."); } /*op->app_name*/ op->app_name = g_strdup(ha_msg_value(msg, F_LRM_APP)); client_log(LOG_INFO, -1, "msg_to_op: end."); return op;}lrm_op_t*lookup_op(int call_id){ GList* node; client_log(LOG_INFO, 1, "lookup_op: start."); for(node=g_list_first(op_list); NULL!=node; node=g_list_next(node)) { lrm_op_t* op = (lrm_op_t*)node->data; if (call_id == op->call_id) { client_log(LOG_INFO, -1, "lookup_op: end."); return op; } } client_log(LOG_INFO, -1, "lookup_op: end."); return NULL;}lrm_mon_t*lookup_mon(int call_id){ GList* node; client_log(LOG_INFO, 1, "lookup_mon: start."); for(node=g_list_first(mon_list); NULL!=node; node=g_list_next(node)) { lrm_mon_t* mon = (lrm_mon_t*)node->data; if (call_id == mon->call_id) { client_log(LOG_INFO, -1, "lookup_mon: end."); return mon; } } client_log(LOG_INFO, -1, "lookup_mon: end."); return NULL;}intget_rc_from_ch(IPC_Channel* ch){ int rc; struct ha_msg* msg = NULL; client_log(LOG_INFO, 1, "get_rc_from_ch: start."); msg = msgfromIPC_noauth(ch); if (NULL == msg) { client_log(LOG_ERR, -1, "get_rc_from_ch: can not recieve msg"); return HA_FAIL; } if (HA_FAIL == ha_msg_value_int(msg, F_LRM_RC, &rc)) { client_log(LOG_ERR, -1, "get_rc_from_ch: can not get rc from msg"); return HA_FAIL; } ha_msg_del(msg); client_log(LOG_INFO, -1, "get_rc_from_ch: end."); return rc;}intget_rc_from_msg(struct ha_msg* msg){ int rc; client_log(LOG_INFO, 1, "get_rc_from_msg: start."); if (NULL == msg) { client_log(LOG_ERR, -1, "get_rc_from_msg: msg is null"); return HA_FAIL; } if (HA_FAIL == ha_msg_value_int(msg, F_LRM_RC, &rc)) { client_log(LOG_ERR, -1, "get_rc_from_msg: can not get rc from msg"); return HA_FAIL; } client_log(LOG_INFO, -1, "get_rc_from_msg: end."); return rc;}lrm_mon_t*copy_mon(lrm_mon_t* mon_in){ lrm_mon_t* mon = g_new(lrm_mon_t, 1); mon->call_id = mon_in->call_id; mon->interval = mon_in->interval; mon->mode = mon_in->mode; mon->op_type = g_strdup(mon_in->op_type); if (NULL != mon_in->params) { char* params_str = hash_table_to_string(mon_in->params); mon->params = string_to_hash_table(params_str); g_free(params_str); } else { mon->params = NULL; } mon->rc = mon_in->rc; mon->rsc = mon_in->rsc; mon->status = mon_in->status; mon->target = mon_in->target; mon->timeout = mon_in->timeout; mon->user_data = mon_in->user_data; return mon; }lrm_op_t*copy_op(lrm_op_t* op_in){ lrm_op_t* op = g_new(lrm_op_t, 1); if (NULL != op_in->app_name) { op->app_name = g_strdup(op_in->app_name); } else { op->app_name = NULL; } op->call_id = op_in->call_id; if (NULL != op_in->data) { op->data = g_strdup(op_in->data); } else { op->data = NULL; } op->op_type = g_strdup(op_in->op_type); if (NULL != op_in->params) { char* params_str = hash_table_to_string(op_in->params); op->params = string_to_hash_table(params_str); g_free(params_str); } else { op->params = NULL; } op->rc = op_in->rc; op->rsc = op_in->rsc; op->status = op_in->status; op->timeout = op_in->timeout; op->user_data = op_in->user_data; return op;}voidfree_op (lrm_op_t* op) { g_free(op->app_name); g_free(op->data); if (NULL != op->params) { free_hash_table(op->params); }}voidfree_mon (lrm_mon_t* mon) { if (NULL != mon->params) { free_hash_table(mon->params); }}#define INDENT 4voidclient_log (int priority, int level, const char* fmt){ int i; static int indent = INDENT; if (LOG_ERR != priority) { return; } printf("\t\tclient_log:"); if( 1 == level) { indent = indent + INDENT; } for (i = 0; i < indent; i++) { printf("%c",' '); } if (LOG_ERR == priority) { printf("%c",'*'); } printf("%s\n",fmt); if( -1 == level) { indent = indent - INDENT; }}voidha_msg_print(struct ha_msg * msg){ int i; printf("print msg:%p\n",msg); printf("\tnfields:%d\n",msg->nfields); for (i = 0; i < msg->nfields; i++){ printf("\tname:%s\tvalue:%s\n",msg->names[i], (char *)msg->values[i]); } printf("print end\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -