Skip to content

Commit 38c4f65

Browse files
authored
Merge pull request #79 from shivangvijay/rasp
Implemented multiple shared memory based on edge name
2 parents d15e9e0 + 705ea42 commit 38c4f65

1 file changed

Lines changed: 81 additions & 66 deletions

File tree

concore.hpp

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ class Concore{
2727
string s="",olds="";
2828
string inpath = "./in";
2929
string outpath = "./out";
30-
int shmId_;
31-
char* sharedData_;
30+
31+
int shmId_create;
32+
int shmId_get;
33+
34+
char* sharedData_create;
35+
char* sharedData_get;
3236
// File sharing:- 0, Shared Memory:- 1
33-
int communication_ = 0;
37+
int communication_iport = 0;
38+
int communication_oport = 0;
3439

3540
public:
3641
double delay = 1;
@@ -45,81 +50,92 @@ class Concore{
4550
oport = mapParser("concore.oport");
4651
std::map<std::string, int>::iterator it_iport = iport.begin();
4752
std::map<std::string, int>::iterator it_oport = oport.begin();
48-
int iport_number = AlphanumericSM(it_iport->first);
49-
int oport_number = AlphanumericSM(it_oport->first);
53+
int iport_number = generateNumberString(it_iport->first);
54+
int oport_number = generateNumberString(it_oport->first);
5055

51-
if(iport_number != -1 || oport_number != -1)
56+
if(oport_number != -1)
5257
{
53-
communication_ = 1;
54-
if(iport_number > oport_number)
55-
{
56-
this->createSharedMemory();
57-
}
58-
else
59-
{
60-
this->getSharedMemory();
61-
}
62-
}
63-
}
58+
communication_oport = 1;
59+
this->createSharedMemory(oport_number);
60+
}
6461

65-
/**
66-
* return number if number is greater than zero
67-
* and alphanumeric
68-
*/
69-
int AlphanumericSM(const std::string& input) {
70-
bool isPureAlphabetic = true;
71-
for (char c : input) {
72-
if (!std::isalpha(c)) {
73-
isPureAlphabetic = false;
74-
break;
75-
}
76-
}
77-
78-
if (isPureAlphabetic) {
79-
return -1;
80-
} else {
81-
if (int(input[0]))
82-
{
83-
return int(input[0]);
84-
}
85-
return -1;
86-
}
62+
if(iport_number != -1)
63+
{
64+
communication_iport = 1;
65+
this->getSharedMemory(iport_number);
66+
}
8767
}
8868

8969
~Concore()
9070
{
9171
// Detach the shared memory segment from the process
92-
shmdt(sharedData_);
72+
shmdt(sharedData_create);
73+
shmdt(sharedData_get);
9374

9475
// Remove the shared memory segment
95-
shmctl(shmId_, IPC_RMID, nullptr);
76+
shmctl(shmId_create, IPC_RMID, nullptr);
77+
}
78+
79+
std::map<char, int> createAlphabetMap() {
80+
std::map<char, int> alphabetMap;
81+
82+
for (char c = 'A'; c <= 'Z'; ++c) {
83+
alphabetMap[c] = c - 'A' + 1;
84+
}
85+
86+
return alphabetMap;
9687
}
9788

