*/
#include "platform.h"
#include "gnunet_arm_service.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_configuration_lib.h"
-#include "gnunet_program_lib.h"
+#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
+#define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+
#define START_ARM GNUNET_YES
#define LOG_BACKOFF GNUNET_NO
#define FIVE_MILLISECONDS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5)
+#define SERVICE "do-nothing"
+
+#define BINARY "mockup-service"
+
+#define CFGFILENAME "test_arm_api_data2.conf"
+
static const struct GNUNET_CONFIGURATION_Handle *cfg;
#endif
+typedef void (*GNUNET_CLIENT_ShutdownTask) (void *cls, int reason);
+
/**
* Context for handling the shutdown of a service.
*/
{
struct ShutdownContext *shutdown_ctx = cls;
- if (NULL == msg)
+ if (NULL == msg)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
if (shutdown_ctx->cont != NULL)
shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_NO);
-
+
GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
GNUNET_free (shutdown_ctx);
if (size < sizeof (struct GNUNET_MessageHeader))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- _("Failed to transmit shutdown request to client.\n"));
- FPRINTF (stderr, "%s", "Failed to send a shutdown request\n");
+ LOG ("Failed to send a shutdown request\n");
shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_SYSERR);
GNUNET_CLIENT_disconnect (shutdown_ctx->sock);
GNUNET_free (shutdown_ctx);
msg = (struct GNUNET_MessageHeader *) buf;
msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_STOP);
msg->size = htons (sizeof (struct GNUNET_MessageHeader));
- strcpy ((char *) &msg[1], "do-nothing");
- FPRINTF (stderr, "%s", "Sent a shutdown request\n");
- return sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1;
+ strcpy ((char *) &msg[1], SERVICE);
+ LOG ("Sent a shutdown request\n");
+ return sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1;
}
{
struct ShutdownContext *shutdown_ctx;
- shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
+ shutdown_ctx = GNUNET_new (struct ShutdownContext);
shutdown_ctx->cont = cont;
shutdown_ctx->cont_cls = cont_cls;
shutdown_ctx->sock = sock;
shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
GNUNET_CLIENT_notify_transmit_ready (sock,
- sizeof (struct GNUNET_MessageHeader) + strlen ("do-nothing") + 1,
+ sizeof (struct GNUNET_MessageHeader) + strlen (SERVICE) + 1,
timeout, GNUNET_NO, &write_shutdown,
shutdown_ctx);
}
+
static void
kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
static void
shutdown_cont (void *cls, int reason)
{
if (GNUNET_NO != reason)
{
/* Re-try shutdown */
- FPRINTF (stderr, "%s", "do-nothing didn't die, trying again\n");
+ LOG ("do-nothing didn't die, trying again\n");
GNUNET_SCHEDULER_add_now (kill_task, NULL);
return;
}
startedWaitingAt = GNUNET_TIME_absolute_get ();
- FPRINTF (stderr, "%s", "do-nothing is dead, starting the countdown\n");
+ LOG ("do-nothing is dead, starting the countdown\n");
}
+
static void
kill_task (void *cbData, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
if (NULL != cbData)
{
waitedFor = GNUNET_TIME_absolute_get_duration (startedWaitingAt);
- FPRINTF (stderr, "Waited for: %llu ms\n", waitedFor.rel_value);
-#if LOG_BACKOFF
- FPRINTF (killLogFilePtr, "Waited for: %llu ms\n",
- (unsigned long long) waitedFor.rel_value);
-#endif
+ LOG ("Waited for: %s\n",
+ GNUNET_STRINGS_relative_time_to_string (waitedFor, GNUNET_YES));
}
else
{
- waitedFor.rel_value = 0;
+ waitedFor.rel_value_us = 0;
}
/* Connect to the doNothing task */
- doNothingConnection = GNUNET_CLIENT_connect ("do-nothing", cfg);
+ doNothingConnection = GNUNET_CLIENT_connect (SERVICE, cfg);
GNUNET_assert (doNothingConnection != NULL);
if (trialCount == 12)
waitedFor_prev = waitedFor;
else if (trialCount == 13)
{
GNUNET_CLIENT_disconnect (doNothingConnection);
- GNUNET_ARM_request_service_stop (arm, "do-nothing", TIMEOUT, NULL, NULL);
- if (waitedFor_prev.rel_value >= waitedFor.rel_value)
+ GNUNET_ARM_request_service_stop (arm, SERVICE, TIMEOUT, NULL, NULL);
+ if (waitedFor_prev.rel_value_us >= waitedFor.rel_value_us)
ok = 9;
else
ok = 0;
TIMEOUT, &shutdown_cont, NULL);
}
+
static void
trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
GNUNET_ARM_disconnect_and_free (arm);
- GNUNET_ARM_monitor_disconnect (mon);
+ GNUNET_ARM_monitor_disconnect_and_free (mon);
}
static void
-arm_stop_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
+arm_stop_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
{
GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
- GNUNET_break (result == GNUNET_ARM_RESULT_STOPPING);
- FPRINTF (stderr, "%s", "ARM service stopped\n");
+ GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
+ LOG ("ARM service stopped\n");
GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
}
-void
-srv_status (void *cls, struct GNUNET_ARM_MonitorHandle *mon, const char *service, enum GNUNET_ARM_ServiceStatus status)
+
+static void
+srv_status (void *cls, const char *service, enum GNUNET_ARM_ServiceStatus status)
{
- FPRINTF (stderr, "Service %s is %u, phase %u\n", service, status, phase);
+ LOG ("Service %s is %u, phase %u\n", service, status, phase);
if (status == GNUNET_ARM_SERVICE_MONITORING_STARTED)
{
phase++;
- GNUNET_ARM_request_service_start (arm, "do-nothing",
+ GNUNET_ARM_request_service_start (arm, SERVICE,
GNUNET_OS_INHERIT_STD_OUT_AND_ERR, TIMEOUT, NULL, NULL);
return;
}
if (phase == 1)
{
GNUNET_break (status == GNUNET_ARM_SERVICE_STARTING);
- GNUNET_break (0 == strcasecmp (service, "do-nothing"));
+ GNUNET_break (0 == strcasecmp (service, SERVICE));
GNUNET_break (phase == 1);
- FPRINTF (stderr, "%s", "do-nothing is starting\n");
+ LOG ("do-nothing is starting\n");
phase++;
ok = 1;
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &kill_task, NULL);
}
- else if ((phase == 2) && (strcasecmp ("do-nothing", service) == 0))
+ else if ((phase == 2) && (strcasecmp (SERVICE, service) == 0))
{
/* We passively monitor ARM for status updates. ARM should tell us
* when do-nothing dies (no need to run a service upness test ourselves).
*/
if (status == GNUNET_ARM_SERVICE_STARTING)
{
- FPRINTF (stderr, "%s", "do-nothing is starting\n");
+ LOG ("do-nothing is starting\n");
GNUNET_SCHEDULER_add_now (kill_task, &ok);
}
else if ((status == GNUNET_ARM_SERVICE_STOPPED) && (trialCount == 14))
}
}
+
static void
-arm_start_cb (void *cls, struct GNUNET_ARM_Handle *h, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
+arm_start_cb (void *cls, enum GNUNET_ARM_RequestStatus status, const char *servicename, enum GNUNET_ARM_Result result)
{
GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
GNUNET_break (phase == 0);
- FPRINTF (stderr, "Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+ LOG ("Sent 'START' request for arm to ARM %s\n",
+ (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
}
+
static void
task (void *cls, char *const *args, const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *c)
GNUNET_free (armconfig);
}
- arm = GNUNET_ARM_alloc (cfg);
- GNUNET_ARM_connect (arm, NULL, NULL);
- mon = GNUNET_ARM_monitor_alloc (cfg);
- GNUNET_ARM_monitor (mon, srv_status, NULL);
+ arm = GNUNET_ARM_connect (cfg, NULL, NULL);
+ if (NULL != arm)
+ {
+ mon = GNUNET_ARM_monitor (cfg, &srv_status, NULL);
+ if (NULL != mon)
+ {
#if START_ARM
- GNUNET_ARM_request_service_start (arm, "arm",
- GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
+ GNUNET_ARM_request_service_start (arm, "arm",
+ GNUNET_OS_INHERIT_STD_OUT_AND_ERR, GNUNET_TIME_UNIT_ZERO, arm_start_cb, NULL);
#else
- arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
+ arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_SERVICE_STARTING);
#endif
+ }
+ else
+ {
+ GNUNET_ARM_disconnect_and_free (arm);
+ arm = NULL;
+ }
+ }
}
+
static int
check ()
{
char *const argv[] = {
"test-exponential-backoff",
- "-c", "test_arm_api_data.conf",
+ "-c", CFGFILENAME,
NULL
};
struct GNUNET_GETOPT_CommandLineOption options[] = {
return ok;
}
+
static int
init ()
{
+ struct GNUNET_CONFIGURATION_Handle *cfg;
+ char pwd[PATH_MAX];
+ char *binary;
+
+ cfg = GNUNET_CONFIGURATION_create ();
+ if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg,
+ "test_arm_api_data.conf"))
+ return GNUNET_SYSERR;
+ if (NULL == getcwd (pwd, PATH_MAX))
+ return GNUNET_SYSERR;
+ GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd, BINARY));
+ GNUNET_CONFIGURATION_set_value_string (cfg, SERVICE, "BINARY", binary);
+ GNUNET_free (binary);
+ if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, CFGFILENAME))
+ {
+ GNUNET_CONFIGURATION_destroy (cfg);
+ return GNUNET_SYSERR;
+ }
+ GNUNET_CONFIGURATION_destroy (cfg);
+
#if LOG_BACKOFF
killLogFileName = GNUNET_DISK_mktemp ("exponential-backoff-waiting.log");
if (NULL == (killLogFilePtr = FOPEN (killLogFileName, "w")))
GNUNET_assert (0 == fclose (killLogFilePtr));
GNUNET_free (killLogFileName);
#endif
+ (void) unlink (CFGFILENAME);
}
"WARNING",
NULL);
- init ();
+ if (GNUNET_OK != init ())
+ return 1;
ret = check ();
houseKeep ();
return ret;