gboolean query_quorum(gpointer data) { int quorum; size_t len; char* s = NULL; char buf[MAXMSG]; struct ha_msg* msg = NULL; struct ha_msg* ret = NULL; if(session != NULL) { msg = ha_msg_new(10); ha_msg_add(msg, "t","quorum"); ha_msg_add_int(msg, "nodenum", nodenum); ha_msg_add_int(msg, "weight", weight); s = msg2wirefmt(msg, &len); gnutls_record_send(session, s, len); free(s); len = gnutls_record_recv(session, buf, MAXMSG); if ((ssize_t)len < 0) { gnutls_bye (session, GNUTLS_SHUT_WR); gnutls_deinit (session); close(sock); session = NULL; cur_quorum = -1; ha_msg_del(msg); return TRUE; } ret = wirefmt2msg(buf, len, FALSE); ha_msg_value_int(ret, "quorum", &quorum); ha_msg_del(ret); ha_msg_del(msg); if (cur_quorum!=-1 && cur_quorum!=quorum && callback!=NULL){ cur_quorum = quorum; callback(); } cur_quorum = quorum; } else { connect_quorum_server(NULL); } return TRUE; }
static int compress2uncompress(struct ha_msg* msg, int index) { char *buf = NULL; size_t buflen = MAXUNCOMPRESSED; struct ha_msg* msgfield; int err = HA_FAIL; buf = cl_malloc(buflen); if (!buf) { cl_log(LOG_ERR, "%s: allocating buffer for uncompression failed", __FUNCTION__); goto out; } if (cl_decompress_field(msg, index, buf, &buflen) != HA_OK){ cl_log(LOG_ERR, "%s: compress field failed", __FUNCTION__); goto out; } msgfield = wirefmt2msg(buf, buflen, 0); if (msgfield == NULL){ cl_log(LOG_ERR, "%s: wirefmt to msg failed", __FUNCTION__); goto out; } err = cl_msg_replace(msg, index, (char*)msgfield, 0, FT_UNCOMPRESS); ha_msg_del(msgfield); out: if (buf) { cl_free(buf); } return err; }
gboolean connect_quorum_server(gpointer data) { struct sockaddr_in addr; struct ha_msg* msg = NULL; struct ha_msg* ret = NULL; const char* version = "2_0_8"; struct hostent* hp; int quorum; size_t len; char* s = NULL; char buf[MAXMSG]; cl_log(LOG_DEBUG, "quorum plugin: quorumd, connect_quorum_server"); /* initialize gnutls */ initialize_tls_global(); /* create socket */ sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1 ) { return FALSE; } /* connect to server*/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; hp = gethostbyname(quorum_server); if (hp == NULL) { return FALSE; } memcpy(&addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); addr.sin_port = htons(5561); if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { return FALSE; } session = initialize_tls_session(sock); if (session == NULL) { close(sock); session = NULL; return FALSE; } /* send the version */ gnutls_record_send(session, version, strlen(version)+1); /* send initialize msg */ msg = ha_msg_new(10); ha_msg_add(msg, "t","init"); ha_msg_add(msg, "cl_name", cluster); s = msg2wirefmt(msg, &len); gnutls_record_send(session, s, len); free(s); len = gnutls_record_recv(session, buf, MAXMSG); if ((ssize_t)len <=0) { close(sock); session = NULL; return FALSE; } ret = wirefmt2msg(buf, len, FALSE); if (STRNCMP_CONST(ha_msg_value(ret, "result"), "ok") != 0) { close(sock); session = NULL; return FALSE; } if (ha_msg_value_int(ret, "interval", &interval)!= HA_OK) { close(sock); session = NULL; return FALSE; } ha_msg_del(ret); ha_msg_del(msg); /* send quorum query msg */ msg = ha_msg_new(10); ha_msg_add(msg, "t","quorum"); ha_msg_add_int(msg, "nodenum", nodenum); ha_msg_add_int(msg, "weight", weight); s = msg2wirefmt(msg, &len); gnutls_record_send(session, s, len); free(s); len = gnutls_record_recv(session, buf, MAXMSG); ret = wirefmt2msg(buf, len, FALSE); ha_msg_value_int(ret, "quorum", &quorum); LOG(LOG_DEBUG,"quorum:%d\n", quorum); cur_quorum = quorum; ha_msg_del(ret); ha_msg_del(msg); /* set the repeatly query */ repeat_timer = g_timeout_add(interval, query_quorum, NULL); return FALSE; }