Beispiel #1
0
int handle_listval (FILE *fh, char *buffer)
{
  char *command;
  char **names = NULL;
  cdtime_t *times = NULL;
  size_t number = 0;
  size_t i;
  int status;

  DEBUG ("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);",
      (void *) fh, buffer);

  command = NULL;
  status = parse_string (&buffer, &command);
  if (status != 0)
  {
    print_to_socket (fh, "-1 Cannot parse command.\n");
    free_everything_and_return (-1);
  }
  assert (command != NULL);

  if (strcasecmp ("LISTVAL", command) != 0)
  {
    print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
    free_everything_and_return (-1);
  }

  if (*buffer != 0)
  {
    print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
    free_everything_and_return (-1);
  }

  status = uc_get_names (&names, &times, &number);
  if (status != 0)
  {
    DEBUG ("command listval: uc_get_names failed with status %i", status);
    print_to_socket (fh, "-1 uc_get_names failed.\n");
    free_everything_and_return (-1);
  }

  print_to_socket (fh, "%i Value%s found\n",
      (int) number, (number == 1) ? "" : "s");
  for (i = 0; i < number; i++)
    print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]),
       	names[i]);

  free_everything_and_return (0);
} /* int handle_listval */
static int
instances_of_types_tree_update (void) {
	char **names = NULL;
	cdtime_t *times = NULL;
	size_t number = 0;
	int i;
	int status = 0;

	if(NULL == instances_of_types_tree) {
		instances_of_types_tree = c_avl_create ((void *) strcmp);
	}

	status = uc_get_names (&names, &times, &number);
	if (status != 0)
	{
			size_t j; 
			DEBUG (OUTPUT_PREFIX_STRING "uc_get_names failed with status %i", status);
			for (j = 0; j < number; j++) { 
					sfree(names[j]); 
			} 
			sfree(names); 
			sfree(times); 
			return(status);
	}

	if(number == 0) {
		return(0);
	}
	pthread_mutex_lock (&instances_of_types_mutex);
	for(i=0; i<number; i++) {
		char *type;
		int l1,l2;
		int pos =0;
		instances_list_t *v;
		char *type_instance;
		int type_instance_is_missing = 1;

		for(l1=0; names[i][l1] && (pos < 2); l1++) {
			if(names[i][l1] == '/') pos++;
		}
		if(names[i][l1] == '\0') {
			sfree(names[i]);
			continue;
		}
		l2 = l1;
		while(names[i][l2] && (names[i][l2] != '-')) l2++;
		if(names[i][l2] == '\0') {
			sfree(names[i]);
			continue;
		}
		type = names[i]+l1;
		names[i][l2] = '\0';
		type_instance = names[i]+l2+1;


		if(0 == c_avl_get(instances_of_types_tree, type,(void*)&v)) {
			int i;
			for(i=0; v->instance[i]; i++) {
				if(0 == strcmp(v->instance[i], type_instance)) {
					type_instance_is_missing = 0;
					break;
				}
			}
			if(type_instance_is_missing) {
				if(i >= v->nb) {
					v->nb += 128;
					if(NULL == (v->instance = realloc(v->instance, v->nb*sizeof(*(v->instance))))) {
						ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
						pthread_mutex_unlock (&instances_of_types_mutex);
						return(-1);
					}
					}
				v->instance[i+1] = NULL;
				if(NULL == (v->instance[i] = sstrdup(type_instance))) {
					ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
					pthread_mutex_unlock (&instances_of_types_mutex);
					return(-1);
				}
			}
		} else {
			char *k;
			if(NULL == (v = malloc(sizeof(*v)))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			v->nb = 128;
			if(NULL == (v->instance = malloc(v->nb*sizeof(*(v->instance))))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			if(NULL == (k = sstrdup(type))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			if(NULL == (v->instance[0] = sstrdup(type_instance))) {
				ERROR(OUTPUT_PREFIX_STRING "Could not allocate memory");
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
			v->instance[1] = NULL;
			if(0 != c_avl_insert(instances_of_types_tree, k,v)) {
				ERROR (OUTPUT_PREFIX_STRING "Could insert data into AVL tree");
				sfree(v->instance[0]);
				sfree(v);
				pthread_mutex_unlock (&instances_of_types_mutex);
				return(-1);
			}
		}
		sfree(names[i]); 
	}
	pthread_mutex_unlock (&instances_of_types_mutex);
	sfree(names); 
	sfree(times); 
	return(0);
}