-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cc
150 lines (112 loc) · 4.07 KB
/
functions.cc
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
#include "functions.h"
#include <omnetpp.h>
#include "dirent.h"
#include "sys/stat.h"
#include <string>
Functions::Functions() {
ServerioDic = { {"in1", "out1"}, {"in2", "out2"}, {"in3", "out3"}, {"in4", "out4"}, {"in5", "out5"} };
FogioDic = { {"in1", "out3"}, {"in2", "out3"} };
HostioDic = { {"in", "out"} };
}
Functions::~Functions() {
// Cleanup if necessary
}
void Functions::Display(cMessage *msg)
{
EV << " Message: " << msg->getName()
<< " Sender: " << msg->getSenderModule()->getFullName()
<< " Receiver: " << msg->getArrivalModule()->getFullName()
<< " Arrival Gate: " << msg->getArrivalGate()->getName() << "\n";
}
void Functions::Display( std::string displayedText)
{
EV << displayedText << "\n";
}
std::string Functions::getDestinationHostGateByMessage(cMessage *msg)
{
std::string desgate="out1";
std::string messagename = msg->getName();
char lastChar = messagename.back();
if (lastChar == '2' || lastChar == '4' || lastChar == '6' || lastChar == '8' || lastChar == '0') {
desgate="out2";
}
return desgate ;
}
std::string Functions::getFogOut(std::string inputgate,cMessage *msg)
{
if (FogioDic.count(inputgate)) {
return FogioDic[inputgate];
} else {
return getDestinationHostGateByMessage(msg);
}
}
std::string Functions::getHostOut(std::string inputgate,cMessage *msg)
{
return HostioDic[inputgate];
}
std::string Functions::getServerOut(std::string inputgate,cMessage *msg)
{ return ServerioDic[inputgate];
}
std::string Functions::getDestGate(std::string nodeName,std::string inputgate, cMessage *msg)
{
if (nodeName.find("fog") != std::string::npos) {
return getFogOut(inputgate, msg);
}
else if (nodeName.find("host") != std::string::npos) {
return getHostOut(inputgate, msg);
}
else if (nodeName.find("Server") != std::string::npos) {
return getServerOut(inputgate, msg);
}
else {
return "out3";
}
}
std::string Functions::getMessageID(cMessage* msg) {
const char* message = msg->getName();
int id = -1;
const char* patternStart = strstr(message, "image-");
if (patternStart) { // If found, attempt to extract the number immediately after it
sscanf(patternStart, "image-%d", &id);
return std::to_string(id);;
}
return "??";
}
std::string Functions::getPcName(cMessage* msg){
const char* message = msg->getName();
int pcNumber = -1; // Initialize to an invalid number
const char* patternStart = strstr(message, "pc");
if (patternStart) {
sscanf(patternStart, "pc%d", &pcNumber);
return "pc" + std::to_string(pcNumber);
}
return "??";
}
std::string Functions::findLastModifiedNpzFile(const std::string& directoryPath) {
DIR* dir = opendir(directoryPath.c_str());
if (dir == nullptr) {
return "";
}
struct dirent* entry;
time_t latestTime = 0; // Start with the oldest possible timestamp
std::string latestFile = "";
while ((entry = readdir(dir)) != nullptr) {
std::string filename = entry->d_name;
// Check if the filename has the ".npz" extension
if (filename.length() >= 4 && filename.substr(filename.length() - 4) == ".npz") {
struct stat fileStat;
std::string fullPath = directoryPath + "/" + filename;
// Use stat to get file attributes
if (stat(fullPath.c_str(), &fileStat) != -1) {
if (fileStat.st_mtime > latestTime) { // Compare modification times
latestTime = fileStat.st_mtime;
latestFile = filename.substr(0, filename.length() - 4); // Store only the filename without the ".npz" extension
}
} else {
// You can handle the error from stat here, if necessary
}
}
}
closedir(dir);
return "( "+latestFile +" )"; // Return the latest modified file or empty string if not found
}