Date: Thursday, January 5, 2006 @ 16:17:27
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: include/Connection.hpp (1.42 -> 1.43)
          include/ConnectionParameters.hpp (1.12 -> 1.13)
          src/Connection.cpp (1.49 -> 1.50) src/ConnectionParameters.cpp
          (1.10 -> 1.11)

Multi connect - preparing ground
Replaced controller by <list of controllers>. Modified connection function and 
connection parameters to handle this controller list


----------------------------------+
 include/Connection.hpp           |   16 +++--
 include/ConnectionParameters.hpp |  111 ++++++++++++++++++++++++-------------
 src/Connection.cpp               |   75 ++++++++++++++++---------
 src/ConnectionParameters.cpp     |   67 +++++++++++++++-------
 4 files changed, 176 insertions(+), 93 deletions(-)


Index: carob/include/Connection.hpp
diff -u carob/include/Connection.hpp:1.42 carob/include/Connection.hpp:1.43
--- carob/include/Connection.hpp:1.42   Wed Jan  4 19:55:05 2006
+++ carob/include/Connection.hpp        Thu Jan  5 16:17:27 2006
@@ -26,6 +26,7 @@
 
 #include "CarobException.hpp"
 #include "ConnectionParameters.hpp"
+#include "ControllerConnectPolicy.hpp"
 #include "CriticalSection.hpp"
 #include "DriverResultSet.hpp"
 #include "DriverSocket.hpp"
@@ -110,15 +111,16 @@
   /**
    * Creates a new connection and connects to a controller with the given
    * parameters
-   * @param controller, base, user, etc. to use for connecting
+   * @param prms controller, base, user, etc. to use for connecting
+   * @throw DriverException if the ConnectPolicy given in parameters is unknown
    * @throw ConnectionException if the requested controller could not be
    *        reached
    * @throw AuthenticationException if a login error occured, or if an 
    *        IO exception occured during authentication (premature close of
    *        connection)
    */
-  Connection(const ConnectionParameters&) throw (ConnectionException,
-      AuthenticationException, UnexpectedException);
+  Connection(const ConnectionParameters& prms) throw (DriverException,
+      ConnectionException, AuthenticationException, UnexpectedException);
 
   /**
    * Closes the connection and destroys its socket
@@ -285,6 +287,7 @@
    * do the job of checking and finishing connection establishment.
    * @param cp parameters of the connection
    * @return true if the connection initialization succeeds
+   * @throw DriverException if the connect policy is NULL
    * @throw ConnectionException if the requested controller could not be
    *        reached
    * @throw AuthenticationException if a login error occured, or if an 
@@ -292,8 +295,8 @@
    *        connection)
    */
   bool                initConnection(const ConnectionParameters& cp)
-                          throw (ConnectionException, AuthenticationException,
-                          UnexpectedException);
+                          throw (DriverException, ConnectionException,
+                          AuthenticationException, UnexpectedException);
   /**
    * Reads the controller acknowledgements and misc parameters that finish 
    * connection. [EMAIL PROTECTED] #initConnection(const 
ConnectionParameters&)} must be 
@@ -360,6 +363,9 @@
    */
   bool                mustBeginTransaction;
   /**
+   * Controller connection policy used for this connection */
+  AbstractControllerConnectPolicy* connect_policy_ptr;
+  /**
    * List of pointers to the [parameter]statements created by this connection.
    * Kept in order to destroy them when closing connection
    */
Index: carob/include/ConnectionParameters.hpp
diff -u carob/include/ConnectionParameters.hpp:1.12 
carob/include/ConnectionParameters.hpp:1.13
--- carob/include/ConnectionParameters.hpp:1.12 Wed Jan  4 19:26:34 2006
+++ carob/include/ConnectionParameters.hpp      Thu Jan  5 16:17:27 2006
@@ -23,8 +23,8 @@
 #define CONNECTIONPARAMETERS_H_
 
 #include <netinet/in.h> // in_port_t
