Remove global variables. Factory for comm/codec messages in application class (Launcher)
[anna.git] / example / diameter / launcher / Launcher.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef example_diameter_launcher_Launcher_hpp
10 #define example_diameter_launcher_Launcher_hpp
11
12 // Standard
13 #include <string>
14 #include <map>
15 #include <fstream>
16
17 // Project
18 #include <anna/core/core.hpp>
19 #include <anna/comm/comm.hpp>
20 #include <anna/diameter.comm/Entity.hpp>
21 #include <anna/time/Date.hpp>
22 #include <anna/timex/Engine.hpp>
23
24 namespace anna {
25   namespace diameter {
26     namespace comm {
27       //class Entity;
28       //class Response;
29       class LocalServer;
30     }
31   }
32 }
33
34 // Process
35 #include "MyCommunicator.hpp"
36 #include "MyCounterRecorder.hpp"
37 #include "MyDiameterEngine.hpp"
38
39
40 class Launcher : public anna::comm::Application {
41
42   MyCommunicator *a_communicator;
43   MyDiameterEngine *a_myDiameterEngine;
44   MyDiameterEntity *a_entity;
45   std::string a_logFile, a_burstLogFile;
46   std::ofstream a_burstLogStream;
47   bool a_splitLog, a_detailedLog, a_dumpLog;
48   anna::time::Date a_start_time;
49   anna::timex::Engine* a_timeEngine;
50   MyCounterRecorder *a_counterRecorder;
51   MyCounterRecorderClock *a_counterRecorderClock;
52   std::string a_cerPathfile;
53   std::string a_dwrPathfile;
54
55   // Burst feature
56   int a_burstCycle;
57   bool a_burstRepeat;
58   bool a_burstActive;
59   std::map < int /* dummy, p.e. used for order number */, anna::diameter::comm::Message* > a_burstMessages;
60   int a_burstLoadIndx;
61   std::map<int, anna::diameter::comm::Message*>::const_iterator a_burstDeliveryIt;
62   int a_otaRequest;
63   int a_burstPopCounter;
64
65   // comm Messages factory:
66   anna::Recycler<anna::diameter::comm::Message> a_commMessages;
67   anna::Recycler<anna::diameter::codec::Message> a_codecMessages;
68
69   anna::comm::ServerSocket* a_httpServerSocket; // HTTP
70   MyLocalServer* a_diameterLocalServer; // DIAMETER
71   void checkTimeMeasure(const char * commandLineParameter, bool optional = true) throw(anna::RuntimeException);
72   void initialize() throw(anna::RuntimeException); // HTTP
73   void run() throw(anna::RuntimeException);
74
75 public:
76   Launcher();
77
78   MyCommunicator *getCommunicator() throw() { return a_communicator; }
79   MyDiameterEngine* getMyDiameterEngine() const throw() { return (a_myDiameterEngine); }
80   void baseProtocolSetupAsClient(void) throw(anna::RuntimeException);
81   MyDiameterEntity *getEntity() throw() { return a_entity; }
82   MyLocalServer* getDiameterLocalServer() throw() { return a_diameterLocalServer; }
83   void eventOperation(const std::string &, std::string &) throw(anna::RuntimeException);
84   bool logEnabled() const throw() { return (((a_logFile == "") || (a_logFile == "null")) ? false : true); }
85   void writeLogFile(const anna::DataBlock & db, const std::string &logExtension, const std::string &detail) const throw();
86   void writeLogFile(const anna::diameter::codec::Message & decodedMessage, const std::string &logExtension, const std::string &detail) const throw();
87   void writeBurstLogFile(const std::string &buffer) throw();
88   bool burstLogEnabled() const throw() { return (((a_burstLogFile == "") || (a_burstLogFile == "null")) ? false : true); }
89   void startDiameterServer(int) throw(anna::RuntimeException);
90   void forceCountersRecord() throw(anna::RuntimeException) { if (a_counterRecorderClock) a_counterRecorderClock->tick(); }
91
92   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
93   void resetStatistics() throw() { a_myDiameterEngine->resetStatistics(); }
94   void resetCounters() throw();
95   void signalUSR2() throw(anna::RuntimeException);
96   std::string help() const throw();
97
98   // Messages factory:
99   anna::diameter::comm::Message *createCommMessage() throw(anna::RuntimeException);
100   void releaseCommMessage(anna::diameter::comm::Message*) throw();
101   anna::diameter::codec::Message *createCodecMessage() throw(anna::RuntimeException);
102   void releaseCodecMessage(anna::diameter::codec::Message*) throw();
103
104   // helpers
105   bool getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const throw();
106
107   // Burst feature
108   int clearBurst() throw(); // returns removed
109   int loadBurstMessage(const anna::DataBlock & db) throw(anna::RuntimeException);
110   void repeatBurst(bool repeat) throw() { a_burstRepeat = repeat; }
111   int startBurst(int initialLoad) throw();  // return processed on start, or -1 if burst list is empty, -2 if invalid initial load (0 or negative)
112   int pushBurst(int loadAmount) throw(); // returns pushed (perhaps is less than provided because of no repeat mode and burst list exhausted), or -1 if burst list is empty, -2 if invalid load (0 or negative)
113   int sendBurst(int loadAmount) throw(); // returns sent (burst always cycled using send), returns -1 if burst list is empty, -2 if invalid load (0 or negative)
114   int popBurst(int releaseAmount) throw(); // returns popped (perhaps is less than provided because of OTA request), or -1 if burst stopped
115   int stopBurst() throw(); // returns remaining on cycle, or -1 if burst already stopped
116   bool burstActive() const throw() { return a_burstActive; }
117   bool sendBurstMessage(bool anyway = false) throw();
118   std::string lookBurst(int order) const throw();
119   std::string gotoBurst(int order) throw();
120 };
121
122 #endif