-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtapplot.c
More file actions
180 lines (158 loc) · 4.23 KB
/
tapplot.c
File metadata and controls
180 lines (158 loc) · 4.23 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/shm.h> //For generating keys in
#include <semaphore.h>
#include <string.h> //For string compare
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define MAX_DATA_LENGTH 20 //Length of the Data name=Value / max length of name / max length of Value
#define MAX_PAIRS_PER_SAMPLE 10 //The maximum number of sample Data
#define MAX_NUM_OF_SAMPLES 500
#define MAX_UNIQUE_NAMES 10
#define MAX_SLOT_LENGTH 1000//Max lenght of each slot of the buffer
int prev = 0;
//Struct that stores parsed data
typedef struct
{
char name[MAX_DATA_LENGTH];
char value[MAX_DATA_LENGTH];
}NameValuePair;
//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;
typedef enum {bit0=0, bit1=1} bit;
typedef struct
{
bit latest;
bit reading;
bit slot[2];
sem_t filledSlots;
sem_t emptySlots;
int head;
int tail;
int bufferSize;
char buffer[];
}ringBuffer;
//Read from Process 2
char *readFromBuffer(ringBuffer *rb)
{
sem_wait(&rb->filledSlots);
char *source = (char *)(rb->buffer + rb->head*MAX_SLOT_LENGTH);
//printf("Reading: %s\n", source);
rb->head = ((rb->head+1) % rb->bufferSize);
sem_post(&rb->emptySlots);
//sleep(1);
return source;
}
char *bufread(ringBuffer *sb)
{
bit pair, index;
pair = sb->latest;
sb->reading = pair;
index = sb->slot[pair];
char *item = "";
if (prev != 2*pair + index)
{
item = (sb->buffer + 2*pair*MAX_SLOT_LENGTH + index*MAX_SLOT_LENGTH);
//printf("Reading in PROCESS 3: %s\n", item);
//printf("Index: %d, Value: %s\n", 2*pair*MAX_SLOT_LENGTH + index*MAX_SLOT_LENGTH, item);
prev = 2*pair + index;
}
usleep(50000);
return (item);
}
NameValuePair parseSampleDataStr(char *sampleData, int argn)
{
NameValuePair eofCheck;
if(strcmp(sampleData, "EOF")==0)
{
strncpy(eofCheck.name, "EOF", MAX_DATA_LENGTH);
return eofCheck;
}
if (strcmp(sampleData, "")==0)
{
strncpy(eofCheck.name, "", MAX_DATA_LENGTH);
return eofCheck;
}
int whichNameValuePair = 1;
char *subString;
while(subString = strtok_r(sampleData ,",", &sampleData))
{
//If its the nameValuePair we care about
//We create the nameValuePair and return it
if(whichNameValuePair == argn)
{
NameValuePair tempNVP;
char *name = strtok(subString, "=");
char *value = strtok(NULL, "=");
strncpy(tempNVP.name, name, MAX_DATA_LENGTH);
strncpy(tempNVP.value, value, MAX_DATA_LENGTH);
return tempNVP;
}
else{
whichNameValuePair++;
}
}
}
int main(int argc, char *argv[])
{
//printf("In process 3\n");
int p2P3shmid = atoi(argv[1]);
int argn = atoi(argv[2]);
char *sync = argv[3];
void *test = shmat(p2P3shmid, NULL, 0);
if(test == (void *)-1)
printf("There is an error with shmid");
ringBuffer * sbP2P3;
ringBuffer * rbP2P3;
if (strcmp(sync, "async") == 0)
{
sbP2P3 = (ringBuffer *)test;
}
else
{
rbP2P3 = (ringBuffer *)test;
}
//Create/Open the file in append mode
FILE *file = fopen("dataFile.txt", "w");
if(file == NULL)
{
perror("Error creating/opening the file");
return 1;
}
int x = 1; //This is sampleNumber
while(true)
{
NameValuePair tempNVP;
if (strcmp(sync, "async") == 0)
{
tempNVP = parseSampleDataStr(bufread(sbP2P3), argn);
}
else
{
tempNVP = parseSampleDataStr(readFromBuffer(rbP2P3), argn);
}
if (strcmp(tempNVP.name, "")!=0)
{
//Exit the loop as soon as we reach a EOF signal
if(strcmp(tempNVP.name, "EOF") == 0)
break;
fprintf(file, "%d %s\n", x, tempNVP.value);
fflush(file);
if (x==1)
{
system("gnuplot 'live_plot.gp' &"); //Allows for gnuplot to run in the background
}
x++;
}
}
fclose(file);
return 0;
}