Skip to content

Commit c5c3432

Browse files
authored
Merge pull request #84 from shivangvijay/rasp
Shared Memory initial digit on edge approch
2 parents 38c4f65 + 2b204fb commit c5c3432

1 file changed

Lines changed: 120 additions & 30 deletions

File tree

concore.hpp

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
using namespace std;
2222

23+
/**
24+
* @class Concore
25+
* @brief Class representing the Concore implementation in C++
26+
*/
2327
class Concore{
2428

2529
private:
@@ -44,14 +48,18 @@ class Concore{
4448
map <string, int> iport;
4549
map <string, int> oport;
4650

47-
//Constructor to put in iport and oport values in map
51+
/**
52+
* @brief Constructor for Concore class.
53+
* Initializes the iport and oport maps by parsing the respective files.
54+
* It also creates or attaches to the shared memory segment if required.
55+
*/
4856
Concore(){
4957
iport = mapParser("concore.iport");
5058
oport = mapParser("concore.oport");
5159
std::map<std::string, int>::iterator it_iport = iport.begin();
5260
std::map<std::string, int>::iterator it_oport = oport.begin();
53-
int iport_number = generateNumberString(it_iport->first);
54-
int oport_number = generateNumberString(it_oport->first);
61+
int iport_number = ExtractNumeric(it_iport->first);
62+
int oport_number = ExtractNumeric(it_oport->first);
5563

5664
if(oport_number != -1)
5765
{
@@ -66,6 +74,10 @@ class Concore{
6674
}
6775
}
6876

77+
/**
78+
* @brief Destructor for Concore class.
79+
* Detaches and removes the shared memory segment if shared memory created.
80+
*/
6981
~Concore()
7082
{
7183
// Detach the shared memory segment from the process
@@ -76,44 +88,43 @@ class Concore{
7688
shmctl(shmId_create, IPC_RMID, nullptr);
7789
}
7890

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;
87-
}
88-
89-
key_t generateNumberString(const std::string& str) {
90-
std::map<char, int> alphabetMap = createAlphabetMap();
91+
/**
92+
* @brief Extracts the numeric part from a string.
93+
* @param str The input string.
94+
* @return The numeric part of the string.
95+
* Returns -1 if the string does not contain a numeric part.
96+
*/
97+
key_t ExtractNumeric(const std::string& str) {
9198
std::string numberString;
9299

93100
// Find the number of leading digits in the input string
94101
size_t numDigits = 0;
95102
std::string start_digit = "";
96103
while (numDigits < str.length() && std::isdigit(str[numDigits])) {
97104
numberString += str[numDigits];
98-
++numDigits;
105+
++numDigits;
99106
}
100107

101108
if (numDigits == 0)
102109
{
103110
return -1;
104111
}
105112

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]);
113+
if (numDigits == 1)
114+
{
115+
if (std::stoi(numberString) <= 0)
116+
{
117+
return -1;
111118
}
112119
}
113120

114121
return std::stoi(numberString);
115122
}
116123

124+
/**
125+
* @brief Creates a shared memory segment with the given key.
126+
* @param key The key for the shared memory segment.
127+
*/
117128
void createSharedMemory(key_t key)
118129
{
119130
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
@@ -129,6 +140,11 @@ class Concore{
129140
}
130141
}
131142

143+
/**
144+
* @brief Retrieves an existing shared memory segment with the given key.
145+
* Waits until the shared memory segment is created by the writer process.
146+
* @param key The key for the shared memory segment.
147+
*/
132148
void getSharedMemory(key_t key)
133149
{
134150
while (true) {
@@ -147,6 +163,11 @@ class Concore{
147163
sharedData_get = static_cast<char*>(shmat(shmId_get, NULL, 0));
148164
}
149165

166+
/**
167+
* @brief Parses a file containing port and number mappings and returns a map of the values.
168+
* @param filename The name of the file to parse.
169+
* @return A map of port names and their corresponding numbers.
170+
*/
150171
map<string,int> mapParser(string filename){
151172
map<string,int> ans;
152173

@@ -191,7 +212,10 @@ class Concore{
191212
return ans;
192213
}
193214

194-
//function to compare and determine whether file content has been changed
215+
/**
216+
* @brief function to compare and determine whether file content has been changed.
217+
* @return true if the content has not changed, false otherwise.
218+
*/
195219
bool unchanged(){
196220
if(olds.compare(s)==0){
197221
s = "";
@@ -203,6 +227,11 @@ class Concore{
203227
}
204228
}
205229

230+
/**
231+
* @brief Parses a string and extracts a vector of double values.
232+
* @param f The input string to parse.
233+
* @return A vector of double values extracted from the input string.
234+
*/
206235
vector<double> parser(string f){
207236
vector<double> temp;
208237
string value = "";
@@ -224,6 +253,13 @@ class Concore{
224253
return temp;
225254
}
226255

256+
/**
257+
* @brief deviate the read to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
258+
* @param port The port number.
259+
* @param name The name of the file.
260+
* @param initstr The initial string
261+
* @return
262+
*/
227263
vector<double> read(int port, string name, string initstr)
228264
{
229265
if(communication_iport == 1)
@@ -234,7 +270,13 @@ class Concore{
234270
return read_FM(port, name, initstr);
235271
}
236272

237-
//accepts the file name as string and returns a string of file content
273+
/**
274+
* @brief Reads data from a specified port and name using the FM (File Method) communication protocol.
275+
* @param port The port number.
276+
* @param name The name of the file.
277+
* @param initstr The initial string.
278+
* @return a string of file content
279+
*/
238280
vector<double> read_FM(int port, string name, string initstr){
239281
chrono::milliseconds timespan((int)(1000*delay));
240282
this_thread::sleep_for(timespan);
@@ -290,7 +332,13 @@ class Concore{
290332

291333
}
292334

293-
//accepts the file name as string and returns a string of file content
335+
/**
336+
* @brief Reads data from the shared memory segment based on the specified port and name.
337+
* @param port The port number.
338+
* @param name The name of the file.
339+
* @param initstr The initial string to use if the shared memory is not found.
340+
* @return string of file content
341+
*/
294342
vector<double> read_SM(int port, string name, string initstr){
295343
chrono::milliseconds timespan((int)(1000*delay));
296344
this_thread::sleep_for(timespan);
@@ -345,6 +393,13 @@ class Concore{
345393

346394
}
347395

396+
/**
397+
* @brief deviate the write to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
398+
* @param port The port number.
399+
* @param name The name of the file.
400+
* @param val The vector of double values to write.
401+
* @param delta The delta value (default: 0).
402+
*/
348403
void write(int port, string name, vector<double> val, int delta=0)
349404
{
350405
if(communication_oport == 1)
@@ -355,6 +410,14 @@ class Concore{
355410
return write_FM(port, name, val, delta);
356411
}
357412

413+
414+
/**
415+
* @brief deviate the write to either the SM (Shared Memory) or FM (File Method) communication protocol based on iport and oport.
416+
* @param port The port number.
417+
* @param name The name of the file.
418+
* @param val The string to write.
419+
* @param delta The delta value (default: 0).
420+
*/
358421
void write(int port, string name, string val, int delta=0)
359422
{
360423
if(communication_oport == 1)
@@ -365,7 +428,13 @@ class Concore{
365428
return write_FM(port, name, val, delta);
366429
}
367430

368-
//write method, accepts a vector double and writes it to the file
431+
/**
432+
* @brief write method, accepts a vector double and writes it to the file
433+
* @param port The port number.
434+
* @param name The name of the file.
435+
* @param val The string to write.
436+
* @param delta The delta value (default: 0).
437+
*/
369438
void write_FM(int port, string name, vector<double> val, int delta=0){
370439

371440
try {
@@ -389,7 +458,13 @@ class Concore{
389458
}
390459
}
391460

392-
//write method, accepts a string and writes it to the file
461+
/**
462+
* @brief write method, accepts a string and writes it to the file
463+
* @param port The port number.
464+
* @param name The name of the file.
465+
* @param val The string to write.
466+
* @param delta The delta value (default: 0).
467+
*/
393468
void write_FM(int port, string name, string val, int delta=0){
394469
chrono::milliseconds timespan((int)(2000*delay));
395470
this_thread::sleep_for(timespan);
@@ -408,7 +483,13 @@ class Concore{
408483
}
409484
}
410485

411-
//write method, accepts a vector double and writes it to the file
486+
/**
487+
* @brief Writes a vector of double values to the shared memory segment based on the specified port and name.
488+
* @param port The port number.
489+
* @param name The name of the file.
490+
* @param val The vector of double values to write.
491+
* @param delta The delta value (default: 0).
492+
*/
412493
void write_SM(int port, string name, vector<double> val, int delta=0){
413494

414495
try {
@@ -432,7 +513,13 @@ class Concore{
432513
}
433514
}
434515

435-
//write method, accepts a string and writes it to the file
516+
/**
517+
* @brief Writes a string to the shared memory segment based on the specified port and name.
518+
* @param port The port number.
519+
* @param name The name of the file.
520+
* @param val The string to write.
521+
* @param delta The delta value (default: 0).
522+
*/
436523
void write_SM(int port, string name, string val, int delta=0){
437524
chrono::milliseconds timespan((int)(2000*delay));
438525
this_thread::sleep_for(timespan);
@@ -447,7 +534,11 @@ class Concore{
447534
}
448535
}
449536

450-
//Initialising
537+
/**
538+
* @brief Initializes the system with the given input values.
539+
* @param f The input string containing the values.
540+
* @return A vector of double values representing the initialized system state.
541+
*/
451542
vector<double> initval(string f){
452543
//parsing
453544
vector<double> val = parser(f);
@@ -459,5 +550,4 @@ class Concore{
459550
val.erase(val.begin());
460551
return val;
461552
}
462-
463553
};

0 commit comments

Comments
 (0)