]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_contract.c
5076b237c1f54a512112fb5a3a2987fe371027f1
[frescor/fwp.git] / fwp / lib / mngt / fwp_contract.c
1 #include "fwp_msg.h"
2 #include "fwp_contract.h"
3 #include "fwp_contract_table.h"
4 #include "fwp_mngt.h"
5
6 static int fwp_contract_is_reserved(fwp_contract_d_t contract)
7 {
8         return (contract->status == FWP_CONT_RESERVED);
9 }
10
11 int fwp_contract_is_negotiated(fwp_contract_d_t contract)
12 {
13         return (contract->status == FWP_CONT_NEGOTIATED);
14 }
15
16 /**
17  * Negotiates contract for application. Negotiation request is sent to 
18  * fwp agent and then waits for response.
19  *
20  * \param[in] contractd descriptor of the contract to negotiate
21  * \param[out] vresd Id of vres after the contract was accepted
22  *
23  * \return  
24  * If successful, the  function returns zero and vres descriptor is
25  * stored in vresdp parameter, otherwise,  an  error  number  shall  be
26  * returned to indicate the error. If the contract is not negotiated EAGAIN 
27  * error is returned.
28  */
29 /*int fwp_contract_negotiate(fwp_contract_d_t contractd, void *vresdp) */
30 int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
31 {
32         fwp_contract_reserve(contractd);
33         if (!fwp_contract_is_reserved(contractd)) {     
34                 return -EAGAIN;
35         }       
36         
37         fwp_contract_commit(contractd, vresdp);
38         return 0;
39 }
40
41 /*fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract, resource_d_t resource)*/
42 fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract)
43 {
44         fwp_contract_data_t *contdata;
45
46         contdata = fwp_contract_data_new();
47         /* To be compatible: reserve vres, get its id, and store it to
48          * condata->id
49          */ 
50         contdata->vresd = fwp_vres_alloc();
51         contdata->id = fwp_vres_get_id(contdata->vresd);
52         
53         memcpy(&contdata->contract, contract, sizeof(*contract));
54         contdata->status = FWP_CONT_NOTNEGOTIATED;      
55         /* Add to contract table */
56         fwp_contract_table_insert(&fwp_participant_this->contract_table, 
57                                         contdata);
58         
59         FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
60         return contdata;
61 }
62
63 int fwp_contract_reserve(fwp_contract_d_t contractd)
64 {
65         fwp_contract_data_t *contdata = contractd;
66         fwp_msgb_t *msgb;
67         fwp_msg_type_t msg_type;
68         fwp_participant_id_t participant_id;
69         
70         /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
71                                                 contract_id);*/
72         /* Send contract to manager */
73         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
74                                 sizeof(struct fwp_msg_contracthdr) +
75                                 sizeof(struct fwp_msg_contract) +
76                                 sizeof(struct fwp_vres_params));
77
78         /* reserve place for header */
79         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
80         /*Add contract header*/
81         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
82         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
83         /* Add contract params */
84         fwp_msg_contract_in(msgb->tail, &contdata->contract);
85         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
86
87         fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
88                         fwp_participant_this, fwp_participant_mngr);
89         fwp_mngt_recv(&msg_type, &participant_id, msgb);
90
91         fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
92         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
93         
94         if (contdata->status == FWP_CONT_RESERVED) {
95                 fwp_msg_vres_params_out(msgb->data, &contdata->vres_params);
96                 FWP_DEBUG("Received vres params budget=%d period=%d ac=%d \n",
97                                 contdata->vres_params.budget, 
98                                 contdata->vres_params.period_usec,
99                                 contdata->vres_params.ac_id);
100                 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
101         }
102         
103         FWP_DEBUG("STATUS = %d\n", contdata->status);
104         return 0;
105 }
106
107 int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
108 {
109         fwp_contract_data_t *contdata = contractd;
110         fwp_msgb_t *msgb;
111         
112         /* Send COMMIT to manager */
113         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
114                                 sizeof(struct fwp_msg_contracthdr)); 
115         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
116         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
117         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
118         FWP_DEBUG("Commit contract id =%d\n", contdata->id);
119         
120         fwp_mngt_send(FWP_MSG_COMMIT, msgb, 
121                         fwp_participant_this, fwp_participant_mngr);
122         
123         contdata->status = FWP_CONT_NEGOTIATED;
124         /* Set parameters of vres 
125          * and activate it if needed */
126         fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
127         *vresdp = contdata->vresd;
128
129         /*TODO: error handling */
130         return 0;
131 }
132
133 int fwp_contract_cancel(fwp_contract_d_t contractd)
134 {
135         fwp_contract_data_t *contdata = contractd;
136         fwp_msgb_t *msgb;
137         
138         /* Send COMMIT to manager */
139         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
140                                 sizeof(struct fwp_msg_contracthdr)); 
141         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
142         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
143         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
144         FWP_DEBUG("Cancel contract id =%d\n", contdata->id);
145         
146         fwp_mngt_send(FWP_MSG_CANCEL, msgb, 
147                         fwp_participant_this, fwp_participant_mngr);
148         
149         contdata->status = FWP_CONT_NOTNEGOTIATED;
150         fwp_vres_destroy(contdata->vresd);      
151         /*contdata->vresd = NULL_vresd;*/
152
153         /*TODO: error handling */
154         return 0;
155 }