]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/fwp_mngr.c
Added: mngr- delete participant from table after BYE message was received
[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_new(&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 fwp_mngr_bye(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
97 {       
98         fwp_participant_t *participant;
99
100         /* Find participant */
101         if (!(participant = fwp_participant_table_find(&participant_id))){
102                 return -EPERM;
103         }
104         
105         fwp_participant_table_delete(participant);
106         fwp_send_endpoint_unbind(participant->epointd);
107         fwp_endpoint_destroy(participant->epointd);
108         fwp_vres_destroy(participant->vresd);
109         /* TODO: iterate through contract table and delete contracts */
110         fwp_participant_delete(participant);
111                         
112         FWP_DEBUG("BYE nodeid = %d appid = %d\n", participant_id.node_id, 
113                         participant_id.app_id);
114         
115         return 0;       
116 }
117
118 int 
119 fwp_mngr_contract_reserve(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
120 {
121         fwp_participant_t *participant;
122         fwp_contract_data_t *contdata;
123
124         /* Find participant */
125         if (!(participant = fwp_participant_table_find(&participant_id))){
126                 return -EPERM;
127         }
128
129         contdata = fwp_contract_data_new();
130         
131         /* Extract contract header */
132         fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
133         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
134         /* Extract contract params */
135         fwp_msg_contract_out(msgb->data, &contdata->contract);
136         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contract));
137
138         /*launch admission test */
139         fwp_admctrl_test(contdata);             
140         
141         free(msgb);
142         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
143                                         sizeof(struct fwp_msg_contract) +
144                                         sizeof(struct fwp_msg_vres_params));
145         fwp_msgb_reserve(msgb,sizeof(struct fwp_msg_header));
146         
147         /*Add contract header*/
148         fwp_msg_contracthdr_in(msgb->tail, contdata->id, contdata->status);
149         fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
150         /* Add contract params */
151         /* No needed to send back if spare capacity is not considered
152          * fwp_msg_contract_in(msgb->tail, &contdata->contract);
153          * fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
154          * */
155         
156         /*Send back contract reservation */
157         if (contdata->status == FWP_CONT_RESERVED) {
158                 fwp_msg_vres_params_in(msgb->tail, &contdata->vres_params);
159                 FWP_DEBUG("Sent vres params budget=%d period=%d ac=%d\n", 
160                                 contdata->vres_params.budget,
161                                 contdata->vres_params.period_usec,
162                                 contdata->vres_params.ac_id);
163                 fwp_msgb_put(msgb, sizeof(struct fwp_msg_vres_params));
164                 /* Add contract to contract table */
165                 fwp_contract_table_insert(&participant->contract_table,contdata);
166                 FWP_DEBUG("Contract id=%d stored in table\n", contdata->id);
167
168         } else {
169                 free(contdata);
170         }       
171         
172         fwp_mngt_send(FWP_MSG_RESERVE, msgb, 
173                         fwp_participant_this, participant);
174         return 0;
175 }
176
177 int 
178 fwp_mngr_contract_commit(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
179 {
180         fwp_participant_t *participant;
181         fwp_contract_data_t *contdata;
182         fwp_contract_id_t  id;
183         fwp_contract_status_t  status;
184
185         /* Find participant */
186         if (!(participant = fwp_participant_table_find(&participant_id))){
187                 return -EPERM;
188         }
189
190         fwp_msg_contracthdr_out(msgb->data, &id, &status);
191         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
192         FWP_DEBUG("Contract id=%d to commit\n", id);
193         
194         contdata = fwp_contract_table_find(&participant->contract_table, id);
195         contdata->status = FWP_CONT_NEGOTIATED; 
196         
197         return 0;       
198 }
199
200 int 
201 fwp_mngr_contract_cancel(fwp_msgb_t *msgb, fwp_participant_id_t participant_id)
202 {
203         fwp_participant_t *participant;
204         fwp_contract_data_t *contdata;
205         fwp_contract_id_t  id;
206         fwp_contract_status_t  status;
207
208         /* Find participant */
209         if (!(participant = fwp_participant_table_find(&participant_id))){
210                 return -EPERM;
211         }
212
213         fwp_msg_contracthdr_out(msgb->data, &id, &status);
214         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
215         
216         contdata = fwp_contract_table_find(&participant->contract_table, id);
217         contdata->status = FWP_CONT_NOTNEGOTIATED;
218         /* release vres - success only for local vres */
219         fwp_vres_destroy(contdata->vresd); 
220         /* delete contract from contract table */
221         fwp_contract_table_delete(&participant->contract_table, contdata);
222         fwp_contract_destroy(contdata);
223         
224         FWP_DEBUG("Contract id=%d to canceled\n", id);
225                 
226         return 0;       
227 }
228
229 void fwp_mngr_msg_handler(fwp_msgb_t *msgb)
230 {
231         fwp_msg_type_t msg_type;
232         fwp_participant_id_t    participant_id;
233
234         fwp_msg_header_out(msgb->data, &msg_type, &participant_id);
235         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
236         
237         switch (msg_type) {
238                 case  FWP_MSG_HELLO:
239                         FWP_DEBUG("Message HELLO received from nodeid = %d "
240                                   "appid = %d\n", participant_id.node_id, 
241                                         participant_id.app_id);
242                         fwp_mngr_hello(msgb, participant_id);
243                         break;
244                 
245                 case  FWP_MSG_BYE:
246                         FWP_DEBUG("Message BYE received from nodeid = %d "
247                                   "appid = %d\n", participant_id.node_id, 
248                                         participant_id.app_id);
249                         fwp_mngr_bye(msgb, participant_id);
250                         break;
251
252
253                 case  FWP_MSG_RESERVE: 
254                         FWP_DEBUG("Message RESERVE received from nodeid = %d " 
255                                   "appid = %d\n", participant_id.node_id, 
256                                         participant_id.app_id);
257                         fwp_mngr_contract_reserve(msgb, participant_id);
258                         break;
259
260                 case  FWP_MSG_COMMIT: 
261                         FWP_DEBUG("Message COMMIT received from nodeid = %d "
262                                   "appid = %d\n", participant_id.node_id, 
263                                         participant_id.app_id);
264                         fwp_mngr_contract_commit(msgb, participant_id);
265                         break;  
266                 
267                 case  FWP_MSG_CANCEL: 
268                         FWP_DEBUG("Message CANCEL received from nodeid = %d "
269                                   "appid = %d\n", participant_id.node_id, 
270                                         participant_id.app_id);
271                         fwp_mngr_contract_cancel(msgb, participant_id);
272                         break;  
273                 
274                 default:
275                         printf("Invalid message\n.");
276                         fwp_msgb_free(msgb);
277         }
278 }
279
280 void fwp_mngr_main_loop()
281 {
282         struct fwp_msgb *msgb;
283
284         /* start admission control thread */
285         while (1 /*exit_flag*/){
286                 fwp_mngr_input(&msgb);
287                 if (msgb)
288                         fwp_mngr_msg_handler(msgb);
289                 FWP_DEBUG("Mngr waiting for next msg.\n");
290         }
291 }
292
293 int main()
294 {
295         if (fwp_init()) {
296                 fprintf(stderr,"FWP manager initialization failed.\n");
297                 exit(1);
298         }
299
300         fwp_mngr_main_loop();
301         
302         return 0;       
303