]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_contract.c
Fix in mngrtest/Makefile.omk -libary order, changed contract ops in libfwp and in...
[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
5
6 /**
7  * Negotiates contract for application. Negotiation request is sent to 
8  * fwp agent and then waits for response.
9  *
10  * \param[in] contract Contract to negotiate
11  * \param[out] vres_id Id of vres after the contract was accepted
12  *
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.
16  *
17  * If an error occured it returns negative error code.
18  */
19 #if 0
20 int fwp_contract_negotiate(struct fwp_contract *contract, fwp_vres_d_t *vresdp)
21 {
22         fwp_vres_params_t vparams;
23         fwp_contract_status_t status;
24         struct fwp_msgb *msgb;
25         unsigned int code;
26         fwp_appcall_id_t appcall_id;
27         struct sockaddr_un from;
28         socklen_t fromlen = sizeof(struct sockaddr_un);
29         int rc;
30         ssize_t len;
31
32         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
33                               sizeof(struct fwp_msg_vres_params)/*FWP_MTU*/);
34         
35         if (!msgb) 
36                 return -ENOMEM;
37         
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));
42         
43         fwp_msg_contract_deflate(msgb->tail, contract);
44         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
45         
46         /* sendto FWP agent through unix socket 
47          * and wait for reply
48          */
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)); 
52         
53         _fwp_recvfrom(len, fwp_client_sockfd, msgb->data, msgb->buf_size, 0,
54                         &from, &fromlen); 
55         FWP_DEBUG("Negotiation response received from agent\n");
56         
57         fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
58         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
59
60         fwp_msg_vres_params_inflate(msgb->data, &status, &vparams);
61         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
62
63         fwp_msgb_free(msgb);
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){
68                         return rc;
69                 }
70                 return FWP_CONTNEGT_ACCEPTED;
71
72         } else {
73                 FWP_DEBUG("Contract rejected\n");
74                 return FWP_CONTNEGT_REJECTED;
75         }
76 }
77 #endif
78
79 fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract)
80 {
81         fwp_msgb_t *msgb;
82         fwp_contract_data_t *contdata;
83
84         contdata = fwp_contract_data_new();
85         contdata->vresd = fwp_vres_alloc();
86         contdata->id = fwp_vres_get_id(contdata->vresd);
87         memcpy(&contdata->contract, contract, sizeof(*contract));
88         contdata->status = FWP_CONT_REQUESTED; 
89         
90         /* Add to contract table */
91         fwp_contract_table_insert(&fwp_participant_this->contract_table, 
92                                         contdata);
93         return 0;
94 }
95
96 int fwp_contract_reserve(fwp_contract_d_t contractd)
97 {
98         fwp_contract_id_t  contract_id = contractd;
99         fwp_contract_data_t *contdata;
100         fwp_msgb_t *msgb;
101         
102         contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
103                                                 contract_id);
104         /* Send contract to manager */
105         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
106                                 sizeof(struct fwp_msg_contract));
107         fwp_msg_contract_deflate(msgb->data, contdata->id, &contdata->contract);
108         fwp_mngt_send(FWP_MSG_NEGT_RESERVE, msgb, 
109                         fwp_participant_this, fwp_participant_mngr);
110         return 0;
111 }
112
113 int fwp_contract_commit(fwp_contract_d_t contractd)
114 {
115         fwp_contract_id_t  contract_id = contractd;
116         fwp_contract_data_t *contdata;
117         fwp_msgb_t *msgb;
118         
119         contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
120                                                 contract_id);
121         /* Send contract to manager */
122         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
123                                 sizeof(struct fwp_msg_contract));
124         fwp_msg_contract_deflate(msgb->data, contdata->id, &contdata->contract);
125         fwp_mngt_send(FWP_MSG_NEGT_COMMIT, msgb, 
126                         fwp_participant_this, fwp_participant_mngr);
127         return 0;
128 }
129
130 #if 0
131 /**
132  *Incomplete
133  */
134 int fwp_contract_cancel(unsigned int vres_id)
135 {
136         return fwp_vres_close(vres_id);
137 }
138
139 #endif
140