89+
key_t generateNumberString(const std::string& str) {
90+
std::map<char, int> alphabetMap = createAlphabetMap();
91+
std::string numberString;
92+
93+
// Find the number of leading digits in the input string
94+
size_t numDigits = 0;
95+
std::string start_digit = "";
96+
while (numDigits < str.length() && std::isdigit(str[numDigits])) {
97+
numberString += str[numDigits];
98+
++numDigits;
99+
}
100+
101+
if (numDigits == 0)
102+
{
103+
return -1;
104+
}
98105

99-
void createSharedMemory()
106+
// Concatenate the numbers for the first three alphabet characters after the digits
107+
for (size_t i = numDigits; i < str.length() && i < numDigits + 3; ++i) {
108+
char c = std::toupper(str[i]);
109+
if (alphabetMap.count(c) > 0) {
110+
numberString += std::to_string(alphabetMap[c]);
111+
}
112+
}
113+
114+
return std::stoi(numberString);
115+
}
116+
117+
void createSharedMemory(key_t key)
100118
{
101-
std::cout << "create SM" << std::endl;
102-
shmId_ = shmget(1237, 256, IPC_CREAT | 0666);
103-
if (shmId_ == -1) {
119+
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
120+
121+
if (shmId_create == -1) {
104122
std::cerr << "Failed to create shared memory segment." << std::endl;
105123
}
106124

107125
// Attach the shared memory segment to the process's address space
108-
sharedData_ = static_cast<char*>(shmat(shmId_, NULL, 0));
109-
if (sharedData_ == reinterpret_cast<char*>(-1)) {
126+
sharedData_create = static_cast<char*>(shmat(shmId_create, NULL, 0));
127+
if (sharedData_create == reinterpret_cast<char*>(-1)) {
110128
std::cerr << "Failed to attach shared memory segment." << std::endl;
111129
}
112130
}
113131

114-
void getSharedMemory()
132+
void getSharedMemory(key_t key)
115133
{
116-
std::cout << "get SM" << std::endl;
117134
while (true) {
118135
// Get the shared memory segment created by Writer
119-
shmId_ = shmget(1237, 256, 0666);
120-
136+
shmId_get = shmget(key, 256, 0666);
121137
// Check if shared memory exists
122-
if (shmId_ != -1) {
138+
if (shmId_get != -1) {
123139
break; // Break the loop if shared memory exists
124140
}
125141

@@ -128,9 +144,8 @@ class Concore{
128144
}
129145

130146
// Attach the shared memory segment to the process's address space
131-
sharedData_ = static_cast<char*>(shmat(shmId_, NULL, 0));
147+
sharedData_get = static_cast<char*>(shmat(shmId_get, NULL, 0));
132148
}
133-
134149

135150
map<string,int> mapParser(string filename){
136151
map<string,int> ans;
@@ -211,7 +226,7 @@ class Concore{
211226

212227
vector<double> read(int port, string name, string initstr)
213228
{
214-
if(communication_ == 1)
229+
if(communication_iport == 1)
215230
{
216231
return read_SM(port, name, initstr);
217232
}
@@ -281,9 +296,9 @@ class Concore{
281296
this_thread::sleep_for(timespan);
282297
string ins = "";
283298
try {
284-
if (shmId_ != -1) {
285-
if (sharedData_ && sharedData_[0] != '\0') {
286-
std::string message(sharedData_, strnlen(sharedData_, 256));
299+
if (shmId_get != -1) {
300+
if (sharedData_get && sharedData_get[0] != '\0') {
301+
std::string message(sharedData_get, strnlen(sharedData_get, 256));
287302
ins = message;
288303
// std::cout << "Received message: " << message << " ins " << ins.length() << std::endl;
289304
}
@@ -303,9 +318,9 @@ class Concore{
303318
while ((int)ins.length()==0){
304319
this_thread::sleep_for(timespan);
305320
try{
306-
if(shmId_ != -1) {
321+
if(shmId_get != -1) {
307322
std::cout << "in read while\n";
308-
std::string message(sharedData_, strnlen(sharedData_, 256));
323+
std::string message(sharedData_get, strnlen(sharedData_get, 256));
309324
ins = message;
310325
retrycount++;
311326
}
@@ -332,7 +347,7 @@ class Concore{
332347

333348
void write(int port, string name, vector<double> val, int delta=0)
334349
{
335-
if(communication_ == 1)
350+
if(communication_oport == 1)
336351
{
337352
return write_SM(port, name, val, delta);
338353
}
@@ -342,7 +357,7 @@ class Concore{
342357

343358
void write(int port, string name, string val, int delta=0)
344359
{
345-
if(communication_ == 1)
360+
if(communication_oport == 1)
346361
{
347362
return write_SM(port, name, val, delta);
348363
}
@@ -398,14 +413,14 @@ class Concore{
398413

399414
try {
400415
std::ostringstream outfile;
401-
if(shmId_ != -1){
416+
if(shmId_create != -1){
402417
val.insert(val.begin(),simtime+delta);
403418
outfile<<'[';
404419
for(int i=0;i<val.size()-1;i++)
405420
outfile<<val[i]<<',';
406421
outfile<<val[val.size()-1]<<']';
407422
std::string result = outfile.str();
408-
std::strncpy(sharedData_, result.c_str(), 256 - 1);
423+
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
409424
}
410425
else{
411426
throw 505;
@@ -422,8 +437,8 @@ class Concore{
422437
chrono::milliseconds timespan((int)(2000*delay));
423438
this_thread::sleep_for(timespan);
424439
try {
425-
if(shmId_ != -1){
426-
std::strncpy(sharedData_, val.c_str(), 256 - 1);
440+
if(shmId_create != -1){
441+
std::strncpy(sharedData_create, val.c_str(), 256 - 1);
427442
}
428443
else throw 505;
429444
}

0 commit comments

Comments
 (0)