2 #include "fwp_contract.h"
3 #include "fwp_contract_table.h"
7 * Negotiates contract for application. Negotiation request is sent to
8 * fwp agent and then waits for response.
10 * \param[in] contract Contract to negotiate
11 * \param[out] vres_id Id of vres after the contract was accepted
13 * \return It returns FWP_CONTNEGT_ACCEPT when the contract was accepted and
14 * identifier of created vres is return in vres_id parameter.
15 * It returns FWP_CONTNEGT_REJECT when the contract was rejected.
17 * If an error occured it returns negative error code.
20 int fwp_contract_negotiate(struct fwp_contract *contract, fwp_vres_d_t *vresdp)
22 fwp_vres_params_t vparams;
23 fwp_contract_status_t status;
24 struct fwp_msgb *msgb;
26 fwp_appcall_id_t appcall_id;
27 struct sockaddr_un from;
28 socklen_t fromlen = sizeof(struct sockaddr_un);
32 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
33 sizeof(struct fwp_msg_vres_params)/*FWP_MTU*/);
38 appcall_id.callid = fwp_callid++;
39 appcall_id.appid = fwp_appid;
40 fwp_msg_header_deflate(msgb->tail, FWP_CONTNEGT_REQ, &appcall_id);
41 fwp_msgb_put(msgb, sizeof(struct fwp_msg_header));
43 fwp_msg_contract_deflate(msgb->tail, contract);
44 fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
46 /* sendto FWP agent through unix socket
49 FWP_DEBUG("Negotiation request sent to agent\n");
50 _fwp_sendto(fwp_client_sockfd, msgb->data, msgb->len, 0,
51 &fwp_agent_addr, sizeof(fwp_agent_addr));
53 _fwp_recvfrom(len, fwp_client_sockfd, msgb->data, msgb->buf_size, 0,
55 FWP_DEBUG("Negotiation response received from agent\n");
57 fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
58 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
60 fwp_msg_vres_params_inflate(msgb->data, &status, &vparams);
61 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
64 FWP_DEBUG("status = %d\n", status);
65 if (status == FWP_CONT_NEGOTIATED) {
66 FWP_DEBUG("Contract negotiated\n");
67 if ((rc = fwp_vres_create(&vparams, vresdp)) < 0){
70 return FWP_CONTNEGT_ACCEPTED;
73 FWP_DEBUG("Contract rejected\n");
74 return FWP_CONTNEGT_REJECTED;
79 int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresd)
81 fwp_contract_t contract;
82 fwp_vres_params_t vres_params;
84 /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
86 fwp_contract_reserve(contractd);
88 fwp_contract_commit(contractd);
91 /*fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract, resource_d_t resource)*/
92 fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract)
95 fwp_contract_data_t *contdata;
97 contdata = fwp_contract_data_new();
98 /* To be compatible: reserve vres, get its id, and store it to
101 contdata->vresd = fwp_vres_alloc();
102 contdata->id = fwp_vres_get_id(contdata->vresd);
104 memcpy(&contdata->contract, contract, sizeof(*contract));
105 contdata->status = FWP_CONT_NOTNEGOTIATED;
106 /* Add to contract table */
107 fwp_contract_table_insert(&fwp_participant_this->contract_table,
112 int fwp_contract_reserve(fwp_contract_d_t contractd)
114 fwp_contract_data_t *contdata = contractd;
116 fwp_msg_type_t msg_type;
117 fwp_participant_id_t participant_id;
119 /* Send contract to manager */
120 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
121 sizeof(struct fwp_msg_contracthdr) +
122 sizeof(struct fwp_msg_contract) +
123 sizeof(struct fwp_vres_params));
125 /* reserve place for header */
126 fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
128 fwp_msgb_contracthdr_in(msgb->tail, contdata->id, contdata->status);
129 fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
131 fwp_msg_contract_in(msgb->tail, &contdata->contract);
132 fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
134 fwp_mngt_send(FWP_MSG_NEGT_RESERVE, msgb,
135 fwp_participant_this, fwp_participant_mngr);
136 fwp_mngt_recv(&msg_type, &participant_id, msgb);
138 fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
139 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
141 if (contdata->status == FWP_CONT_RESERVED) {
142 fwp_msg_vres_params_out(msgb->data, &contdata->vres_params);
143 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
149 int fwp_contract_commit(fwp_contract_d_t contractd)
151 fwp_contract_data_t *contdata = contractd;
154 /* Send COMMIT to manager */
155 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
156 sizeof(struct fwp_msg_contracthdr) +
157 fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
158 fwp_msgb_contracthdr_in(msgb->tail, contdata->id, contdata->status);
159 fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr);
161 fwp_mngt_send(FWP_MSG_NEGT_COMMIT, msgb,
162 fwp_participant_this, fwp_participant_mngr);