]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/contract/fres_contract.h
Implemented support for contract renegotiation
[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); i++) {
92                 if (((char*)id)[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 struct fres_contract *fres_contract_duplicate(struct fres_contract *src);
110
111 /** 
112  * Adds a block of the given type to the contract.
113  *
114  * This function uses fres_container_add_block() to do its job.
115  * 
116  * @param contract Where to add the @a block.
117  * @param type Type of contract being added.
118  * @param block Pointer to the malloced block of given @a type.
119  * 
120  * @return Zero on success, -1 on error and errno is set appropriately.
121  */
122 static inline int
123 fres_contract_add_block(struct fres_contract *contract,
124                         enum fres_block_type type, void *block)
125 {
126         return fres_container_add_block(contract->container, type, block);
127 }
128 /** 
129  * Deletes a block from the contract and frees it from memory.
130  *
131  * This function uses fres_container_del_block() to do its job.
132  * 
133  * @param contract Where to delete the block.
134  * @param type Type of contract to delete.
135  */
136 static inline void
137 fres_contract_del_block(struct fres_contract *contract,
138                         enum fres_block_type type)
139 {
140         fres_container_del_block(contract->container, type);
141 }
142
143 /** 
144  * Returns pointer to a contract block of a particular @a type.
145  * 
146  * This function uses fres_container_get_block() to do its job.
147  * 
148  * @param contract
149  * @param type Type of the block to be returned.
150  * 
151  * @return Pointer to the block or NULL if the block is not present or
152  * deserialized. The memory area pointed by this pointer is owned by
153  * the contract. If the user needs to store the block, it must be
154  * duplicated.
155  */
156 static inline void *
157 fres_contract_get_block(struct fres_contract *contract,
158                         enum fres_block_type type)
159 {
160         return fres_container_get_block(contract->container, type);
161 }
162
163 int
164 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
165
166 bool
167 fres_contract_get_deadline(const frsh_contract_t *contract,
168                            frsh_rel_time_t       *deadline);
169 void
170 fres_contract_print(char *prefix, const struct fres_contract *c);
171
172 static inline int
173 fres_contract_get_num_blocks(const struct fres_contract *c)
174 {
175         return fres_container_get_num_blocks(c->container);
176 }
177
178 static inline int
179 fres_contract_merge(struct fres_contract *dest,
180                      const struct fres_contract *src)
181 {
182         return fres_container_merge(dest->container, src->container);
183 }
184
185
186 /**
187  * Macro which defines type-safe contract "accessor" functions for
188  * various blocks.
189  *
190  * This macro declares the following inline functions:
191  * - fres_contract_add_<type>
192  * - fres_contract_get_<type>
193  * - fres_contract_del_<type>
194  *
195  * The defined functions simply use the container "accessor" functions
196  * (usually defined) by #FRES_CONTAINER_ACCESSOR and are equivalent to
197  * fres_contract_add_block(), fres_contract_del_block() and
198  * fres_contract_get_block() with appropriate parameters.
199  */
200 #define FRES_CONTRACT_ACCESSOR(type)                                    \
201         static inline int                                               \
202         fres_contract_add_##type(struct fres_contract *contract,        \
203                                  fres_block_##type *block)              \
204         {                                                               \
205                 return fres_container_add_##type(contract->container,   \
206                                                  block);                \
207         }                                                               \
208         static inline fres_block_##type *                               \
209         fres_contract_get_##type(const struct fres_contract *contract)  \
210         {                                                               \
211                 return fres_container_get_##type(contract->container);  \
212         }                                                               \
213         static inline void                                              \
214         fres_contract_del_##type(struct fres_contract *contract)        \
215         {                                                               \
216                 fres_container_del_##type(contract->container);         \
217         }
218
219 FRES_CONTRACT_ACCESSOR(label)
220 FRES_CONTRACT_ACCESSOR(resource)
221 FRES_CONTRACT_ACCESSOR(basic)
222 FRES_CONTRACT_ACCESSOR(timing_reqs)
223 FRES_CONTRACT_ACCESSOR(csects)
224 FRES_CONTRACT_ACCESSOR(spare_capacity)
225
226 #ifdef __cplusplus
227 } /* extern "C"*/
228 #endif
229
230 #endif