]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_mngt.c
Incorporate fwp_configuration into fwp_mngt_init
[frescor/fwp.git] / fwp / lib / mngt / fwp_mngt.c
1 #include "fwp_conf.h"
2 #include "fwp_mngt.h"
3
4 #include <stdlib.h>
5 /* 
6  * Global mngt variables
7  */
8
9 fwp_participant_t       *fwp_participant_this;
10 fwp_participant_t       *fwp_participant_mngr;
11
12 /*fwp_endpoint_d_t      fwp_mngt_repointd;*/
13
14 /**
15  * struct resource {
16  *      char name[10];
17  *      int id;
18  *      participant_my;
19  *      participant_mngr;
20  *      contract_ops;
21  *      fna_ops;
22  * }
23  */
24
25 int fwp_mngt_send(fwp_msg_type_t type,fwp_msgb_t *msgb,
26                   fwp_participant_t *source, fwp_participant_t *dest)
27 {
28         fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
29         fwp_msg_header_in(msgb->data, type, source->id);
30
31         fwp_send(dest->epointd, msgb->data, msgb->len);
32
33         return 0;
34 }
35
36 int fwp_mngt_recv(fwp_msg_type_t *type, fwp_participant_id_t *participant_id,
37                         fwp_msgb_t *msgb)
38 {
39         int size;
40         
41         fwp_msgb_reset_data(msgb);
42         size = fwp_recv(fwp_participant_this->epointd, msgb->data, 
43                         msgb->buffer_size);
44         fwp_msgb_put(msgb, size);
45
46         fwp_msg_header_out(msgb->data, type, participant_id);
47         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
48         
49         FWP_DEBUG("Received msg: type=%d  from nodeid=%d appid=%d\n", *type,
50                         participant_id->node_id, participant_id->app_id);
51
52         return 0;
53
54
55 int fwp_mngt_service_vres_create(fwp_vres_d_t* fwp_service_vresd)
56 {
57         struct fwp_vres_params  fwp_service_vparams;
58         
59         /* TODO: Add to contract table */
60         /* create service vres */
61         fwp_service_vparams.ac_id = FWP_AC_BK; 
62         fwp_service_vparams.budget = 100;
63         fwp_service_vparams.period_usec = 1000;
64         
65         if ((fwp_vres_create(&fwp_service_vparams, fwp_service_vresd) < 0)) {
66                 fprintf(stderr,"Unable to open service vres\n");
67                 return -1;
68         }
69         
70         FWP_DEBUG("Service vres negotiated\n");
71         
72         return 0;
73 }
74
75 /* Launch discovery/connect process to 
76  * introduce itself to fwp manager and get description of manager*/
77 int fwp_mngt_connect()
78 {
79         fwp_participant_info_t  my_info, mngr_info;
80         fwp_participant_id_t    participant_id;
81         fwp_msgb_t              *msgb;
82         fwp_msg_type_t          msg_type;
83
84
85         /* Create discovery endpoint */
86         FWP_DEBUG("Service vres created\n");
87         fwp_mngt_service_vres_create(&fwp_participant_mngr->vresd);
88         
89         FWP_DEBUG("Discovery send endpoint created\n");
90         fwp_send_endpoint_create(fwp_participant_mngr->id.node_id,
91                                  fwp_participant_mngr->stream_id,
92                                  0, &fwp_participant_mngr->epointd);    
93         fwp_send_endpoint_bind(fwp_participant_mngr->epointd, 
94                                 fwp_participant_mngr->vresd);
95         
96         /* prepare hello message */
97         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
98                               sizeof(struct fwp_msg_hello));
99         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
100         
101         my_info.id = fwp_participant_this->id;
102         my_info.stream_id = fwp_participant_this->stream_id;
103
104         fwp_msg_hello_in(msgb->tail, &my_info);
105         fwp_msgb_put(msgb, sizeof(struct fwp_msg_hello));
106
107         /* Send hello to manager */
108         fwp_mngt_send(FWP_MSG_HELLO, msgb, 
109                         fwp_participant_this, fwp_participant_mngr);
110
111         /* receive hello from manager */
112         fwp_mngt_recv(&msg_type, &participant_id, msgb);
113         FWP_DEBUG("Received HELLO msg from nodeid= %d appid= %d\n", 
114                         participant_id.node_id, participant_id.app_id);
115         
116         /* Process hello msg from manager */
117         fwp_msg_hello_out(msgb->data, &mngr_info);
118         fwp_participant_mngr->id  = mngr_info.id;
119         fwp_participant_mngr->stream_id  = mngr_info.stream_id;
120         FWP_DEBUG("Received HELLO msg contains nodeid= %d appid= %d\n", 
121                         mngr_info.id.node_id, mngr_info.id.app_id);
122         
123         /* unbind and delete discovery mngr send endoint */
124         fwp_send_endpoint_unbind(fwp_participant_mngr->epointd);
125         /*fwp_endpoint_free(fwp_participant_mngr->epointd)*/
126
127         /* Create mngt send endpoint to manager */
128         fwp_send_endpoint_create(fwp_participant_mngr->id.node_id, 
129                                  fwp_participant_mngr->stream_id, 0,
130                                  &fwp_participant_mngr->epointd);
131         FWP_DEBUG("Management send endpoint created\n");
132         fwp_send_endpoint_bind(fwp_participant_mngr->epointd, 
133                                 fwp_participant_mngr->vresd);
134         return 0;
135 }
136
137 int fwp_mngt_init()
138 {
139         fwp_participant_info_t  my_info, mngr_info;
140         unsigned int node_id;
141         int flags;
142         char *value;
143         
144         /* Create fwp_participant_this */
145         
146         my_info.id.node_id = inet_addr(fwp_configuration.my_addr);
147         if (my_info.id.node_id == inet_addr("127.0.0.1")) {
148                 /* if default then check env variable */
149                 value = getenv("FWP_MY_ADDR");
150                 if (value) {
151                         my_info.id.node_id = inet_addr(value);
152                 }       
153         }
154         fwp_configuration.my_node_id = my_info.id.node_id;
155         my_info.id.app_id = getpid();
156         my_info.stream_id = fwp_configuration.my_stream_id;
157
158         fwp_participant_this = fwp_participant_create(&my_info);        
159         fwp_receive_endpoint_create(my_info.stream_id, 0,
160                                         &fwp_participant_this->epointd);
161         /* FIXME 
162         fwp_endpoint_get_params(&(fwp_participant_this->id.node_id), 
163                                 &fwp_participant_this->stream_id,
164                                 &flags,
165                                 fwp_participant_this->epointd);
166         */
167         fwp_endpoint_get_params(&node_id, 
168                                 &fwp_participant_this->stream_id,
169                                 &flags,
170                                 fwp_participant_this->epointd);
171         FWP_DEBUG("Participant_this created node_id id= %d stream id= %d\n",
172                         fwp_participant_this->id.node_id, 
173                         fwp_participant_this->stream_id);
174         
175         /* Create fwp_participant_mngr */
176         
177         mngr_info.id.node_id = inet_addr(fwp_configuration.mngr_addr);
178         FWP_DEBUG("mngr node=%s node_id=%d\n", 
179                         fwp_configuration.mngr_addr,
180                         mngr_info.id.node_id);
181         if (mngr_info.id.node_id == inet_addr("255.255.255.255")) {
182                 /* if default then check env variable */
183                 value = getenv("FWP_MNGR_ADDR");
184                 if (value) {
185                         mngr_info.id.node_id = inet_addr(value);
186                 }       
187         }
188         fwp_configuration.mngr_node_id = mngr_info.id.node_id;
189         mngr_info.id.app_id = getpid();
190         mngr_info.stream_id = fwp_configuration.mngr_stream_id;
191         
192         if ((mngr_info.id.node_id == inet_addr("127.0.0.1")) && 
193                 (my_info.stream_id == mngr_info.stream_id)) {
194                 /* I am a manager  */
195                 FWP_DEBUG("I am FWP manager\n");
196                 fwp_participant_mngr = fwp_participant_this;
197         } else {
198                 fwp_participant_mngr = fwp_participant_create(&mngr_info);
199                 /* Connet to FWP manager */
200                 fwp_mngt_connect();
201         }       
202         
203         return 0;
204 }