-
 #include <string>
+#include <vector>
 
 #include "CarobException.hpp"
 
@@ -37,16 +37,45 @@
   DEBUG_LEVEL_DEBUG
 };
 
+enum ConnectPolicy
+{
+  ROUND_ROBIN
+};
+
+class ControllerInfo
+{
+public:
+  /** Empty constructor to set default values (host="" and port=0) */
+  ControllerInfo();
+  /** Constructs a Controller info with the given name and port */
+  ControllerInfo(const std::wstring& host, in_port_t port);
+  virtual ~ControllerInfo() {}
+  /** Copy operator */
+  ControllerInfo& operator =(const ControllerInfo& ci);
+  /** Comparison operator */
+  bool operator ==(const ControllerInfo& ci) const;
+  /** Gives the host name */
+  std::wstring  getHostName() const { return host_name; }
+  /** Gives the host port */
+  in_port_t     getHostPort() const { return host_port; }
+private:
+  /** String representation of the host name, fully qualified or IP address */
+  std::wstring  host_name;
+  /** Host port */
+  in_port_t     host_port;
+};
+
 /**
  * Constants for connection defaults
  */
-#define DEFAULT_PORT        ((in_port_t)25322)
 #define DEFAULT_HOST        L"localhost"
+#define DEFAULT_PORT        ((in_port_t)25322)
 #define DEFAULT_USER        L"user"
 #define DEFAULT_PASSWD      L""
 #define DEFAULT_DB          L"myDB"
 #define DEFAULT_DEBUG_LEVEL DEBUG_LEVEL_OFF
-
+#define DEFAULT_POLICY      ROUND_ROBIN
+#define DEFAULT_RETRY_INTERVAL 10
 /**
  * This class contains the parameters for a connection to the controller.
  * This connection parameters will be used by the Connection class.
@@ -56,65 +85,69 @@
 {
 public:
   /**
-   * Default constructor, sets all members to default values
-   */
-       ConnectionParameters();
-  /**
    * Creates instance with set of values
    */
-  ConnectionParameters(const std::wstring&, const in_port_t&,
-      const std::wstring&, const std::wstring&, const std::wstring&,
-      const DebugLevel&) throw (ConnectionException, UnexpectedException);
-  
+  ConnectionParameters(const std::wstring&  host              = DEFAULT_HOST,
+                       in_port_t            port              = DEFAULT_PORT,
+                       const std::wstring&  db                = DEFAULT_DB,
+                       const std::wstring&  uname             = DEFAULT_USER,
+                       const std::wstring&  upass             = DEFAULT_PASSWD,
+                       DebugLevel           dl                = 
DEFAULT_DEBUG_LEVEL,
+                       ConnectPolicy        cp                = DEFAULT_POLICY,
+                       int                  retryIntervalInMs = 
DEFAULT_RETRY_INTERVAL)
+                   throw (ConnectionException, UnexpectedException);
        virtual ~ConnectionParameters();
-  
+  /**
+   * Adds the given controller to the list of controllers
+   */
+  void                addController(const std::wstring& host, in_port_t port);
   /**
    * Validates database name. If invalid returns false and set param char to 
the
    * first invalid character.
    */
-  static bool         isValidDatabaseName(const std::wstring&, wchar_t&);
+  bool                isValidDatabaseName(const std::wstring&, wchar_t&) const;
   /**
    * Validates host name. If invalid returns false and set param char to the
    * first invalid character.
    */
-  static bool         isValidHostName(const std::wstring&, wchar_t&);
+  bool                isValidHostName(const std::wstring&, wchar_t&) const;
 
-  /** Gives the host name */
-  inline std::wstring getHostName() const { return hostName; }
   /** Checks the host name */
