]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - contract.c
Start of integration with FRSH API
[frescor/frsh.git] / contract.c
1 #include <contract.h>
2 #include "contract_internal.h"
3 #include <stdbool.h>
4 #include <ul_list.h>
5 #include <string.h>
6 #include <frsh_error.h>
7
8
9 struct contract *
10 contract_new(void)
11 {
12         struct contract *c;
13
14         c = malloc(sizeof(*c));
15         if (c) {
16                 memset(c, 0, sizeof(*c));
17         }
18         return c;
19 }
20
21 void
22 contract_destroy(struct contract *contract)
23 {
24         /* TODO handle all other deallocations */
25         free(contract);
26 }
27
28 int
29 contract_add_params(struct contract *contract,
30                     enum contract_part_type type,
31                     void *part_data)
32 {
33         if (!TYPE_VALID(type) ||
34             contract->parts[type].state != CONTRACT_PART_EMPTY)
35                 return FRSH_ERR_BAD_ARGUMENT;
36
37         contract->parts[type].u.data = part_data;
38         return 0;
39 }
40
41 int
42 contract_del_params(struct contract *contract,
43                     enum contract_part_type type)
44 {
45         if (TYPE_VALID(type)) {
46                 switch (contract->parts[type].state) {
47                         case CONTRACT_PART_EMPTY:
48                                 /* nothing to do */
49                                 break;
50                         case CONTRACT_PART_DATA:
51                                 free(contract->parts[type].u.data);
52                                 contract->parts[type].state = CONTRACT_PART_EMPTY;
53                                 break;
54                         case CONTRACT_PART_STREAM:
55                                 /* TODO: stream_free(contract->parts[type].u.seq); */
56                                 contract->parts[type].state = CONTRACT_PART_EMPTY;
57                                 break;
58                 }
59         }
60         return 0;
61 }
62
63 void *
64 contract_get_params(struct contract *contract,
65                     enum contract_part_type type)
66 {
67         if (contract &&
68             TYPE_VALID(type) &&
69             contract->parts[type].state == CONTRACT_PART_DATA) {
70                 return contract->parts[type].u.data;
71         } else {
72                 return NULL;
73         }
74 }
75
76 CORBA_boolean
77 contract_serialize(CDR_Codec *codec, const struct contract *contract)
78 {
79         CORBA_long l;
80         CORBA_long count;
81         CORBA_short s;
82         int i;
83         if (!contract)
84                 return false;
85
86         l = contract->resource_id;
87         CORBA_long_serialize(codec, &l);
88         l = contract->resource_type;
89         CORBA_long_serialize(codec, &l);
90         /* TODO: Label */
91
92         count=0;
93         for (i=0; i<_FRSH_CONTRACT_NUM_PARAMS; i++) {
94                 if (contract->parts[i].state != CONTRACT_PART_EMPTY)
95                         count++;
96         }
97
98         if (!CORBA_long_serialize(codec, &count))
99                 goto err;
100         
101         for (i=0; i<_FRSH_CONTRACT_NUM_PARAMS; i++) {
102                 if (contract->parts[i].state != CONTRACT_PART_EMPTY) {
103                         s = i;
104                         CORBA_short_serialize(codec, &s);
105                         
106                 }
107         }
108         return CORBA_TRUE;
109 err:
110         return CORBA_FALSE;
111 }
112