ANNA Suite  2020b
Multipurpose development suite for Telco applications
Session.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_Session_hpp
10 #define anna_diameter_comm_Session_hpp
11 
12 
13 // STL
14 #include <string>
15 
19 #include <anna/timex/Timer.hpp>
20 
25 
26 
27 namespace anna {
28 class DataBlock;
29 namespace timex {
30 class Engine;
31 }
32 }
33 
34 
35 namespace anna {
36 
37 namespace diameter {
38 
39 namespace stack {
40 class Engine;
41 }
42 
43 namespace comm {
44 
45 class Timer;
46 class Engine;
47 class Response;
48 class OriginHost;
49 
50 
51 
55 class Session : public anna::timex::Timer {
56 
57 public:
58 
59  Session(const char *className, const char *timerName);
60  //virtual ~Session();
61 
66 
70  static const int DefaultPort;
71 
72 
73 
74 
79  struct State {
80  enum _v {
81  /* client + server */ Closed,
82  /* client */ WaitingBind,
83  /* client + server */ Bound,
84  /* client */ Failover,
85  /* server */ Suspect,
87 // .......
88 
89 
90  // Cierre de iniciativa local:
91  // 1. Envio DPR al PCRF y me pongo en estado 'WaitingDPA'. En este estado no habr� keep-alive DWR/DWA.
92  // 2. No dejo pasar nuevas peticiones (BLOCK-SEND).
93  // 3. Cierro al recibir el DPA.
94  // 4. Si expira el DPA, tambien cierro.
97  // Cierre de iniciativa remota:
98  // 1. Recibo DPR del PCRF y me pongo en estado 'Disconnecting'. En este estado no habr� keep-alive DWR/DWA.
99  // 2. No dejo pasar nuevas peticiones (BLOCK-SEND).
100  // 3. Espero cursar las peticiones pendientes (a m�s tardar, ser� una expiracion Tx desde la recepcion del DPR).
101  // 4. Envio DPA y activo un temporizador de cierre local (2*Tx) como proteccion (por si el servidor no cierra).
104  // Cierre abrupto con/sin espera de pendings:
105  Closing
106  };
107  };
108 
113  State::_v getState() const { return a_state; }
114 
118  struct OnDisconnect { enum _v { IgnorePendings, WaitPendings /* graceful unbind */ }; };
119 
124  void setOnDisconnect(const OnDisconnect::_v onDisconnect) { a_onDisconnect = onDisconnect; }
125 
130  OnDisconnect::_v getOnDisconnect() const { return a_onDisconnect; }
131 
132 
133 
138  virtual const std::string& getAddress() const = 0;
139 
144  virtual int getPort() const = 0;
145 
150  int getSocketId() const { return a_socketId; }
151 
157  const HopByHop & getNextHopByHop() const { return a_nextHopByHop; }
158 
163  const EndToEnd & getNextEndToEnd() const { return a_nextEndToEnd; }
164 
165 
173  void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) { a_timeouts [v] = millisecond; }
174 
179  anna::Millisecond getClassCodeTimeout(const ClassCode::_v v) const { return a_timeouts [v]; }
180 
181 
186  bool isBound() const { return a_state == State::Bound; }
187 
188 
189 // Envia el mensaje recibido como parametro al servidor con el que estamos conectados mediante esta
190 // sesion diameter. Dicho mensaje puede ser una peticion o una respuesta (no temporizada).
191 //
192 // En caso de enviar una peticion se activara automaticamente un temporizador. Si este llegara a caducar
193 // se cancelara la busqueda y se invocara al metodo Session::eventResponse indicado que se ha producido
194 // un error de temporizaci�n. La duracion del temporizador sera la establecida por
195 // diameter::comm::TimerManager::setTimeout o el valor defecto.
196 //
197 // \param message Mensaje a enviar al servidor diameter con el que estamos conectados.
198 // @return Diameter response reference asociated to a request. NULL if answer sent.
199 // \warning Solo se podra hacer uso de este metodo cuando el metodo #isBound devuelva \em true.
200  virtual const Response* send(const Message* message) noexcept(false) = 0;
201  const Response* send(const Message& message) noexcept(false) { return send(&message); }
202 
203 // Desconecta del extremo remoto
204 // Se notifica la terminaci�n de cada una de las peticiones pendientes invocando al m�todo Session::eventResponse
205 // \warning Despu�s de invocar a este m�todo habr�a que volver a iniciar una sesion.
206  virtual bool unbind(bool forceDisconnect /* se usa en timer, para el actionTimer del tipo SessionUnbind, etc. */ = false) noexcept(false) = 0;
207  // returns true if done at call time (no pendings or ignore pendings, except Disconnecting state by mean DPR/DPA)
208 
209 
215  const anna::Millisecond & getLastIncomingActivityTime() const { return a_lastIncomingActivityTime; }
216 
222  const anna::Millisecond & getLastOutgoingActivityTime() const { return a_lastOutgoingActivityTime; }
223 
229  int getOTARequests() const { return a_responses.size(); }
230 
231 
237  bool idle() const { return (getOTARequests() == 0); }
238 
239 
251  void notifyOrphansOnExpiration(bool defer = true) { a_notifyOrphansOnExpiration = defer; }
252 
257  virtual std::string asString() const ;
258 
259 
265  virtual anna::xml::Node* asXML(anna::xml::Node* parent) const ;
266 
267 protected:
268 
269  // Internal, traces, etc.
270  const char *a_className;
271 
272  // Main session attributes
273  int a_socketId; // multiple connection functionality
277 
278  // Sequencing
281  virtual void initialize() ;
282  void initializeSequences() ; // debe invocarse despues de haber asignado el a_parent
283  void generateNextSequences() { a_nextHopByHop++; a_nextEndToEnd++; }
284 
285  // Context Responses
286  struct SortById {
287  static HopByHop value(const Response*) ;
288  };
292  response_container a_responses;
294 
295  void response_add(Response* response) ;
296  void response_erase(Response* response) ;
297  Response* response_find(const HopByHop hopByHop) noexcept(false);
298 
299  response_iterator response_begin() { return a_responses.begin(); }
300  response_iterator response_end() { return a_responses.end(); }
301  static Response* response(response_iterator ii) { return response_container::data(ii); }
302 
303  const_response_iterator response_begin() const { return a_responses.begin(); }
304  const_response_iterator response_end() const { return a_responses.end(); }
305  static const Response* response(const_response_iterator ii) { return response_container::data(ii); }
306 
307  // Activity
309  anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over the session
310  anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over the session
311  virtual void updateIncomingActivityTime() ;
312  virtual void updateOutgoingActivityTime() ;
313 
314  // Self-timer expiration handler
315  virtual void expire(anna::timex::Engine *timeController) noexcept(false) {;}
316 
317  // Timming:
318  anna::Millisecond a_timeouts [ClassCode::Max];
319 
320  // Handlers:
326  virtual void eventPeerShutdown() = 0;
327 
333  virtual void eventRequestRetransmission(Message *request) = 0;
334 
341  virtual void eventResponse(const Response& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
342 
349  virtual void eventRequest(const anna::DataBlock& request, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
350  //void eventRequest(const Message& request) noexcept(false);
351 
352 
359  virtual void eventUnknownResponse(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
360 
367  virtual void eventDPA(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
368 
369 
370 
374  virtual void receive(const anna::comm::Message& message) noexcept(false) = 0;
375 //PROTOCOL ERRORS
376 //The errors at the protocol level are reported in response messages that contain the �E� bit and the error code in the AVP result-Code (various errors having been produced only the first one of them is reported). Examples of these errors are:
377 //An unrecognized AVP with the �M� bit is received.
378 //An AVP is received with an unrecognized value (in the AVP failed-AVP indicates the attribute that the error caused).
379 //An mandatory AVP is not received.
380 //Length of operation incorrect.
381 //Length of AVP incorrect.
382 //Coding of some AVP incorrect.
383 //Erroneous inclusion of parameters.
384 
385 
386 
387 
388 
389  virtual void finalize() ; // invoked from ClientSessionReceiver::eventBreakConnection()
390 
391 
392  virtual void expireResponse(Response*) ;
393  void sendDPA(const Engine *commEngine, const anna::DataBlock &dprDataBlock) noexcept(false);
394  void activateActionTimer(const anna::diameter::comm::Timer::Type::_v type) ;
395  void cancelActionTimer() ;
396  void activateTimer() ; // Session timer
397  void cancelTimer() ; // Session timer
398  virtual void timerStopped() {;}
399  virtual void timerStarted() {;}
400 
401 
402  virtual void setState(State::_v state) ;
403 
404  //anna::diameter::comm::Timer *getActionTimer() const { return (a_actionTimer); }
405 
406 
407  // helpers
408  static const char* asText(const State::_v) ;
409  static const char* asText(const OnDisconnect::_v) ;
410 
411 
413  friend class Response;
414 };
415 
416 }
417 }
418 }
419 
420 #endif
421 
container::iterator iterator
Definition: SortedVector.hpp:33
int a_socketId
Definition: Session.hpp:273
_v
Definition: Session.hpp:80
Definition: Millisecond.hpp:24
response_container::const_iterator const_response_iterator
Definition: Session.hpp:291
const Response * send(const Message &message) noexcept(false)
Definition: Session.hpp:201
response_iterator response_end()
Definition: Session.hpp:300
unsigned int EndToEnd
Definition: defines.hpp:70
void generateNextSequences()
Definition: Session.hpp:283
response_iterator response_begin()
Definition: Session.hpp:299
anna::diameter::comm::Timer * a_actionTimer
Definition: Session.hpp:276
Definition: Engine.hpp:102
unsigned int HopByHop
Definition: defines.hpp:68
Definition: Node.hpp:56
Definition: Timer.hpp:30
EndToEnd a_nextEndToEnd
Definition: Session.hpp:280
const anna::Millisecond & getLastOutgoingActivityTime() const
Definition: Session.hpp:222
bool idle() const
Definition: Session.hpp:237
anna::timex::Engine * a_timeController
Definition: Session.hpp:308
_v
Definition: Timer.hpp:33
virtual void expire(anna::timex::Engine *timeController) noexcept(false)
Definition: Session.hpp:315
Definition: OriginHost.hpp:41
bool a_notifyOrphansOnExpiration
Definition: Session.hpp:293
OnDisconnect::_v a_onDisconnect
Definition: Session.hpp:275
static const anna::Millisecond DefaultTimeout
Definition: Session.hpp:65
State::_v a_state
Definition: Session.hpp:274
anna::SortedVector< Response, SortById, HopByHop > response_container
Definition: Session.hpp:289
State::_v getState() const
Definition: Session.hpp:113
_v
Definition: ClassCode.hpp:28
response_container a_responses
Definition: Session.hpp:292
const EndToEnd & getNextEndToEnd() const
Definition: Session.hpp:163
Definition: Session.hpp:286
Definition: Message.hpp:26
void setOnDisconnect(const OnDisconnect::_v onDisconnect)
Definition: Session.hpp:124
Definition: Response.hpp:44
Definition: Message.hpp:45
HopByHop a_nextHopByHop
Definition: Session.hpp:279
Definition: Session.hpp:79
int getSocketId() const
Definition: Session.hpp:150
anna::Millisecond getClassCodeTimeout(const ClassCode::_v v) const
Definition: Session.hpp:179
Definition: Timer.hpp:25
void notifyOrphansOnExpiration(bool defer=true)
Definition: Session.hpp:251
container::const_iterator const_iterator
Definition: SortedVector.hpp:34
int getOTARequests() const
Definition: Session.hpp:229
void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond &millisecond)
Definition: Session.hpp:173
bool isBound() const
Definition: Session.hpp:186
response_container::iterator response_iterator
Definition: Session.hpp:290
anna::Millisecond a_lastIncomingActivityTime
Definition: Session.hpp:309
Definition: app.hpp:12
static const Response * response(const_response_iterator ii)
Definition: Session.hpp:305
virtual void timerStarted()
Definition: Session.hpp:399
OnDisconnect::_v getOnDisconnect() const
Definition: Session.hpp:130
static Response * response(response_iterator ii)
Definition: Session.hpp:301
virtual void timerStopped()
Definition: Session.hpp:398
Definition: Engine.hpp:62
anna::Millisecond a_lastOutgoingActivityTime
Definition: Session.hpp:310
const char * a_className
Definition: Session.hpp:270
const HopByHop & getNextHopByHop() const
Definition: Session.hpp:157
Definition: Session.hpp:55
const anna::Millisecond & getLastIncomingActivityTime() const
Definition: Session.hpp:215
Definition: DataBlock.hpp:24
const_response_iterator response_end() const
Definition: Session.hpp:304
static const int DefaultPort
Definition: Session.hpp:70
const_response_iterator response_begin() const
Definition: Session.hpp:303