-  static
-  const std::wstring& checkHostName(const std::wstring&)
-    throw (ConnectionException, UnexpectedException);
-  /** Gives the host port */
-  inline in_port_t   getHostPort() const { return hostPort; }
-  /** Gives the database name */
-  inline std::wstring getDatabaseName() const { return databaseName; }
+  const std::wstring& checkHostName(const std::wstring&) const
+      throw (ConnectionException, UnexpectedException);
+  /** Gives the list of hosts */
+  std::vector<ControllerInfo> getControllerList() const { return 
controller_list; }
   /** Checks the database name */
-  static
-  const std::wstring& checkDatabaseName(const std::wstring&)
-    throw (ConnectionException, UnexpectedException);
+  const std::wstring& checkDatabaseName(const std::wstring&) const
+      throw (ConnectionException, UnexpectedException);
+  /** Gives the database name */
+  std::wstring        getDatabaseName() const { return database_name; }
   /** Gives the user name */
-  inline std::wstring getUserName() const { return userName; }
+  std::wstring        getUserName() const { return user_name; }
   /** Gives the user password */
-  inline std::wstring getUserPass() const { return userPass; }
+  std::wstring        getUserPass() const { return user_pass; }
   /** Gives the debug level */
-  inline DebugLevel   getDebugLevel() const { return debugLevel; }
+  DebugLevel          getDebugLevel() const { return debug_level; }
+  /** Gives the connection policy */
+  ConnectPolicy       getConnectPolicy() const { return connect_policy; }
+  /** Gives the retry interval in ms */
+  int                 getRetryInterval() const { return retry_interval_in_ms; }
   
 private:
-  /** String representation of the host name, fully qualified or IP address */
-  const std::wstring  hostName;
-  /** Host port */
-  const in_port_t     hostPort;
+  /** List of controllers to connect to */
+  std::vector<ControllerInfo> controller_list;
   /** Name of the database */
-  const std::wstring  databaseName;
+  std::wstring        database_name;
   /** User name for database access */
-  const std::wstring  userName;
+  std::wstring        user_name;
   /** User password for database access */
-  const std::wstring  userPass;
+  std::wstring        user_pass;
   /** Connection debug level */
-  const DebugLevel    debugLevel;
-
-  //TODO: ControllerInfo, ConnectionPolicy
+  DebugLevel          debug_level;
+  /** Controller connection policy */
+  ConnectPolicy       connect_policy;
+  /** Time in ms between two connection attempts */
+  int                 retry_interval_in_ms;
 };
 
 } //namespace CarobNS
Index: carob/src/Connection.cpp
diff -u carob/src/Connection.cpp:1.49 carob/src/Connection.cpp:1.50
--- carob/src/Connection.cpp:1.49       Wed Jan  4 11:53:51 2006
+++ carob/src/Connection.cpp    Thu Jan  5 16:17:27 2006
@@ -28,38 +28,50 @@
 using namespace CarobNS;
 
 Connection::Connection(const ConnectionParameters& prms)
-    throw (ConnectionException, AuthenticationException, UnexpectedException) :
+    throw (DriverException, ConnectionException, AuthenticationException, 
UnexpectedException) :
+  driverSocketPtr(NULL),
   isClosed(true),
   autoCommit(true),
   readOnly(false),
   writeExecutedInTransaction(false),
