]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/mngt/fwp_mngt.c
Fix in mngrtest/Makefile.omk -libary order, changed contract ops in libfwp and in...
[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  *      general_ops;
17  *      fna_ops;
18  * }
19  */
20
21 int fwp_mngt_send(fwp_msg_type_t type,fwp_msgb_t *msgb,
22                   fwp_participant_t *source, fwp_participant_t *dest)
23 {
24         fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
25         fwp_msg_header_deflate(msgb->data, type, source->id);
26
27         fwp_send(dest->epointd, msgb->data, msgb->len);
28
29         return 0;
30 }
31
32 int fwp_mngt_recv(fwp_msg_type_t *type, fwp_participant_id_t *participant_id,
33                         fwp_msgb_t *msgb)
34 {
35         int size;
36         
37         fwp_msgb_reset_data(msgb);
38         size = fwp_recv(fwp_participant_this->epointd, msgb->data, 
39                         msgb->buffer_size);
40         fwp_msgb_put(msgb, size);
41
42         fwp_msg_header_inflate(msgb->data, type, participant_id);
43         fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
44         
45         FWP_DEBUG("Received msg: type=%d  from nodeid=%d appid=%d\n", *type,
46                         participant_id->node_id, participant_id->app_id);
47
48         return 0;
49
50
51 int fwp_mngt_service_vres_create(fwp_vres_d_t* fwp_service_vresd)
52 {
53         struct fwp_vres_params  fwp_service_vparams;
54         
55         /* TODO: Add to contract table */
56         /* create service vres */
57         fwp_service_vparams.ac_id = FWP_AC_BK; 
58         fwp_service_vparams.budget = 100;
59         fwp_service_vparams.period_usec = 1000;
60         
61         if ((fwp_vres_create(&fwp_service_vparams, fwp_service_vresd) < 0)) {
62                 fprintf(stderr,"Unable to open service vres\n");
63                 return -1;
64         }
65         
66         FWP_DEBUG("Service vres negotiated\n");
67         
68         return 0;
69 }
70
71 /* Launch discovery/connect process to 
72  * introduce itself to fwp manager and get description of manager*/
73 void fwp_mngt_connect()
74 {
75         fwp_participant_info_t  my_info, mngr_info;
76         fwp_participant_id_t    participant_id;
77         fwp_msgb_t              *msgb;
78         fwp_msg_type_t          msg_type;
79
80
81         /* prepare hello message */
82         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) + 
83                               sizeof(struct fwp_msg_hello));
84         fwp_msgb_reserve(msgb, sizeof(struct fwp_msg_header));
85         
86         my_info.id = fwp_participant_this->id;
87         my_info.stream_id = fwp_participant_this->stream_id;
88
89         fwp_msg_hello_deflate(msgb->tail, &my_info);
90         fwp_msgb_put(msgb, sizeof(struct fwp_msg_hello));
91
92         /* Send hello to manager */
93         fwp_mngt_send(FWP_MSG_HELLO, msgb, 
94                         fwp_participant_this, fwp_participant_mngr);
95
96
97         /* receive hello from manager */
98         fwp_mngt_recv(&msg_type, &participant_id, msgb);
99         FWP_DEBUG("Received HELLO msg from nodeid= %d appid= %d\n", 
100                         participant_id.node_id, participant_id.app_id);
101         
102         
103         /* Process hello msg from manager */
104         fwp_msg_hello_inflate(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 }
122
123 int fwp_mngt_init()
124 {
125         fwp_participant_info_t  my_info, mngr_info;
126         unsigned int node_id;
127         int flags;
128         
129         /* Create fwp_participant_this */
130         my_info.id.node_id = inet_addr("127.0.0.1");
131         my_info.id.app_id = getpid();
132         my_info.stream_id = 0;
133
134         fwp_participant_this = fwp_participant_create(&my_info);        
135         fwp_receive_endpoint_create(0, 0, &fwp_participant_this->epointd);
136         /* FIXME 
137         fwp_endpoint_get_params(&(fwp_participant_this->id.node_id), 
138                                 &fwp_participant_this->stream_id,
139                                 &flags,
140                                 fwp_participant_this->epointd);
141         */
142         fwp_endpoint_get_params(&node_id, 
143                                 &fwp_participant_this->stream_id,
144                                 &flags,
145                                 fwp_participant_this->epointd);
146         FWP_DEBUG("Participant_this created node_id id= %d stream id= %d\n",
147                         fwp_participant_this->id.node_id, 
148                         fwp_participant_this->stream_id);
149         
150         /* Create fwp_participant_mngr */
151         mngr_info.id.node_id = inet_addr("127.0.0.1");
152         /*mngr_info.id.node_id = inet_addr("255.255.255.255");*/
153         mngr_info.id.app_id = getpid();
154         mngr_info.stream_id = FWP_MNGR_STREAM_ID;
155         
156         fwp_participant_mngr = fwp_participant_create(&mngr_info);
157         
158         /* Create discovery endpoint */
159         FWP_DEBUG("Service vres created\n");
160         fwp_mngt_service_vres_create(&fwp_participant_mngr->vresd);
161         
162         FWP_DEBUG("Discovery send endpoint created\n");
163         fwp_send_endpoint_create(fwp_participant_mngr->id.node_id,
164                                  fwp_participant_mngr->stream_id,
165                                  0, &fwp_participant_mngr->epointd);    
166         fwp_send_endpoint_bind(fwp_participant_mngr->epointd, 
167                                 fwp_participant_mngr->vresd);
168         
169         return 0;
170 }