]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/fwp_mngr.c
0565991fd160c83639f7f8bad0e7ea6a85434d66
[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 void fwp_mngr_msg_handler(fwp_msgb_t *msgb)
179 {
180         fwp_msg_type_t msg_type;
181         fwp_participant_id_t    participant_id;
182
183         fwp_msg_header_out(msgb->data, &msg_type, &participant_id);
184         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
185         
186         switch (msg_type) {
187                 case  FWP_MSG_HELLO:
188                         FWP_DEBUG("Message HELLO received from nodeid = %d "
189                                   "appid = %d\n", participant_id.node_id, 
190                                         participant_id.app_id);
191                         fwp_mngr_hello(msgb, participant_id);
192                         break;
193
194                 case  FWP_MSG_RESERVE: 
195                         FWP_DEBUG("Message RESERVE received from nodeid = %d " 
196                                   "appid = %d\n", participant_id.node_id, 
197                                         participant_id.app_id);
198                         fwp_mngr_contract_reserve(msgb, participant_id);
199                         break;
200
201                 case  FWP_MSG_COMMIT: 
202                         FWP_DEBUG("Message COMMIT received from nodeid = %d "
203                                   "appid = %d\n", participant_id.node_id, 
204                                         participant_id.app_id);
205                         fwp_mngr_contract_commit(msgb, participant_id);
206                         break;  
207                 default:
208                         printf("Invalid message\n.");
209                         fwp_msgb_free(msgb);
210         }
211 }
212
213 void fwp_mngr_main_loop()
214 {
215         struct fwp_msgb *msgb;
216
217         /* start admission control thread */
218         while (1 /*exit_flag*/){
219                 fwp_mngr_input(&msgb);
220                 if (msgb)
221                         fwp_mngr_msg_handler(msgb);
222                 FWP_DEBUG("Mngr waiting for next msg.\n");
223         }
224 }
225
226 #if 0
227 int fwp_mngr_init()
228 {
229         fwp_participant_info_t  my_info;
230         int rv;
231
232         if ((rv = fwp_endpoint_table_init(fwp_configuration.max_endpoints)) ||
233             (rv = fwp_vres_table_init(fwp_configuration.max_vres))) {
234
235                 return rv;
236         }
237         
238         /* Create fwp_participant_this */
239         my_info.id.node_id = inet_addr("127.0.0.1");
240         my_info.id.app_id = getpid();
241         my_info.stream_id = FWP_MNGR_STREAM_ID;
242
243         fwp_participant_this = fwp_participant_create(&my_info);
244         fwp_participant_mngr = fwp_participant_this;
245         fwp_receive_endpoint_create(my_info.stream_id, 0,
246                                         &fwp_participant_this->epointd);
247         FWP_DEBUG("Participant_this created node_id id= %d stream id= %d\n",
248                                fwp_participant_this->id.node_id,
249                                fwp_participant_this->stream_id);
250         return 0;
251
252 }
253 #endif
254
255 int main()
256 {
257         if (fwp_init()) {
258                 fprintf(stderr,"FWP manager initialization failed.\n");
259                 exit(1);
260         }
261
262         fwp_mngr_main_loop();
263         
264         return 0;       
265