-  mustBeginTransaction(false)
+  mustBeginTransaction(false),
+  connect_policy_ptr(NULL)
 {
   wstring fctName(L"Connection::Connection");
-  if (isInfoEnabled())
+  switch (prms.getConnectPolicy())
   {
-    wstring msg = L"Authenticating with controller " + prms.getHostName() + 
-                  L":" + toWString((int)prms.getHostPort());
-    if (isInfoEnabled())
-      logInfo(fctName, msg);
+    case ROUND_ROBIN:
+      connect_policy_ptr = new 
RoundRobinConnectPolicy(prms.getControllerList(),
+                                                       
prms.getRetryInterval());
+    break;
+    default:
+      throw DriverException(L"Unsupported connection policy #" + 
toWString(prms.getConnectPolicy()));
   }
-  //Do the authentication stuff, then receive ack and other params
-  if (initConnection(prms) && finalizeConnect())
+  try
   {
-    if (isInfoEnabled())
-      logInfo(fctName, L"Connection succeded");
-    isClosed = false;
+    //Do the authentication stuff, then receive ack and other params
+    if (initConnection(prms) && finalizeConnect())
+    {
+      if (isInfoEnabled())
+        logInfo(fctName, L"Connection succeded");
+      isClosed = false;
+    }
+  }
+  catch (...)
+  {
+    //Do some clean up: we are in constructor so if we throw an exception, the
+    //destructor will never be called...
+    delete driverSocketPtr; driverSocketPtr = NULL;
+    delete connect_policy_ptr; connect_policy_ptr = NULL;
+    throw;
   }
 }
 
 Connection::~Connection()
 {
   close();
-  if (driverSocketPtr != NULL)
-  {
-    delete driverSocketPtr;
-    driverSocketPtr = NULL;
-  }
+  delete driverSocketPtr;
+  delete connect_policy_ptr;
 }
 
 void Connection::sendCommand(const DriverSocket& socket, int command)
@@ -76,19 +88,31 @@
 }
 
 bool Connection::initConnection(const ConnectionParameters& prms)
