1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
13 #include <anna/diameter.comm/OriginHost.hpp>
15 #include <anna/diameter.comm/Engine.hpp>
16 #include <anna/diameter.comm/Message.hpp>
17 #include <anna/diameter.comm/Entity.hpp>
18 #include <anna/diameter.comm/LocalServer.hpp>
19 #include <anna/diameter/codec/EngineManager.hpp>
20 #include <anna/core/core.hpp>
21 #include <anna/time/Date.hpp>
22 #include <anna/xml/Compiler.hpp>
24 using namespace anna::diameter::comm;
27 OriginHost::OriginHost(anna::diameter::comm::Engine* commEngine, unsigned int applicationId) :
28 a_commEngine(commEngine), a_applicationId(applicationId) {
30 a_codecEngine = anna::diameter::codec::EngineManager::instantiate().getCodecEngine(applicationId); // i know, this is going to exist (getCodecEngine is not null)
35 a_detailedLog = false;
38 a_diameterServer = NULL;
41 a_requestRetransmissions = 0;
45 a_burstRepeat = false;
46 a_burstActive = false;
48 a_burstDeliveryIt = a_burstMessages.begin();
50 a_burstPopCounter = 0;
53 const std::string &OriginHost::getName() const throw() {
54 return a_commEngine->getOriginHostName();
57 void OriginHost::createEntity(const std::string &entityRepresentation, const anna::Millisecond &bindTimeout, const anna::Millisecond &applicationTimeout) throw(anna::RuntimeException) {
59 anna::socket_v servers = anna::functions::getSocketVectorFromString(entityRepresentation);
60 std::string entityDescription = "Launcher diameter entity for "; entityDescription += getName();
61 a_entity = (anna::diameter::comm::Entity*)a_commEngine->createEntity(servers, entityDescription);
62 a_entity->setClassCodeTimeout(anna::diameter::comm::ClassCode::Bind, bindTimeout);
63 a_entity->setClassCodeTimeout(anna::diameter::comm::ClassCode::ApplicationMessage, applicationTimeout);
66 void OriginHost::createDiameterServer(const std::string &serverRepresentation, int sessions, const anna::Millisecond &inactivityTimeout, const anna::Millisecond &applicationTimeout, const std::string &ceaPathfile) throw(anna::RuntimeException) {
68 //if(sessions <= 0) return; negative implies no limit for accepted connections
70 std::string address; int port;
71 anna::functions::getAddressAndPortFromSocketLiteral(serverRepresentation, address, port);
72 std::string serverDescription = "Launcher diameter local server for "; serverDescription += getName();
73 a_commEngine->setCEA(ceaPathfile);
74 a_diameterServer = (anna::diameter::comm::LocalServer*)(a_commEngine->createLocalServer(address, port, sessions));
75 // we could set sessions = 0, and after application run(), use setMaxConnections(real sessions)
76 // over the local server in order to start it.
78 a_diameterServer->setDescription(serverDescription);
79 a_diameterServer->setAllowedInactivityTime(inactivityTimeout);
80 a_diameterServer->setClassCodeTimeout(anna::diameter::comm::ClassCode::ApplicationMessage, applicationTimeout);
83 anna::diameter::comm::Message *OriginHost::createCommMessage() throw(anna::RuntimeException) {
84 anna::diameter::comm::Message *result = a_commMessages.create();
85 result->setRetries(a_requestRetransmissions);
86 if (a_requestRetransmissions > 0) result->setOnExpiry(anna::diameter::comm::Message::OnExpiry::Retransmit);
91 void OriginHost::releaseCommMessage(anna::diameter::comm::Message *msg) throw() {
92 a_commMessages.release(msg);
96 void OriginHost::writeLogFile(const anna::DataBlock & db, const std::string &logExtension, const std::string &detail) const throw() {
97 anna::diameter::codec::Message codecMsg;
98 try { codecMsg.decode(db); } catch(anna::RuntimeException &ex) { ex.trace(); }
99 writeLogFile(codecMsg, logExtension, detail);
103 void OriginHost::writeLogFile(const anna::diameter::codec::Message &decodedMessage, const std::string &logExtension, const std::string &detail) const throw() {
105 std::string targetFile = a_logFile;
109 targetFile += logExtension;
112 std::ofstream out(targetFile.c_str(), std::ifstream::out | std::ifstream::app);
114 std::string title = "[";
115 title += logExtension;
117 // Build complete log:
118 std::string log = "\n";
119 std::string xml = decodedMessage.asXMLString();
123 anna::time::Date now;
126 title += now.asString();
127 log += anna::functions::highlight(title, anna::functions::TextHighlightMode::OverAndUnderline);
130 log += anna::functions::highlight("Used resource");
141 // <unix ms timestamp>.<originHost>.<hop by hop>.<end to end>.<message code>.<request|answer>.<type of event>.xml
142 std::string name = anna::functions::asString((anna::Millisecond)anna::functions::millisecond());
144 name += getCommEngine()->getOriginHostName();
146 name += anna::functions::asString(decodedMessage.getHopByHop());
148 name += anna::functions::asString(decodedMessage.getEndToEnd());
150 name += anna::functions::asString(decodedMessage.getId().first);
152 name += ((decodedMessage.getId().second) ? "request.":"answer.");
153 name += logExtension;
155 std::ofstream outMsg(name.c_str(), std::ifstream::out | std::ifstream::app);
156 outMsg.write(xml.c_str(), xml.size());
161 out.write(log.c_str(), log.size());
165 void OriginHost::writeBurstLogFile(const std::string &buffer) throw() {
166 std::ofstream out(a_burstLogFile.c_str(), std::ifstream::out | std::ifstream::app);
167 out.write(buffer.c_str(), buffer.size());
168 out.close(); // close() will be called when the object is destructed (i.e., when it goes out of scope).
169 // you'd call close() only if you indeed for some reason wanted to close the filestream
170 // earlier than it goes out of scope.
173 int OriginHost::clearBurst() throw() {
174 int size = a_burstMessages.size();
177 std::map<int, anna::diameter::comm::Message*>::const_iterator it;
178 std::map<int, anna::diameter::comm::Message*>::const_iterator it_min(a_burstMessages.begin());
179 std::map<int, anna::diameter::comm::Message*>::const_iterator it_max(a_burstMessages.end());
181 for(it = it_min; it != it_max; it++) releaseCommMessage((*it).second);
183 a_burstMessages.clear();
185 std::string msg = "Burst list already empty. Nothing done";
186 std::cout << msg << std::endl;
187 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
190 a_burstActive = false;
192 a_burstDeliveryIt = a_burstMessages.begin();
196 int OriginHost::loadBurstMessage(const anna::DataBlock & db) throw(anna::RuntimeException) {
197 anna::diameter::comm::Message *msg = createCommMessage();
199 a_burstMessages[a_burstLoadIndx++] = msg;
200 return (a_burstLoadIndx - 1);
203 int OriginHost::stopBurst() throw() {
205 std::string msg = "Burst launch is already stopped. Nothing done";
206 std::cout << msg << std::endl;
207 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
211 a_burstActive = false;
212 // Remaining on cycle:
213 return (a_burstMessages.size() - (*a_burstDeliveryIt).first);
216 int OriginHost::popBurst(int releaseAmount) throw() {
218 std::string msg = "Burst launch is stopped. Nothing done";
219 std::cout << msg << std::endl;
220 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
224 if(releaseAmount < 1) {
225 std::string msg = "No valid release amount is specified. Ignoring burst pop";
226 std::cout << msg << std::endl;
227 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
231 int currentOTArequests = a_entity->getOTARequests();
232 a_burstPopCounter = (releaseAmount > currentOTArequests) ? currentOTArequests : releaseAmount;
233 return a_burstPopCounter;
236 int OriginHost::pushBurst(int loadAmount) throw() {
237 if(a_burstMessages.size() == 0) {
238 std::string msg = "Burst data not found (empty list). Ignoring burst launch";
239 std::cout << msg << std::endl;
240 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
245 std::string msg = "No valid load amount is specified. Ignoring burst push";
246 std::cout << msg << std::endl;
247 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
251 a_burstActive = true;
254 for(count = 0; count < loadAmount; count++)
255 if(!sendBurstMessage()) break;
260 int OriginHost::sendBurst(int loadAmount) throw() {
261 if(a_burstMessages.size() == 0) {
262 std::string msg = "Burst data not found (empty list). Ignoring burst launch";
263 std::cout << msg << std::endl;
264 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
269 std::string msg = "No valid load amount is specified. Ignoring burst send";
270 std::cout << msg << std::endl;
271 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
277 for(count = 0; count < loadAmount; count++)
278 if(!sendBurstMessage(true /* anyway */)) break;
283 int OriginHost::startBurst(int initialLoad) throw() {
284 if(initialLoad < 1) {
285 std::string msg = "No initial load is specified. Ignoring burst start";
286 std::cout << msg << std::endl;
287 LOGWARNING(anna::Logger::warning(msg, ANNA_FILE_LOCATION));
291 a_burstActive = true;
293 a_burstDeliveryIt = a_burstMessages.begin();
294 return (pushBurst(initialLoad));
297 bool OriginHost::sendBurstMessage(bool anyway) throw() {
298 if(!anyway && !burstActive()) return false;
300 if(a_burstPopCounter > 0) {
301 if(burstLogEnabled()) writeBurstLogFile("x");
307 if(a_burstDeliveryIt == a_burstMessages.end()) {
308 a_burstDeliveryIt = a_burstMessages.begin();
314 if(burstLogEnabled()) writeBurstLogFile(anna::functions::asString("\nCompleted burst cycle. Starting again (repeat mode) on cycle %d.\n", a_burstCycle));
316 if(burstLogEnabled()) writeBurstLogFile("\nCompleted burst cycle. Burst finished (repeat mode disabled).\n");
324 anna::diameter::comm::Message *msg = (*a_burstDeliveryIt).second;
325 int order = (*a_burstDeliveryIt).first + 1;
329 bool result = a_entity->send(msg);
331 if(burstLogEnabled()) {
332 if(a_burstMessages.size() >= 100)
333 dot = (order % (a_burstMessages.size() / 100));
336 writeBurstLogFile(".");
338 writeBurstLogFile(anna::functions::asString(" %d", order));
339 int otaReqs = a_entity->getOTARequests();
341 if(result && (otaReqs != a_otaRequest)) {
342 // false if was a sending after an answer received (no OTA change in this case)
343 // true after push and pop operations
344 a_otaRequest = otaReqs;
345 writeBurstLogFile(anna::functions::asString("[OTA %d]", a_otaRequest));
352 anna::diameter::comm::Server *usedServer = a_entity->getLastUsedResource();
353 anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
354 std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
355 writeLogFile(msg->getBody(), (result ? "sent2e" : "send2eError"), detail); // el del nodo de trabajo
361 std::string OriginHost::lookBurst(int order) const throw() {
363 if (order == -1) order = a_burstDeliveryIt->first;
365 std::string result = "No message found for order provided (";
366 result += anna::functions::asString(order);
368 std::map<int, anna::diameter::comm::Message*>::const_iterator it = a_burstMessages.find(order - 1);
370 if(it != a_burstMessages.end()) {
371 anna::diameter::codec::Message codecMsg;
372 try { codecMsg.decode((*it).second->getBody()); result = codecMsg.asXMLString(); } catch(anna::RuntimeException &ex) { ex.trace(); }
378 std::string OriginHost::gotoBurst(int order) throw() {
379 std::string result = "Position not found for order provided (";
380 std::map<int, anna::diameter::comm::Message*>::iterator it = a_burstMessages.find(order - 1);
382 if(it != a_burstMessages.end()) {
383 a_burstDeliveryIt = it;
384 result = "Position updated for order provided (";
387 result += anna::functions::asString(order);
392 anna::xml::Node* OriginHost::asXML(anna::xml::Node* parent) const
394 anna::xml::Node* result = parent->createChild("OriginHost");
396 result->createAttribute("originHost", getName());
397 result->createAttribute("ApplicationId", a_applicationId);
398 result->createAttribute("originRealm", a_commEngine->getOriginRealmName());
399 result->createAttribute("LogFile", a_logFile);
400 result->createAttribute("SplitLog", a_splitLog ? "yes" : "no");
401 result->createAttribute("DetailedLog", a_detailedLog ? "yes" : "no");
402 result->createAttribute("DumpLog", a_dumpLog ? "yes" : "no");
403 result->createAttribute("BurstLogFile", a_burstLogFile);
404 result->createAttribute("RequestRetransmissions", a_requestRetransmissions);
406 anna::xml::Node* commEngine = result->createChild("CommEngine");
407 a_commEngine->asXML(commEngine);
412 std::string OriginHost::asXMLString() const throw() {
413 anna::xml::Node root("root");
414 return anna::xml::Compiler().apply(asXML(&root));