-
Notifications
You must be signed in to change notification settings - Fork 0
/
concore.hpp
463 lines (396 loc) · 12.9 KB
/
concore.hpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// concore.hpp -- this C++ include file will be the equivalent of concore.py
#include <iostream>
#include <vector>
#include <iomanip> //for setprecision
#include <map>
//libraries for files
#include <fstream>
#include <sstream>
#include <string>
//libraries for platform independent delay. Supports C++11 upwards
#include <chrono>
#include <thread>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <cstring>
#include <cctype>
using namespace std;
class Concore{
private:
//private variables
string s="",olds="";
string inpath = "./in";
string outpath = "./out";
int shmId_create;
int shmId_get;
char* sharedData_create;
char* sharedData_get;
// File sharing:- 0, Shared Memory:- 1
int communication_iport = 0;
int communication_oport = 0;
public:
double delay = 1;
int retrycount = 0;
double simtime;
map <string, int> iport;
map <string, int> oport;
//Constructor to put in iport and oport values in map
Concore(){
iport = mapParser("concore.iport");
oport = mapParser("concore.oport");
std::map<std::string, int>::iterator it_iport = iport.begin();
std::map<std::string, int>::iterator it_oport = oport.begin();
int iport_number = generateNumberString(it_iport->first);
int oport_number = generateNumberString(it_oport->first);
if(oport_number != -1)
{
communication_oport = 1;
this->createSharedMemory(oport_number);
}
if(iport_number != -1)
{
communication_iport = 1;
this->getSharedMemory(iport_number);
}
}
~Concore()
{
// Detach the shared memory segment from the process
shmdt(sharedData_create);
shmdt(sharedData_get);
// Remove the shared memory segment
shmctl(shmId_create, IPC_RMID, nullptr);
}
std::map<char, int> createAlphabetMap() {
std::map<char, int> alphabetMap;
for (char c = 'A'; c <= 'Z'; ++c) {
alphabetMap[c] = c - 'A' + 1;
}
return alphabetMap;
}
key_t generateNumberString(const std::string& str) {
std::map<char, int> alphabetMap = createAlphabetMap();
std::string numberString;
// Find the number of leading digits in the input string
size_t numDigits = 0;
std::string start_digit = "";
while (numDigits < str.length() && std::isdigit(str[numDigits])) {
numberString += str[numDigits];
++numDigits;
}
if (numDigits == 0)
{
return -1;
}
// Concatenate the numbers for the first three alphabet characters after the digits
for (size_t i = numDigits; i < str.length() && i < numDigits + 3; ++i) {
char c = std::toupper(str[i]);
if (alphabetMap.count(c) > 0) {
numberString += std::to_string(alphabetMap[c]);
}
}
return std::stoi(numberString);
}
void createSharedMemory(key_t key)
{
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
if (shmId_create == -1) {
std::cerr << "Failed to create shared memory segment." << std::endl;
}
// Attach the shared memory segment to the process's address space
sharedData_create = static_cast<char*>(shmat(shmId_create, NULL, 0));
if (sharedData_create == reinterpret_cast<char*>(-1)) {
std::cerr << "Failed to attach shared memory segment." << std::endl;
}
}
void getSharedMemory(key_t key)
{
while (true) {
// Get the shared memory segment created by Writer
shmId_get = shmget(key, 256, 0666);
// Check if shared memory exists
if (shmId_get != -1) {
break; // Break the loop if shared memory exists
}
std::cout << "Shared memory does not exist. Make sure the writer process is running." << std::endl;
sleep(1); // Sleep for 1 second before checking again
}
// Attach the shared memory segment to the process's address space
sharedData_get = static_cast<char*>(shmat(shmId_get, NULL, 0));
}
map<string,int> mapParser(string filename){
map<string,int> ans;
ifstream portfile;
string portstr;
portfile.open(filename);
if(portfile){
ostringstream ss;
ss << portfile.rdbuf();
portstr = ss.str();
portfile.close();
}
portstr[portstr.size()-1]=',';
portstr+='}';
int i=0;
string portname="";
string portnum="";
while(portstr[i]!='}'){
if(portstr[i]=='\''){
i++;
while(portstr[i]!='\''){
portname+=portstr[i];
i++;
}
ans.insert({portname,0});
}
if(portstr[i]==':'){
i++;
while(portstr[i]!=','){
portnum+=portstr[i];
i++;
}
ans[portname]=stoi(portnum);
portnum="";
portname="";
}
i++;
}
return ans;
}
//function to compare and determine whether file content has been changed
bool unchanged(){
if(olds.compare(s)==0){
s = "";
return true;
}
else{
olds = s;
return false;
}
}
vector<double> parser(string f){
vector<double> temp;
string value = "";
//Changing last bracket to comma to use comma as a delimiter
f[f.length()-1]=',';
for(int i=1;i<f.length();i++){
if(f[i]!=',')
value+=f[i];
else{
if((int)value.size()!=0)
temp.push_back(stod(value));
//reset value
value = "";
}
}
return temp;
}
vector<double> read(int port, string name, string initstr)
{
if(communication_iport == 1)
{
return read_SM(port, name, initstr);
}
return read_FM(port, name, initstr);
}
//accepts the file name as string and returns a string of file content
vector<double> read_FM(int port, string name, string initstr){
chrono::milliseconds timespan((int)(1000*delay));
this_thread::sleep_for(timespan);
string ins;
try {
ifstream infile;
infile.open(inpath+to_string(port)+"/"+name, ios::in);
if(infile) {
ostringstream ss;
ss << infile.rdbuf(); // reading data
ins = ss.str(); //saving data as string
infile.close();
}
else {
throw 505;}
}
catch (...) {
ins = initstr;
}
while ((int)ins.length()==0){
this_thread::sleep_for(timespan);
try{
ifstream infile;
infile.open(inpath+to_string(port)+"/"+name, ios::in);
if(infile) {
ostringstream ss;
ss << infile.rdbuf(); // reading data
ins = ss.str();
retrycount++;
infile.close();
}
else{
retrycount++;
throw 505;
}
}
//observed retry count in C++ from various tests is approx 80.
catch(...){
cout<<"Read error";
}
}
s += ins;
vector<double> inval = parser(ins);
simtime = simtime > inval[0] ? simtime : inval[0];
//returning a string with data excluding simtime
inval.erase(inval.begin());
return inval;
}
//accepts the file name as string and returns a string of file content
vector<double> read_SM(int port, string name, string initstr){
chrono::milliseconds timespan((int)(1000*delay));
this_thread::sleep_for(timespan);
string ins = "";
try {
if (shmId_get != -1) {
if (sharedData_get && sharedData_get[0] != '\0') {
std::string message(sharedData_get, strnlen(sharedData_get, 256));
ins = message;
// std::cout << "Received message: " << message << " ins " << ins.length() << std::endl;
}
else
{
throw 505;
}
}
else
{
throw 505;
}
} catch (...) {
ins = initstr;
}
while ((int)ins.length()==0){
this_thread::sleep_for(timespan);
try{
if(shmId_get != -1) {
std::cout << "in read while\n";
std::string message(sharedData_get, strnlen(sharedData_get, 256));
ins = message;
retrycount++;
}
else{
retrycount++;
throw 505;
}
}
//observed retry count in C++ from various tests is approx 80.
catch(...){
cout<<"Read error";
}
}
s += ins;
vector<double> inval = parser(ins);
simtime = simtime > inval[0] ? simtime : inval[0];
//returning a string with data excluding simtime
inval.erase(inval.begin());
return inval;
}
void write(int port, string name, vector<double> val, int delta=0)
{
if(communication_oport == 1)
{
return write_SM(port, name, val, delta);
}
return write_FM(port, name, val, delta);
}
void write(int port, string name, string val, int delta=0)
{
if(communication_oport == 1)
{
return write_SM(port, name, val, delta);
}
return write_FM(port, name, val, delta);
}
//write method, accepts a vector double and writes it to the file
void write_FM(int port, string name, vector<double> val, int delta=0){
try {
ofstream outfile;
outfile.open(outpath+to_string(port)+"/"+name, ios::out);
if(outfile){
val.insert(val.begin(),simtime+delta);
outfile<<'[';
for(int i=0;i<val.size()-1;i++)
outfile<<val[i]<<',';
outfile<<val[val.size()-1]<<']';
outfile.close();
}
else{
throw 505;
}
}
catch(...){
cout<<"skipping +"<<outpath<<port<<" /"<<name;
}
}
//write method, accepts a string and writes it to the file
void write_FM(int port, string name, string val, int delta=0){
chrono::milliseconds timespan((int)(2000*delay));
this_thread::sleep_for(timespan);
try {
string temp;
ofstream outfile;
outfile.open(outpath+to_string(port)+"/"+name, ios::out);
if(outfile){
outfile<<val;
outfile.close();
}
else throw 505;
}
catch(...){
cout<<"skipping +"<<outpath<<port<<" /"<<name;
}
}
//write method, accepts a vector double and writes it to the file
void write_SM(int port, string name, vector<double> val, int delta=0){
try {
std::ostringstream outfile;
if(shmId_create != -1){
val.insert(val.begin(),simtime+delta);
outfile<<'[';
for(int i=0;i<val.size()-1;i++)
outfile<<val[i]<<',';
outfile<<val[val.size()-1]<<']';
std::string result = outfile.str();
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
}
else{
throw 505;
}
}
catch(...){
cout<<"skipping +"<<outpath<<port<<" /"<<name;
}
}
//write method, accepts a string and writes it to the file
void write_SM(int port, string name, string val, int delta=0){
chrono::milliseconds timespan((int)(2000*delay));
this_thread::sleep_for(timespan);
try {
if(shmId_create != -1){
std::strncpy(sharedData_create, val.c_str(), 256 - 1);
}
else throw 505;
}
catch(...){
cout<<"skipping +"<<outpath<<port<<" /"<<name;
}
}
//Initialising
vector<double> initval(string f){
//parsing
vector<double> val = parser(f);
//determining simtime
simtime = val[0];
//returning the rest of the values(except simtime) in val
val.erase(val.begin());
return val;
}
};