Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1230818d056ae567534fbfdc3e6dacdeb150e840
[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, unsigned int destination_id, char *mailbox, const char *hostname) {
17
18   task_data_t data = xbt_new(s_task_data_t,1);
19
20   data->type = TASK_FIND_NODE;
21   data->sender_id = sender_id;
22   data->destination_id = destination_id;
23   data->answer = NULL;
24   data->answer_to = mailbox;
25   data->issuer_host_name = hostname;
26
27
28   msg_task_t task = MSG_task_create(NULL,COMP_SIZE,COMM_SIZE,data);  
29   
30   return task;
31 }
32 /**
33   * Creates a new "answer to find node" task
34   *  @param sender_id the node who sent the task
35   *  @param destination_id the node that should be found
36   *  @param answer the answer to send
37   *  @param mailbox The mailbox of the sender
38   *  @param hostname sender hostname
39   */
40 msg_task_t task_new_find_node_answer(unsigned int sender_id, unsigned int destination_id, answer_t answer, char *mailbox, const char *hostname) {
41
42   task_data_t data = xbt_new(s_task_data_t,1);
43   
44   data->type = TASK_FIND_NODE_ANSWER;
45   data->sender_id = sender_id;
46   data->destination_id = destination_id;
47   data->answer = answer;
48   data->answer_to = mailbox;
49   data->issuer_host_name = hostname;      
50
51   msg_task_t task = MSG_task_create(NULL,COMP_SIZE,COMM_SIZE,data);  
52   
53   return task;
54 }
55 /**
56   * Creates a new "ping" task
57   * @param sender_id : sender node identifier
58   * @param mailbox : mailbox where we should respond
59   * @param hostname : hostname of the sender, for debugging purposes
60   */
61 msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, const char *hostname) {
62
63   task_data_t data = xbt_new(s_task_data_t,1);
64
65   data->type = TASK_PING;
66   data->sender_id = sender_id;
67   data->destination_id = 0;
68   data->answer = NULL;
69   data->answer_to = mailbox;
70   data->issuer_host_name = hostname;
71
72   msg_task_t task = MSG_task_create(NULL,COMP_SIZE,COMM_SIZE,data);  
73   
74   return task;
75 }
76 /**
77   * Creates a new "ping answer" task
78   * @param sender_id : sender node identifier
79   * @param mailbox : mailbox of the sender
80   * @param hostname : hostname of the sender, for debugging purposes
81   */
82 msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, const char *hostname) {
83
84   task_data_t data = xbt_new(s_task_data_t,1);
85
86   data->type = TASK_PING_ANSWER;
87   data->sender_id = sender_id;
88   data->destination_id = 0;
89   data->answer = NULL;
90   data->answer_to = mailbox;
91   data->issuer_host_name = hostname;
92
93   msg_task_t task = MSG_task_create(NULL,COMP_SIZE,COMM_SIZE,data);  
94   
95   return task;
96 }
97 /**
98   * Destroys a task and its data
99   * @param task the task that'll be destroyed
100   */
101 void task_free(msg_task_t task) {
102   xbt_assert( (task != NULL), "Tried to free a NULL task");
103
104   task_data_t data = MSG_task_get_data(task);
105     
106   if (data->answer) {
107     answer_free(data->answer);
108   }
109   xbt_free(data);
110   
111   MSG_task_destroy(task);
112
113 }
114 /**
115   * Destroys a task and its data (taking a void* pointer
116   * @param task The task that'll be destroyed
117   */
118 void task_free_v(void *task) {
119   task_free(task);
120 }