2 #include "fwp_contract.h"
3 #include "fwp_contract_table.h"
6 static int fwp_contract_is_reserved(fwp_contract_d_t contract)
8 return (contract->status == FWP_CONT_RESERVED);
11 int fwp_contract_is_negotiated(fwp_contract_d_t contract)
13 return (contract->status == FWP_CONT_NEGOTIATED);
17 * Negotiates contract for application. Negotiation request is sent to
18 * fwp agent and then waits for response.
20 * \param[in] contractd descriptor of the contract to negotiate
21 * \param[out] vresd Id of vres after the contract was accepted
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
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)
32 fwp_contract_reserve(contractd);
33 if (!fwp_contract_is_reserved(contractd)) {
37 fwp_contract_commit(contractd, vresdp);
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)
44 fwp_contract_data_t *contdata;
46 contdata = fwp_contract_data_new();
47 /* To be compatible: reserve vres, get its id, and store it to
50 contdata->vresd = fwp_vres_alloc();
51 contdata->id = fwp_vres_get_id(contdata->vresd);
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,
59 FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
63 int fwp_contract_reserve(fwp_contract_d_t contractd)
65 fwp_contract_data_t *contdata = contractd;
67 fwp_msg_type_t msg_type;
68 fwp_participant_id_t participant_id;
70 /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
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));
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));
87 fwp_mngt_send(FWP_MSG_RESERVE, msgb,
88 fwp_participant_this, fwp_participant_mngr);
89 fwp_mngt_recv(&msg_type, &participant_id, msgb);
91 fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
92 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
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));
103 FWP_DEBUG("STATUS = %d\n", contdata->status);
107 int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
109 fwp_contract_data_t *contdata = contractd;
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);
120 fwp_mngt_send(FWP_MSG_COMMIT, msgb,
121 fwp_participant_this, fwp_participant_mngr);
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;
129 /*TODO: error handling */
133 int fwp_contract_cancel(fwp_contract_d_t contractd)
135 fwp_contract_data_t *contdata = contractd;
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);
146 fwp_mngt_send(FWP_MSG_CANCEL, msgb,
147 fwp_participant_this, fwp_participant_mngr);
149 contdata->status = FWP_CONT_NOTNEGOTIATED;
150 fwp_vres_destroy(contdata->vresd);
151 /*contdata->vresd = NULL_vresd;*/
153 /*TODO: error handling */