Date: Thursday, December 21, 2006 @ 18:50:20
  Author: gilles
    Path: /cvsroot/carob/carob

   Added: include/SocketAddress.hpp (1.1) src/SocketAddress.cpp (1.1)

(new class)
SocketAddress wraps a sockaddr and its length to provide unique identifier of 
controllers


---------------------------+
 include/SocketAddress.hpp |   83 ++++++++++++++++++++++++++++++++++++
 src/SocketAddress.cpp     |  101 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 184 insertions(+)


Index: carob/include/SocketAddress.hpp
diff -u /dev/null carob/include/SocketAddress.hpp:1.1
--- /dev/null   Thu Dec 21 18:50:20 2006
+++ carob/include/SocketAddress.hpp     Thu Dec 21 18:50:20 2006
@@ -0,0 +1,83 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2006 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. 
+ *
+ * Initial developer(s): Gilles Rayrat.
+ * Contributor(s): ______________________.
+ */
+#ifndef SOCKETADDRESS_HPP_
+#define SOCKETADDRESS_HPP_
+
+#include "CarobException.hpp"
+
+#ifdef __MINGW32__
+  #include <winsock2.h>
+  // Needed ? #include <ws2tcpip.h>
+  #include "Common.hpp" //for in_port_t
+#else
+  #include <sys/socket.h> // sockaddr & socklen_t
+#endif
+
+namespace CarobNS {
+
+/**
+ * Wraps a sockaddr, its size and provides a less than operator so it can be
+ * the keys of stl maps
+ */
+class SocketAddress
+{
+public:
+  /** Empty constructor */
+  SocketAddress();
+  /**
+   * Copy constructor, calls = operator
+   * @param sa the socket address to copy infos from
+   */
+  SocketAddress(const SocketAddress& sa);
+  /** frees address field */
+  ~SocketAddress();
+  /**
+   * Creates a <code>SocketAddress</code> with the given address and length.
+   * Calls #setAddress(sockaddr* sa, socklen_t l)
+   * @param sa the socket address
+   * @param l length of sa
+   */
+  SocketAddress(sockaddr* sa, socklen_t l);
+  /**
+   * Set the given address and length
+   * @param sa the socket address to set
+   * @param l length of sa
+   */
+  void            setAddress(sockaddr* sa, socklen_t l);
+  /** Assignement operator, calls #setAddress(sockaddr* sa, socklen_t l) */
+  SocketAddress&  operator = (const SocketAddress& sa);
+  /** Less than operator, so SocketAddress can be keys of stl maps */
+  bool            operator < (const SocketAddress& sa) const;
+  /** Returns a pointer to the socket address */
+  sockaddr*       getAddressPtr() const { return address; }
+  /** Returns the socket address length */
+  socklen_t       getAddressLength() const { return address_length; }
+  /** Gets the address family of this socket */
+  sa_family_t     getFamily() const;
+private:
+  /** Pointer to the sockaddr struct */
+  sockaddr*       address;
+  /** Size of the sockaddr struct */
+  socklen_t       address_length;
+};
+
+} //namespace CarobNS
+#endif /*SOCKETADDRESS_HPP_*/
Index: carob/src/SocketAddress.cpp
diff -u /dev/null carob/src/SocketAddress.cpp:1.1
--- /dev/null   Thu Dec 21 18:50:20 2006
+++ carob/src/SocketAddress.cpp Thu Dec 21 18:50:20 2006
@@ -0,0 +1,101 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2006 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. 
+ *
+ * Initial developer(s): Gilles Rayrat.
+ * Contributor(s): ______________________.
+ */
+
+#ifdef __MINGW32__
+  #include <ws2tcpip.h>
+#else
+  #include <netinet/in.h> // in_port_t
+#endif
+
+
+#include "SocketAddress.hpp"
+#include "Common.hpp" // for logging
+
+using namespace CarobNS;
+
+SocketAddress::SocketAddress() : address(NULL), 
address_length(static_cast<socklen_t>(-1))
+{
+}
+
+SocketAddress::SocketAddress(const SocketAddress& sa)
+{
+  *this = sa;
+}
+
+SocketAddress::SocketAddress(sockaddr* sa, socklen_t l) : address_length(l)
+{
+  setAddress(sa, l);
+}
+
+void SocketAddress::setAddress(sockaddr* sa, socklen_t l)
+{
+  address_length = l;
+  address = static_cast<sockaddr*>(malloc(l));
+  memcpy(address, sa, l);
+}
+
+SocketAddress& SocketAddress::operator = (const SocketAddress& sa)
+{
+  setAddress(sa.address, sa.address_length);
+  return *this;
+}  
+
+bool SocketAddress::operator < (const SocketAddress& sa) const
+{
+  if (address == NULL)
+    return true;
+  if (sa.address == NULL)
+    return true;
+  if (address_length != sa.address_length)
+    return (address_length < sa.address_length);
+  if (address->sa_family != sa.address->sa_family)
+    return (address->sa_family < sa.address->sa_family);
+  if (getFamily() == AF_INET) // ipv4
+  {
+    sockaddr_in this_in, sa_in;
+    memcpy(&this_in, address, address_length);
+    memcpy(&sa_in, &sa.address, sa.address_length);
+    return (this_in.sin_addr.s_addr < sa_in.sin_addr.s_addr);
+  }
+  else if (getFamily() == AF_INET6)// ipv6
+  {
+    sockaddr_in6 this_in6, sa_in6;
+    memcpy(&this_in6, address, address_length);
+    memcpy(&sa_in6, &sa.address, sa.address_length);
+    for (int i=0; i<16; i++)
+      if (this_in6.sin6_addr.s6_addr[i] >= sa_in6.sin6_addr.s6_addr[i])
+        return false;
+    return true;
+  }
+  else // should never happen
+  {
+    if (isErrorEnabled())
+      logError(L"SocketAddress::operator <", L"Unknown socket address 
family!");
+    return false;
+  }
+}
+
+sa_family_t SocketAddress::getFamily() const
+{
+  if (address == NULL)
+    return AF_UNSPEC;
+  return address->sa_family;
+}

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

Reply via email to