]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/contract/fres_contract.h
Merge branch 'HEAD' of rtime.felk.cvut.cz:/var/git/frescor/frsh_forb.git/
[frescor/frsh.git] / fres / contract / fres_contract.h
1 /**
2  * @file   fres_contract.h
3  * @author Michal Sojka <sojkam1@fel.cvut.cz>
4  * @date   Sun Nov  9 22:15:09 2008
5  * 
6  * @brief  Declaration of contract type and functions
7  * 
8  * 
9  */
10 #ifndef FRES_CONTRACT_H
11 #define FRES_CONTRACT_H
12
13 #include <fres_blocks.h>
14 #include <fres_container.h>
15 #include <ul_gavl.h>
16 #include <fres_contract_type.h>
17 #include <fres_contract_idl.h>
18 #include <forb/server_id.h>
19
20 /**
21  * Contract data type.
22  * 
23  */
24 struct fres_contract {
25         fres_contract_id_t id;  /**< Global contract ID */
26         struct fres_container *container;       /**< Pointer to the block container (opaque type). */
27 };
28
29 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
30                                        const fres_contract_id_t *b)
31 {
32         return forb_server_id_cmp((forb_server_id*)a,
33                                   (forb_server_id*)b);
34 }
35
36 static inline char *fres_contract_id_to_string(char *dest,
37                                                const fres_contract_id_t *id,
38                                                size_t n)
39 {
40         return forb_server_id_to_string(dest, (forb_server_id*)id, n);
41 }
42
43 struct fres_contract *fres_contract_new(void);
44 void fres_contract_destroy(struct fres_contract *contract);
45 struct fres_contract *fres_contract_duplicate(struct fres_contract *src);
46
47 /** 
48  * Adds a block of the given type to the contract.
49  *
50  * This function uses fres_container_add_block() to do its job.
51  * 
52  * @param contract Where to add the @a block.
53  * @param type Type of contract being added.
54  * @param block Pointer to the malloced block of given @a type.
55  * 
56  * @return Zero on success, -1 on error and errno is set appropriately.
57  */
58 static inline int
59 fres_contract_add_block(struct fres_contract *contract,
60                         enum fres_block_type type, void *block)
61 {
62         return fres_container_add_block(contract->container, type, block);
63 }
64 /** 
65  * Deletes a block from the contract and frees it from memory.
66  *
67  * This function uses fres_container_del_block() to do its job.
68  * 
69  * @param contract Where to delete the block.
70  * @param type Type of contract to delete.
71  */
72 static inline void
73 fres_contract_del_block(struct fres_contract *contract,
74                         enum fres_block_type type)
75 {
76         fres_container_del_block(contract->container, type);
77 }
78
79 /** 
80  * Returns pointer to a contract block of a particular @a type.
81  * 
82  * This function uses fres_container_get_block() to do its job.
83  * 
84  * @param contract
85  * @param type Type of the block to be returned.
86  * 
87  * @return Pointer to the block or NULL if the block is not present or
88  * deserialized. The memory area pointed by this pointer is owned by
89  * the contract. If the user needs to store the block, it must be
90  * duplicated.
91  */
92 static inline void *
93 fres_contract_get_block(struct fres_contract *contract,
94                         enum fres_block_type type)
95 {
96         return fres_container_get_block(contract->container, type);
97 }
98
99 int
100 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
101
102 bool
103 fres_contract_get_deadline(const frsh_contract_t *contract,
104                            frsh_rel_time_t       *deadline);
105 void
106 fres_contract_print(char *prefix, const struct fres_contract *c);
107
108 /**
109  * Macro which defines type-safe contract "accessor" functions for
110  * various blocks.
111  *
112  * This macro declares the following inline functions:
113  * - fres_contract_add_<type>
114  * - fres_contract_get_<type>
115  * - fres_contract_del_<type>
116  *
117  * The defined functions simply use the container "accessor" functions
118  * (usually defined) by #FRES_CONTAINER_ACCESSOR and are equivalent to
119  * fres_contract_add_block(), fres_contract_del_block() and
120  * fres_contract_get_block() with appropriate parameters.
121  */
122 #define FRES_CONTRACT_ACCESSOR(type)                                    \
123         static inline int                                               \
124         fres_contract_add_##type(struct fres_contract *contract,        \
125                                  fres_block_##type *block)              \
126         {                                                               \
127                 return fres_container_add_##type(contract->container,   \
128                                                  block);                \
129         }                                                               \
130         static inline fres_block_##type *                               \
131         fres_contract_get_##type(struct fres_contract *contract)        \
132         {                                                               \
133                 return fres_container_get_##type(contract->container);  \
134         }                                                               \
135         static inline void                                              \
136         fres_contract_del_##type(struct fres_contract *contract)        \
137         {                                                               \
138                 fres_container_del_##type(contract->container);         \
139         }
140
141 FRES_CONTRACT_ACCESSOR(label)
142 FRES_CONTRACT_ACCESSOR(resource)
143 FRES_CONTRACT_ACCESSOR(basic)
144 FRES_CONTRACT_ACCESSOR(timing_reqs)
145 FRES_CONTRACT_ACCESSOR(spare_capacity)
146
147 #endif