]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/fwp_mngr.c
Added fwp_contract_cancel and destroy calls to fwp_mngrtest
[frescor/fwp.git] / fwp / mngr / fwp_mngr.c
1 #define CONFIGURE_FWP_MY_STREAM_ID 3000
2 #define CONFIGURE_FWP_MNGR_ADDR "127.0.0.1"
3
4 #include "fwp_confdefs.h"
5 #include "fwp.h"
6
7 #include "fwp_contract_table.h"
8 #include "fwp_participant_table.h"
9 #include "fwp_admctrl.h"
10 #include "fwp_mngt.h"
11
12 #define FWP_MTU         2346  
13 #define BUFFSIZE        FWP_MTU 
14
15 /* buffer and socket for incomming message */
16 static unsigned char    buffer[FWP_MTU];
17
18 /* Admission control test */
19 fwp_admctrl_test_t fwp_admctrl_test = &fwp_admctrl_stupid;
20
21 /**
22  * fwp_mngt_input 
23  *
24  * Function waits for remote or local message 
25  * 
26  * @msgb  received message 
27  * \return 
28  * On success, it returns 0 and the pointer to received message in msgb parameter.
29  * On error, it returns negative error code
30  *
31  */
32 int fwp_mngr_input(struct fwp_msgb **pmsgb)
33 {
34         struct fwp_msgb *msgb;
35         ssize_t size;
36
37         FWP_DEBUG("Waiting for messages\n");
38         /* TODO: consider to replace with fwp_mngt_recv call */
39         size = fwp_recv(fwp_participant_this->epointd, buffer, BUFFSIZE, 0);
40          
41         /* For future: fwp_socket could be allocated behind data in msgb*/
42         if (!(msgb = fwp_msgb_alloc(size))) {
43                 perror("No memory available.\n");
44                 return -ENOMEM;
45         }
46         /*memcpy(fwp_msgb_put(msgb, len), buffer, len); */
47         msgb->data = buffer;
48         fwp_msgb_put(msgb, size);
49         
50         *pmsgb = msgb;
51         return (0);
52 }
53
54 void fwp_mngr_hello(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
55 {
56         fwp_participant_info_t participant_info, my_info;
57         fwp_participant_t *participant;
58         fwp_endpoint_attr_t attr;
59
60         FWP_DEBUG("Received HELLO msg from nodeid= %d appid= %d\n", 
61                         participant_id.node_id, participant_id.app_id);
62
63         fwp_endpoint_attr_init(&attr);
64         fwp_endpoint_attr_setreliability(&attr, FWP_MNGT_RELIABILITY);
65         /* Create a new participant */
66         fwp_msg_hello_out(msgb->data, &participant_info);
67         participant = fwp_participant_create(&participant_info);
68         fwp_mngt_service_vres_create(&participant->vresd);
69         fwp_send_endpoint_create(participant->id.node_id, participant->stream_id,
70                                         &attr, &participant->epointd);
71         fwp_send_endpoint_bind(participant->epointd, participant->vresd);
72         fwp_contract_table_init(&participant->contract_table);
73
74         /* Insert participant into table */
75         fwp_participant_table_insert(participant);
76
77         /* Send back hello msg with mngr`s info */
78         /* prepare hello message */
79         fwp_msgb_reset_data(msgb);
80         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
81         
82         my_info.id = fwp_participant_this->id;
83         my_info.stream_id = fwp_participant_this->stream_id;
84
85         fwp_msg_hello_in(msgb->tail, &my_info);
86         fwp_msgb_put(msgb, sizeof(struct fwp_msg_hello));
87
88         /* Send hello to manager */
89         fwp_mngt_send(FWP_MSG_HELLO, msgb, 
90                         fwp_participant_this, participant);
91
92         FWP_DEBUG("Sent HELLO msg from nodeid= %d appid= %d\n", 
93                         participant_id.node_id, participant_id.app_id);
94 }
95
96 int 
97 fwp_mngr_contract_reserve(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
98 {
99         fwp_participant_t *participant;
100         fwp_contract_data_t *contdata;
101
102         /* Find participant */
103         if (!(participant = fwp_participant_table_find(&participant_id))){
104                 return -EPERM;
105         }
106
107         contdata = fwp_contract_data_new();
108         
109         /* Extract contract header */
110         fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
111         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
112         /* Extract contract params */
113         fwp_msg_contract_out(msgb->data, &contdata->contract);
114         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contract));
115
116         /*launch admission test */
117         fwp_admctrl_test(contdata);             
118         
119         free(msgb);
120         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
121                                         sizeof(struct fwp_msg_contract) +
122                                         sizeof(struct fwp_msg_vres_params));
123         fwp_msgb_reserve(msgb,sizeof(struct fwp_msg_header));
124         
125         /*Add contract header*/
126         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
127         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
128         /* Add contract params */
129         /* No needed to send back if spare capacity is not considered
130          * fwp_msg_contract_in(msgb->tail, &contdata->contract);
131          * fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
132          * */
133         
134         /*Send back contract reservation */
135         if (contdata->status == FWP_CONT_RESERVED) {
136                 fwp_msg_vres_params_in(msgb->tail, &contdata->vres_params);
137                 FWP_DEBUG("Sent vres params budget=%d period=%d ac=%d\n", 
138                                 contdata->vres_params.budget,
139                                 contdata->vres_params.period_usec,
140                                 contdata->vres_params.ac_id);
141                 fwp_msgb_put(msgb, sizeof(struct fwp_msg_vres_params));
142                 /* Add contract to contract table */
143                 fwp_contract_table_insert(&participant->contract_table,contdata);
144                 FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
145
146         } else {
147                 free(contdata);
148         }       
149         
150         fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
151                         fwp_participant_this, participant);
152         return 0;
153 }
154
155 int 
156 fwp_mngr_contract_commit(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
157 {
158         fwp_participant_t *participant;
159         fwp_contract_data_t *contdata;
160         fwp_contract_id_t  id;
161         fwp_contract_status_t  status;
162
163         /* Find participant */
164         if (!(participant = fwp_participant_table_find(&participant_id))){
165                 return -EPERM;
166         }
167
168         fwp_msg_contracthdr_out(msgb->data, &id, &status);
169         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
170         FWP_DEBUG("Contract id=%d to commit\n", id);
171         
172         contdata = fwp_contract_table_find(&participant->contract_table, id);
173         contdata->status = FWP_CONT_NEGOTIATED; 
174         
175         return 0;       
176 }
177
178 int 
179 fwp_mngr_contract_cancel(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
180 {
181         fwp_participant_t *participant;
182         fwp_contract_data_t *contdata;
183         fwp_contract_id_t  id;
184         fwp_contract_status_t  status;
185
186         /* Find participant */
187         if (!(participant = fwp_participant_table_find(&participant_id))){
188                 return -EPERM;
189         }
190
191         fwp_msg_contracthdr_out(msgb->data, &id, &status);
192         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
193         
194         contdata = fwp_contract_table_find(&participant->contract_table, id);
195         contdata->status = FWP_CONT_NOTNEGOTIATED;
196         /* release vres - success only for local vres */
197         fwp_vres_destroy(contdata->vresd); 
198         /* delete contract from contract table */
199         fwp_contract_table_delete(&participant->contract_table, contdata);
200         fwp_contract_destroy(contdata);
201         
202         FWP_DEBUG("Contract id=%d to canceled\n", id);
203                 
204         return 0;       
205 }
206
207 void fwp_mngr_msg_handler(fwp_msgb_t *msgb)
208 {
209         fwp_msg_type_t msg_type;
210         fwp_participant_id_t    participant_id;
211
212         fwp_msg_header_out(msgb->data, &msg_type, &participant_id);
213         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
214         
215         switch (msg_type) {
216                 case  FWP_MSG_HELLO:
217                         FWP_DEBUG("Message HELLO received from nodeid = %d "
218                                   "appid = %d\n", participant_id.node_id, 
219                                         participant_id.app_id);
220                         fwp_mngr_hello(msgb, participant_id);
221                         break;
222
223                 case  FWP_MSG_RESERVE: 
224                         FWP_DEBUG("Message RESERVE received from nodeid = %d " 
225                                   "appid = %d\n", participant_id.node_id, 
226                                         participant_id.app_id);
227                         fwp_mngr_contract_reserve(msgb, participant_id);
228                         break;
229
230                 case  FWP_MSG_COMMIT: 
231                         FWP_DEBUG("Message COMMIT received from nodeid = %d "
232                                   "appid = %d\n", participant_id.node_id, 
233                                         participant_id.app_id);
234                         fwp_mngr_contract_commit(msgb, participant_id);
235                         break;  
236                 
237                 case  FWP_MSG_CANCEL: 
238                         FWP_DEBUG("Message CANCEL received from nodeid = %d "
239                                   "appid = %d\n", participant_id.node_id, 
240                                         participant_id.app_id);
241                         fwp_mngr_contract_cancel(msgb, participant_id);
242                         break;  
243                 
244                 default:
245                         printf("Invalid message\n.");
246                         fwp_msgb_free(msgb);
247         }
248 }
249
250 void fwp_mngr_main_loop()
251 {
252         struct fwp_msgb *msgb;
253
254         /* start admission control thread */
255         while (1 /*exit_flag*/){
256                 fwp_mngr_input(&msgb);
257                 if (msgb)
258                         fwp_mngr_msg_handler(msgb);
259                 FWP_DEBUG("Mngr waiting for next msg.\n");
260         }
261 }
262
263 #if 0
264 int fwp_mngr_init()
265 {
266         fwp_participant_info_t  my_info;
267         int rv;
268
269         if ((rv = fwp_endpoint_table_init(fwp_configuration.max_endpoints)) ||
270             (rv = fwp_vres_table_init(fwp_configuration.max_vres))) {
271
272                 return rv;
273         }
274         
275         /* Create fwp_participant_this */
276         my_info.id.node_id = inet_addr("127.0.0.1");
277         my_info.id.app_id = getpid();
278         my_info.stream_id = FWP_MNGR_STREAM_ID;
279
280         fwp_participant_this = fwp_participant_create(&my_info);
281         fwp_participant_mngr = fwp_participant_this;
282         fwp_receive_endpoint_create(my_info.stream_id, 0,
283                                         &fwp_participant_this->epointd);
284         FWP_DEBUG("Participant_this created node_id id= %d stream id= %d\n",
285                                fwp_participant_this->id.node_id,
286                                fwp_participant_this->stream_id);
287         return 0;
288
289 }
290 #endif
291
292 int main()
293 {
294         if (fwp_init()) {
295                 fprintf(stderr,"FWP manager initialization failed.\n");
296                 exit(1);
297         }
298
299         fwp_mngr_main_loop();
300         
301         return 0;       
302