Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bcfc22888a4d7f9eb15c252774a93212ce59ee57
[simgrid.git] / examples / deprecated / msg / dht-kademlia / task.c
1 /* Copyright (c) 2012-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "task.h"
8 #include "answer.h"
9
10 /** @brief Creates a new "find node" task
11   * @param sender_id the id of the node who sends the task
12   * @param destination_id the id the sender is trying to find
13   * @param hostname the hostname of the node, for logging purposes
14   */
15 msg_task_t task_new_find_node(unsigned int sender_id, unsigned int destination_id, char *mailbox, const char *hostname)
16 {
17   task_data_t data = xbt_new(s_task_data_t, 1);
18
19   data->type = TASK_FIND_NODE;
20   data->sender_id = sender_id;
21   data->destination_id = destination_id;
22   data->answer = NULL;
23   data->answer_to = mailbox;
24   data->issuer_host_name = hostname;
25
26   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
27
28   return task;
29 }
30
31 /** @brief Creates a new "answer to find node" task
32   *  @param sender_id the node who sent the task
33   *  @param destination_id the node that should be found
34   *  @param answer the answer to send
35   *  @param mailbox The mailbox of the sender
36   *  @param hostname sender hostname
37   */
38 msg_task_t task_new_find_node_answer(unsigned int sender_id, unsigned int destination_id, answer_t answer,
39                                      char *mailbox, const char *hostname)
40 {
41   task_data_t data = xbt_new(s_task_data_t, 1);
42
43   data->type = TASK_FIND_NODE_ANSWER;
44   data->sender_id = sender_id;
45   data->destination_id = destination_id;
46   data->answer = answer;
47   data->answer_to = mailbox;
48   data->issuer_host_name = hostname;
49
50   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
51
52   return task;
53 }
54
55 /** @brief Destroys a task and its data
56   * @param task the task that'll be destroyed
57   */
58 void task_free(msg_task_t task)
59 {
60   xbt_assert((task != NULL), "Tried to free a NULL task");
61
62   task_data_t data = MSG_task_get_data(task);
63
64   if (data->answer) {
65     answer_free(data->answer);
66   }
67   xbt_free(data);
68
69   MSG_task_destroy(task);
70 }
71
72 /** @brief Destroys a task and its data (taking a void* pointer
73   * @param task The task that'll be destroyed
74   */
75 void task_free_v(void *task)
76 {
77   task_free(task);
78 }