-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilterSpec.c
executable file
·263 lines (231 loc) · 7.46 KB
/
FilterSpec.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <pvm3.h>
#include "FilterSpec.h"
#include "Messages.h"
#include <unistd.h>
/*
* Matheus 14,07/2004: changed all this stuff
*/
/* Constructor and destroyer **********************************************/
FilterSpec *createFilterSpec(char *filterName, char *libname) {
int i;
FilterSpec *fs = (FilterSpec *)malloc(sizeof(FilterSpec));
for (i=0;i<MAXINSTANCES;i++){
fs->filterPlacement.hosts[i] = (char*)malloc(sizeof(char)*MAX_HNAME_LENGTH);
fs->filterPlacement.numInstances = 0;
}
strncpy(fs->name, filterName, MAX_FNAME_LENGTH);
strncpy(fs->libname, libname, MAX_LNAME_LENGTH);
return fs;
}
void destroyFilterSpec(FilterSpec *f){
//we dont need to free stuff, everything is allocated statically
free(f);
}
/**************************************************************************/
/* add hosts *************************************************************/
/// adds a host to the filter, return 1 on success 0 otherwise
int addHostToFilter(FilterSpec *fs, char *hostName){
if (fs->filterPlacement.numInstances >= MAXINSTANCES){
printf("FilterSpec.c: cant add host, maximum number of hosts reached for this filter");
return -1;
}
else{
strncpy(fs->filterPlacement.hosts[fs->filterPlacement.numInstances++], hostName, MAX_HNAME_LENGTH);
}
return (1);
}
int addResourceToFilter(FilterSpec *fs, char *resourceName){
if (fs->filterPlacement.numInstances >= MAXINSTANCES){
printf("FilterSpec.c: cant add host, maximum number of hosts reached for this filter");
return -1;
}
else{
fs->filterPlacement.resources[fs->filterPlacement.numInstances-1] = (char *) malloc(sizeof(char) * MAX_RNAME_LENGTH);
strncpy(fs->filterPlacement.resources[fs->filterPlacement.numInstances-1], resourceName, MAX_RNAME_LENGTH);
}
return (1);
}
/// adds a host qty times
int addHostsToFilter(FilterSpec *fs, char *hostName, int qty){
int i;
for (i=0; i<qty; i++){
if (!addHostToFilter(fs, hostName)) return -1;
}
return (1);
}
/****************************************************************************/
/* add streams **************************************************************/
/// Adds a stream to the inputs of a filter
int addInputToFilter(FilterSpec *fs, StreamSpec *ss){
if (fs->numInputs >= MAXINPSTREAMS)
return 0;
fs->inputs[fs->numInputs++] = ss;
return 1;
}
/// Adds a stream to the outputs of a filter
int addOutputToFilter(FilterSpec *fs, StreamSpec *ss){
if (fs->numInputs >= MAXOUTSTREAMS)
return 0;
fs->outputs[fs->numOutputs++] = ss;
return 1;
}
/***************************************************************************/
#ifdef ATTACH
int setAttached(FilterSpec* filter, int attached){
filter->attached = attached;
return 1;
}
int setAttach(FilterSpec* filter, int attach){
filter->attach = attach;
return 1;
}
int readAttConf(FilterSpec*fs, FILE *attFile){
char line[MAX_HNAME_LENGTH + 22];
char hName[MAX_HNAME_LENGTH + 1];
int numInstancesCount = 0;
int trash = 0;
fgets (line, 2000, attFile);
// this is initialized before but in this case we need fill the
// filter Spec structure using a file, so we back to 0 instaces
fs->filterPlacement.numInstances = 0;
fs->command = (char *) malloc(MAX_COM_LENGTH+1);
sscanf(line, "%s", fs->command);
fgets (line, 2000, attFile);
while(!feof(attFile)){
// printf("line = %s\n", line);
sscanf(line, "%d %d %s", &trash,&(fs->filterPlacement.tids[numInstancesCount]), hName);
// printf("tid = %d host = %s\n", fs->filterPlacement.tids[numInstancesCount], hName);
addHostToFilter(fs, hName);
fgets (line, 2000, attFile);
numInstancesCount++;
}
return 1;
}
#endif
void setLastFilter(FilterSpec *fs, int lastFilter) {
#ifdef BMI_FT
fs->lastFilter = lastFilter;
#endif
}
void handleSpawnError(int errorCondition) {
switch (errorCondition){
case PvmBadParam:
printf("FilterSpec.c: Invalid argument\n");
break;
case PvmNoHost:
printf("FilterSpec.c: Specified host not on virtual machine\n");
break;
case PvmNoFile:
printf("FilterSpec.c: Cant find executable on remote machine\n");
break;
case PvmNoMem:
printf("FilterSpec.c: out of memory\n");
break;
case PvmSysErr:
printf("FilterSpec.c: system error\n");
break;
case PvmOutOfRes:
printf("FilterSpec.c: out of resources\n");
break;
default:
pvm_perror("pvm_spawn()");
break;
}
}
/* Spawns the instances of a filter */
int fsSpawnInstances(FilterSpec *fs, char *command, char **argvSpawn){
int j, spawnReturn;
#ifdef ATTACH
FILE *attachConfFile;
char cwd[MAX_CWD_LENGTH];
char attachConf[MAX_FNAME_LENGTH + MAX_CWD_LENGTH+5];
getcwd(cwd, MAX_CWD_LENGTH);
sprintf(attachConf, "%s/%s.atc", cwd, fs->name);
// if this filter is attached we dont need spawn it
if(fs->attached){
attachConfFile = fopen(attachConf,"r");
if(attachConfFile == NULL){
printf("FilterSpec.c: Error opennig attachConfFile. file path = %s\n", attachConf);
return -1;
}
readAttConf(fs, attachConfFile);
fclose(attachConfFile);
for(j = 0; j < fs->filterPlacement.numInstances; j++) {
pvm_notify(PvmTaskExit, MSGT_TEXIT, 1, &(fs->filterPlacement.tids[j]));
pvm_notify(PvmHostDelete, MSGT_HDEL, 1, &(fs->filterPlacement.tids[j]));
}
}else{
// used to create the file "filter.atc" whos content is:
// a list of the tid and host of each filter instance
attachConfFile = fopen(attachConf,"w");
if(attachConfFile == NULL){
printf("FilterSpec.c: Error creating attachConfFile. file path = %s\n", attachConf);
return -1;
}
fprintf(attachConfFile, "%s\n", command);
#endif
//for each instance of this filter...
for(j = 0; j < fs->filterPlacement.numInstances; j++) {
// runs the instance
char hname[MAX_HNAME_LENGTH+1];
strcpy(hname, fs->filterPlacement.hosts[j]);
spawnReturn = pvm_spawn(command, argvSpawn, PvmTaskHost,
hname, 1,
&(fs->filterPlacement.tids[j]));
#ifdef ATTACH
fprintf(attachConfFile, "%d %d %s\n", j,fs->filterPlacement.tids[j], hname);
#endif
//error handling
if (spawnReturn < 0) {
printf("FilterSpec.c: Error creating filter %s, host %s\n",
fs->name, hname);
pvm_perror("");
exit(1);
}
else if (spawnReturn == 0){
printf("FilterSpec.c: Error creating filter %s, host %s\n",
fs->name, hname);
handleSpawnError(fs->filterPlacement.tids[j]);
return -1;
}
pvm_notify(PvmTaskExit, MSGT_TEXIT, 1, &(fs->filterPlacement.tids[j]));
pvm_notify(PvmHostDelete, MSGT_HDEL, 1, &(fs->filterPlacement.tids[j]));
}
#ifdef ATTACH
fclose(attachConfFile);
}
#endif
return 0;
}
/* Spawns one instance of a filter */
int fsSpawnOneInstance(FilterSpec *fs, int instanceAddress, char *command, char **argvSpawn){
int spawnReturn;
char hname[MAX_HNAME_LENGTH+1];
strcpy(hname, fs->filterPlacement.hosts[instanceAddress]);
spawnReturn = pvm_spawn(command, argvSpawn, PvmTaskHost,
hname, 1, &(fs->filterPlacement.tids[instanceAddress]));
//error handling
if (spawnReturn < 0) {
printf("FilterSpec.c: Error creating filter %s, host %s\n",
fs->name, hname);
pvm_perror("");
exit(1);
}
else if (spawnReturn == 0){
printf("FilterSpec.c: Error creating filter %s, host %s\n",
fs->name, hname);
handleSpawnError(fs->filterPlacement.tids[instanceAddress]);
return -1;
}
pvm_notify(PvmTaskExit, MSGT_TEXIT, 1, &(fs->filterPlacement.tids[instanceAddress]));
pvm_notify(PvmHostDelete, MSGT_HDEL, 1, &(fs->filterPlacement.tids[instanceAddress]));
return 0;
}
void fsSetFaultStatus(FilterSpec *fs, int faultStatus) {
#ifdef BMI_FT
fs->faultStatus = faultStatus;
#endif
}