]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - fres/contract/fres_contract.h
Do not use random contract IDs
[frescor/frsh-forb.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 #include <stdio.h>
66
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70
71 /**
72  * Contract data type.
73  * 
74  */
75 struct fres_contract {
76         fres_contract_id_t id;  /**< Global contract ID */
77         struct fres_container *container;       /**< Pointer to the block container (opaque type). */
78 };
79
80 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
81                                        const fres_contract_id_t *b)
82 {
83         return forb_server_id_cmp((forb_server_id*)a,
84                                   (forb_server_id*)b);
85 }
86
87 static inline bool fres_contract_id_is_empty(const fres_contract_id_t *id)
88 {
89 /*      bool empty = true; */
90 /*      unsigned i; */
91
92 /*      for (i=0; i<sizeof(id->byte); i++) { */
93 /*              if (id->byte[i] != 0) { */
94 /*                      empty = false; */
95 /*                      break; */
96 /*              } */
97 /*      } */
98 /*      return empty; */
99         return *id == 0;
100 }
101
102 static inline char *fres_contract_id_to_string(char *dest,
103                                                const fres_contract_id_t *id,
104                                                size_t n)
105 {
106         snprintf(dest, n, "%llx", *id);
107 /*      return forb_server_id_to_string(dest, (forb_server_id*)id, n); */
108         return dest;
109 }
110
111 struct fres_contract *fres_contract_new(void);
112 void fres_contract_destroy(struct fres_contract *contract);
113
114 /** Destructor for easy use in forb_sequence_free() */
115 static inline void fres_contract_ptr_destroy(struct fres_contract **p)
116 {
117         fres_contract_destroy(*p);
118 }
119
120
121 struct fres_contract *fres_contract_duplicate(struct fres_contract *src);
122
123 /** 
124  * Adds a block of the given type to the contract.
125  *
126  * This function uses fres_container_add_block() to do its job.
127  * 
128  * @param contract Where to add the @a block.
129  * @param type Type of contract being added.
130  * @param block Pointer to the malloced block of given @a type.
131  * 
132  * @return Zero on success, -1 on error and errno is set appropriately.
133  */
134 static inline int
135 fres_contract_add_block(struct fres_contract *contract,
136                         enum fres_block_type type, void *block)
137 {
138         return fres_container_add_block(contract->container, type, block);
139 }
140 /** 
141  * Deletes a block from the contract and frees it from memory.
142  *
143  * This function uses fres_container_del_block() to do its job.
144  * 
145  * @param contract Where to delete the block.
146  * @param type Type of contract to delete.
147  */
148 static inline void
149 fres_contract_del_block(struct fres_contract *contract,
150                         enum fres_block_type type)
151 {
152         fres_container_del_block(contract->container, type);
153 }
154
155 /** 
156  * Returns pointer to a contract block of a particular @a type.
157  * 
158  * This function uses fres_container_get_block() to do its job.
159  * 
160  * @param contract
161  * @param type Type of the block to be returned.
162  * 
163  * @return Pointer to the block or NULL if the block is not present or
164  * deserialized. The memory area pointed by this pointer is owned by
165  * the contract. If the user needs to store the block, it must be
166  * duplicated.
167  */
168 static inline void *
169 fres_contract_get_block(struct fres_contract *contract,
170                         enum fres_block_type type)
171 {
172         return fres_container_get_block(contract->container, type);
173 }
174
175 int
176 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
177
178 /* #define FRES_C2S_FL_FMT_ONELINE */
179 /* #define FRES_C2S_FL_FMT_BLOCKPERLINE */
180 int
181 fres_contract_to_string2(char *dest, size_t size, const struct fres_contract *c, int indent, uint32_t flags);
182
183 bool
184 fres_contract_get_deadline(const frsh_contract_t *contract,
185                            frsh_rel_time_t       *deadline);
186 bool
187 fres_contract_get_budget(const frsh_contract_t *contract,
188                          frsh_rel_time_t       *budget);
189 bool
190 fres_contract_get_period(const frsh_contract_t *contract,
191                          frsh_rel_time_t       *period);
192 frsh_contract_type_t
193 fres_contract_get_type(const frsh_contract_t *contract);
194
195 void
196 fres_contract_print(char *prefix, const struct fres_contract *c);
197
198 static inline int
199 fres_contract_get_num_blocks(const struct fres_contract *c)
200 {
201         return fres_container_get_num_blocks(c->container);
202 }
203
204 static inline int
205 fres_contract_merge(struct fres_contract *dest,
206                      const struct fres_contract *src)
207 {
208         return fres_container_merge(dest->container, src->container);
209 }
210
211
212 /**
213  * Macro which defines type-safe contract "accessor" functions for
214  * various blocks.
215  *
216  * This macro declares the following inline functions:
217  * - fres_contract_add_<type>
218  * - fres_contract_get_<type>
219  * - fres_contract_del_<type>
220  *
221  * The defined functions simply use the container "accessor" functions
222  * (usually defined) by #FRES_CONTAINER_ACCESSOR and are equivalent to
223  * fres_contract_add_block(), fres_contract_del_block() and
224  * fres_contract_get_block() with appropriate parameters.
225  */
226 #define FRES_CONTRACT_ACCESSOR(type)                                    \
227         static inline int                                               \
228         fres_contract_add_##type(struct fres_contract *contract,        \
229                                  fres_block_##type *block)              \
230         {                                                               \
231                 return fres_container_add_##type(contract->container,   \
232                                                  block);                \
233         }                                                               \
234         static inline fres_block_##type *                               \
235         fres_contract_get_##type(const struct fres_contract *contract)  \
236         {                                                               \
237                 return fres_container_get_##type(contract->container);  \
238         }                                                               \
239         static inline void                                              \
240         fres_contract_del_##type(struct fres_contract *contract)        \
241         {                                                               \
242                 fres_container_del_##type(contract->container);         \
243         }
244
245 FRES_CONTRACT_ACCESSOR(label)
246 FRES_CONTRACT_ACCESSOR(resource)
247 FRES_CONTRACT_ACCESSOR(basic)
248 FRES_CONTRACT_ACCESSOR(timing_reqs)
249 FRES_CONTRACT_ACCESSOR(csects)
250 FRES_CONTRACT_ACCESSOR(spare_capacity)
251 FRES_CONTRACT_ACCESSOR(power_management)
252
253 #ifdef __cplusplus
254 } /* extern "C"*/
255 #endif
256
257 #endif