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