-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstruct_t.c
More file actions
162 lines (146 loc) · 6.16 KB
/
reconstruct_t.c
File metadata and controls
162 lines (146 loc) · 6.16 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
#include "libobjdata.h"
#define MAX_PAIRS_PER_SAMPLE 10 //The maximum number of sample Data
#define MAX_NUM_OF_SAMPLES 500
#define MAX_UNIQUE_NAMES 10
//SampleData (contains a list of NameValuePairs) For each sample can hold up to 50 NameValPair
typedef struct
{
NameValuePair sample[MAX_PAIRS_PER_SAMPLE];
int numOfNameValuePair;
}sampleData;
void *reconstruct(void *arg)
{
//printf("In process 2\n");
thread_arg *targ = (thread_arg *) arg; //get the arguments sent to thread
int currentSampleIndex = 0;
bool endNameIdentified = false;
char endName [20];
int currentSampleDataIndex = 0;
int prevSampleNVPIndex = 0; //Keeps track of index when incremneting through the prevSample
//Creating an Array of sampleData strucutures, each sampleData strucutre can hold up to 50 NAMEVALPAIRS
sampleData sd [MAX_NUM_OF_SAMPLES];
//Intializing sampleData
for(int x = 0; x < MAX_NUM_OF_SAMPLES; x++)
{
for(int i = 0; i < MAX_PAIRS_PER_SAMPLE; i++)
{
sd[x].sample[i].name[0]= '\0';
sd[x].sample[i].value[0] = '\0';
sd[x].numOfNameValuePair = 0;
}
}
while(true)
{
char *str;
if (targ->is_sync == 0)
{
str = consume (targ->buff[0]);
}
else if (targ->is_sync == 1)
{
str = slotread (targ->buff[0]);
}
if (strcmp(str, "") != 0)
{
//Checking if we reached an EOF signal
if(strcmp(str, "EOF")==0)
{
//Checking if the sampleIndex is still 0 And We reached a EOF signal means one data input graph
if(currentSampleDataIndex == 0)
currentSampleDataIndex++;
break;
}
//Reparsing the string again to there respective name and value pairs
char *name = strtok(str, "=");
char *value = strtok(NULL, "="); //Null = contine searching the same string
NameValuePair tempNVP;
strncpy(tempNVP.name, name, MAX_DATA_LENGTH);
strncpy(tempNVP.value, value, MAX_DATA_LENGTH);
//End name has not been identified yet
if(endNameIdentified == false)
{
//Checking if name EXIST in DATASAMPLE to determine endName
for(int sampleIndex = 0; sampleIndex < sd[0].numOfNameValuePair; sampleIndex++)
{
if(strcmp(sd[0].sample[sampleIndex].name, tempNVP.name)== 0)
{
endNameIdentified = true;
strncpy(endName, sd[0].sample[sd[0].numOfNameValuePair-1].name, MAX_DATA_LENGTH);
break;
}
}
if(endNameIdentified == false)
{
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].name, tempNVP.name, MAX_DATA_LENGTH);
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].value, tempNVP.value, MAX_DATA_LENGTH);
sd[currentSampleDataIndex].numOfNameValuePair = sd[currentSampleDataIndex].numOfNameValuePair + 1;
}
else
{
currentSampleDataIndex++;
}
}
//EndName has been identified or its the secondSampleData (Check ReadMe for explanation)
if(currentSampleDataIndex == 1 || endNameIdentified == true)
{
int prevSample = currentSampleDataIndex - 1;
//Use prevSample DATA for CurrentSampleData
while(strcmp(sd[prevSample].sample[sd[currentSampleDataIndex].numOfNameValuePair].name, tempNVP.name) != 0)
{
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].name, sd[prevSample].sample[sd[currentSampleDataIndex].numOfNameValuePair].name, MAX_DATA_LENGTH);
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].value, sd[prevSample].sample[sd[currentSampleDataIndex].numOfNameValuePair].value, MAX_DATA_LENGTH);
sd[currentSampleDataIndex].numOfNameValuePair = sd[currentSampleDataIndex].numOfNameValuePair + 1;
}
//Adding new Data to the currentSample
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].name, tempNVP.name, MAX_DATA_LENGTH);
strncpy(sd[currentSampleDataIndex].sample[sd[currentSampleDataIndex].numOfNameValuePair].value, tempNVP.value, MAX_DATA_LENGTH);
sd[currentSampleDataIndex].numOfNameValuePair = sd[currentSampleDataIndex].numOfNameValuePair + 1;
//If we have reached the end name, we start again with a new Sample set {}
if(strcmp(tempNVP.name, endName) == 0)
{
currentSampleDataIndex++;
}
}
}
}
//Creating a string that repersents each SampleData
for(int sampleSet = 0; sampleSet < currentSampleDataIndex; sampleSet++)
{
//Creating a String that repersents each SampleData + Initalizing this data
char sampleDataAsString[MAX_DATA_LENGTH*sd[0].numOfNameValuePair];
sampleDataAsString[0] = '\0';
for(int sampleVal = 0; sampleVal < sd[sampleSet].numOfNameValuePair; sampleVal++)
{
//Stores a signle nameValPair (+1 for the null terminating sequence)
char nameValCombined [MAX_DATA_LENGTH*2+1];
//The conditions to ADD specifc commas between NameValuePairs
if(sampleVal+1 == sd[sampleSet].numOfNameValuePair)
sprintf(nameValCombined, "%s=%s", sd[sampleSet].sample[sampleVal].name, sd[sampleSet].sample[sampleVal].value);
else
sprintf(nameValCombined, "%s=%s,", sd[sampleSet].sample[sampleVal].name, sd[sampleSet].sample[sampleVal].value);
//Concatenate to the end of the sampleDataAsString structure
strncat(sampleDataAsString, nameValCombined, strlen(nameValCombined));
}
//Writing to the P2P3 Buffer Slot
if (targ->is_sync == 0)
{
produce (targ->buff[1], sampleDataAsString, strlen(sampleDataAsString)+1);
for (int j = 0; j < 1E8; j++);
}
else if (targ->is_sync == 1)
{
slotwrite (targ->buff[1], sampleDataAsString, strlen(sampleDataAsString)+1);
}
}
//EOF ending signal
if (targ->is_sync == 0)
{
produce (targ->buff[1], "EOF", strlen("EOF")+1);
}
else if (targ->is_sync == 1)
{
slotwrite (targ->buff[1], "EOF", strlen("EOF")+1);
}
//printf("Complete Process 2\n");
pthread_exit(NULL);
}