Revision ffebdf87

View differences:

src/streaming.c
115 115
		if(((*stc)->ch_locks))
116 116
			chunk_locks_destroy(&((*stc)->ch_locks));
117 117
		if(((*stc)->transactions))
118
			transactions_destroy((*stc)->transactions);
118
			transaction_destroy(&((*stc)->transactions));
119 119
		if(((*stc)->cb))
120 120
			cb_destroy((*stc)->cb);
121 121
		free((*stc));
src/transaction.c
63 63
			stl->forward->backward = stl->backward;
64 64
		}
65 65
	}
66
	nodeid_free(stl->st.id);
66 67
	free(stl);
67 68
}	
68 69

  
......
112 113
}
113 114

  
114 115
// register the moment when a transaction is started
115
// return a  new transaction id
116
// return a  new transaction id or 0 in case of failure
116 117
uint16_t transaction_create(struct service_times_element ** stl, struct nodeID *id)
117 118
{
118
	static uint16_t trans_id = 1;
119

  
120 119
	struct service_times_element *stl2;
121 120
	struct timeval current_time;
122 121

  
123
	check_neighbor_status_list(stl);
122
	if (stl && id)
123
	{
124
		check_neighbor_status_list(stl);
124 125

  
125
	gettimeofday(&current_time, NULL);
126
		gettimeofday(&current_time, NULL);
126 127

  
127
	//create new trans_id;
128
	trans_id++;
129
	//skip 0
130
	if (!trans_id) trans_id++;
131 128

  
132
	// create a new element in the list with its offer_sent_time and set accept_received_time to -1.0
133
	stl2 = (struct service_times_element*) malloc(sizeof(struct service_times_element));
134
	stl2->st.trans_id = trans_id;
135
	stl2->st.offer_sent_time = current_time.tv_sec + current_time.tv_usec*1e-6;
136
	stl2->st.accept_received_time = -1.0;
137
	stl2->st.id = id;	//TODO: nodeid_dup
129
		// create a new element in the list with its offer_sent_time and set accept_received_time to -1.0
130
		stl2 = (struct service_times_element*) malloc(sizeof(struct service_times_element));
131
		stl2->st.offer_sent_time = current_time.tv_sec + current_time.tv_usec*1e-6;
132
		stl2->st.accept_received_time = -1.0;
133
		stl2->st.id = nodeid_dup(id);
138 134

  
139
	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));
135
		stl2->backward = NULL;
140 136

  
141
	stl2->backward = NULL;
137
		if (*stl)
138
		{
139
			stl2->st.trans_id = (*stl)->st.trans_id + 1 ? (*stl)->st.trans_id + 1 : 1;
140
			(*stl)->backward = stl2;
141
			stl2->forward = (*stl);
142
			*stl = stl2;
143
		} else {
144
			stl2->st.trans_id = 1;
145
			stl2->forward = NULL;
146
			*stl = stl2;
147
		}
148
		dprintf("LIST: adding trans_id %d to the list, offer_sent_time %f -- LIST IS NOT EMPTY\n", stl2->st.trans_id, (current_time.tv_sec + current_time.tv_usec*1e-6));
142 149

  
143
	if (*stl)
144
	{
145
		(*stl)->backward = stl2;
146
		stl2->forward = (*stl);
147
		*stl = stl2;
148
	} else {
149
		stl2->forward = NULL;
150
		*stl = stl2;
150
		return stl2->st.trans_id;
151 151
	}
152

  
153
	return trans_id;
152
	return 0;
154 153
}
155 154

  
156 155

  
......
161 160
	struct service_times_element *stl_iterator;
162 161
	struct timeval current_time;
163 162

  
164
	gettimeofday(&current_time, NULL);
165
	stl_iterator = stl;
166

  
167
	// if an accept was received, look for the trans_id and add current_time to accept_received_time field
168
	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);
169
	// Iterate the list until you get the right element
