2 * @file fres_contract.h
3 * @author Michal Sojka <sojkam1@fel.cvut.cz>
4 * @date Sun Nov 9 22:15:09 2008
6 * @brief Declaration of contract type and functions
10 #ifndef FRES_CONTRACT_H
11 #define FRES_CONTRACT_H
13 #include <fres_blocks.h>
14 #include <fres_container.h>
16 #include <fres_contract_type.h>
17 #include <fres_contract_idl.h>
18 #include <forb/server_id.h>
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). */
33 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
34 const fres_contract_id_t *b)
36 return forb_server_id_cmp((forb_server_id*)a,
40 static inline char *fres_contract_id_to_string(char *dest,
41 const fres_contract_id_t *id,
44 return forb_server_id_to_string(dest, (forb_server_id*)id, n);
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);
52 * Adds a block of the given type to the contract.
54 * This function uses fres_container_add_block() to do its job.
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.
60 * @return Zero on success, -1 on error and errno is set appropriately.
63 fres_contract_add_block(struct fres_contract *contract,
64 enum fres_block_type type, void *block)
66 return fres_container_add_block(contract->container, type, block);
69 * Deletes a block from the contract and frees it from memory.
71 * This function uses fres_container_del_block() to do its job.
73 * @param contract Where to delete the block.
74 * @param type Type of contract to delete.
77 fres_contract_del_block(struct fres_contract *contract,
78 enum fres_block_type type)
80 fres_container_del_block(contract->container, type);
84 * Returns pointer to a contract block of a particular @a type.
86 * This function uses fres_container_get_block() to do its job.
89 * @param type Type of the block to be returned.
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
97 fres_contract_get_block(struct fres_contract *contract,
98 enum fres_block_type type)
100 return fres_container_get_block(contract->container, type);
104 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
107 fres_contract_get_deadline(const frsh_contract_t *contract,
108 frsh_rel_time_t *deadline);
110 fres_contract_print(char *prefix, const struct fres_contract *c);
113 * Macro which defines type-safe contract "accessor" functions for
116 * This macro declares the following inline functions:
117 * - fres_contract_add_<type>
118 * - fres_contract_get_<type>
119 * - fres_contract_del_<type>
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.
126 #define FRES_CONTRACT_ACCESSOR(type) \
128 fres_contract_add_##type(struct fres_contract *contract, \
129 fres_block_##type *block) \
131 return fres_container_add_##type(contract->container, \
134 static inline fres_block_##type * \
135 fres_contract_get_##type(const struct fres_contract *contract) \
137 return fres_container_get_##type(contract->container); \
140 fres_contract_del_##type(struct fres_contract *contract) \
142 fres_container_del_##type(contract->container); \
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)