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