]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_contract.c
Changing name of serialization funmctions; added functions for contract id, status...
[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 int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresd)
80 {
81         fwp_contract_t  contract;
82         fwp_vres_params_t vres_params;
83
84         /*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
85                                                 contract_id);*/
86         fwp_contract_reserve(contractd);
87         
88         fwp_contract_commit(contractd);
89 }
90
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)
93 {
94         fwp_msgb_t *msgb;
95         fwp_contract_data_t *contdata;
96
97         contdata = fwp_contract_data_new();
98         /* To be compatible: reserve vres, get its id, and store it to
99          * condata->id
100          */ 
101         contdata->vresd = fwp_vres_alloc();
102         contdata->id = fwp_vres_get_id(contdata->vresd);
103         
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, 
108                                         contdata);
109         return condata;
110 }
111
112 int fwp_contract_reserve(fwp_contract_d_t contractd)
113 {
114         fwp_contract_data_t *contdata = contractd;
115         fwp_msgb_t *msgb;
116         fwp_msg_type_t msg_type;
117         fwp_participant_id_t participant_id;
118         
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));
124
125         /* reserve place for header */
126         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
127
128         fwp_msgb_contracthdr_in(msgb->tail, contdata->id, contdata->status);
129         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
130
131         fwp_msg_contract_in(msgb->tail, &contdata->contract);
132         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
133
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);
137
138         fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
139         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
140         
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));
144         }
145
146         return 0;
147 }
148
149 int fwp_contract_commit(fwp_contract_d_t contractd)
150 {
151         fwp_contract_data_t *contdata = contractd;
152         fwp_msgb_t *msgb;
153         
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);
160         
161         fwp_mngt_send(FWP_MSG_NEGT_COMMIT, msgb, 
162                         fwp_participant_this, fwp_participant_mngr);
163         return 0;
164 }
165