#include "fwp_contract.h"
#include "fwp_contract_table.h"
-
/**
* Negotiates contract for application. Negotiation request is sent to
* fwp agent and then waits for response.
*
- * \param[in] contract Contract to negotiate
- * \param[out] vres_id Id of vres after the contract was accepted
+ * \param[in] contractd descriptor of the contract to negotiate
+ * \param[out] vresd Id of vres after the contract was accepted
*
* \return It returns FWP_CONTNEGT_ACCEPT when the contract was accepted and
- * identifier of created vres is return in vres_id parameter.
+ * the descriptor of created vres is returned in vresd parameter.
* It returns FWP_CONTNEGT_REJECT when the contract was rejected.
*
* If an error occured it returns negative error code.
*/
-#if 0
-int fwp_contract_negotiate(struct fwp_contract *contract, fwp_vres_d_t *vresdp)
-{
- fwp_vres_params_t vparams;
- fwp_contract_status_t status;
- struct fwp_msgb *msgb;
- unsigned int code;
- fwp_appcall_id_t appcall_id;
- struct sockaddr_un from;
- socklen_t fromlen = sizeof(struct sockaddr_un);
- int rc;
- ssize_t len;
-
- msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
- sizeof(struct fwp_msg_vres_params)/*FWP_MTU*/);
-
- if (!msgb)
- return -ENOMEM;
-
- appcall_id.callid = fwp_callid++;
- appcall_id.appid = fwp_appid;
- fwp_msg_header_deflate(msgb->tail, FWP_CONTNEGT_REQ, &appcall_id);
- fwp_msgb_put(msgb, sizeof(struct fwp_msg_header));
-
- fwp_msg_contract_deflate(msgb->tail, contract);
- fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
-
- /* sendto FWP agent through unix socket
- * and wait for reply
- */
- FWP_DEBUG("Negotiation request sent to agent\n");
- _fwp_sendto(fwp_client_sockfd, msgb->data, msgb->len, 0,
- &fwp_agent_addr, sizeof(fwp_agent_addr));
-
- _fwp_recvfrom(len, fwp_client_sockfd, msgb->data, msgb->buf_size, 0,
- &from, &fromlen);
- FWP_DEBUG("Negotiation response received from agent\n");
-
- fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
- fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
-
- fwp_msg_vres_params_inflate(msgb->data, &status, &vparams);
- fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
-
- fwp_msgb_free(msgb);
- FWP_DEBUG("status = %d\n", status);
- if (status == FWP_CONT_NEGOTIATED) {
- FWP_DEBUG("Contract negotiated\n");
- if ((rc = fwp_vres_create(&vparams, vresdp)) < 0){
- return rc;
- }
- return FWP_CONTNEGT_ACCEPTED;
-
- } else {
- FWP_DEBUG("Contract rejected\n");
- return FWP_CONTNEGT_REJECTED;
- }
-}
-#endif
-
-int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresd)
+int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
{
fwp_contract_t contract;
fwp_vres_params_t vres_params;
/*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
contract_id);*/
fwp_contract_reserve(contractd);
-
- fwp_contract_commit(contractd);
+ if (!fwp_contract_is_reserved(contdata)) {
+ return -EAGAIN;
+ }
+ fwp_contract_commit(contractd, vresdp);
+ return 0;
}
/*fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract, resource_d_t resource)*/
/* reserve place for header */
fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
-
+ /*Add contract header*/
fwp_msgb_contracthdr_in(msgb->tail, contdata->id, contdata->status);
fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
-
+ /* Add contract params */
fwp_msg_contract_in(msgb->tail, &contdata->contract);
fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
return 0;
}
-int fwp_contract_commit(fwp_contract_d_t contractd)
+int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
{
fwp_contract_data_t *contdata = contractd;
fwp_msgb_t *msgb;
/* Send COMMIT to manager */
msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
- sizeof(struct fwp_msg_contracthdr) +
+ sizeof(struct fwp_msg_contracthdr));
fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
fwp_msgb_contracthdr_in(msgb->tail, contdata->id, contdata->status);
- fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr);
+ fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
fwp_mngt_send(FWP_MSG_NEGT_COMMIT, msgb,
fwp_participant_this, fwp_participant_mngr);
+ /* Set parameters of vres
+ * and activate it if needed */
+ fwp_vres_set_params(contdata->vresd, contdata->vres_params);
+ *vresdp = contdata->vresd;
+ /*TODO: error handling */
return 0;
}