Revision f1bad954

View differences:

transaction.c
12 12
#include <sys/time.h>
13 13

  
14 14
#include "dbg.h"
15
#include "measures.h"
16 15
#include "transaction.h"
17

  
18
typedef struct {
19
	uint16_t trans_id;
20
	double offer_sent_time;
21
	double accept_received_time;
22
	struct nodeID *id;
23
	} service_time;
24

  
25
// List to trace peer's service times
26
struct service_times_element {
27
	service_time st;
28
	struct service_times_element *backward;
29
	struct service_times_element *forward;
30
	};
16
#include "hrc.h"
31 17

  
32 18
static struct service_times_element *stl = NULL;
33 19

  
34
// Check the service times list to find elements over the timeout
35
void check_neighbor_status_list() {
36
	struct service_times_element *stl_iterator, *stl_aux;
37
	struct timeval current_time;
38
	bool something_got_removed;
39

  
40
	gettimeofday(&current_time, NULL);
41
	something_got_removed = false;
42
	
43
        dprintf("LIST: check trans_id list\n");
44
	
45
	// Check if list is empty
46
	if (stl == NULL) {
47
		return;
48
		}
49
        
50
	// Start from the beginning of the list
51
	stl_iterator = stl;
52
	stl_aux = stl;
53
	// Iterate the list until you get the right element
54
	while (stl_iterator != NULL) {
55
		// If the element has been in the list for a period greater than the timeout, remove it
56
//		if ( (stl_iterator->st.accept_received_time > 0.0 && ( (current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.accept_received_time) > TRANS_ID_MAX_LIFETIME) ||  ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.offer_sent_time > TRANS_ID_MAX_LIFETIME ) ) {
57
		if ( (current_time.tv_sec + current_time.tv_usec*1e-6 - stl_iterator->st.offer_sent_time) > TRANS_ID_MAX_LIFETIME) {
58
			 dprintf("LIST TIMEOUT: trans_id %d, offer_sent_time %f, accept_received_time %f\n", stl_iterator->st.trans_id, (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.offer_sent_time  ), (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.accept_received_time));
59
			 //fprintf(stderr, "LIST TIMEOUT: trans_id %d, offer_sent_time %f, accept_received_time %f\n", stl_iterator->st.trans_id, (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.offer_sent_time  ), (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.accept_received_time));
60
			// If it is the first element
61
			if (stl_iterator->backward == NULL) {
62
				stl = stl_iterator->forward;
63
				// Check if I have more than one element in the list
64
				if (stl_iterator->forward != NULL)
65
					stl_iterator->forward->backward = NULL;
66
				stl_iterator->forward = NULL;				
67
				}
68
			else { 	// I have to remove the last element of the list
69
				if (stl_iterator->forward == NULL) {
70
					stl_iterator->backward->forward = NULL;
71
					}
72
				// I have to remove an element in the middle
73
				else {
74
					stl_iterator->backward->forward = stl_iterator->forward;
75
					stl_iterator->forward->backward = stl_iterator->backward;
76
					}
77
				}
78
			something_got_removed = true;
79
			stl_aux = stl_iterator->forward;
80
			// Free the memory
81
			free(stl_iterator);
82
			}
83
		if (something_got_removed) {
84
			stl_iterator = stl_aux;
85
			something_got_removed = false;
86
			}
87
		else
88
			stl_iterator = stl_iterator->forward;
89
		}
90
	return;
91
}
92

  
93
// register the moment when a transaction is started
94
// return a  new transaction id
95
uint16_t transaction_create(struct nodeID *id)
96
{
97
	static uint16_t trans_id = 1;
98

  
20
// Add the moment I received a positive select in a list
21
// return true if a valid trans_id is found
22
uint16_t transaction_create(struct nodeID *id) {
99 23
	struct service_times_element *stl2, *stl_iterator;
100 24
	struct timeval current_time;
101

  
102
	check_neighbor_status_list();
103

  
104 25
	gettimeofday(&current_time, NULL);
105 26
	stl_iterator = stl;
106

  
107
	//create new trans_id;
108
	trans_id++;
109
	//skip 0
110
	if (!trans_id) trans_id++;
111

  
112
	// create a new element in the list with its offer_sent_time and set accept_received_time to -1.0
113
	stl2 = (struct service_times_element*) malloc(sizeof(struct service_times_element));
114
	stl2->st.trans_id = trans_id;
115
	stl2->st.offer_sent_time = current_time.tv_sec + current_time.tv_usec*1e-6;
116
	stl2->st.accept_received_time = -1.0;
117
	stl2->st.id = id;	//TODO: nodeid_dup
118
	stl2->backward = NULL;
119
	if (stl != NULL) {	//List is not empty
27
	static uint16_t trans_id = 0;
28
	
29
	++trans_id;
30
	if (trans_id == 0) 
31
		++trans_id; // skip transid 0 which is reserved for the source (for when trans_id reaches his maximum)
32
	        
33
	dprintf("LIST: adding trans_id %d to the list, offer_sent_time %f\n", trans_id, (current_time.tv_sec + current_time.tv_usec*1e-6));
34
	//List is not empty
35
	if (stl != NULL) {
120 36
		dprintf("LIST: adding trans_id %d to the list, offer_sent_time %f -- LIST IS NOT EMPTY\n", trans_id, (current_time.tv_sec + current_time.tv_usec*1e-6));
37
		stl2 = (struct service_times_element*) malloc(sizeof(struct service_times_element));
38
		stl2->st.trans_id = trans_id;
39
		stl2->st.offer_sent_time = current_time.tv_sec + current_time.tv_usec*1e-6;
40
		stl2->st.accept_received_time = -1.0;
41
		stl2->st.id = id;
121 42
		stl2->forward = stl;
43
		stl2->backward = NULL;
122 44
		stl->backward = stl2;
123 45
		stl = stl2; 
124
	} else { // This is the first element to insert
46
		}
47
	else { // This is the first element to insert
125 48
		dprintf("LIST: adding trans_id %d to the list, offer_sent_time %f -- LIST IS EMPTY\n", trans_id, current_time.tv_sec + current_time.tv_usec*1e-6);
126
		stl2->forward = NULL;
127
		stl = stl2; 
128
	}
49
		stl = (struct service_times_element*) malloc(sizeof(struct service_times_element));
50
		stl->st.trans_id = trans_id;
51
		stl->st.offer_sent_time = current_time.tv_sec + current_time.tv_usec*1e-6;
52
		stl->st.accept_received_time = -1.0;
53
		stl->st.id = id;
54
		stl->forward = NULL;
55
		stl->backward = NULL;
56
		}
129 57
	return trans_id;
130
}
131

  
58
	}