-    throw (ConnectionException, AuthenticationException, UnexpectedException)
+    throw (DriverException, ConnectionException, AuthenticationException,
+        UnexpectedException)
 {
   wstring fctName(L"Connection::initConnection");
   bool  protocolVersionSend = false,
         dbNameSend = false,
         userNameSend = false,
         userPassSend = false;
-  
+  if (connect_policy_ptr == NULL)
+    throw DriverException(L"Connection policy error. Aborting.");
+  ControllerInfo controller = connect_policy_ptr->getController();
+  if (isInfoEnabled())
+  {
+    wstring msg = L"Authenticating with controller "
+                      + controller.getHostName() + L":"
+                      + toWString((int)controller.getHostPort());
+    if (isInfoEnabled())
+      logInfo(fctName, msg);
+  }
   try
   {
     //Here is the connection protocol...
     // 1. connect to the controller
-    driverSocketPtr = new DriverSocket(prms.getHostName(), prms.getHostPort());
+    driverSocketPtr = new DriverSocket(controller.getHostName(),
+                                          controller.getHostPort());
     // 2. sent the Protocol version information
     *driverSocketPtr<<ProtocolVersion;
     protocolVersionSend = true;
@@ -104,8 +128,8 @@
   }
   catch (ConnectionException connExcpt)
   {
-    wstring msg = L"Unable to connect to controller on " + prms.getHostName()
-                  + L":" + toWString((int)prms.getHostPort())
+    wstring msg = L"Unable to connect to controller on " + 
controller.getHostName()
+                  + L":" + toWString((int)controller.getHostPort())
                   + L" (" + connExcpt.description() + L").";
     if (isErrorEnabled())
       logError(fctName, msg);
@@ -114,11 +138,8 @@
   }
   catch (SocketIOException sockIOExcpt)
   {
-    // at this point, driverSocketPtr has been allocated. Destroy it
-    delete driverSocketPtr;
-
-    wstring msg = L"Could not authentify to " + prms.getHostName()
-                  + L":" + toWString((int)prms.getHostPort());
+    wstring msg = L"Could not authentify to " + controller.getHostName()
+                  + L":" + toWString((int)controller.getHostPort());
     if (!protocolVersionSend)
     {
       msg+=L". Error while sending protocol version ("
Index: carob/src/ConnectionParameters.cpp
diff -u carob/src/ConnectionParameters.cpp:1.10 
carob/src/ConnectionParameters.cpp:1.11
--- carob/src/ConnectionParameters.cpp:1.10     Wed Jan  4 19:26:34 2006
+++ carob/src/ConnectionParameters.cpp  Thu Jan  5 16:17:27 2006
@@ -24,35 +24,57 @@
 using std::wstring;
 
 using namespace CarobNS;
+ControllerInfo::ControllerInfo() : host_name(L""), host_port(0)
+{
+}
+ControllerInfo::ControllerInfo(const wstring &name, in_port_t port) :
+    host_name(name), host_port(port)
+{
+}
+
+ControllerInfo& ControllerInfo::operator =(const ControllerInfo& ci)
+{
+  host_name = ci.host_name;
+  host_port = ci.host_port;
+  return *this;
+}
 
-ConnectionParameters::ConnectionParameters() :
-hostName(DEFAULT_HOST),
-hostPort(DEFAULT_PORT),
-databaseName(DEFAULT_DB),
-userName(DEFAULT_USER),
-userPass(DEFAULT_PASSWD),
-debugLevel(DEFAULT_DEBUG_LEVEL)
+bool ControllerInfo::operator ==(const ControllerInfo& ci) const
 {
+  return ((host_name == ci.host_name) && (host_port == ci.host_port));
 }
 
-ConnectionParameters::ConnectionParameters(const wstring& host, const 
in_port_t& port, 
-                                           const wstring& db, const wstring& 
uname, 
-                                           const wstring& upass, const 
DebugLevel& dl)
-    throw (ConnectionException, UnexpectedException) :
-    hostName(checkHostName(host)),
-    hostPort(port),
-    databaseName(checkDatabaseName(db)),
-    userName(uname),
-    userPass(upass),
-    debugLevel(dl)
+
+ConnectionParameters::ConnectionParameters(
+    const std::wstring& host /*= DEFAULT_DB*/,
+    in_port_t port /*= DEFAULT_PORT*/,
+    const std::wstring& db /*= DEFAULT_DB*/,
+    const std::wstring& uname /*= DEFAULT_USER*/,
+    const std::wstring& upass /*= DEFAULT_PASSWD*/,
+    DebugLevel dl /*= DEFAULT_DEBUG_LEVEL*/,
+    ConnectPolicy cp /*= DEFAULT_POLICY*/,
+    int retryIntervalInMs /*= DEFAULT_RETRY_INTERVAL*/)
+        throw (ConnectionException, UnexpectedException) :
+            database_name(checkDatabaseName(db)),
+            user_name(uname),
+            user_pass(upass),
+            debug_level(dl),
+            connect_policy(cp),
+            retry_interval_in_ms(retryIntervalInMs)
 {
+  addController(host, port);
 }
 
 ConnectionParameters::~ConnectionParameters()
 {
 }
-  
-const wstring& ConnectionParameters::checkHostName(const wstring& hostNamePrm)
+
+void ConnectionParameters::addController(const wstring& host, in_port_t port)
+{
+  controller_list.push_back(ControllerInfo(checkHostName(host), port));
+}
+
+const wstring& ConnectionParameters::checkHostName(const wstring& hostNamePrm) 
const
     throw (ConnectionException, UnexpectedException)
 {
   wchar_t pbChar;
@@ -64,7 +86,7 @@
   return hostNamePrm;
 }
 
-const wstring& ConnectionParameters::checkDatabaseName(const wstring& dbname)
+const wstring& ConnectionParameters::checkDatabaseName(const wstring& dbname) 
const
     throw (ConnectionException, UnexpectedException)
 {
   wchar_t pbChar;
@@ -73,7 +95,7 @@
   return dbname;
 }
 
-bool ConnectionParameters::isValidDatabaseName(const wstring& databaseName, 
wchar_t& pbChar)
+bool ConnectionParameters::isValidDatabaseName(const wstring& databaseName, 
wchar_t& pbChar) const
 {
   int size = databaseName.length();
   wchar_t c;
@@ -91,7 +113,8 @@
   return true;
 }
 
-bool ConnectionParameters::isValidHostName(const wstring& hostNamePrm, 
wchar_t& pbChar)
+bool ConnectionParameters::isValidHostName(const wstring& hostNamePrm,
+    wchar_t& pbChar) const
 {
   int size = hostNamePrm.length();
   wchar_t c;

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to