ANNA Suite  2020b
Multipurpose development suite for Telco applications
ClientSession.hpp
Go to the documentation of this file.
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_ClientSession_hpp
10 #define anna_diameter_comm_ClientSession_hpp
11 
12 
13 // STL
14 #include <string>
15 
19 
25 
26 
27 namespace anna {
28 class DataBlock;
29 namespace timex {
30 class Engine;
31 }
32 
33 namespace comm {
34 class Server;
35 }
36 }
37 
38 
39 namespace anna {
40 
41 namespace diameter {
42 
43 namespace comm {
44 
45 class Server;
46 
47 
51 class ClientSession : public Session {
52 
53  // Helper:
54  static std::string getKey(const std::string & addr, int port, int socketId) {
55  return (anna::functions::asString("%s:%d|%d", addr.c_str(), port, socketId));
56  }
57 
58  bool a_hidden; // hide resource for restricted delivery over servers/entities
59 
60 
61 public:
62 
63  ClientSession();
64 
65 
66  /* virtual */void initialize() ;
67 
72 
76  std::string getKey() const { return ClientSession::getKey(getAddress(), getPort(), getSocketId()); }
77 
82  /* virtual */const std::string& getAddress() const ;
83 
88  /* virtual */int getPort() const ;
89 
94  const Server *getParent() const { return a_parent; }
95 
100  anna::comm::Server * getServer() { return a_server; }
101 
105  void disable() { a_server->disable(); }
106 
111  void setAutoRecovery(bool autoRecovery = true) { a_autoRecovery = autoRecovery; a_server->setAutoRecovery(autoRecovery); }
112 
118  bool getAutoRecovery() const { return a_autoRecovery; }
119 
127  void setMaxConnectionDelay(const anna::Millisecond & maxConnectionDelay) { a_server->setMaxConnectionDelay(maxConnectionDelay); }
128 
134  const anna::Millisecond & getMaxConnectionDelay() { return a_server->getMaxConnectionDelay(); }
135 
136  // Internal
137  void bind() noexcept(false);
138 
139  /* virtual */const Response* send(const Message* message) noexcept(false);
140  /* virtual */bool unbind(bool forceDisconnect /* se usa en timer, para el actionTimer del tipo SessionUnbind, etc. */ = false) noexcept(false); // returns true if done at call time (no pendings or ignore pendings, except Disconnecting state by mean DPR/DPA)
141 
145  void hide() { a_hidden = true; }
146 
150  void show() { a_hidden = false; }
151 
155  bool hidden() const { return a_hidden; }
156 
160  bool shown() const { return !a_hidden; }
161 
162 
167  /* virtual */std::string asString() const ;
168 
169 
175  /* virtual */anna::xml::Node* asXML(anna::xml::Node* parent) const ;
176 
177 
178 private:
179 
180  // Receiver factory
181  ReceiverFactoryImpl<ClientSession, ClientSessionReceiver> a_receiverFactory;
182 
183  // Parent information
184  Server *a_parent;
185 
186  // ClientSession messages:
187  Engine *a_engine; // it is unique for a client session (not in server session, which have one per origin host)
188  Message a_cer;
189  Message a_dwr;
190 
191  // Connectivity
192  bool a_autoRecovery;
193 
194  // Server
195  anna::comm::Server *a_server;
196 
197  // Watchdog control:
198  struct WatchdogState {
199  enum _v {
200  TimerStopped, // Until CEA (bound state), timer is stopped
201  WaitingTimerExpiration, // DWA has been received and we wait for next expiration to send DWR
202  WaitingDWA // DWR has been sent, but DWA hasn't been received yet
203  };
204  };
205  WatchdogState::_v a_watchdogState;
206  void setWatchdogState(WatchdogState::_v wState) ;
207 
208  /* virtual */void expire(anna::timex::Engine *timeController) noexcept(false);
209  void setWatchdogPeriod(const anna::Millisecond & watchdogPeriod) ;
210 
211  /*virtual*/ void timerStopped() ;
212  /*virtual*/ void timerStarted() ;
213 
214 
215  // Activity:
216  /* virtual */void updateIncomingActivityTime() ;
217  /* virtual */void updateOutgoingActivityTime() ;
218  void countSendings(const diameter::CommandId & cid, unsigned int aid, bool ok) ;
219 
220  // Handlers:
226  void eventPeerShutdown() ;
227 
233  void eventRequestRetransmission(Message *request) ;
234 
241  void eventResponse(const Response& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
242 
249  void eventRequest(const anna::DataBlock& request, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
250  //void eventRequest(const Message& request) noexcept(false);
251 
258  void eventUnknownResponse(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
259 
266  void eventDPA(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
267 
268 
269 
273  /* virtual */void receive(const anna::comm::Message& message) noexcept(false);
274  /* virtual */void finalize() ;
275  void recover() ;
276 
277  /* virtual */void expireResponse(Response*) ;
278  /* virtual */void setState(State::_v state) ;
279 
280  void sendDWAToServer(const anna::DataBlock& dwrDB) noexcept(false); // non-usual behaviour, but DWR could be received from server
281 
282  // helpers
283  static const char* asText(const WatchdogState::_v) ;
284 
285 
286  friend class Server;
287  friend class Engine;
288  friend class ClientSessionReceiver;
289 };
290 
291 }
292 }
293 }
294 
295 #endif
296 
std::string getKey() const
Definition: ClientSession.hpp:76
Definition: Millisecond.hpp:24
const anna::Millisecond & getMaxConnectionDelay()
Definition: ClientSession.hpp:134
Definition: Server.hpp:53
Definition: Engine.hpp:102
static const anna::Millisecond DefaultWatchdogPeriod
Definition: ClientSession.hpp:71
Definition: Node.hpp:56
Definition: Server.hpp:54
Definition: OriginHost.hpp:41
static std::string asString(const int number)
bool getAutoRecovery() const
Definition: ClientSession.hpp:118
bool shown() const
Definition: ClientSession.hpp:160
void disable()
Definition: ClientSession.hpp:105
Definition: Message.hpp:26
const Server * getParent() const
Definition: ClientSession.hpp:94
Definition: Response.hpp:44
Definition: Message.hpp:45
Definition: ClientSession.hpp:51
Definition: app.hpp:12
bool hidden() const
Definition: ClientSession.hpp:155
anna::comm::Server * getServer()
Definition: ClientSession.hpp:100
void setAutoRecovery(bool autoRecovery=true)
Definition: ClientSession.hpp:111
void hide()
Definition: ClientSession.hpp:145
Definition: Engine.hpp:62
std::pair< U24, bool > CommandId
Definition: defines.hpp:32
Definition: ClientSessionReceiver.hpp:35
Definition: Session.hpp:55
void setMaxConnectionDelay(const anna::Millisecond &maxConnectionDelay)
Definition: ClientSession.hpp:127
Definition: DataBlock.hpp:24
void show()
Definition: ClientSession.hpp:150