-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproposal.c
148 lines (121 loc) · 4.81 KB
/
proposal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* proposal.c
*
* Created on: Aug 22, 2019
* Author: tonglin
*/
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "proposal.h"
time_stamp proposal_get_time_usec() {
struct timeval tv;
gettimeofday(&tv, NULL);
return 1000000 * tv.tv_sec + tv.tv_usec;
}
proposal* compose_proposal(proposal_id pid, int op_type, void* p_data, size_t p_data_len){
proposal* p = calloc(1, sizeof(proposal));
p->pid = pid;
p->state = PS_DEFAULT;
p->time = proposal_get_time_usec();
p->isLocal = 0;//set to 1 ONLY when approved and before execute locally.
p->op_type = op_type;
p->p_data_len = p_data_len;
p->proposal_data = p_data;//calloc(1, p_data_len);
p->result_obj_local = NULL;
//printf("%s:%d: test proposal_data len = %lu, data = %p\n", __func__, __LINE__, p->p_data_len, p->proposal_data);
return p;
}
void proposal_check(proposal* p){
assert(p);
printf("%s:%d: pid = %d, op_type = %d, isLocal = %d, state = %d, time =%lu, p_data_len = %lu, p_data = %p, result_obj_local = %p\n",
__func__, __LINE__, p->pid, p->op_type, p->isLocal, p->state, p->time, p->p_data_len, p->proposal_data, p->result_obj_local);
}
size_t proposal_encoder(proposal* p, void**buf_out){
size_t cal_size = sizeof(proposal_id) +
sizeof(proposal_state) +
sizeof(time_stamp) +
sizeof(int) +
sizeof(int) +
sizeof(size_t) +
p->p_data_len;
//printf("%s:%d: pid = %d\n", __func__, __LINE__, p->pid);
//printf("%s:%d: p->state = %d\n", __func__, __LINE__, p->state);
//printf("%s:%d: p->time = %lu\n", __func__, __LINE__, p->time);
//printf("%s:%d: p->isLocal = %d\n", __func__, __LINE__, p->isLocal);
//printf("%s:%d: p->op_type = %d\n", __func__, __LINE__, p->op_type);
//printf("%s:%d: proposal_encoder: cal_size(total size) = %lu\n", __func__, __LINE__, cal_size);
//printf("%s:%d: proposal_encoder p->p_data_len = %lu\n", __func__, __LINE__, p->p_data_len);
*buf_out = calloc(1, cal_size);//sizeof(proposal) + p->p_data_len
void* cur = *buf_out;
*(proposal_id*)cur = p->pid;
cur = (char*)cur + sizeof(proposal_id);
*(proposal_state*)cur = p->state;
cur = (char*)cur + sizeof(proposal_state);
*(time_stamp*)cur = p->time;
cur = (char*)cur + sizeof(time_stamp);
*(int*)cur = p->isLocal;
cur = (char*)cur + sizeof(int);
*(int*)cur = p->op_type;
cur = (char*)cur + sizeof(int);
*(size_t*)cur = p->p_data_len;
cur = (char*)cur + sizeof(size_t);
if(p->p_data_len > 0) {
memcpy(cur, p->proposal_data, p->p_data_len);
//cur = (char*)cur + p->p_data_len;
}
return cal_size;
}
//This is called only when you have a proposal_buf
proposal* proposal_decoder(void* buf_in){
proposal* p = calloc(1, sizeof(proposal));
p->pid = *(proposal_id*)buf_in;
buf_in = (char*)buf_in + sizeof(proposal_id);
//printf("%s:%d: pid = %d\n", __func__, __LINE__, p->pid);
p->state = *(proposal_state*)buf_in;
buf_in = (char*)buf_in + sizeof(proposal_state);
//printf("%s:%d: p->state = %d\n", __func__, __LINE__, p->state);
p->time = *(time_stamp*)buf_in;
buf_in = (char*)buf_in + sizeof(time_stamp);
//printf("%s:%d: p->time = %lu\n", __func__, __LINE__, p->time);
p->isLocal = *(int*)buf_in;
buf_in = (char*)buf_in + sizeof(int);
//printf("%s:%d: p->isLocal = %d\n", __func__, __LINE__, p->isLocal);
p->op_type = *(int*)buf_in;
buf_in = (char*)buf_in + sizeof(int);
//printf("%s:%d: p->op_type = %d\n", __func__, __LINE__, p->op_type);
p->p_data_len = *(size_t*)buf_in;
buf_in = (char*)buf_in + sizeof(size_t);
//printf("%s:%d: p->p_data_len = %lu\n", __func__, __LINE__, p->p_data_len);
if(p->p_data_len > 0) {
p->proposal_data = calloc(1, p->p_data_len);
memcpy(p->proposal_data, buf_in, p->p_data_len);
buf_in = (char*)buf_in + p->p_data_len;
}
else
p->proposal_data = NULL;
// a remote proposal won't carry a resulting obj, so it's safe to set to NULL.
// This field should be assigned only when the proposal is to execute locally (then carry a resulting obj.)
p->result_obj_local = NULL;
return p;
}
void proposal_buf_test(void* buf_in){
proposal* p = proposal_decoder(buf_in);
printf("Checking proposal content: p->pid = %d, p->state = %d, p->time = %lu, p->isLocal = %d, p->op_type = %d, p->p_data_len = %lu\n",
p->pid, p->state, p->time, p->isLocal, p->op_type, p->p_data_len);
free(p);
}
time_stamp set_proposal_time(proposal* p){
assert(p);
p->time = proposal_get_time_usec();
return p->time;
}
proposal_id new_proposal_ID(){
struct timeval tv;
gettimeofday(&tv, NULL);
return getpid() + tv.tv_usec;
}