Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use cat+here document, instead of non-portable echo -e.
[simgrid.git] / examples / msg / kademlia / task.c
1 /* Copyright (c) 2012. 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 #include "task.h"
7 #include "answer.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_task,
9                              "Messages specific for this msg example");
10 /**
11   * Creates a new "find node" task
12   * @param sender_id the id of the node who sends the task
13   * @param destination_id the id the sender is trying to find
14   * @param hostname the hostname of the node, for logging purposes
15   */
16 msg_task_t task_new_find_node(unsigned int sender_id,
17                               unsigned int destination_id, char *mailbox,
18                               const char *hostname)
19 {
20
21   task_data_t data = xbt_new(s_task_data_t, 1);
22
23   data->type = TASK_FIND_NODE;
24   data->sender_id = sender_id;
25   data->destination_id = destination_id;
26   data->answer = NULL;
27   data->answer_to = mailbox;
28   data->issuer_host_name = hostname;
29
30
31   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
32
33   return task;
34 }
35
36 /**
37   * Creates a new "answer to find node" task
38   *  @param sender_id the node who sent the task
39   *  @param destination_id the node that should be found
40   *  @param answer the answer to send
41   *  @param mailbox The mailbox of the sender
42   *  @param hostname sender hostname
43   */
44 msg_task_t task_new_find_node_answer(unsigned int sender_id,
45                                      unsigned int destination_id,
46                                      answer_t answer, char *mailbox,
47                                      const char *hostname)
48 {
49
50   task_data_t data = xbt_new(s_task_data_t, 1);
51
52   data->type = TASK_FIND_NODE_ANSWER;
53   data->sender_id = sender_id;
54   data->destination_id = destination_id;
55   data->answer = answer;
56   data->answer_to = mailbox;
57   data->issuer_host_name = hostname;
58
59   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
60
61   return task;
62 }
63
64 /**
65   * Creates a new "ping" task
66   * @param sender_id : sender node identifier
67   * @param mailbox : mailbox where we should respond
68   * @param hostname : hostname of the sender, for debugging purposes
69   */
70 msg_task_t task_new_ping(unsigned int sender_id, char *mailbox,
71                          const char *hostname)
72 {
73
74   task_data_t data = xbt_new(s_task_data_t, 1);
75
76   data->type = TASK_PING;
77   data->sender_id = sender_id;
78   data->destination_id = 0;
79   data->answer = NULL;
80   data->answer_to = mailbox;
81   data->issuer_host_name = hostname;
82
83   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
84
85   return task;
86 }
87
88 /**
89   * Creates a new "ping answer" task
90   * @param sender_id : sender node identifier
91   * @param mailbox : mailbox of the sender
92   * @param hostname : hostname of the sender, for debugging purposes
93   */
94 msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox,
95                                 const char *hostname)
96 {
97
98   task_data_t data = xbt_new(s_task_data_t, 1);
99
100   data->type = TASK_PING_ANSWER;
101   data->sender_id = sender_id;
102   data->destination_id = 0;
103   data->answer = NULL;
104   data->answer_to = mailbox;
105   data->issuer_host_name = hostname;
106
107   msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data);
108
109   return task;
110 }
111
112 /**
113   * Destroys a task and its data
114   * @param task the task that'll be destroyed
115   */
116 void task_free(msg_task_t task)
117 {
118   xbt_assert((task != NULL), "Tried to free a NULL task");
119
120   task_data_t data = MSG_task_get_data(task);
121
122   if (data->answer) {
123     answer_free(data->answer);
124   }
125   xbt_free(data);
126
127   MSG_task_destroy(task);
128
129 }
130
131 /**
132   * Destroys a task and its data (taking a void* pointer
133   * @param task The task that'll be destroyed
134   */
135 void task_free_v(void *task)
136 {
137   task_free(task);
138 }