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_ldap_Session_hpp
10 #define anna_ldap_Session_hpp
11 
12 #include <string>
13 
15 
16 #include <anna/comm/Handler.hpp>
17 
18 #include <anna/ldap/defines.hpp>
19 #include <anna/ldap/ClassCode.hpp>
20 
21 namespace anna {
22 
23 namespace xml {
24 class Node;
25 }
26 
27 namespace ldap {
28 
29 class Request;
30 class Response;
31 class Timer;
32 class Engine;
33 class ResultCode;
34 
38 class Session : public comm::Handler {
39 public:
44  struct State {
45  enum _v {
48  Bound
49  };
50  };
51 
56  struct Option {
61  struct Defer {
62  enum _v {
66  Always
67  };
68  };
74  struct Referral { enum _v { Off, On }; };
75  };
76 
81 
86  const std::string& getURL() const { return a_url; }
87 
92  const std::string& getUser() const { return a_user; }
93 
98  const std::string& getPassword() const { return a_password; }
99 
104  int getCategory() const { return a_category; }
105 
109  int getExternalID() const { return a_externalID; }
110 
115  void* getLDAP() { return a_ldap; }
116 
122  int getDangerousFileDescriptor() const noexcept(false);
123 
128  Option::Defer::_v getDefer() const { return a_defer; }
129 
136  Option::Referral::_v getReferral() const { return a_referral; }
137 
142  State::_v getState() const { return a_state; }
143 
148  const Millisecond &getTimeout(const ClassCode::_v v) const { return a_timeouts [v]; }
149 
156  Millisecond result(1000 * a_networkTimeout.tv_sec);
157  return Millisecond(result + a_networkTimeout.tv_usec / 1000);
158  }
159 
164  bool hasNetworkTimeout() const { return a_networkTimeout.tv_sec != -1; }
165 
172  void bind() noexcept(false);
173 
178  bool isBound() const { return a_state == State::Bound; }
179 
184  void setOption(const Option::Defer::_v defer) { a_defer = defer; }
185 
190  void setOption(const Option::Referral::_v referral) { a_referral = referral; }
191 
201  void setNetworkTimeout(const Millisecond &timeout) { a_networkTimeout.tv_sec = timeout / 1000; a_networkTimeout.tv_usec = (timeout % 1000) * 1000; }
202 
209  void clearNetworkTimeout() { a_networkTimeout.tv_sec = -1; a_networkTimeout.tv_usec = 0; }
210 
219  void setTimeout(const ClassCode::_v v, const Millisecond &millisecond) { a_timeouts [v] = millisecond; }
220 
234  const Response* send(const Request* request) noexcept(false);
235 
249  const Response* send(const Request& request) noexcept(false) { return send(&request); }
250 
258  void unbind() noexcept(false);
259 
264  std::string asString() const ;
265 
271  xml::Node* asXML(xml::Node* parent) const ;
272 
273 protected:
278  Session();
279 
287  virtual void eventServerShutdown() {;}
288 
294  virtual void eventResponse(const Response& response) noexcept(false) = 0;
295 
302  virtual void eventResponseError(const ResultCode& resultCode, const bool disconnect) {;}
303 
310  virtual void eventIntermediateResponseError(const Response& response) {;}
311 
312 private:
313  struct SortById {
314  static IdMessage value(const Response*) ;
315  };
317  typedef response_container::iterator response_iterator;
318  typedef response_container::const_iterator const_response_iterator;
319 
320  typedef void* HandleLDAP;
321  typedef void* HandleMessage;
322 
323  int a_category;
324  HandleLDAP a_ldap;
325  State::_v a_state;
326  std::string a_url;
327  std::string a_user;
328  std::string a_password;
329  Option::Defer::_v a_defer;
330  Option::Referral::_v a_referral;
331  response_container a_responses;
332  int a_externalID;
333  Millisecond a_timeouts [ClassCode::Max];
334  struct timeval a_networkTimeout;
335 
336  /* Dependiendo de cómo se cree la sesión este miembro puede ser una copia de a_user
337  * o el identificador numérico que pasaron como parámetro al crear la sesión
338  */
339  std::string a_keymap;
340 
341  void apply() noexcept(false);
342  void receiveBind(const IdMessage, HandleMessage) noexcept(false);
343  void receiveEntry(const IdMessage, HandleMessage) noexcept(false);
344  void receiveReference(const IdMessage, HandleMessage) noexcept(false);
345  void receiveResult(const IdMessage, HandleMessage) noexcept(false);
346  void expireResponse(Response*) ;
347 
348  void finalize() ;
349 
350  response_iterator response_begin() { return a_responses.begin(); }
351  response_iterator response_end() { return a_responses.end(); }
352  void response_add(Response* response) ;
353  void response_erase(Response* response) ;
354  Response* response_find(const IdMessage idMessage) noexcept(false);
355 
356  static Response* response(response_iterator ii) { return response_container::data(ii); }
357 
358  const_response_iterator response_begin() const { return a_responses.begin(); }
359  const_response_iterator response_end() const { return a_responses.end(); }
360  static const Response* response(const_response_iterator ii) { return response_container::data(ii); }
361 
362  static const char* asText(const State::_v) ;
363 
364  friend class Timer;
365  friend class Engine;
366 };
367 
368 }
369 }
370 
371 #endif
372 
container::iterator iterator
Definition: SortedVector.hpp:33
Option::Referral::_v getReferral() const
Definition: Session.hpp:136
Definition: Session.hpp:47
virtual void eventServerShutdown()
Definition: Session.hpp:287
void * getLDAP()
Definition: Session.hpp:115
Definition: Response.hpp:35
Definition: Millisecond.hpp:24
Definition: Session.hpp:38
int getExternalID() const
Definition: Session.hpp:109
void setOption(const Option::Referral::_v referral)
Definition: Session.hpp:190
Definition: Session.hpp:44
Definition: ResultCode.hpp:26
const std::string & getUser() const
Definition: Session.hpp:92
Definition: Timer.hpp:23
virtual void eventResponseError(const ResultCode &resultCode, const bool disconnect)
Definition: Session.hpp:302
void clearNetworkTimeout()
Definition: Session.hpp:209
virtual void eventIntermediateResponseError(const Response &response)
Definition: Session.hpp:310
_v
Definition: ClassCode.hpp:22
State::_v getState() const
Definition: Session.hpp:142
Definition: Node.hpp:56
_v
Definition: Session.hpp:45
const std::string & getPassword() const
Definition: Session.hpp:98
static const Millisecond DefaultTimeout
Definition: Session.hpp:80
void setNetworkTimeout(const Millisecond &timeout)
Definition: Session.hpp:201
bool hasNetworkTimeout() const
Definition: Session.hpp:164
const Millisecond & getTimeout(const ClassCode::_v v) const
Definition: Session.hpp:148
Definition: Session.hpp:56
int getCategory() const
Definition: Session.hpp:104
void setTimeout(const ClassCode::_v v, const Millisecond &millisecond)
Definition: Session.hpp:219
Option::Defer::_v getDefer() const
Definition: Session.hpp:128
xml::Node Node
Definition: Node.hpp:21
Definition: Session.hpp:74
Definition: Request.hpp:28
container::const_iterator const_iterator
Definition: SortedVector.hpp:34
bool isBound() const
Definition: Session.hpp:178
Definition: app.hpp:12
_v
Definition: Session.hpp:74
Definition: Session.hpp:46
Definition: Handler.hpp:33
const Response * send(const Request &request) noexcept(false)
Definition: Session.hpp:249
int IdMessage
Definition: defines.hpp:16
void setOption(const Option::Defer::_v defer)
Definition: Session.hpp:184
const std::string & getURL() const
Definition: Session.hpp:86
Definition: Session.hpp:61
Definition: Engine.hpp:50
_v
Definition: Session.hpp:62
Millisecond getNetworkTimeout() const
Definition: Session.hpp:155