static void tick(int *len, char *buf, struct per_session_data *data) { int path_length = 0; path_t path; mask_t random_dots = EMPTY_MASK; json_object *result; const char *s; /* If it's a new game, only send the grid and don't compute a move. */ if (data->new_game) { data->new_game = 0; } else { struct timeval tv; long ms; mask_t move; int no_moves; gettimeofday(&tv, NULL); ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); if ((ms - data->last_updated) < MIN_UPDATE_INTERVAL) { return; } data->last_updated = ms; move = choose_move(data->grid, 0, 100, &no_moves); apply_move(data->grid, move); fill_grid(data->grid, GET_CYCLE_COLOR(move)); if (no_moves) { random_dots = move; } else { mask_to_path(move, &path_length, path); } } result = json_object_new_object(); json_object_object_add(result, "grid", json_grid(data->grid)); if (path_length == 0 && random_dots == EMPTY_MASK) { json_object_object_add(result, "newGrid", json_object_new_boolean(TRUE)); } if (random_dots) { json_object_object_add(result, "shrinkRandom", json_shrink_random(random_dots)); } if (path_length) { json_object_object_add(result, "path", json_path(path_length, path)); } s = json_object_to_json_string(result); *len = strlen(s); memcpy(buf, s, *len); buf[*len] = 0; json_object_put(result); }
struct watchman_query_result * watchman_do_query(struct watchman_connection *conn, const char *fs_path, const struct watchman_query *query, const struct watchman_expression *expr, struct watchman_error *error) { /* construct the json */ json_t *json = json_array(); json_array_append_new(json, json_string("query")); json_array_append_new(json, json_string(fs_path)); json_t *obj = json_object(); json_object_set_new(obj, "expression", to_json(expr)); if (query) { if (query->fields) { json_object_set_new(obj, "fields", fields_to_json(query->fields)); } if (query->empty_on_fresh) { json_object_set_new(obj, "empty_on_fresh_instance", json_true()); } if (query->s.time) { if (query->since_is_str) { json_object_set_new(obj, "since", json_string(query->s.str)); } else { json_t *since = json_integer(query->s.time); json_object_set_new(obj, "since", since); } } if (query->nr_suffixes) { /* Note that even if you have only one suffix, * watchman requires this to be an array. */ int i; json_t *suffixes = json_array(); for (i = 0; i < query->nr_suffixes; ++i) { json_array_append_new(suffixes, json_string(query->suffixes[i])); } json_object_set_new(obj, "suffix", suffixes); } if (query->nr_paths) { int i; json_t *paths = json_array(); for (i = 0; i < query->nr_paths; ++i) { json_array_append_new(paths, json_path(&query->paths[i])); } json_object_set_new(obj, "path", paths); } if (query->all) { json_object_set_new(obj, "all", json_string("all")); } if (query->sync_timeout >= 0) { json_object_set_new(obj, "sync_timeout", json_integer(query->sync_timeout)); } } json_array_append_new(json, obj); /* do the query */ struct watchman_query_result *r = watchman_query_json(conn, json, error); json_decref(json); return r; }