-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayout.c
More file actions
executable file
·217 lines (195 loc) · 5.51 KB
/
Layout.c
File metadata and controls
executable file
·217 lines (195 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <unistd.h>
#include "Layout.h"
#include "FilterSpec.h"
#include "StreamSpec.h"
#include "Hosts.h"
/*
* 02/07/2004 Coutinho: Implementei getFilterByName
* 05/07/2004 Coutinho: createFilter2() agora chama initFilterSpec().
* 14/09/2004 Matheus: changed the struct completly
*/
/* Constructor and destroyer ************************************************/
Layout *createLayout() {
Layout *l = (Layout *) malloc(sizeof(Layout));
//hosts
l->hostsStruct = hostsCreate();
//filterspec
l->numFilters = 0;
//streamspec
l->numStreams = 0;
//xName, cwd and command
if (getcwd(l->cwd, MAX_CWD_LENGTH) == NULL){
fprintf(stderr, "Layout.c: directory name is too big\n");
return NULL;
}
sprintf(l->xName, "initScript");
sprintf(l->command, "%s/%s", l->cwd, l->xName);
//manager state
l->managerState.numWorksSent = 0;
l->managerState.oldestWork = 0;
return l;
}
void destroyLayout(Layout *l){
int i;
hostsDestroy(l->hostsStruct);
for (i=0;i<l->numFilters;i++)
destroyFilterSpec(l->filters[i]);
for (i=0;i<l->numStreams;i++)
destroyStreamSpec(l->streams[i]);
free(l);
}
/***************************************************************************/
/* Filter stuff ************************************************************/
// adds a filter to the layout
int addFilterSpec(Layout *l, FilterSpec *f) {
if (l->numFilters == MAXFILTERS)
return 0;
else {
l->filters[l->numFilters++] = f;
return 1;
}
}
//returns the pointer to the Filter, NULL if not found
FilterSpec *getFilterSpecByName(Layout *l, char *name){
int i;
for (i=0; i<l->numFilters; i++) {
if (strcmp(name, l->filters[i]->name) == 0) {
return (l->filters[i]);
}
}
return NULL;
}
void getFilterByTid(Layout *layout, int tid, FilterSpec **pFilterAddress, int *instanceAddress){
int z, w;
FilterSpec *pFilter = NULL;
instanceAddress[0] = -1;
//find who sent the EOW
for (z=0; z < layout->numFilters; z++){
pFilter = layout->filters[z];
for (w=0; w < pFilter->filterPlacement.numInstances; w++){
if (pFilter->filterPlacement.tids[w] == tid){
instanceAddress[0] = w;
pFilterAddress[0] = pFilter;
return;
}
}
}
}
/**************************************************************************/
/**Sanity check for Two Senders and One Receiver
*
* The following scenario is not permited by AH currently.
* <pre>
* FilterA
* \
* v
* FilterC, inputPort X
* ^
* /
* FilterB
* </pre>
*
* OTOH, the scenario with the streams inverted is allowed in this
* version of AntHill (Itamar's alterations... Not even God knows if
* they work)
*
* @return NO RETURN! This function terminates program execution if an error
* is found.
*/
void sanityCheckTwoSendersOneReceiver(Layout* l, StreamSpec* s)
{
int i, j, k;
_filter_spec_ptr sIF = NULL; /* current stream input filter*/
_filter_spec_ptr sOF = NULL; /* current stream output filter*/
_filter_spec_ptr otherIF = NULL;
_filter_spec_ptr otherOF = NULL;
char* err_msg = "Invalid layout: filters '%s' and '%s' are outputting"
" to the same input port (%s) of filter '%s'\n";
sIF = s->fromFilter;
for (i = 0; i < l->numStreams; ++i ) {
StreamSpec* other = l->streams[i];
otherIF = other->fromFilter;
/* Same input port ... */
if (strcmp(s->toPortName, other->toPortName) == 0 ) {
/* ... and same output filter? No can do, buddy.*/
for(j = 0; j< s->numToSend; ++j) {
sOF = s->toFilter[j];
for(k = 0; k < other->numToSend; ++k) {
otherOF = other->toFilter[k];
if (sOF == otherOF ){
fprintf(stderr,
err_msg,
sIF->name,
otherIF->name,
s->toPortName,
sOF->name);
/* No reason to continue. */
exit(EXIT_FAILURE);
}
}
}
}
}
}
/**Sanity check for one sender writing for two receivers in two streams.
*
* The following scenario is not permited by AH currently.
* <pre>
* FilterX
* ^
* /
* FilterA, ouputPort A
* \ << The test is executed from this stream's POV
* v
* FilterY
* </pre>
*
*/
void sanityCheckOneSenderTwoReceivers(Layout* l, StreamSpec* s)
{
int i;
_filter_spec_ptr sIF = NULL; /* current stream input filter*/
_filter_spec_ptr otherIF = NULL; /* other stream input filter */
char* err_msg = "Invalid layout: filter '%s' @ port '%s' is"
" outputting to different streams at the same time\n";
sIF = s->fromFilter;
for (i = 0; i < l->numStreams; ++i ) {
StreamSpec* other = l->streams[i];
otherIF = other->fromFilter;
/* Same output port same (output) filter? No can do, buddy. */
if ( (strcmp(s->fromPortName, other->fromPortName) == 0) &&
(sIF == otherIF) )
{
fprintf(stderr, err_msg, sIF->name, s->fromPortName);
/* No reason to continue. */
exit(EXIT_FAILURE);
}
}
}
/* StreamSpec *************************************************************/
//adds a stream to the layout, returns 1 on success, 0 otherwise
int addStreamSpec(Layout *l, StreamSpec *s){
/* Sanity checks
*
* TODO: move documentation comments of those functions to .h
*/
sanityCheckOneSenderTwoReceivers(l, s);
sanityCheckTwoSendersOneReceiver(l, s);
/* Register Stream */
if (l->numStreams == MAXSTREAMS)
return 0;
else {
l->streams[l->numStreams++] = s;
return 1;
}
}
//returns the pointer to the stream on success, NULL if not found
StreamSpec *getStreamSpecByName(Layout *l, char *name){
int i;
for (i=0; i<l->numStreams; i++) {
if (strcmp(name, l->streams[i]->name) == 0) {
return (l->streams[i]);
}
}
return NULL;
}