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>
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). */
29 static inline int fres_contract_id_cmp(const fres_contract_id_t *a,
30 const fres_contract_id_t *b)
32 return forb_server_id_cmp((forb_server_id*)a,
36 static inline char *fres_contract_id_to_string(char *dest,
37 const fres_contract_id_t *id,
40 return forb_server_id_to_string(dest, (forb_server_id*)id, n);
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);
48 * Adds a block of the given type to the contract.
50 * This function uses fres_container_add_block() to do its job.
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.
56 * @return Zero on success, -1 on error and errno is set appropriately.
59 fres_contract_add_block(struct fres_contract *contract,
60 enum fres_block_type type, void *block)
62 return fres_container_add_block(contract->container, type, block);
65 * Deletes a block from the contract and frees it from memory.
67 * This function uses fres_container_del_block() to do its job.
69 * @param contract Where to delete the block.
70 * @param type Type of contract to delete.
73 fres_contract_del_block(struct fres_contract *contract,
74 enum fres_block_type type)
76 fres_container_del_block(contract->container, type);
80 * Returns pointer to a contract block of a particular @a type.
82 * This function uses fres_container_get_block() to do its job.
85 * @param type Type of the block to be returned.
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
93 fres_contract_get_block(struct fres_contract *contract,
94 enum fres_block_type type)
96 return fres_container_get_block(contract->container, type);
100 fres_contract_to_string(char *dest, size_t size, const struct fres_contract *c);
103 fres_contract_get_deadline(const frsh_contract_t *contract,
104 frsh_rel_time_t *deadline);
106 fres_contract_print(char *prefix, const struct fres_contract *c);
109 * Macro which defines type-safe contract "accessor" functions for
112 * This macro declares the following inline functions:
113 * - fres_contract_add_<type>
114 * - fres_contract_get_<type>
115 * - fres_contract_del_<type>
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.
122 #define FRES_CONTRACT_ACCESSOR(type) \
124 fres_contract_add_##type(struct fres_contract *contract, \
125 fres_block_##type *block) \
127 return fres_container_add_##type(contract->container, \
130 static inline fres_block_##type * \
131 fres_contract_get_##type(struct fres_contract *contract) \
133 return fres_container_get_##type(contract->container); \
136 fres_contract_del_##type(struct fres_contract *contract) \
138 fres_container_del_##type(contract->container); \
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)