4 * Routines for manipulation with contract
8 #include "fwp_contract.h"
9 #include "fwp_contract_table.h"
13 static int fwp_contract_is_reserved(fwp_contract_d_t contract)
15 return (contract->status == FWP_CONT_RESERVED);
18 int fwp_contract_is_negotiated(fwp_contract_d_t contract)
20 return (contract->status == FWP_CONT_NEGOTIATED);
24 * Negotiates contract for application. Negotiation request is sent to
25 * fwp agent and then waits for response.
27 * \param[in] contractd Descriptor of the contract to negotiate
28 * \param[out] vresd Vres descriptor after the contract was accepted
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
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)
39 fwp_contract_reserve(contractd);
40 if (!fwp_contract_is_reserved(contractd)) {
44 fwp_contract_commit(contractd, vresdp);
51 * \param[in] contract User-level contract
53 * \return On success, returns contract descriptor
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)
59 fwp_contract_data_t *contdata;
61 contdata = fwp_contract_data_new();
62 /* To be compatible: reserve vres, get its id, and store it to
65 contdata->vresd = fwp_vres_alloc();
66 contdata->id = fwp_vres_get_id(contdata->vresd);
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,
74 FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
81 * \param[in] contractd Contract descriptor
83 * \return On success, returns zero.
84 * On error, returns negative error code.
87 int fwp_contract_destroy(fwp_contract_d_t contractd)
89 fwp_contract_data_t *contdata = contractd;
91 if (contdata->status != FWP_CONT_NOTNEGOTIATED)
93 fwp_contract_data_delete(contdata);
100 * \param[in] contractd Contract descriptor
102 * \return On success, returns zero.
103 * On error, returns negative error code.
106 int fwp_contract_reserve(fwp_contract_d_t contractd)
108 fwp_contract_data_t *contdata = contractd;
110 fwp_msg_type_t msg_type;
111 fwp_participant_id_t participant_id;
113 /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
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));
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));
130 fwp_mngt_send(FWP_MSG_RESERVE, msgb,
131 fwp_participant_this, fwp_participant_mngr);
132 fwp_mngt_recv(&msg_type, &participant_id, msgb);
134 fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
135 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
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));
146 FWP_DEBUG("STATUS = %d\n", contdata->status);
153 * \param[in] contractd Contract descriptor
154 * \param[out] vresd Descriptor of vres
156 * \return On success, returns zero.
157 * On error, returns negative error code.
160 int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
162 fwp_contract_data_t *contdata = contractd;
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);
173 fwp_mngt_send(FWP_MSG_COMMIT, msgb,
174 fwp_participant_this, fwp_participant_mngr);
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;
182 /*TODO: error handling */
189 * \param[in] contractd Contract descriptor
191 * \return On success, returns zero.
192 * On error, returns negative error code.
195 int fwp_contract_cancel(fwp_contract_d_t contractd)
197 fwp_contract_data_t *contdata = contractd;
200 if (contdata->status != FWP_CONT_NEGOTIATED)
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);
211 fwp_mngt_send(FWP_MSG_CANCEL, msgb,
212 fwp_participant_this, fwp_participant_mngr);
214 contdata->status = FWP_CONT_NOTNEGOTIATED;
215 fwp_vres_destroy(contdata->vresd);
216 /*contdata->vresd = NULL_vresd;*/
218 /*TODO: error handling */