]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_contract.c
Adding more comments
[frescor/fwp.git] / fwp / lib / mngt / fwp_contract.c
1 /**
2  * \file fwp_contract.c
3  *
4  * Routines for manipulation with contract
5  *
6  */
7 #include "fwp_msg.h"
8 #include "fwp_contract.h"
9 #include "fwp_contract_table.h"
10 #include "fwp_mngt.h"
11 #include <pthread.h>
12
13 static int fwp_contract_is_reserved(fwp_contract_d_t contract)
14 {
15         return (contract->status == FWP_CONT_RESERVED);
16 }
17
18 int fwp_contract_is_negotiated(fwp_contract_d_t contract)
19 {
20         return (contract->status == FWP_CONT_NEGOTIATED);
21 }
22
23 /**
24  * Negotiates contract for application. Negotiation request is sent to 
25  * fwp agent and then waits for response.
26  *
27  * \param[in] contractd Descriptor of the contract to negotiate
28  * \param[out] vresd Vres descriptor after the contract was accepted
29  *
30  * \return  
31  * If successful, the  function returns zero and vres descriptor is
32  * stored in vresdp parameter, otherwise,  an  error  number  shall  be
33  * returned to indicate the error. If the contract is not negotiated EAGAIN 
34  * error is returned.
35  */
36 /*int fwp_contract_negotiate(fwp_contract_d_t contractd, void *vresdp) */
37 int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
38 {
39         fwp_contract_reserve(contractd);
40         if (!fwp_contract_is_reserved(contractd)) {     
41                 return -EAGAIN;
42         }       
43         
44         fwp_contract_commit(contractd, vresdp);
45         return 0;
46 }
47
48 /**
49  * Creates contract
50  * 
51  * \param[in] contract User-level contract
52  *
53  * \return On success, returns contract descriptor
54  *
55  */
56 /*fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract, resource_d_t resource)*/
57 fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract)
58 {
59         fwp_contract_data_t *contdata;
60
61         contdata = fwp_contract_data_new();
62         /* To be compatible: reserve vres, get its id, and store it to
63          * condata->id
64          */ 
65         contdata->vresd = fwp_vres_alloc();
66         contdata->id = fwp_vres_get_id(contdata->vresd);
67         
68         memcpy(&contdata->contract, contract, sizeof(*contract));
69         contdata->status = FWP_CONT_NOTNEGOTIATED;      
70         /* Add to contract table */
71         fwp_contract_table_insert(&fwp_participant_this->contract_table, 
72                                         contdata);
73         
74         FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
75         return contdata;
76 }
77
78 /**
79  * Destroys contract
80  * 
81  * \param[in] contractd Contract descriptor
82  *
83  * \return On success, returns zero.
84  * On error, returns negative error code.
85  *
86  */
87 int fwp_contract_destroy(fwp_contract_d_t contractd)
88 {
89         fwp_contract_data_t *contdata = contractd;
90
91         if (contdata->status != FWP_CONT_NOTNEGOTIATED)
92                 return -EPERM;
93         fwp_contract_data_delete(contdata);
94         return 0;
95 }
96
97 /**
98  * Reserves contract
99  * 
100  * \param[in] contractd Contract descriptor
101  *
102  * \return On success, returns zero.
103  * On error, returns negative error code.
104  *
105  */
106 int fwp_contract_reserve(fwp_contract_d_t contractd)
107 {
108         fwp_contract_data_t *contdata = contractd;
109         fwp_msgb_t *msgb;
110         fwp_msg_type_t msg_type;
111         fwp_participant_id_t participant_id;
112         
113         /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
114                                                 contract_id);*/
115         /* Send contract to manager */
116         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
117                                 sizeof(struct fwp_msg_contracthdr) +
118                                 sizeof(struct fwp_msg_contract) +
119                                 sizeof(struct fwp_vres_params));
120
121         /* reserve place for header */
122         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
123         /*Add contract header*/
124         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
125         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
126         /* Add contract params */
127         fwp_msg_contract_in(msgb->tail, &contdata->contract);
128         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
129
130         fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
131                         fwp_participant_this, fwp_participant_mngr);
132         fwp_mngt_recv(&msg_type, &participant_id, msgb);
133
134         fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
135         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
136         
137         if (contdata->status == FWP_CONT_RESERVED) {
138                 fwp_msg_vres_params_out(msgb->data, &contdata->vres_params);
139                 FWP_DEBUG("Received vres params budget=%d period=%d ac=%d \n",
140                                 contdata->vres_params.budget, 
141                                 contdata->vres_params.period_usec,
142                                 contdata->vres_params.ac_id);
143                 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
144         }
145         
146         FWP_DEBUG("STATUS = %d\n", contdata->status);
147         return 0;
148 }
149
150 /**
151  * Commits contract
152  * 
153  * \param[in] contractd Contract descriptor
154  * \param[out] vresd Descriptor of vres
155  *
156  * \return On success, returns zero.
157  * On error, returns negative error code.
158  *
159  */
160 int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
161 {
162         fwp_contract_data_t *contdata = contractd;
163         fwp_msgb_t *msgb;
164         
165         /* Send COMMIT to manager */
166         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
167                                 sizeof(struct fwp_msg_contracthdr)); 
168         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
169         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
170         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
171         FWP_DEBUG("Commit contract id =%d\n", contdata->id);
172         
173         fwp_mngt_send(FWP_MSG_COMMIT, msgb, 
174                         fwp_participant_this, fwp_participant_mngr);
175         
176         contdata->status = FWP_CONT_NEGOTIATED;
177         /* Set parameters of vres 
178          * and activate it if needed */
179         fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
180         *vresdp = contdata->vresd;
181
182         /*TODO: error handling */
183         return 0;
184 }
185
186 /**
187  * Cancels contract
188  * 
189  * \param[in] contractd Contract descriptor
190  *
191  * \return On success, returns zero.
192  * On error, returns negative error code.
193  *
194  */
195 int fwp_contract_cancel(fwp_contract_d_t contractd)
196 {
197         fwp_contract_data_t *contdata = contractd;
198         fwp_msgb_t *msgb;
199         
200         if (contdata->status != FWP_CONT_NEGOTIATED) 
201                 return -EPERM;
202
203         /* Send CANCEL to manager */
204         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
205                                 sizeof(struct fwp_msg_contracthdr)); 
206         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
207         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
208         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
209         FWP_DEBUG("Cancel contract id =%d\n", contdata->id);
210         
211         fwp_mngt_send(FWP_MSG_CANCEL, msgb, 
212                         fwp_participant_this, fwp_participant_mngr);
213         
214         contdata->status = FWP_CONT_NOTNEGOTIATED;
215         fwp_vres_destroy(contdata->vresd);      
216         /*contdata->vresd = NULL_vresd;*/
217
218         /*TODO: error handling */
219         return 0;
220 }