OpenBus SDK C++ TAO  2.0.2.0
 Tudo Classes Namespaces Ficheiros Funções Definições de tipos Páginas
Connection.hpp
Ir para a documentação deste ficheiro.
1 // -*- coding: iso-8859-1-unix -*-
2 
9 #ifndef TECGRAF_SDK_OPENBUS_CONNECTION_H_
10 #define TECGRAF_SDK_OPENBUS_CONNECTION_H_
11 
12 #include "openbus/decl.hpp"
13 #include "scsC.h"
14 #include "coreC.h"
15 #include "access_controlC.h"
16 #include "offer_registryC.h"
17 #include "openbus/interceptors/ORBInitializer_impl.hpp"
18 #include "openbus/crypto/PrivateKey.hpp"
19 #include "openbus/crypto/PublicKey.hpp"
20 #ifndef TECGRAF_SDK_OPENBUS_LRUCACHE_H_
21 #define TECGRAF_SDK_OPENBUS_LRUCACHE_H_
22 #include "openbus/LRUCache_impl.hpp"
23 #endif
24 
25 #include <boost/array.hpp>
26 #include <boost/function.hpp>
27 #include <boost/scoped_ptr.hpp>
28 #ifdef OPENBUS_SDK_MULTITHREAD
29  #include <boost/thread.hpp>
30 #endif
31 #include <boost/shared_ptr.hpp>
32 #include <memory>
33 #include <stdexcept>
34 #include <string>
35 #include <vector>
36 #include <cstring>
37 
41 namespace openbus
42 {
45  namespace idl_or = tecgraf::openbus::core::v2_0::services::offer_registry;
46 
47  class OpenBusContext;
48  class LoginCache;
49 #ifndef OPENBUS_SDK_MULTITHREAD
50  class RenewLogin;
51 #endif
52 
53  namespace interceptors
54  {
55  struct orb_info;
56  struct ServerInterceptor;
57  struct ClientInterceptor;
58  }
59 }
60 
61 namespace openbus
62 {
63 struct OPENBUS_SDK_DECL BusChanged : public std::exception
64 {
65  const char *what() const throw()
66  {
67  return "openbus::BusChanged";
68  }
69 };
70 
71 struct OPENBUS_SDK_DECL AlreadyLoggedIn : public std::exception
72 {
73  const char *what() const throw()
74  {
75  return "openbus::AlreadyLoggedIn";
76  }
77 };
78 
79 struct OPENBUS_SDK_DECL InvalidLoginProcess : public std::exception
80 {
81  const char *what() const throw()
82  {
83  return "openbus::InvalidLoginProcess";
84  }
85 };
86 
87 struct OPENBUS_SDK_DECL InvalidPropertyValue : public std::exception
88 {
89  InvalidPropertyValue(const std::string &p, const std::string &v) throw()
90  : property(p), value(v)
91  {
92  }
93 
94  ~InvalidPropertyValue() throw()
95  {
96  }
97 
98  const char *what() const throw()
99  {
100  return "openbus::InvalidPropertyValue";
101  }
102 
103  const std::string property;
104  const std::string value;
105 };
106 
107 class Connection;
108 
121 class OPENBUS_SDK_DECL SharedAuthSecret
122 {
123 public:
129  void cancel();
130 
135  std::string busid() const
136  {
137  return busid_;
138  }
139 private:
141  SharedAuthSecret(const std::string &bus_id, idl_ac::LoginProcess_var,
142  const idl::OctetSeq &secret,
143  interceptors::ORBInitializer *);
144  std::string busid_;
145  idl_ac::LoginProcess_var login_process_;
146  idl::OctetSeq secret_;
147  interceptors::ORBInitializer *orb_initializer_;
148  friend class OpenBusContext;
149  friend class Connection;
150 };
151 
177 class OPENBUS_SDK_DECL Connection
178 {
179 public:
180  typedef std::vector<std::pair<std::string, std::string> >
181  ConnectionProperties;
197  typedef boost::function<void (Connection & conn, idl_ac::LoginInfo login)>
199 
218  void loginByPassword(const std::string &entity, const std::string &password);
219 
241  void loginByCertificate(const std::string &entity, const PrivateKey &privKey);
242 
263  SharedAuthSecret startSharedAuth();
264 
283  void loginBySharedAuth(const SharedAuthSecret &secret);
284 
298  bool logout();
299 
322  void onInvalidLogin(InvalidLoginCallback_t p);
323 
328  InvalidLoginCallback_t onInvalidLogin() const;
329 
334  const idl_ac::LoginInfo *login() const;
335 
339  const std::string busid() const;
340  ~Connection();
341 private:
345  Connection(const std::string host, const unsigned short port, CORBA::ORB_ptr,
346  interceptors::ORBInitializer *, OpenBusContext &,
347  const ConnectionProperties &props);
348 
349  Connection(const Connection &);
350  Connection &operator=(const Connection &);
351 
352 #ifdef OPENBUS_SDK_MULTITHREAD
353  static void renewLogin(Connection &conn, idl_ac::AccessControl_ptr acs,
354  OpenBusContext &ctx, idl_ac::ValidityTime t);
355 #endif
356  void login(idl_ac::LoginInfo &loginInfo,
357  idl_ac::ValidityTime validityTime);
358 
359  void checkBusid() const;
360  bool _logout(bool local = true);
361  CORBA::ORB_ptr orb() const
362  {
363  return _orb;
364  }
365 
366  idl_ac::LoginRegistry_var login_registry() const
367  {
368  return _login_registry;
369  }
370 
371  idl_ac::AccessControl_var access_control() const
372  {
373  return _access_control;
374  }
375 
376  const idl_ac::LoginInfo *_login() const
377  {
378 #ifdef OPENBUS_SDK_MULTITHREAD
379  boost::lock_guard<boost::mutex> lock(_mutex);;
380 #endif
381  return _loginInfo.get();
382  }
383 
384  idl_or::OfferRegistry_var getOfferRegistry() const
385  {
386  return _offer_registry;
387  }
388 
389  idl_ac::LoginRegistry_var getLoginRegistry() const
390  {
391  return _login_registry;
392  }
393 
394  idl_ac::LoginInfo get_login();
395 
396  const std::string _host;
397  const unsigned short _port;
398  interceptors::ORBInitializer * _orb_init;
399  CORBA::ORB_ptr _orb;
400 #ifdef OPENBUS_SDK_MULTITHREAD
401  boost::thread _renewLogin;
402  mutable boost::mutex _mutex;
403 #else
404  boost::scoped_ptr<RenewLogin> _renewLogin;
405 #endif
406  boost::scoped_ptr<idl_ac::LoginInfo> _loginInfo, _invalid_login;
407  InvalidLoginCallback_t _onInvalidLogin;
408 
409  enum LegacyDelegate
410  {
411  CALLER,
412  ORIGINATOR
413  };
414 
415  enum State
416  {
417  LOGGED,
418  UNLOGGED,
419  INVALID
420  } _state;
421 
422  /* Variaveis que sao modificadas somente no construtor. */
423  OpenBusContext &_openbusContext;
424  PrivateKey _key;
425  scs::core::IComponent_var _iComponent;
426  idl_ac::AccessControl_var _access_control;
427  idl_ac::LoginRegistry_var _login_registry;
428  idl_or::OfferRegistry_var _offer_registry;
429  boost::scoped_ptr<LoginCache> _loginCache;
430  std::string _busid;
431  boost::scoped_ptr<PublicKey> _buskey;
432  LegacyDelegate _legacyDelegate;
433  bool _legacyEnabled;
434 
435 
436  struct SecretSession
437  {
438  SecretSession()
439  : id(0), ticket(0)
440  {
441  secret.fill(0);
442  }
443  CORBA::ULong id;
444  std::string remote_id;
445  boost::array<unsigned char, secret_size> secret;
446  CORBA::ULong ticket;
447  friend bool operator==(const SecretSession &lhs, const SecretSession &rhs);
448  friend bool operator!=(const SecretSession &lhs, const SecretSession &rhs);
449  };
450  typedef LRUCache<hash_value, std::string> profile2login_LRUCache;
451  profile2login_LRUCache _profile2login;
452  LRUCache<std::string, SecretSession> _login2session;
453 
454  friend struct openbus::interceptors::ServerInterceptor;
455  friend struct openbus::interceptors::ClientInterceptor;
456  friend class openbus::OpenBusContext;
457  friend bool operator==(const SecretSession &lhs, const SecretSession &rhs);
458  friend bool operator!=(const SecretSession &lhs, const SecretSession &rhs);
459 };
460 
461 inline bool operator==(const Connection::SecretSession &lhs,
462  const Connection::SecretSession &rhs)
463 {
464  return lhs.id == rhs.id
465  && lhs.remote_id == rhs.remote_id
466  && lhs.secret == rhs.secret
467  && lhs.ticket == rhs.ticket;
468 }
469 
470 inline bool operator!=(const Connection::SecretSession &lhs,
471  const Connection::SecretSession &rhs)
472 {
473  return !(lhs == rhs);
474 }
475 
476 }
477 
478 #endif
openbus
Definition: Connection.hpp:41
Segredo para compartilhamento de autenticação.
Definition: Connection.hpp:121
Definition: OpenBusContext.hpp:47
std::string busid() const
Retorna o identificador do barramento em que o segredo pode ser utilizado.
Definition: Connection.hpp:135
Conexão para acesso identificado a um barramento.
Definition: Connection.hpp:177
boost::function< void(Connection &conn, idl_ac::LoginInfo login)> InvalidLoginCallback_t
Callback de login inválido.
Definition: Connection.hpp:198
Permite controlar o contexto das chamadas de um ORB para acessar informações que identificam essas ch...
Definition: OpenBusContext.hpp:262