170
	while (stl_iterator != NULL) {
171
			if (stl_iterator->st.trans_id == trans_id) {
172
				stl_iterator->st.accept_received_time = current_time.tv_sec + current_time.tv_usec*1e-6;
173
#ifndef MONL
174
				offer_accept_rtt_measure(id,stl_iterator->st.accept_received_time - stl_iterator->st.offer_sent_time);
175
				reception_measure(id);
176
#endif
177
				return true;
178
				}
179
			stl_iterator = stl_iterator->forward;
163
	if (stl && id && trans_id)
164
	{
165
		gettimeofday(&current_time, NULL);
166
		stl_iterator = stl;
167

  
168
		// if an accept was received, look for the trans_id and add current_time to accept_received_time field
169
		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);
170
		// Iterate the list until you get the right element
171
		while (stl_iterator != NULL) {
172
				if (stl_iterator->st.trans_id == trans_id) {
173
					stl_iterator->st.accept_received_time = current_time.tv_sec + current_time.tv_usec*1e-6;
174
	#ifndef MONL
175
					offer_accept_rtt_measure(id,stl_iterator->st.accept_received_time - stl_iterator->st.offer_sent_time);
176
					reception_measure(id);
177
	#endif
178
					return true;
179
					}
180
				stl_iterator = stl_iterator->forward;
181
		}
180 182
	}
181 183
	return false;
182 184
}
......
216 218
	return to_return;
217 219
}
218 220

  
219
void transactions_destroy(struct service_times_element * head)
221
void transaction_destroy(struct service_times_element ** head)
220 222
{
221
	while(head)
223
	while(*head)
222 224
	{
223
		_transaction_remove(&head, head);
225
		_transaction_remove(head, *head);
224 226
		// in case of nodeid_dup we should free that as well
225 227
	}
226 228

  
src/transaction.h
44 44
// it return -1.0 in case no trans_id is found
45 45
double transaction_remove(struct service_times_element ** head, uint16_t trans_id);
46 46

  
47
void transactions_destroy(struct service_times_element * head);
47
void transaction_destroy(struct service_times_element ** head);
48 48

  
49 49
#endif // TRANSACTION_H
test/Makefile
7 7
TARGET_SRC=$(wildcard ../src/*.c)
8 8
GRAPESLIB=$(GRAPES)/src/libgrapes.a
9 9

  
10
CFLAGS += -g -W -Wall -I ../include
10
CFLAGS += -g -W -Wall -I ../include -I../src -I$(GRAPES)/include
11 11
LDFLAGS += -l pstreamer -L ../src -lgrapes -L $(GRAPES)/src
12 12

  
13 13
all: $(OBJS)
test/transaction_test.c
1
#include<malloc.h>
2
#include<assert.h>
3
#include<transaction.h>
4

  
5
void transaction_create_test()
6
{
7
	struct service_times_element * head = NULL;
8
	struct nodeID * id;
9
	uint16_t tid;
10

  
11
	tid = transaction_create(NULL, NULL);
12
	assert(tid == 0);
13

  
14
	tid = transaction_create(&head, NULL);
15
	assert(tid == 0);
16

  
17
	id = create_node("127.0.0.1", 6000);
18
	tid = transaction_create(&head, id);
19
	assert(tid == 1);
20

  
21
	tid = transaction_create(&head, id);
22
	assert(tid == 2);
23

  
24
	transaction_destroy(&head);
25
	assert(head == NULL);
26
	nodeid_free(id);
27
	fprintf(stderr,"%s successfully passed!\n",__func__);
28
}
29

  
30
void transaction_reg_accept_test()
31
{
32
	struct service_times_element * head = NULL;
33
	struct nodeID * id = NULL;
34
	uint16_t tid = 1;
35
	bool res;
36

  
37
	res = transaction_reg_accept(head, 0, id);
38
	assert(!res);
39

  
40
	res = transaction_reg_accept(head, tid, id);
41
	assert(!res);
42

  
43
	id = create_node("127.0.0.1", 6000);
44
	tid = transaction_create(&head, id);
45

  
46
	res = transaction_reg_accept(head, tid, id);
47
	assert(res);
48

  
49
	res = transaction_reg_accept(head, tid+1, id);
50
	assert(!res);
51

  
52
	nodeid_free(id);
53
	transaction_destroy(&head);
54
	fprintf(stderr,"%s successfully passed!\n",__func__);
55
}
56

  
57
int main()
58
{
59
	transaction_create_test();
60
	transaction_reg_accept_test();
61
	return 0;
62
}

Also available in: Unified diff