void service_tag_copy(struct service_tag_s *dst, struct service_tag_s *src) { if (!dst || !src) return; g_strlcpy(dst->name, src->name, sizeof(dst->name)); switch (src->type) { case STVT_I64: service_tag_set_value_i64(dst, src->value.i); return; case STVT_REAL: service_tag_set_value_float(dst, src->value.r); return; case STVT_BOOL: service_tag_set_value_boolean(dst, src->value.b); return; case STVT_STR: service_tag_set_value_string(dst, src->value.s); return; case STVT_BUF: service_tag_set_value_string(dst, src->value.buf); return; } }
void _add_custom_tags(service_info_t *si) { struct service_tag_s *tag = NULL; for (GSList *cursor = custom_tags; cursor; cursor = cursor->next) { gchar **kv = cursor->data; tag = service_info_ensure_tag(si->tags, kv[0]); service_tag_set_value_string(tag, kv[1]); } }
static gboolean _configure_registration(struct sqlx_service_s *ss) { struct service_info_s *si = ss->si; si->tags = g_ptr_array_new(); g_strlcpy(si->ns_name, ss->ns_name, sizeof(si->ns_name)); g_strlcpy(si->type, ss->service_config->srvtype, sizeof(si->type)); grid_string_to_addrinfo(ss->announce->str, &(si->addr)); service_tag_set_value_string( service_info_ensure_tag(si->tags, "tag.type"), ss->service_config->srvtag); _add_custom_tags(si, ss->custom_tags); service_tag_set_value_string( service_info_ensure_tag(si->tags, "tag.vol"), ss->volume); return TRUE; }
static void _add_custom_tags(struct service_info_s *si, GSList *tags) { for (; tags ;tags = g_slist_next(tags)) { gchar** tokens = g_strsplit((gchar*)(tags->data), "=", 2); if (!tokens) continue; // TODO add more checks on the name and value. if (tokens[0] && tokens[1]) { gchar *n = g_strconcat("tag.", tokens[0], NULL); service_tag_set_value_string(service_info_ensure_tag(si->tags, n), tokens[1]); g_free(n); } g_strfreev(tokens); } }
static struct service_tag_s * _srvtag_load_json (const gchar *name, struct json_object *obj) { struct service_tag_s *tag = g_malloc0(sizeof(struct service_tag_s)); g_strlcpy(tag->name, name, sizeof(tag->name)); if (json_object_is_type(obj, json_type_int)) { service_tag_set_value_i64(tag, json_object_get_int64(obj)); } else if (json_object_is_type(obj, json_type_string)) { service_tag_set_value_string(tag, json_object_get_string(obj)); } else if (json_object_is_type(obj, json_type_double)) { service_tag_set_value_float(tag, json_object_get_double(obj)); } else if (json_object_is_type(obj, json_type_boolean)) { service_tag_set_value_boolean(tag, json_object_get_boolean(obj)); } else { service_tag_set_value_boolean(tag, FALSE); } return tag; }
static void parse_output(const gchar *cmd, service_info_t *si) { int fd; FILE *stream_in; gchar line[1024]; gchar cmd_with_args[4096] = {0,0,0}; g_snprintf(cmd_with_args, sizeof(cmd_with_args), "%s %s", cmd, svc_id); INFO("Executing [%s]", cmd_with_args); if (0 > (fd = command_get_pipe(cmd_with_args))) { WARN("Exec failed: %s", strerror(errno)); return; } if (!(stream_in = fdopen(fd, "r"))) { WARN("fdopen failed: %s", strerror(errno)); metautils_pclose(&fd); return; } while (!feof(stream_in) && !ferror(stream_in)) { GMatchInfo *mi = NULL; bzero(line, sizeof(line)); if (!fgets(line, sizeof(line), stream_in)) { break; } /* chomp the line */ my_chomp(line); if (!g_regex_match(regex_tag, line, 0, &mi)) { NOTICE("Unrecognized pattern for output line [%s]", line); } else { struct service_tag_s *tag; gchar *str_type, *str_name, *str_value; str_name = g_match_info_fetch(mi, 1); str_type = g_match_info_fetch(mi, 2); str_value = g_match_info_fetch(mi, 4); if (!g_ascii_strcasecmp(str_type, "tag")) { tag = service_info_ensure_tag(si->tags, str_name); service_tag_set_value_string(tag, str_value); } else if (!g_ascii_strcasecmp(str_type, "stat")) { gdouble dval; dval = g_ascii_strtod(str_value, NULL); tag = service_info_ensure_tag(si->tags, str_name); service_tag_set_value_float(tag, dval); } g_free(str_value); g_free(str_type); g_free(str_name); } g_match_info_free(mi); } fclose(stream_in); }