incubator-etch-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vei...@apache.org
Subject svn commit: r1241827 - in /incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu: ./ include/capu/ include/capu/os/ include/capu/os/arch/ include/capu/os/arch/Linux/ include/capu/os/arch/Win32/ test/os/
Date Wed, 08 Feb 2012 09:14:09 GMT
Author: veithm
Date: Wed Feb  8 09:14:09 2012
New Revision: 1241827

URL: http://svn.apache.org/viewvc?rev=1241827&view=rev
Log:
ETCH-145 Implementation of OS independent Sockets

Socket and ServerSocket for blocking IO

Change-Id: I53f6259271471b7a0cda7dc6150921728fa54a4e

Added:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/ServerSocket.h
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/Socket.h
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/ServerSocket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/Socket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/ServerSocket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Socket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/ServerSocket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/Socket.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/ServerSocketTest.cpp
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/SocketTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Config.h

Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt?rev=1241827&r1=1241826&r2=1241827&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt Wed Feb  8 09:14:09 2012
@@ -32,6 +32,8 @@ ADD_CLASS(container/Pair)
 ADD_CLASS(util/Traits)
 ADD_CLASS(container/HashTable)
 ADD_CLASS(container/HashSet)
+ADD_CLASS(os/Socket SOURCE_GROUP arch_source_group)
+ADD_CLASS(os/ServerSocket SOURCE_GROUP arch_source_group)
 
 REQUIRED_PACKAGE(Thread)
 

Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Config.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Config.h?rev=1241827&r1=1241826&r2=1241827&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Config.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Config.h Wed Feb  8 09:14:09 2012
@@ -33,7 +33,9 @@ namespace capu
 {
    #ifdef _WIN32
     typedef char int8_t;
+    typedef unsigned char uint8_t;
     typedef short int16_t;
+    typedef unsigned short uint16_t;
     typedef int int32_t;
     typedef signed __int64 int64_t;
     typedef unsigned int uint32_t;
@@ -43,7 +45,9 @@ namespace capu
     typedef bool bool_t;
     #else //other OS should be C99 compliant
     typedef signed char int8_t;
+    typedef unsigned char uint8_t;
     typedef short int int16_t;
+    typedef unsigned short int uint16_t;
     typedef int int32_t;
     typedef long long int int64_t;
     typedef unsigned long int uint32_t;

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/ServerSocket.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/ServerSocket.h?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/ServerSocket.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/ServerSocket.h Wed Feb  8 09:14:09 2012
@@ -0,0 +1,91 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifndef __SERVERSOCKET_H__
+#define __SERVERSOCKET_H__
+
+#define SERVER_SOCKER_INC_HEADER
+#include "arch/ServerSocket.inc"
+#undef SERVER_SOCKER_INC_HEADER
+
+#include "capu/os/Socket.h"
+#include "capu/Error.h"
+
+namespace capu {
+
+  class ServerSocket {
+#define SERVER_SOCKET_INC_MEMBER
+#include "arch/ServerSocket.inc"
+#undef SERVER_SOCKET_INC_MEMBER
+
+  public:
+
+    /**
+     * Constructor of ServerSocket
+     */
+    inline ServerSocket();
+
+    /**
+     * Destructor of Server Socket
+     */
+    inline ~ServerSocket();
+
+    /**
+     * The program flow will be blocked until a connection arrives
+     * Programmer is responsible for deallocating memory of returning socket.
+     * @return Socket if a connection is accepted
+     *         NULL otherwise
+     */
+    inline Socket* accept();
+
+    /**
+     * Close the socket which is used for accepting connection
+     * @return CAPU_OK if the socket is successfully closed
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     */
+    inline status_t close();
+
+    /**
+     *
+     * @param port indicates port number
+     * @param address to bind if it is not given it accepts all connection from any address
+     * @return CAPU_OK  if the server socket is successfully bound
+     *         CAPU_SOCKET_EADDR if the addr is faulty
+     *         CAPU_ERROR  if the socket is already bound
+     *         CAPU_EINVAL if the addr is NULL or port is equal to 0
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     */
+    inline status_t bind(uint16_t port, const char * addr = NULL);
+
+    /**
+     * 
+     * @param backlog (maximum length of the queue of pending connections)
+     * @return CAPU_OK if the listen is successful
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t listen(uint8_t backlog);
+
+  };
+
+#define SERVER_SOCKET_INC_IMPL
+#include "arch/ServerSocket.inc"
+#undef SERVER_SOCKET_INC_IMPL
+}
+#endif
+

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/Socket.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/Socket.h?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/Socket.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/Socket.h Wed Feb  8 09:14:09 2012
@@ -0,0 +1,185 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifndef __SOCKET_H__
+#define __SOCKET_H__
+
+#define SOCKET_INC_HEADER
+#include "arch/Socket.inc"
+#undef SOCKET_INC_HEADER
+
+#include "capu/Error.h"
+
+namespace capu {
+
+  class Socket {
+#define SOCKET_INC_MEMBER
+#include "arch/Socket.inc"
+#undef SOCKET_INC_MEMBER
+  public:
+
+    /**
+     * Default Constructor
+     */
+    inline Socket();
+
+    /**
+     * Destructor
+     */
+    inline ~Socket();
+
+    /**
+     * Send the messages
+     * @param buffer    the content of message that will be sent to destination
+     * @param length    the length of message that will be sent to destination
+     * @return CAPU_OK if the sent is successful
+     *         CAPU_EINVAL if the buffer is NULL
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t send(unsigned char * buffer, int32_t length);
+
+    /**
+     * Receive message
+     * @param buffer    buffer that will be used to store incoming message
+     * @param length    buffer size
+     * @param numBytes  number of bytes on socket
+     * @return CAPU_OK if the receive is successfully executed
+     *         CAPU_TIMEOUT if there has been a timeout
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t receive(unsigned char * buffer, int32_t length, int32_t& numBytes);
+
+    /**
+     * close the socket
+     * @return CAPU_OK if the socket is correctly closed
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t close();
+
+    /**
+     * connect to the given address
+     * @param dest_addr destination address of server
+     * @param port      port number of service
+     * @return  CAPU_OK if correctly connects to specified address
+     *          CAPU_SOCKET_ECONNECT if the connection is not successful
+     *          CAPU_SOCKET_EADDR if the given address is not resolved.
+     *          CAPU_EINVAL if the dest_addr is NULL
+     *          CAPU_SOCKET_ESOCKET if the socket is not created
+     */
+    inline status_t connect(unsigned char * dest_addr, uint16_t port);
+
+    /**
+     * Sets the maximum socket buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead)
+     * Set the receive buffer size
+     * Sets buffer size information.
+     * @return CAPU_OK if the buffer is successfully set for both receive and send operations
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t setBufferSize(int32_t bufferSize);
+
+    /**
+     * Set the linger option for socket
+     * Specifies whether the socket lingers on close() if data is present.
+     * @return CAPU_OK if the LingerOption is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t setLingerOption(bool_t isLinger, int32_t linger);
+
+
+    /**
+     * Set no delay option
+     * Specifies whether the Nagle algorithm used by TCP for send coalescing is to be disabled.
+     * @return CAPU_OK if the NoDelay is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t setNoDelay(bool_t noDelay);
+
+    /**
+     * Set Keep Alive option
+     * Keeps connections active by enabling periodic transmission of messages, if this is supported by the protocol.
+     * @return CAPU_OK if the KeepAlive is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t setKeepAlive(bool_t keepAlive);
+
+    /**
+     * Set Timeout
+     * Sets the timeout value that specifies the maximum amount of time an input function waits until it completes
+     * @return CAPU_OK if the Timeout for receive operation is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t setTimeout(int32_t timeout);
+
+    /**
+     * get the send and receive buffer size
+     * gets buffer size information.
+     * @return CAPU_OK if the buffer is successfully set for both receive and send operations
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t getBufferSize(int32_t& bufferSize);
+
+    /**
+     * get the linger option for socket
+     * @return CAPU_OK if the LingerOption is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t getLingerOption(bool_t& isLinger, int32_t& linger);
+
+    /**
+     * Get no delay option
+     * @return CAPU_OK if the NoDelay is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t getNoDelay(bool_t& noDelay);
+
+    /**
+     * Get Keep Alive option
+     * @return CAPU_OK if the KeepAlive is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t getKeepAlive(bool_t& keepAlive);
+
+
+    /**
+     * Get Timeout
+     * @return CAPU_OK if the Timeout for receive operation is successfully set
+     *         CAPU_SOCKET_ESOCKET if the socket is not created
+     *         CAPU_ERROR otherwise
+     */
+    inline status_t getTimeout(int32_t& timeout);
+  };
+
+#define SOCKET_INC_IMPL
+#include "arch/Socket.inc"
+#undef SOCKET_INC_IMPL
+}
+
+#endif /* Socket_H */
+

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/ServerSocket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/ServerSocket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/ServerSocket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/ServerSocket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,112 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifdef SERVER_SOCKET_INC_HEADER
+#include <sys/socket.h>
+#include <netdb.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#endif
+
+#ifdef SERVER_SOCKET_INC_MEMBER
+private:
+int32_t mServerSock;
+bool_t mIsBound;
+#endif
+
+#ifdef SERVER_SOCKET_INC_IMPL
+
+inline ServerSocket::ServerSocket() {
+  mServerSock = -1;
+  mIsBound = false;
+  mServerSock = socket(AF_INET, SOCK_STREAM, 0);
+}
+
+inline ServerSocket::~ServerSocket() {
+  close();
+}
+
+inline Socket* ServerSocket::accept() {
+  int32_t clientAddrSize = sizeof (sockaddr_in);
+  struct sockaddr_in serverAddr;
+  Socket *s = new Socket(::accept(mServerSock, (sockaddr *) &serverAddr, (socklen_t *) & clientAddrSize));
+  s->mServerAddress = serverAddr;
+  return s;
+}
+
+inline status_t ServerSocket::close() {
+  int32_t returnValue = CAPU_OK;
+  if (mServerSock != -1) {
+    shutdown(mServerSock, SHUT_RDWR);
+    if (::close(mServerSock) < 0) {
+      returnValue = CAPU_ERROR;
+    }
+  } else {
+    returnValue = CAPU_SOCKET_ESOCKET;
+  }
+  mServerSock = -1;
+  mIsBound = false;
+  return returnValue;
+}
+
+inline status_t ServerSocket::bind(uint16_t port, const char * addr) {
+
+  sockaddr_in mServerAddress;
+  if (port == 0)
+    return CAPU_EINVAL;
+
+  if (mIsBound)
+    return CAPU_ERROR;
+
+  if (mServerSock == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  int32_t optval = 1;
+  setsockopt(mServerSock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));
+  memset((char *) &mServerAddress, 0x00, sizeof (mServerAddress));
+  mServerAddress.sin_family = AF_INET;
+  if (addr == NULL)
+    mServerAddress.sin_addr.s_addr = INADDR_ANY;
+  else if (inet_aton(addr, &mServerAddress.sin_addr) == 0)
+    return CAPU_SOCKET_EADDR;
+  mServerAddress.sin_port = htons(port);
+
+  int32_t res = ::bind(mServerSock, (sockaddr *) & mServerAddress, sizeof (struct sockaddr_in));
+  if (res < 0) {
+    return CAPU_SOCKET_EBIND;
+  }
+
+  mIsBound = true;
+  return CAPU_OK;
+}
+
+inline status_t ServerSocket::listen(uint8_t backlog) {
+  if (!mIsBound)
+    return CAPU_EINVAL;
+
+  if (mServerSock == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  if (::listen(mServerSock, backlog) < 0)
+    return CAPU_ERROR;
+  return CAPU_OK;
+}
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/Socket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/Socket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/Socket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/Socket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,286 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifdef SOCKET_INC_HEADER
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <errno.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#endif
+
+#ifdef SOCKET_INC_MEMBER
+private:
+int32_t mSocket;
+struct sockaddr_in mServerAddress;
+struct hostent *mServer;
+
+public:
+friend class ServerSocket;
+
+private:
+//private constructor needed in ServerSocket
+inline Socket(int32_t socket);
+#endif
+
+#ifdef SOCKET_INC_IMPL
+
+inline Socket::Socket() {
+  mSocket = -1;
+  mSocket = socket(AF_INET, SOCK_STREAM, 0);
+}
+
+inline Socket::Socket(int32_t socket) {
+  mSocket = socket;
+}
+
+inline Socket::~Socket() {
+  close();
+}
+
+inline status_t Socket::send(unsigned char * buffer, int32_t length) {
+  if ((buffer == NULL) || (length < 0)) {
+    return CAPU_EINVAL;
+  }
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  int32_t res = ::send(mSocket, buffer, length, 0);
+
+  if (res < 0) {
+    return CAPU_ERROR;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::receive(unsigned char * buffer, int32_t length, int32_t & numBytes) {
+  if ((buffer == NULL) || (length < 0)) {
+    return CAPU_EINVAL;
+  }
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  int32_t res = ::recv(mSocket, buffer, length, 0);
+
+  if (res == -1) {
+    numBytes = 0;
+    if (errno == EAGAIN)
+      return CAPU_TIMEOUT;
+    else
+      return CAPU_ERROR;
+  }
+  numBytes = res;
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::close() {
+  if (mSocket == -1) {
+    return CAPU_SOCKET_ESOCKET;
+  } else {
+    if (::close(mSocket) < 0)
+      return CAPU_SOCKET_ECLOSE;
+    mSocket = -1;
+    return CAPU_OK;
+  }
+}
+
+inline status_t Socket::connect(unsigned char * dest_addr, uint16_t port) {
+  if ((dest_addr == NULL) || (port == 0))
+    return CAPU_EINVAL;
+
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  mServer = gethostbyname((const char *) dest_addr);
+  if (mServer == NULL)
+    return CAPU_SOCKET_EADDR;
+
+  // for padding
+  memset((char *) &mServerAddress, 0x00, sizeof (mServerAddress));
+  mServerAddress.sin_family = AF_INET;
+
+  //destination ip is set
+  memcpy((char *) &mServerAddress.sin_addr.s_addr, (char *) mServer->h_addr_list[0], mServer->h_length);
+
+  //port is set
+  mServerAddress.sin_port = htons(port);
+
+  int32_t res = ::connect(mSocket, (const sockaddr *) &mServerAddress, sizeof (mServerAddress));
+
+  if (res == -1)
+    return CAPU_SOCKET_ECONNECT;
+  return CAPU_OK;
+}
+
+inline status_t Socket::setBufferSize(int32_t bufferSize) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  if (bufferSize < 0)
+    return CAPU_EINVAL;
+  if (setsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof (bufferSize)) < 0)
+    return CAPU_ERROR;
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::setLingerOption(bool_t isLinger, int32_t linger) {
+  if (linger < 0) {
+    return CAPU_EINVAL;
+  }
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  struct linger so_linger;
+  if (isLinger) {
+    so_linger.l_onoff = 1;
+    so_linger.l_linger = linger;
+  } else {
+    so_linger.l_onoff = 0;
+    so_linger.l_linger = 0;
+  }
+
+  if (setsockopt(mSocket, SOL_SOCKET, SO_LINGER, &so_linger, sizeof (so_linger)) < 0)
+    return CAPU_ERROR;
+  return CAPU_OK;
+}
+
+inline status_t Socket::setNoDelay(bool_t noDelay) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t opt;
+  if (noDelay)
+    opt = 1;
+  else
+    opt = 0;
+  if (setsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof (opt)) < 0)
+    return CAPU_ERROR;
+  return CAPU_OK;
+}
+
+inline status_t Socket::setKeepAlive(bool_t keepAlive) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t opt;
+  if (keepAlive)
+    opt = 1;
+  else
+    opt = 0;
+  if (setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof (opt)) < 0)
+    return CAPU_ERROR;
+  return CAPU_OK;
+}
+
+inline status_t Socket::setTimeout(int32_t timeout) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  struct timeval _timeout;
+  _timeout.tv_sec = timeout;
+  _timeout.tv_usec = 0;
+
+  if (setsockopt(mSocket, SOL_SOCKET, SO_RCVTIMEO, &_timeout, sizeof (_timeout)) < 0)
+    return CAPU_ERROR;
+  if (setsockopt(mSocket, SOL_SOCKET, SO_SNDTIMEO, &_timeout, sizeof (_timeout)) < 0)
+    return CAPU_ERROR;
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getBufferSize(int32_t& bufferSize) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t size;
+  socklen_t len = sizeof (size);
+  if (getsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, &size, &len) < 0)
+    return CAPU_ERROR;
+  bufferSize = size;
+  return CAPU_OK;
+}
+
+inline status_t Socket::getLingerOption(bool_t& isLinger, int32_t& _linger) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  struct linger so_linger;
+
+  socklen_t len = sizeof (so_linger);
+  if (getsockopt(mSocket, SOL_SOCKET, SO_LINGER, &so_linger, &len) < 0)
+    return CAPU_ERROR;
+  _linger = so_linger.l_linger;
+  if (so_linger.l_onoff == 1)
+    isLinger = true;
+  else
+    isLinger = false;
+  return CAPU_OK;
+}
+
+inline status_t Socket::getNoDelay(bool_t& noDelay) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t opt;
+  socklen_t len = sizeof (opt);
+  if (getsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, &opt, &len) < 0)
+    return CAPU_ERROR;
+
+  if (opt == 1)
+    noDelay = true;
+  else
+    noDelay = false;
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getKeepAlive(bool_t& keepAlive) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  int32_t opt;
+
+  socklen_t len = sizeof (opt);
+  if (getsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &opt, &len) < 0)
+    return CAPU_ERROR;
+
+  if (opt == 1)
+    keepAlive = true;
+  else
+    keepAlive = false;
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getTimeout(int32_t& timeout) {
+  if (mSocket == -1)
+    return CAPU_SOCKET_ESOCKET;
+
+  struct timeval _timeout;
+
+  socklen_t len = sizeof (_timeout);
+
+  if (getsockopt(mSocket, SOL_SOCKET, SO_RCVTIMEO, &_timeout, &len) < 0)
+    return CAPU_ERROR;
+
+  timeout = _timeout.tv_sec;
+
+  return CAPU_OK;
+}
+#endif
+
+

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/ServerSocket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/ServerSocket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/ServerSocket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/ServerSocket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,23 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifdef ARCH_LINUX
+#include "Linux/ServerSocket.inc"
+#elif ARCH_WIN32
+#include "Win32/ServerSocket.inc"
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Socket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Socket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Socket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Socket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,28 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+
+
+#ifdef ARCH_LINUX
+#include "Linux/Socket.inc"
+#elif ARCH_WIN32
+#include "Win32/Socket.inc"
+#endif
+
+
+

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/ServerSocket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/ServerSocket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/ServerSocket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/ServerSocket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,139 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifdef SERVER_SOCKET_INC_HEADER
+#define _WINSOCKAPI_
+// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
+#pragma comment (lib, "Ws2_32.lib")
+#pragma comment (lib, "Mswsock.lib")
+#pragma comment (lib, "AdvApi32.lib")
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+
+#ifdef SERVER_SOCKET_INC_MEMBER
+  SOCKET mServerSocket;
+  WSADATA mWsaData;
+  bool_t mIsBound;
+#endif
+
+#ifdef SERVER_SOCKET_INC_IMPL
+inline ServerSocket::ServerSocket()
+{
+  mServerSocket = INVALID_SOCKET;
+  mIsBound = false;
+  //Initialize Winsock
+  int32_t result = WSAStartup(MAKEWORD(2,2), &mWsaData);
+  if (result == 0) {
+    //create the socket which is used to connect the server
+    mServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    if (mServerSocket != INVALID_SOCKET) {
+      int32_t optVal = 1;
+      setsockopt(mServerSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&optVal, sizeof(optVal));
+    }
+  } else {
+    WSACleanup();
+    mServerSocket = INVALID_SOCKET;
+  }
+}
+
+inline ServerSocket::~ServerSocket()
+{
+  close();
+}
+
+inline Socket* ServerSocket::accept()
+{
+  Socket *clientSocket = new Socket(::accept(mServerSocket, NULL, NULL));
+  return clientSocket;
+}
+
+inline status_t ServerSocket::close()
+{
+  int32_t returnValue = CAPU_OK;
+  if (mServerSocket == INVALID_SOCKET) {
+    returnValue = CAPU_SOCKET_ESOCKET;
+  } else {
+    if (closesocket(mServerSocket) != 0) {
+      int32_t result = WSAGetLastError();
+      if (result != WSANOTINITIALISED){ //socket has already been closed
+        returnValue = CAPU_SOCKET_ECLOSE;
+      }
+    }
+  }
+  mServerSocket = INVALID_SOCKET;
+  mIsBound = false;
+  WSACleanup();
+  return returnValue;
+}
+
+
+inline status_t ServerSocket::bind(uint16_t port, const char *addr) {
+  if (port == 0) {
+    return CAPU_EINVAL;
+  }
+
+  //check if the address is valid
+
+  if (mIsBound) {
+    return CAPU_ERROR;
+  }
+
+  if (mServerSocket == INVALID_SOCKET)
+    return CAPU_SOCKET_ESOCKET;
+
+  sockaddr_in socketAddr;
+  memset((char*) &socketAddr, 0x00, sizeof(socketAddr));
+  socketAddr.sin_family = AF_INET;
+  if (addr == NULL) {
+    socketAddr.sin_addr.s_addr = INADDR_ANY;
+  } else if (inet_addr(addr) == INADDR_NONE) {
+    return CAPU_SOCKET_EADDR;
+  } else {
+    socketAddr.sin_addr.s_addr = inet_addr(addr);
+  }
+  socketAddr.sin_port = htons(port);
+
+  int32_t result = ::bind(mServerSocket, (sockaddr*) &socketAddr, sizeof(socketAddr));
+  if (result == SOCKET_ERROR) {
+    return CAPU_SOCKET_EBIND;
+  }
+
+  mIsBound = true;
+  return CAPU_OK;
+}
+
+inline status_t ServerSocket::listen(uint8_t backlog) {
+  if (!mIsBound) {
+    return CAPU_EINVAL;
+  }
+
+  if (mServerSocket == -1) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  int32_t result = ::listen(mServerSocket, backlog);
+
+  if (result == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  return CAPU_OK;
+}
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/Socket.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/Socket.inc?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/Socket.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/Socket.inc Wed Feb  8 09:14:09 2012
@@ -0,0 +1,346 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifdef SOCKET_INC_HEADER
+#define _WINSOCKAPI_
+// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
+#pragma comment (lib, "Ws2_32.lib")
+#pragma comment (lib, "Mswsock.lib")
+#pragma comment (lib, "AdvApi32.lib")
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+
+#ifdef SOCKET_INC_MEMBER
+public:
+  friend class ServerSocket;
+private:
+  SOCKET mSocket;
+  WSADATA mWsaData;
+
+  //private constructor needed in ServerSocket
+  inline Socket(SOCKET socket);
+#endif
+
+#ifdef SOCKET_INC_IMPL
+
+inline Socket::Socket() {
+  //Initialize Winsock
+  int32_t result = WSAStartup(MAKEWORD(2,2), &mWsaData);
+  if (result == 0) {
+    //create the socket which is used to connect the server
+    mSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    if (mSocket == INVALID_SOCKET) {
+      WSACleanup();
+    }
+  } else {
+    mSocket = INVALID_SOCKET;
+  }
+}
+
+inline Socket::Socket(SOCKET socket) {
+  //Initialize Winsock
+  int32_t result = WSAStartup(MAKEWORD(2,2), &mWsaData);
+  if (result == 0) {
+    //create the socket which is used to connect the server
+    mSocket = socket;
+    if (mSocket == INVALID_SOCKET) {
+      WSACleanup();
+    }
+  } else {
+    mSocket = INVALID_SOCKET;
+  }
+}
+
+inline Socket::~Socket() {
+  close();
+}
+
+inline status_t Socket::send(unsigned char *buffer, int32_t length) {
+  if ((buffer == NULL) || (length < 0)) {
+    return CAPU_EINVAL;
+  }
+
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  int32_t result;
+  result = ::send(mSocket, (const char*)buffer, length, 0);
+  if (result == SOCKET_ERROR) {
+    close();
+    return CAPU_ERROR;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::receive(unsigned char *buffer, int32_t length, int32_t &numBytes) {
+  if ((buffer == NULL) || (length < 0)) {
+    return CAPU_EINVAL;
+  }
+
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  int32_t result = recv(mSocket, (char*)buffer, length, 0);
+  if (result == SOCKET_ERROR) {
+    numBytes = 0;
+    result = WSAGetLastError();
+    if (result == WSAETIMEDOUT) {
+      return CAPU_TIMEOUT;
+    }
+    else{
+      return CAPU_ERROR;
+    }
+  }
+  numBytes = result;
+  return CAPU_OK;
+}
+
+inline status_t Socket::close() {
+  int32_t returnValue = CAPU_OK;
+  if (mSocket == INVALID_SOCKET) {
+    returnValue = CAPU_SOCKET_ESOCKET;
+  } else {
+    int32_t result = closesocket(mSocket);
+    if (result != 0) {
+      result = WSAGetLastError();
+      if (result != WSANOTINITIALISED){ //socket has already been closed
+        returnValue = CAPU_SOCKET_ECLOSE;
+      }
+    }
+  }
+  mSocket = INVALID_SOCKET;
+  WSACleanup();
+  return returnValue;
+}
+
+inline status_t Socket::connect(unsigned char *dest_addr, uint16_t port) {
+
+  //check parameters
+  if ((dest_addr == NULL) || (port == 0)) {
+    return CAPU_EINVAL;
+  }
+
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  struct hostent* serverHost = gethostbyname((const char *) dest_addr);
+  if (serverHost == NULL) {
+    return CAPU_SOCKET_EADDR;
+  }
+
+  struct sockaddr_in serverAddress;
+  memset((char *) &serverAddress, 0x00, sizeof (serverAddress));
+
+  serverAddress.sin_family = AF_INET;
+  memcpy((char *) &serverAddress.sin_addr.s_addr, (char *) serverHost->h_addr_list[0], serverHost->h_length);
+  serverAddress.sin_port = htons(port);
+
+  int32_t result = ::connect(mSocket, (sockaddr *) &serverAddress, sizeof (serverAddress));
+  if (result == SOCKET_ERROR) {
+    int32_t errorCode = WSAGetLastError();
+    close();
+    return CAPU_SOCKET_ECONNECT;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::setBufferSize(int32_t bufferSize) {
+  if (bufferSize < 0) {
+    return CAPU_EINVAL;
+  }
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  if (setsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, (char*)&bufferSize, sizeof(bufferSize)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::setLingerOption(bool_t isLinger, int32_t linger) {
+  if (linger < 0) {
+    return CAPU_EINVAL;
+  }
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  struct linger soLinger;
+  if (isLinger) {
+    soLinger.l_onoff = 1;
+    soLinger.l_linger = linger;
+  } else {
+    soLinger.l_onoff = 0;
+    soLinger.l_linger = 0;
+  }
+
+  if (setsockopt(mSocket, SOL_SOCKET, SO_LINGER, (char*)&soLinger, sizeof(soLinger)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::setNoDelay(bool_t noDelay) {
+  if (mSocket == INVALID_SOCKET)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t opt;
+  if (noDelay) {
+    opt = 1;
+  } else {
+    opt = 0;
+  }
+  if (setsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(opt)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::setKeepAlive(bool_t keepAlive) {
+  if (mSocket == INVALID_SOCKET)
+    return CAPU_SOCKET_ESOCKET;
+  int32_t opt;
+  if (keepAlive) {
+    opt = 1;
+  } else {
+    opt = 0;
+  }
+  if (setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&opt, sizeof(opt)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+  return CAPU_OK;
+}
+
+inline status_t Socket::setTimeout(int32_t timeout) {
+  if (mSocket == INVALID_SOCKET)
+    return CAPU_SOCKET_ESOCKET;
+
+  struct timeval soTimeout;
+  soTimeout.tv_sec = timeout;
+  soTimeout.tv_usec = 0;
+
+  if (setsockopt(mSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&soTimeout, sizeof(soTimeout)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+  if (setsockopt(mSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&soTimeout, sizeof(soTimeout)) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getBufferSize(int32_t& bufferSize) {
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  socklen_t len = sizeof (bufferSize);
+  if (getsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, (char*)&bufferSize, &len) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getLingerOption(bool_t& isLinger, int32_t& _linger) {
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  struct linger soLinger;
+  int32_t len = sizeof (soLinger);
+
+  if (getsockopt(mSocket, SOL_SOCKET, SO_LINGER, (char*)&soLinger, &len) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  _linger = soLinger.l_linger;
+
+  if (soLinger.l_onoff == 1) {
+    isLinger = true;
+  } else {
+    isLinger = false;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getNoDelay(bool_t& noDelay) {
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  int32_t opt = 0;
+  socklen_t len = sizeof(opt);
+
+  if (getsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, &len) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  if (opt == 1) {
+    noDelay = true;
+  } else {
+    noDelay = false;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getKeepAlive(bool_t& keepAlive) {
+  if (mSocket == INVALID_SOCKET) {
+    return CAPU_SOCKET_ESOCKET;
+  }
+
+  int32_t opt = 0;
+  int32_t len = sizeof (opt);
+
+  if (getsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&opt, &len) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  if (opt == 1) {
+    keepAlive = true;
+  } else {
+    keepAlive = false;
+  }
+
+  return CAPU_OK;
+}
+
+inline status_t Socket::getTimeout(int32_t& timeout) {
+  if (mSocket == INVALID_SOCKET)
+    return CAPU_SOCKET_ESOCKET;
+
+  struct timeval soTimeout;
+  socklen_t len = sizeof(soTimeout);
+
+  if (getsockopt(mSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&soTimeout, &len) == SOCKET_ERROR) {
+    return CAPU_ERROR;
+  }
+
+  timeout = soTimeout.tv_sec;
+
+  return CAPU_OK;
+}
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/ServerSocketTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/ServerSocketTest.cpp?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/ServerSocketTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/ServerSocketTest.cpp Wed Feb  8 09:14:09 2012
@@ -0,0 +1,71 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#include <gtest/gtest.h>
+#include "capu/os/ServerSocket.h"
+#include "capu/Error.h"
+
+TEST(ServerSocket, CloseTest) {
+  capu::ServerSocket *socket = new capu::ServerSocket();
+  //try to close serversocket
+  EXPECT_TRUE(socket->close() == capu::CAPU_OK);
+  //try to close already closed socket
+  EXPECT_TRUE(socket->close() == capu::CAPU_SOCKET_ESOCKET);
+  delete socket;
+}
+
+TEST(ServerSocket, BindTest) {
+  capu::ServerSocket *socket = new capu::ServerSocket();
+  //try to bind on a invalid port
+  EXPECT_TRUE(socket->bind(0, "0.0.0.0") == capu::CAPU_EINVAL);
+
+  //try to bind on a valid port and address
+  EXPECT_TRUE(socket->bind(5000) == capu::CAPU_OK);
+  //call bind twice
+  EXPECT_TRUE(socket->bind(5000, "127.0.0.1") == capu::CAPU_ERROR);
+
+  delete socket;
+
+  socket = new capu::ServerSocket();
+  //try to bind 5 digit port
+  EXPECT_TRUE(socket->bind(15000, "0.0.0.0") == capu::CAPU_OK);
+  EXPECT_TRUE(socket->close() == capu::CAPU_OK);
+  delete socket;
+
+  socket = new capu::ServerSocket();
+  //expect true if address is wrong
+  EXPECT_TRUE(socket->bind(5000, "0.0.0.600") == capu::CAPU_SOCKET_EADDR);
+  EXPECT_TRUE(socket->close() == capu::CAPU_OK);
+  //deallocate socket
+  delete socket;
+}
+
+TEST(ServerSocket, ListenTest) {
+  capu::ServerSocket *socket = new capu::ServerSocket();
+
+  //try to start listening on a unbound socket
+  EXPECT_TRUE(socket->listen(3) == capu::CAPU_EINVAL);
+
+  //faulty bind and listen
+  EXPECT_TRUE(socket->bind(3000, "0.0.0.2564") == capu::CAPU_SOCKET_EADDR);
+  EXPECT_TRUE(socket->listen(3) == capu::CAPU_EINVAL);
+  //bind and listen in a correct way
+  EXPECT_TRUE(socket->bind(3000, "0.0.0.0") == capu::CAPU_OK);
+  EXPECT_TRUE(socket->listen(3) == capu::CAPU_OK);
+  delete socket;
+}

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/SocketTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/SocketTest.cpp?rev=1241827&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/SocketTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/SocketTest.cpp Wed Feb  8 09:14:09 2012
@@ -0,0 +1,272 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#include <gtest/gtest.h>
+#include "capu/os/ServerSocket.h"
+#include "capu/os/Thread.h"
+#include "capu/os/Socket.h"
+
+class ThreadClientTest {
+public:
+  //client thread to test data exchange between client and server
+
+  inline void operator()(void * param) {
+    capu::int32_t communication_variable;
+    capu::int32_t numBytes = 0;
+    //ALLOCATION AND SYNCH OF cient and  server
+    capu::Socket *cli_socket = new capu::Socket();
+    capu::Thread::Sleep(2000);
+    //TRY TO CONNECT TO IPV6
+    EXPECT_TRUE(cli_socket->connect((unsigned char *) "::1", 5000) == capu::CAPU_SOCKET_EADDR);
+    //connects to he given id
+    EXPECT_TRUE(cli_socket->connect((unsigned char *) "localhost", 5000) == capu::CAPU_OK);
+    capu::int32_t i = 5;
+    //send data
+    EXPECT_TRUE(cli_socket->send((unsigned char*) &i, sizeof (capu::int32_t)) == capu::CAPU_OK);
+    //receive
+    EXPECT_TRUE(cli_socket->receive((unsigned char *) &communication_variable, sizeof (capu::int32_t), numBytes) == capu::CAPU_OK);
+    //CHECK VALUE
+    EXPECT_TRUE(communication_variable == 6);
+    //socket close
+    EXPECT_TRUE(cli_socket->close() == capu::CAPU_OK);
+    //deallocating
+    delete cli_socket;
+  }
+};
+
+class ThreadTimeoutClientTest {
+public:
+  //timeout test
+
+  inline void operator()(void * param) {
+    capu::int32_t communication_variable;
+    capu::int32_t numBytes = 0;
+    //ALLOCATION AND SYNCH OF cient and  server
+    capu::Socket *cli_socket = new capu::Socket();
+    capu::Thread::Sleep(2000);
+    //timeout is 5 second;
+    cli_socket->setTimeout(5);
+    //TRY TO CONNECT TO IPV6
+    EXPECT_TRUE(cli_socket->connect((unsigned char *) "::1", 5000) == capu::CAPU_SOCKET_EADDR);
+    //connects to he given id
+    EXPECT_TRUE(cli_socket->connect((unsigned char *) "localhost", 5000) == capu::CAPU_OK);
+    capu::int32_t i = 5;
+    //send data
+    EXPECT_TRUE(cli_socket->send((unsigned char*) &i, sizeof (capu::int32_t)) == capu::CAPU_OK);
+    //receive
+    EXPECT_TRUE(cli_socket->receive((unsigned char *) &communication_variable, sizeof (capu::int32_t), numBytes) == capu::CAPU_TIMEOUT);
+    //socket close
+    EXPECT_TRUE(cli_socket->close() == capu::CAPU_OK);
+    //deallocating
+    delete cli_socket;
+  }
+};
+
+class ThreadServerTest {
+public:
+  //SERVER thread to test data exchange between client and server
+
+  inline void operator()(void * param) {
+    capu::int32_t communication_variable;
+    capu::int32_t numBytes = 0;
+    //server socket allocation
+    capu::ServerSocket *socket = new capu::ServerSocket();
+
+    //bind to given address
+    EXPECT_TRUE(socket->bind(5000, "0.0.0.0") == capu::CAPU_OK);
+    //start listening
+    EXPECT_TRUE(socket->listen(5) == capu::CAPU_OK);
+    //accept connection
+    capu::Socket *new_socket = socket->accept();
+    //receive data
+    EXPECT_TRUE(new_socket->receive((unsigned char *) &communication_variable, sizeof (capu::int32_t), numBytes) != -1);
+    //CHECK VALUE
+    EXPECT_TRUE(communication_variable == 5);
+    //update data
+    communication_variable++;
+    //send it back
+    capu::Thread::Sleep(4000);
+    EXPECT_TRUE(new_socket->send((unsigned char *) &communication_variable, sizeof (capu::int32_t)) == capu::CAPU_OK);
+    //close session
+    EXPECT_TRUE(new_socket->close() == capu::CAPU_OK);
+    //deallocate session identifier
+    delete new_socket;
+    EXPECT_EQ(capu::CAPU_OK, socket->close());
+    delete socket;
+  }
+};
+
+class ThreadTimeoutServerTest {
+public:
+  //timeout test
+
+  inline void operator()(void * param) {
+    capu::int32_t communication_variable;
+    capu::int32_t numBytes = 0;
+    //server socket allocation
+    capu::ServerSocket *socket = new capu::ServerSocket();
+
+    //bind to given address
+    EXPECT_TRUE(socket->bind(5000, "0.0.0.0") == capu::CAPU_OK);
+    //start listening
+    EXPECT_TRUE(socket->listen(5) == capu::CAPU_OK);
+    //accept connection
+    capu::Socket *new_socket = socket->accept();
+    //receive data
+    EXPECT_TRUE(new_socket->receive((unsigned char *) &communication_variable, sizeof (capu::int32_t), numBytes) != -1);
+    //CHECK VALUE
+    EXPECT_TRUE(communication_variable == 5);
+    //update data
+    communication_variable++;
+    //send it back
+    capu::Thread::Sleep(9000);
+    //close session
+    EXPECT_TRUE(new_socket->close() == capu::CAPU_OK);
+    //deallocate session identifier
+    delete new_socket;
+    EXPECT_EQ(capu::CAPU_OK, socket->close());
+    delete socket;
+  }
+};
+
+TEST(Socket, ConnectTest) {
+  capu::Socket *socket = new capu::Socket();
+  //pass null
+  EXPECT_TRUE(socket->connect(NULL, 5) == capu::CAPU_EINVAL);
+
+  EXPECT_TRUE(socket->connect((unsigned char *)"www.test", 5) == capu::CAPU_SOCKET_EADDR);
+  
+  delete socket;
+}
+
+TEST(Socket, CloseReceiveAndSendTest) {
+  capu::Socket *socket = new capu::Socket();
+  capu::int32_t i = 0;
+  capu::int32_t numBytes = 0;
+  EXPECT_TRUE(socket->close() == capu::CAPU_OK);
+  //try to send data via closed socket
+  EXPECT_TRUE(socket->send((unsigned char *) "asda", 4) == capu::CAPU_SOCKET_ESOCKET);
+  //try to receive data from closed socket
+  EXPECT_TRUE(socket->receive((unsigned char *) &i, 4, numBytes) == capu::CAPU_SOCKET_ESOCKET);
+  //Deallocation of socket
+  delete socket;
+}
+
+TEST(Socket, SetAndGetPropertiesTest) {
+  capu::Socket *socket = new capu::Socket();
+  capu::ServerSocket *serverSocket = new capu::ServerSocket();
+  //TRY TO CHANGE THE PROPERTIES OF NOT CONNECTED SOCKET
+  EXPECT_TRUE(socket->setBufferSize(1024) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setKeepAlive(true) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setLingerOption(true, 90) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setNoDelay(false) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setTimeout(90) == capu::CAPU_OK);
+
+  capu::int32_t int_tmp;
+  capu::bool_t bool_tmp;
+
+  //CHECK THE PROPERTIES ARE CORRECTLY SET
+  EXPECT_TRUE(socket->getBufferSize(int_tmp) == capu::CAPU_OK);
+
+
+  //On Linux the kernel adjust the buffer size and set it to doubles of given size (at least)
+  //therefore we have to check here for >=
+  EXPECT_TRUE(int_tmp >= 1024);
+  EXPECT_TRUE(socket->getKeepAlive(bool_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(bool_tmp == true);
+  EXPECT_TRUE(socket->getLingerOption(bool_tmp, int_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(int_tmp == 90);
+  EXPECT_TRUE(bool_tmp == true);
+  EXPECT_TRUE(socket->getNoDelay(bool_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(bool_tmp == false);
+  EXPECT_TRUE(socket->getTimeout(int_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(int_tmp == 90);
+
+
+  serverSocket->bind(5000, "0.0.0.0");
+  serverSocket->listen(3);
+
+  socket->connect((unsigned char *) "127.0.0.1", 5000);
+
+  //TRY TO CHANGE THE PROPERTIES OF CONNECTED SOCKET
+  EXPECT_TRUE(socket->setBufferSize(2024) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setKeepAlive(false) == capu::CAPU_OK);
+
+  EXPECT_TRUE(socket->setLingerOption(false, 0) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setNoDelay(true) == capu::CAPU_OK);
+  EXPECT_TRUE(socket->setTimeout(92) == capu::CAPU_OK);
+
+  //CHECK THE PROPERTIES ARE CORRECTLY SET
+  EXPECT_TRUE(socket->getBufferSize(int_tmp) == capu::CAPU_OK);
+  //kernel adjust the buffer size and set it to doubles of given size (at least)
+
+  EXPECT_TRUE(int_tmp >= 2024);
+  EXPECT_TRUE(socket->getKeepAlive(bool_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(bool_tmp == false);
+  EXPECT_TRUE(socket->getLingerOption(bool_tmp, int_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(bool_tmp == false);
+  EXPECT_TRUE(socket->getNoDelay(bool_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(bool_tmp == true);
+  EXPECT_TRUE(socket->getTimeout(int_tmp) == capu::CAPU_OK);
+  EXPECT_TRUE(int_tmp == 92);
+
+  socket->close();
+  //TRY TO CHANGE THE PROPERTIES OF CLOSED SOCKET
+  EXPECT_TRUE(socket->setBufferSize(1024) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->setKeepAlive(true) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->setLingerOption(true, 90) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->setNoDelay(false) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->setTimeout(90) == capu::CAPU_SOCKET_ESOCKET);
+  //TRY TO GET PROPERTIES OF CLOSED SOCKET
+  EXPECT_TRUE(socket->getBufferSize(int_tmp) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->getKeepAlive(bool_tmp) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->getLingerOption(bool_tmp, int_tmp) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->getNoDelay(bool_tmp) == capu::CAPU_SOCKET_ESOCKET);
+  EXPECT_TRUE(socket->getTimeout(int_tmp) == capu::CAPU_SOCKET_ESOCKET);
+
+  delete socket;
+  delete serverSocket;
+}
+
+TEST(SocketAndServerSocket, CommunicationTest) {
+  ThreadServerTest server;
+  ThreadClientTest client;
+  capu::Thread * server_thread = new capu::Thread(&server, NULL);
+  capu::Thread * client_thread = new capu::Thread(&client, NULL);
+  //Create two threads which will behave like client and server to test functionality
+  server_thread->join();
+  client_thread->join();
+
+  delete client_thread;
+  delete server_thread;
+}
+
+TEST(SocketAndServerSocket, TimeoutTest) {
+  ThreadTimeoutServerTest server;
+  ThreadTimeoutClientTest client;
+  capu::Thread * server_thread = new capu::Thread(&server, NULL);
+  capu::Thread * client_thread = new capu::Thread(&client, NULL);
+  //Create two threads which will behave like client and server to test functionality
+  server_thread->join();
+  client_thread->join();
+
+  delete client_thread;
+  delete server_thread;
+}
+
+



Mime
View raw message