+/**
+ * Retrieve latest values of counters for a channel master.
+ *
+ * The current value of counters are needed when a channel master is restarted,
+ * so that it can continue incrementing the counters from their last value.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * Public key that identifies the channel.
+ * @param ccb
+ * Callback to call with the result.
+ * @param ccb_cls
+ * Closure for the @a ccb callback.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
+ struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+ GNUNET_PSYCSTORE_CountersCallback counters_cb,
+ void *cls)
+{
+ struct OperationRequest *req;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET);
+ req->channel_key = *channel_key;
+
+ struct GNUNET_PSYCSTORE_OperationHandle *
+ op = op_create (h, h->op, NULL, NULL);
+ op->counters_cb = counters_cb;
+ op->cls = cls;
+ return op_send (h, op, env, &req->op_id);
+}
+
+
+/**
+ * Apply modifiers of a message to the current channel state.
+ *
+ * An error is returned if there are missing messages containing state
+ * operations before the current one.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param message_id
+ * ID of the message that contains the @a modifiers.
+ * @param state_delta
+ * Value of the _state_delta PSYC header variable of the message.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for @a result_cb.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
+ const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+ uint64_t message_id,
+ uint64_t state_delta,
+ GNUNET_PSYCSTORE_ResultCallback result_cb,
+ void *cls)
+{
+ struct StateModifyRequest *req;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_MODIFY);
+ req->channel_key = *channel_key;
+ req->message_id = GNUNET_htonll (message_id);
+ req->state_delta = GNUNET_htonll (state_delta);
+
+ return op_send (h, op_create (h, h->op, result_cb, cls),
+ env, &req->op_id);
+}
+
+
+struct StateSyncClosure
+{
+ GNUNET_PSYCSTORE_ResultCallback result_cb;
+ void *cls;
+ uint8_t last;
+};
+
+
+static void
+state_sync_result (void *cls, int64_t result,
+ const char *err_msg, uint16_t err_msg_size)
+{
+ struct StateSyncClosure *ssc = cls;
+ if (GNUNET_OK != result || ssc->last)
+ ssc->result_cb (ssc->cls, result, err_msg, err_msg_size);
+ GNUNET_free (ssc);
+}
+
+
+/**
+ * Store synchronized state.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param max_state_message_id
+ * ID of the last stateful message before @a state_hash_message_id.
+ * @param state_hash_message_id
+ * ID of the message that contains the state_hash PSYC header variable.
+ * @param modifier_count
+ * Number of elements in the @a modifiers array.
+ * @param modifiers
+ * Full state to store.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for the callback.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_state_sync (struct GNUNET_PSYCSTORE_Handle *h,
+ const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+ uint64_t max_state_message_id,
+ uint64_t state_hash_message_id,
+ size_t modifier_count,
+ const struct GNUNET_PSYC_Modifier *modifiers,
+ GNUNET_PSYCSTORE_ResultCallback result_cb,
+ void *cls)
+{
+ struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
+ size_t i;
+
+ for (i = 0; i < modifier_count; i++) {
+ struct StateSyncRequest *req;
+ uint16_t name_size = strlen (modifiers[i].name) + 1;
+
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg_extra (req,
+ sizeof (*req) + name_size + modifiers[i].value_size,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
+
+ req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_SYNC);
+ req->header.size = htons (sizeof (*req) + name_size
+ + modifiers[i].value_size);
+ req->channel_key = *channel_key;
+ req->max_state_message_id = GNUNET_htonll (max_state_message_id);
+ req->state_hash_message_id = GNUNET_htonll (state_hash_message_id);
+ req->name_size = htons (name_size);
+ req->flags
+ = (0 == i)
+ ? STATE_OP_FIRST
+ : (modifier_count - 1 == i)
+ ? STATE_OP_LAST
+ : 0;
+
+ GNUNET_memcpy (&req[1], modifiers[i].name, name_size);
+ GNUNET_memcpy ((char *) &req[1] + name_size, modifiers[i].value, modifiers[i].value_size);
+
+ struct StateSyncClosure *ssc = GNUNET_malloc (sizeof (*ssc));
+ ssc->last = (req->flags & STATE_OP_LAST);
+ ssc->result_cb = result_cb;
+ ssc->cls = cls;
+
+ op_send (h, op_create (h, h->op, state_sync_result, ssc),
+ env, &req->op_id);
+ }
+ // FIXME: only one operation is returned,
+ // add pointers to other operations and make all cancellable.
+ return op;
+}
+
+
+/**
+ * Reset the state of a channel.
+ *
+ * Delete all state variables stored for the given channel.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for the callback.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_state_reset (struct GNUNET_PSYCSTORE_Handle *h,
+ const struct GNUNET_CRYPTO_EddsaPublicKey
+ *channel_key,
+ GNUNET_PSYCSTORE_ResultCallback result_cb,
+ void *cls)
+{
+ struct OperationRequest *req;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_RESET);
+ req->channel_key = *channel_key;
+
+ return
+ op_send (h, op_create (h, h->op, result_cb, cls),
+ env, &req->op_id);
+}
+
+
+/**
+ * Update signed values of state variables in the state store.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param message_id
+ * Message ID that contained the state @a hash.
+ * @param hash
+ * Hash of the serialized full state.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for the callback.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
+ const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+ uint64_t message_id,
+ const struct GNUNET_HashCode *hash,
+ GNUNET_PSYCSTORE_ResultCallback result_cb,
+ void *cls)
+{
+ struct StateHashUpdateRequest *req;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_HASH_UPDATE);
+ req->channel_key = *channel_key;
+ req->hash = *hash;
+
+ return
+ op_send (h, op_create (h, h->op, result_cb, cls),
+ env, &req->op_id);
+}
+
+
+/**
+ * Retrieve the best matching state variable.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param name
+ * Name of variable to match, the returned variable might be less specific.
+ * @param state_cb
+ * Callback to return the matching state variable.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for the callbacks.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */
+struct GNUNET_PSYCSTORE_OperationHandle *
+GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
+ const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+ const char *name,
+ GNUNET_PSYCSTORE_StateCallback state_cb,
+ GNUNET_PSYCSTORE_ResultCallback result_cb,
+ void *cls)
+{
+ size_t name_size = strlen (name) + 1;
+ struct OperationRequest *req;
+ struct GNUNET_MQ_Envelope *
+ env = GNUNET_MQ_msg_extra (req, name_size,
+ GNUNET_MESSAGE_TYPE_PSYCSTORE_STATE_GET);
+ req->channel_key = *channel_key;
+ GNUNET_memcpy (&req[1], name, name_size);
+
+ struct GNUNET_PSYCSTORE_OperationHandle *
+ op = op_create (h, h->op, result_cb, cls);
+ op->state_cb = state_cb;
+ op->cls = cls;
+ return op_send (h, op, env, &req->op_id);
+}
+
+
+/**
+ * Retrieve all state variables for a channel with the given prefix.
+ *
+ * @param h
+ * Handle for the PSYCstore.
+ * @param channel_key
+ * The channel we are interested in.
+ * @param name_prefix
+ * Prefix of state variable names to match.
+ * @param state_cb
+ * Callback to return matching state variables.
+ * @param result_cb
+ * Callback to call with the result of the operation.
+ * @param cls
+ * Closure for the callbacks.
+ *
+ * @return Handle that can be used to cancel the operation.
+ */