132 59

  
133
// Add the moment I received a positive select in a list
134
// return true if a valid trans_id is found
135
bool transaction_reg_accept(uint16_t trans_id, struct nodeID *id)
136
{
60
// Change transaction status when an accept has been received
61
bool transaction_change(uint16_t trans_id, struct nodeID *id) {
137 62
	struct service_times_element *stl_iterator;
138 63
	struct timeval current_time;
139

  
140 64
	gettimeofday(&current_time, NULL);
141 65
	stl_iterator = stl;
142

  
66
	        
143 67
	// if an accept was received, look for the trans_id and add current_time to accept_received_time field
144
	dprintf("LIST: changing trans_id %d to the list, accept received %f\n", trans_id, current_time.tv_sec + current_time.tv_usec*1e-6);
68
	dprintf("LIST: changing trans_id %d to the list -- ACCEPT\n", trans_id);
145 69
	// Iterate the list until you get the right element
146 70
	while (stl_iterator != NULL) {
147
			if (stl_iterator->st.trans_id == trans_id) {
148
				stl_iterator->st.accept_received_time = current_time.tv_sec + current_time.tv_usec*1e-6;
149
				return true;
150
				}
151
			stl_iterator = stl_iterator->forward;
152
	}
71
		if (stl_iterator->st.trans_id == trans_id) {
72
			stl_iterator->st.accept_received_time = current_time.tv_sec + current_time.tv_usec*1e-6;
73
			return true;
74
			}
75
		stl_iterator = stl_iterator->forward;
76
		}
153 77
	return false;
154 78
}
155 79

  
80

  
156 81
// Used to get the time elapsed from the moment I get a positive select to the moment i get the ACK
157 82
// related to the same chunk
158 83
// it return -1.0 in case no trans_id is found
......
160 85
	struct service_times_element *stl_iterator;
161 86
	double to_return;
162 87

  
163
    dprintf("LIST: deleting trans_id %d\n", trans_id);
164

  
88
    	dprintf("LIST: deleting trans_id %d\n", trans_id);
89
        	
165 90
	// Start from the beginning of the list
166 91
	stl_iterator = stl;
167 92
	// Iterate the list until you get the right element
......
172 97
		}
173 98
	if (stl_iterator == NULL){
174 99
		// not found
175
        dprintf("LIST: deleting trans_id %d -- STL is already NULL \n", trans_id);
100
		dprintf(stderr, "LIST: deleting trans_id %d -- STL is already NULL \n", trans_id);
176 101
		return -2.0;
177 102
	}
178

  
103
	
179 104
	to_return = stl_iterator->st.accept_received_time;
180

  
105
               
