]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/contract/fres_contract.h
15087077ac425f9795148c1096722b8b6afb4b51
[frescor/frsh.git] / fres / contract / fres_contract.h
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   fres_contract.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Nov  9 22:15:09 2008
51  * 
52  * @brief  Declaration of contract type and functions
53  * 
54  * 
55  */
56 #ifndef FRES_CONTRACT_H
57 #define FRES_CONTRACT_H
58
59 #include <fres_blocks.h>
60 #include <fres_container.h>
61 #include <ul_gavl.h>
62 #include <fres_contract_type.h>
63 #include <fres_contract_idl.h>
64 #include <forb/server_id.h>
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 /**
71  * Contract data type.
72  * 
73  */
74 struct fres_contract {
75         fres_contract_id_t id;  /**< Global contract ID */
76         struct fres_container *container;       /**< Pointer to the block container (opaque type). */
77 };
78
79 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
80                                        const fres_contract_id_t *b)
81 {
82         return forb_server_id_cmp((forb_server_id*)a,
83                                   (forb_server_id*)b);
84 }
85
86 static inline bool fres_contract_id_is_empty(const fres_contract_id_t *id)
87 {
88         bool empty = true;
89         unsigned i;
90
91         for (i=0; i<sizeof(id->byte); i++) {
92                 if (id->byte[i] != 0) {
93                         empty = false;
94                         break;
95                 }
96         }
97         return empty;
98 }
99
100 static inline char *fres_contract_id_to_string(char *dest,
101                                                const fres_contract_id_t *id,
102                                                size_t n)
103 {
104         return forb_server_id_to_string(dest, (forb_server_id*)id, n);
105 }
106
107 struct fres_contract *fres_contract_new(void);
108 void fres_contract_destroy(struct fres_contract *contract);
109
110 /** Destructor for easy use in forb_sequence_free() */
111 static inline void fres_contract_ptr_destroy(struct fres_contract **p)
112 {
113         fres_contract_destroy(*p);
114 }
115
116
117 struct fres_contract *fres_contract_duplicate(struct fres_contract *src);
118
119 /** 
120  * Adds a block of the given type to the contract.
121  *
122  * This function uses fres_container_add_block() to do its job.
123  * 
124  * @param contract Where to add the @a block.
125  * @param type Type of contract being added.
126  * @param block Pointer to the malloced block of given @a type.
127  * 
128  * @return Zero on success, -1 on error and errno is set appropriately.
129  */
130 static inline int
131 fres_contract_add_block(struct fres_contract *contract,
132                         enum fres_block_type type, void *block)
133 {
134         return fres_container_add_block(contract->container, type, block);
135 }
136 /** 
137  * Deletes a block from the contract and frees it from memory.
138  *
139  * This function uses fres_container_del_block() to do its job.
140  * 
141  * @param contract Where to delete the block.
142  * @param type Type of contract to delete.
143  */
144 static inline void
145 fres_contract_del_block(struct fres_contract *contract,
146                         enum fres_block_type type)
147 {
148         fres_container_del_block(contract->container, type);
149 }
150
151 /** 
152  * Returns pointer to a contract block of a particular @a type.
153  * 
154  * This function uses fres_container_get_block() to do its job.
155  * 
156  * @param contract
157  * @param type Type of the block to be returned.
158  * 
159  * @return Pointer to the block or NULL if the block is not present or
160  * deserialized. The memory area pointed by this pointer is owned by
161  * the contract. If the user needs to store the block, it must be
162  * duplicated.
163  */
164 static inline void *
165 fres_contract_get_block(struct fres_contract *contract,
166                         enum fres_block_type type)
167 {
168         return fres_container_get_block(contract->container, type);
169 }
170
171 int
172 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
173
174 /* #define FRES_C2S_FL_FMT_ONELINE */
175 /* #define FRES_C2S_FL_FMT_BLOCKPERLINE */
176 int
177 fres_contract_to_string2(char *dest, size_t size, const struct fres_contract *c, int indent, uint32_t flags);
178
179 bool
180 fres_contract_get_deadline(const frsh_contract_t *contract,
181                            frsh_rel_time_t       *deadline);
182 bool
183 fres_contract_get_budget(const frsh_contract_t *contract,
184                          frsh_rel_time_t       *budget);
185 bool
186 fres_contract_get_period(const frsh_contract_t *contract,
187                          frsh_rel_time_t       *period);
188 frsh_contract_type_t
189 fres_contract_get_type(const frsh_contract_t *contract);
190
191 void
192 fres_contract_print(char *prefix, const struct fres_contract *c);
193
194 static inline int
195 fres_contract_get_num_blocks(const struct fres_contract *c)
196 {
197         return fres_container_get_num_blocks(c->container);
198 }
199
200 static inline int
201 fres_contract_merge(struct fres_contract *dest,
202                      const struct fres_contract *src)
203 {
204         return fres_container_merge(dest->container, src->container);
205 }
206
207
208 /**
209  * Macro which defines type-safe contract "accessor" functions for
210  * various blocks.
211  *
212  * This macro declares the following inline functions:
213  * - fres_contract_add_<type>
214  * - fres_contract_get_<type>
215  * - fres_contract_del_<type>
216  *
217  * The defined functions simply use the container "accessor" functions
218  * (usually defined) by #FRES_CONTAINER_ACCESSOR and are equivalent to
219  * fres_contract_add_block(), fres_contract_del_block() and
220  * fres_contract_get_block() with appropriate parameters.
221  */
222 #define FRES_CONTRACT_ACCESSOR(type)                                    \
223         static inline int                                               \
224         fres_contract_add_##type(struct fres_contract *contract,        \
225                                  fres_block_##type *block)              \
226         {                                                               \
227                 return fres_container_add_##type(contract->container,   \
228                                                  block);                \
229         }                                                               \
230         static inline fres_block_##type *                               \
231         fres_contract_get_##type(const struct fres_contract *contract)  \
232         {                                                               \
233                 return fres_container_get_##type(contract->container);  \
234         }                                                               \
235         static inline void                                              \
236         fres_contract_del_##type(struct fres_contract *contract)        \
237         {                                                               \
238                 fres_container_del_##type(contract->container);         \
239         }
240
241 FRES_CONTRACT_ACCESSOR(label)
242 FRES_CONTRACT_ACCESSOR(resource)
243 FRES_CONTRACT_ACCESSOR(basic)
244 FRES_CONTRACT_ACCESSOR(timing_reqs)
245 FRES_CONTRACT_ACCESSOR(csects)
246 FRES_CONTRACT_ACCESSOR(spare_capacity)
247 FRES_CONTRACT_ACCESSOR(power_management)
248
249 #ifdef __cplusplus
250 } /* extern "C"*/
251 #endif
252
253 #endif