-
Notifications
You must be signed in to change notification settings - Fork 20
/
concore.hpp
557 lines (492 loc) · 16.7 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// 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
* @brief Class representing the Concore implementation in C++
*/
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; // iport refers to input port
int communication_oport = 0; // oport refers to input port
public:
double delay = 1;
int retrycount = 0;
double simtime;
map <string, int> iport;
map <string, int> oport;
/**
* @brief Constructor for Concore class.
* Initializes the iport and oport maps by parsing the respective files.
* It also creates or attaches to the shared memory segment if required.
*/
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 = ExtractNumeric(it_iport->first);
int oport_number = ExtractNumeric(it_oport->first);
// if iport_number and oport_number is equal to -1 then it refers to File Method,
// otherwise it refers to Shared Memory and the number represent the unique key.
if(oport_number != -1)
{
// oport_number is not equal to -1 so refers to SM and value is key.
communication_oport = 1;
this->createSharedMemory(oport_number);
}
if(iport_number != -1)
{
// iport_number is not equal to -1 so refers to SM and value is key.
communication_iport = 1;
this->getSharedMemory(iport_number);
}
}
/**
* @brief Destructor for Concore class.
* Detaches and removes the shared memory segment if shared memory created.
*/
~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);
}
/**
* @brief Extracts the numeric part from a string.
* @param str The input string.
* @return The numeric part of the string.
* Returns -1 if the string does not contain a numeric part.
*/
key_t ExtractNumeric(const std::string& str) {
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;
}
if (numDigits == 1)
{
// this case is to avoid shared memory when there is just 0 or any negative value in front of edge.
if (std::stoi(numberString) <= 0)
{
return -1;
}
}
return std::stoi(numberString);
}
/**
* @brief Creates a shared memory segment with the given key.
* @param key The key for the shared memory segment.
*/
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;
}
}
/**
* @brief Retrieves an existing shared memory segment with the given key.
* Waits until the shared memory segment is created by the writer process.
* @param key The key for the shared memory segment.
*/
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));
}
/**
* @brief Parses a file containing port and number mappings and returns a map of the values.
* @param filename The name of the file to parse.
* @return A map of port names and their corresponding numbers.
*/
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;
}
/**
* @brief function to compare and determine whether file content has been changed.
* @return true if the content has not changed, false otherwise.
*/
bool unchanged(){
if(olds.compare(s)==0){
s = "";
return true;
}
else{
olds = s;
return false;
}
}
/**
* @brief Parses a string and extracts a vector of double values.
* @param f The input string to parse.
* @return A vector of double values extracted from the input string.
*/
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;
}
/**
* @brief deviate the read to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
* @param port The port number.
* @param name The name of the file.
* @param initstr The initial string
* @return
*/
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);
}
/**
* @brief Reads data from a specified port and name using the FM (File Method) communication protocol.
* @param port The port number.
* @param name The name of the file.
* @param initstr The initial string.
* @return 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;
}
/**
* @brief Reads data from the shared memory segment based on the specified port and name.
* @param port The port number.
* @param name The name of the file.
* @param initstr The initial string to use if the shared memory is not found.
* @return 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;
}
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::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(...){
std::cout << "Read error" << std::endl;
}
}
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;
}
/**
* @brief deviate the write to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
* @param port The port number.
* @param name The name of the file.
* @param val The vector of double values to write.
* @param delta The delta value (default: 0).
*/
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);
}
/**
* @brief deviate the write to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
* @param port The port number.
* @param name The name of the file.
* @param val The string to write.
* @param delta The delta value (default: 0).
*/
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);
}
/**
* @brief write method, accepts a vector double and writes it to the file
* @param port The port number.
* @param name The name of the file.
* @param val The string to write.
* @param delta The delta value (default: 0).
*/
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;
}
}
/**
* @brief write method, accepts a string and writes it to the file
* @param port The port number.
* @param name The name of the file.
* @param val The string to write.
* @param delta The delta value (default: 0).
*/
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;
}
}
/**
* @brief Writes a vector of double values to the shared memory segment based on the specified port and name.
* @param port The port number.
* @param name The name of the file.
* @param val The vector of double values to write.
* @param delta The delta value (default: 0).
*/
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;
}
}
/**
* @brief Writes a string to the shared memory segment based on the specified port and name.
* @param port The port number.
* @param name The name of the file.
* @param val The string to write.
* @param delta The delta value (default: 0).
*/
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;
}
}
/**
* @brief Initializes the system with the given input values.
* @param f The input string containing the values.
* @return A vector of double values representing the initialized system state.
*/
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;
}
};