Logo AND Algorithmique Numérique Distribuée

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