/*
* This file is part of GNUnet
* Copyright (C) 2013 GNUnet e.V.
*
* GNUnet is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 3, or (at your
* option) any later version.
*
* GNUnet is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNUnet; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* @file psycstore/psycstore_api.c
* @brief API to interact with the PSYCstore service
* @author Gabor X Toth
* @author Christian Grothoff
*/
#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_constants.h"
#include "gnunet_protocols.h"
#include "gnunet_psycstore_service.h"
#include "gnunet_multicast_service.h"
#include "psycstore.h"
#define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
/**
* Handle for an operation with the PSYCstore service.
*/
struct GNUNET_PSYCSTORE_OperationHandle
{
/**
* Main PSYCstore handle.
*/
struct GNUNET_PSYCSTORE_Handle *h;
/**
* Data callbacks.
*/
union {
GNUNET_PSYCSTORE_FragmentCallback fragment_cb;
GNUNET_PSYCSTORE_CountersCallback counters_cb;
GNUNET_PSYCSTORE_StateCallback state_cb;
};
/**
* Closure for callbacks.
*/
void *cls;
/**
* Message envelope.
*/
struct GNUNET_MQ_Envelope *env;
/**
* Operation ID.
*/
uint64_t op_id;
};
/**
* Handle for the service.
*/
struct GNUNET_PSYCSTORE_Handle
{
/**
* Configuration to use.
*/
const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Client connection.
*/
struct GNUNET_MQ_Handle *mq;
/**
* Async operations.
*/
struct GNUNET_OP_Handle *op;
/**
* Task doing exponential back-off trying to reconnect.
*/
struct GNUNET_SCHEDULER_Task *reconnect_task;
/**
* Delay for next connect retry.
*/
struct GNUNET_TIME_Relative reconnect_delay;
GNUNET_PSYCSTORE_FragmentCallback *fragment_cb;
GNUNET_PSYCSTORE_CountersCallback *counters_cb;
GNUNET_PSYCSTORE_StateCallback *state_cb;
/**
* Closure for callbacks.
*/
void *cb_cls;
};
static int
check_result_code (void *cls, const struct OperationResult *opres)
{
uint16_t size = ntohs (opres->header.size);
const char *str = (const char *) &opres[1];
if ( (sizeof (*opres) < size) &&
('\0' != str[size - sizeof (*opres) - 1]) )
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
static void
handle_result_code (void *cls, const struct OperationResult *opres)
{
struct GNUNET_PSYCSTORE_Handle *h = cls;
struct GNUNET_PSYCSTORE_OperationHandle *op = NULL;
uint16_t size = ntohs (