Fix local server for multiple applications
[anna.git] / include / anna / diameter.comm / OriginHost.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 anna_diameter_comm_OriginHost_hpp
10 #define anna_diameter_comm_OriginHost_hpp
11
12 // Standard
13 #include <string>
14 #include <fstream>
15
16 // Project
17 #include <anna/core/util/Millisecond.hpp>
18 #include <anna/core/util/Recycler.hpp>
19 #include <anna/core/DataBlock.hpp>
20 #include <anna/diameter/codec/Message.hpp>
21 #include <anna/diameter.comm/Message.hpp>
22 #include <anna/diameter/codec/MessagesDeque.hpp>
23
24
25 namespace anna {
26   namespace diameter {
27     namespace codec {
28       class Engine;
29     }
30     namespace comm {
31       class Engine;
32       class Entity;
33       class LocalServer;
34
35 /**
36  * OriginHost with single access point for client and server
37  * That is to say: only one communication engine, then:
38  * - For client: one CER, then one application Id
39  * - For server: no app id control, and multiple serverSession overwrites tsw server session (TestManager), no way to distinguish with single port ...
40  */
41 class OriginHost {
42
43   anna::diameter::comm::Engine *a_commEngine;
44   anna::diameter::comm::Entity *a_entity; // we could get it finding the unique instante within comm engine, but it is more comfortable assign here.
45   anna::diameter::comm::LocalServer* a_diameterServer; // idem
46   anna::diameter::codec::Engine *a_codecEngine;
47
48   // resources
49   int a_requestRetransmissions;
50   anna::Recycler<anna::diameter::comm::Message> a_commMessages;
51
52   // main
53   unsigned int a_applicationId;
54
55   // Logs & burst
56   std::string a_logFile;
57   bool a_splitLog, a_detailedLog, a_dumpLog;
58   std::string a_burstLogFile;
59   std::ofstream a_burstLogStream;
60   int a_burstCycle;
61   bool a_burstRepeat;
62   bool a_burstActive;
63   std::map < int /* dummy, p.e. used for order number */, anna::diameter::comm::Message* > a_burstMessages;
64   int a_burstLoadIndx;
65   std::map<int, anna::diameter::comm::Message*>::const_iterator a_burstDeliveryIt;
66   int a_otaRequest;
67   int a_burstPopCounter;
68
69   anna::diameter::codec::MessagesDeque a_reactingAnswers;
70
71 public:
72   OriginHost(anna::diameter::comm::Engine* commEngine, unsigned int applicationId);
73   ~OriginHost() {;}
74
75   // OriginHost name:
76   const std::string &getName() const ;
77
78   // Core resources:
79   anna::diameter::comm::Engine* getCommEngine() const { return a_commEngine; }
80   unsigned int getApplicationId() const { return a_applicationId; }
81   anna::diameter::codec::Engine *getCodecEngine() const { return a_codecEngine; }
82
83   void createEntity(const std::string &entityRepresentation, const anna::Millisecond &bindTimeout, const anna::Millisecond &applicationTimeout) noexcept(false);
84   anna::diameter::comm::Entity *getEntity() const { return a_entity; }
85
86   void createDiameterServer(const std::string &serverRepresentation, int sessions, const anna::Millisecond &inactivityTimeout, const anna::Millisecond &applicationTimeout, const std::string &ceaPathfile) noexcept(false);
87   anna::diameter::comm::LocalServer* getDiameterServer() { return a_diameterServer; }
88   void setRequestRetransmissions(int r) { if (r >= 0) a_requestRetransmissions = r; }
89
90   // Messages factory:
91   anna::diameter::comm::Message *createCommMessage() noexcept(false);
92   void releaseCommMessage(anna::diameter::comm::Message*) ;
93
94   // Traffic logs & burst:
95   void setLogs(const std::string &log, bool splitLog, bool detailedLog, bool dumpLog, const std::string &burstLog) {
96     a_logFile = log;
97     a_splitLog = splitLog;
98     a_detailedLog = detailedLog;
99     a_dumpLog = dumpLog;
100     a_burstLogFile = burstLog;
101   }
102
103   bool logEnabled() const { return (((a_logFile == "") || (a_logFile == "null")) ? false : true); }
104   void writeLogFile(const anna::DataBlock & db, const std::string &logExtension, const std::string &detail) const ;
105   void writeLogFile(const anna::diameter::codec::Message & decodedMessage, const std::string &logExtension, const std::string &detail) const ;
106   void writeBurstLogFile(const std::string &buffer) ;
107   bool burstLogEnabled() const { return (((a_burstLogFile == "") || (a_burstLogFile == "null")) ? false : true); }
108   int clearBurst() ; // returns removed
109   int loadBurstMessage(const anna::DataBlock & db) noexcept(false);
110   void repeatBurst(bool repeat) { a_burstRepeat = repeat; }
111   int startBurst(int initialLoad) ;  // return processed on start, or -1 if burst list is empty, -2 if invalid initial load (0 or negative)
112   int pushBurst(int loadAmount) ; // 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) ; // 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) ; // returns popped (perhaps is less than provided because of OTA request), or -1 if burst stopped
115   int stopBurst() ; // returns remaining on cycle, or -1 if burst already stopped
116   bool burstActive() const { return a_burstActive; }
117   bool sendBurstMessage(bool anyway = false) ;
118   std::string lookBurst(int order = -1) const ;
119   std::string gotoBurst(int order) ;
120
121   anna::xml::Node* asXML(anna::xml::Node* parent) const ;
122   std::string asXMLString() const ;
123
124   anna::diameter::codec::MessagesDeque *getReactingAnswers() { return (anna::diameter::codec::MessagesDeque*)&a_reactingAnswers; }
125 };
126
127 }
128 }
129 }
130
131 #endif