181 106
	// I have to remove the element from the list
182 107
	// If it is the first element
183 108
	if (stl_iterator->backward == NULL) {
......
199 124
			}
200 125
		}
201 126
	free(stl_iterator);
202
	// Remove RTT measure from queue delay
203
// 	if (hrc_enabled() && (to_return.accept_received_time > 0.0 && get_measure(to_return.id, 1, MIN) > 0.0 && get_measure(to_return.id, 1, MIN) != NAN))
204
// 		return (to_return.accept_received_time - get_measure(to_return.id, 1, MIN));
205 127
	return to_return;
206
}
128
	}
129
	
130
// Check the service times list to find elements over the timeout
131
void transactions_check() {
132
	struct service_times_element *stl_iterator, *stl_aux;
133
	struct timeval current_time;
134
	bool something_got_removed;
135

  
136
	gettimeofday(&current_time, NULL);
137
	something_got_removed = false;
138
	
139
	// Check if list is empty
140
	if (stl == NULL) {
141
		return;
142
		}
143
        
144
	// Start from the beginning of the list
145
	stl_iterator = stl;
146
	stl_aux = stl;
147
	// Iterate the list until you get the right element
148
	while (stl_iterator != NULL) {
149
		// If the element has been in the list for a period greater than the timeout, remove it
150
		if ( ( (current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.offer_sent_time) > TRANS_ID_MAX_LIFETIME) {
151
			dprintf("LIST TIMEOUT: trans_id %d, offer_sent_time %f, accept_received_time %f\n", stl_iterator->st.trans_id, (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.offer_sent_time  ), (double) ((current_time.tv_sec + current_time.tv_usec*1e-6) - stl_iterator->st.accept_received_time));
152
			if (stl_iterator->backward == NULL) {
153
				stl = stl_iterator->forward;
154
				// Check if I have more than one element in the list
155
				if (stl_iterator->forward != NULL)
156
					stl_iterator->forward->backward = NULL;
157
				stl_iterator->forward = NULL;
158
				}
159
			else { 	// I have to remove the last element of the list
160
				if (stl_iterator->forward == NULL) {
161
					stl_iterator->backward->forward = NULL;
162
					free(stl_iterator);
163
					return;
164
					}
165
				// I have to remove an element in the middle
166
				else {
167
					stl_iterator->backward->forward = stl_iterator->forward;
168
					stl_iterator->forward->backward = stl_iterator->backward;
169
					}
170
				}
171
			
172
			something_got_removed = true;
173
			stl_aux = stl_iterator->forward;
174
			// Free the memory
175
			free(stl_iterator);
176
			// A thread was interrupted, so free it
177
			adjust_offer_threads(THREAD_TIMEOUT, -1);
178
			}
179
		if (something_got_removed) {
180
			stl_iterator = stl_aux;
181
			something_got_removed = false;
182
			}
183
		else
184
			stl_iterator = stl_iterator->forward;
185
		}
186
	// reincarnate eventual deleted offers
187
	return;
188
	}
transaction.h
8 8
#ifndef TRANSACTION_H
9 9
#define TRANSACTION_H
10 10

  
11
#include <stdbool.h>
12

  
13 11
/* timeout of the offers thread. If it is not updated, it is deleted */
14
#define TRANS_ID_MAX_LIFETIME 1.0
12
#define TRANS_ID_MAX_LIFETIME 1.5
13

  
14
// struct nodeID;
15

  
16
typedef struct {
17
	uint16_t trans_id;
18
	double offer_sent_time;
19
	double accept_received_time;
20
	struct nodeID *id;
21
	} service_time;
15 22

  
16
struct nodeID;
23

  
24
// List to trace peer's service times
25
struct service_times_element {
26
	service_time st;
27
	struct service_times_element *backward;
28
	struct service_times_element *forward;
29
	};
17 30

  
18 31
// register the moment when a transaction is started
19 32
// return a  new transaction id
20 33
uint16_t transaction_create(struct nodeID *id);
21 34

  
22
// Add the moment I received a positive select in a list
23
// return true if a valid trans_id is found
24
bool transaction_reg_accept(uint16_t trans_id, struct nodeID *id);
35
// Change transaction status when an accept has been received
36
bool transaction_change(uint16_t trans_id, struct nodeID *id);
25 37

  
26 38
// Used to get the time elapsed from the moment I get a positive select to the moment i get the ACK
27 39
// related to the same chunk
28 40
// it return -1.0 in case no trans_id is found
29 41
double transaction_remove(uint16_t trans_id);
30 42

  
43
// Check neighbor list to prune elapsed pending threads
44
void transactions_check();
45

  
31 46
#endif // TRANSACTION_H

Also available in: Unified diff