You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/03/02 08:34:19 UTC
svn commit: r1296073 - in /incubator/etch/trunk/binding-cpp/runtime: ./
include/common/ include/transport/ lib/capu/modules/capu/include/capu/
src/main/ src/main/common/ src/main/transport/ src/test/ src/test/transport/
Author: veithm
Date: Fri Mar 2 07:34:18 2012
New Revision: 1296073
URL: http://svn.apache.org/viewvc?rev=1296073&view=rev
Log:
ETCH-173 EtchTransport Layer Implementation and Tests
EtchTcpConnection, EtchTcpListener and EtchTcpOption has been implemented
Change-Id: Ia7ef1c9a693b2e54b9e8a316c33d13d3098f048a
Added:
incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchConnection.h
incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpConnection.h
incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpListener.h
incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpOption.h
incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpConnection.cpp
incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpListener.cpp
incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpOption.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpConnectionTest.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpListenerTest.cpp
Modified:
incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchError.h
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h
incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
Modified: incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/CMakeLists.txt Fri Mar 2 07:34:18 2012
@@ -29,6 +29,10 @@ SET(VLD ${ETCH_EXTERNAL_DEPENDS}/vld/1.9
# GTest
SET(GTEST ${ETCH_EXTERNAL_DEPENDS}/gtest/1.6.0)
+
+# GMock
+SET(GMOCK ${ETCH_EXTERNAL_DEPENDS}/gmock/1.6.0)
+
# Set build settings
SET(BUILD_CHECK_MEMORY FALSE)
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchError.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchError.h?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchError.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchError.h Fri Mar 2 07:34:18 2012
@@ -39,8 +39,11 @@ enum {
ETCH_SOCKET_EADDR = 10,
ETCH_ENO_MEMORY = 11,
ETCH_TIMEOUT = 12,
- ETCH_ENOT_EXIST = 13
+ ETCH_ENOT_EXIST = 13,
+ ETCH_ENOT_SUPPORTED = 14
+
//RANGE BETWEEN 0XFFFF TO OXFFFFFFF ARE ERROR CODES FOR ONLY ETCH
+
};
#endif
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h Fri Mar 2 07:34:18 2012
@@ -116,11 +116,6 @@ public:
const char* c_str() const;
/**
- * Returns c styled string.
- */
- const char* c_str();
-
- /**
* Assignment operator overloading
*/
EtchString& operator=(const EtchString &str);
Added: incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchConnection.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchConnection.h?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchConnection.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchConnection.h Fri Mar 2 07:34:18 2012
@@ -0,0 +1,115 @@
+/* $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 __ETCHCONNECTION_H__
+#define __ETCHCONNECTION_H__
+#include "transport/EtchTransport.h"
+#include "common/EtchString.h"
+#include "common/EtchObject.h"
+#include "capu/os/Thread.h"
+#include "capu/util/SmartPointer.h"
+#include "common/EtchError.h"
+#include "common/EtchInt32.h"
+#include "capu/os/Mutex.h"
+
+template <class S>
+class EtchConnection : public virtual EtchTransport<S> {
+public:
+
+ /** Source query to get the local address. */
+ static const EtchString LOCAL_ADDRESS;
+
+ /** Source query to get the remote address. */
+ static const EtchString REMOTE_ADDRESS;
+
+ /**
+ * Host name to specify to select listening on all interfaces.
+ * The value is "0.0.0.0".
+ */
+ static const EtchString ALL_INTFS;
+
+ /**
+ * Check that the receiving thread is working or not
+ * @return true if it is started
+ * false otherwise
+ */
+ virtual capu::bool_t isStarted() = 0;
+
+ /**
+ * Main function that will be executed by thread
+ * @param param parameter that will be passed to the thread
+ */
+ virtual void operator()(void * param) = 0;
+
+ /**
+ * Terminates operations on the socket.
+ */
+ virtual status_t close() = 0;
+
+ /**
+ * Sets up a newly opened socket. This may involve setting socket
+ * options and opening input and output streams.
+ */
+ virtual status_t setupSocket() = 0;
+
+protected:
+
+ /**
+ * Opens the socket of a connection.
+ * @param reconnect true if we are trying to reconnect, false if this
+ * is the first time.
+ * @param true if we should reconnect, false if we should stop.
+ * @return ETCH_OK if socket is successfully opened
+ * ETCH_ERROR otherwise
+ */
+ virtual status_t openSocket(capu::bool_t reconnect) = 0;
+
+
+ /**
+ * Performs the usual and customary operations on a socket, such
+ * as read or accept.
+ */
+ virtual status_t readSocket() = 0;
+
+ capu::Thread *mThread;
+
+ capu::bool_t mIsStarted;
+
+ static capu::Mutex mMutex;
+
+ static capu::Mutex mMutexConnection;
+};
+
+template <class S>
+const EtchString EtchConnection<S>::LOCAL_ADDRESS("LOCAL_ADDRESS");
+
+template <class S>
+const EtchString EtchConnection<S>::REMOTE_ADDRESS("REMOTE_ADDRESS");
+
+template <class S>
+const EtchString EtchConnection<S>::ALL_INTFS("0.0.0.0");
+
+template <class S>
+capu::Mutex EtchConnection<S>::mMutex;
+
+template <class S>
+capu::Mutex EtchConnection<S>::mMutexConnection;
+
+
+#endif /* ETCHCONNECTION_H */
+
Added: incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpConnection.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpConnection.h?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpConnection.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpConnection.h Fri Mar 2 07:34:18 2012
@@ -0,0 +1,155 @@
+/* $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 __ETCHTCPCONNECTION_H__
+#define __ETCHTCPCONNECTION_H__
+#include "transport/EtchConnection.h"
+#include "common/EtchObject.h"
+#include "common/EtchError.h"
+#include "common/EtchString.h"
+#include "util/EtchURL.h"
+#include "util/EtchResources.h"
+#include "transport/EtchTransportData.h"
+#include "common/EtchSocket.h"
+#include "transport/EtchFlexBuffer.h"
+#include "transport/EtchSessionData.h"
+#include "transport/EtchTcpOption.h"
+
+/**
+ * Implementation of connection which handles a tcp connection.
+ */
+
+class EtchTcpConnection : public EtchConnection<EtchSessionData>, public EtchTransportData {
+public:
+
+ /**
+ * Constructs the Connection from a uri.
+ * @param socket
+ * @param uri
+ * @param resources
+ */
+ EtchTcpConnection(EtchSocket* socket, EtchURL* uri);
+
+ /**
+ * Destructor
+ */
+ ~EtchTcpConnection();
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportControl(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value);
+
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportData(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchFlexBuffer> buf);
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportNotify(capu::SmartPointer<EtchObject> event);
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportQuery(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result);
+
+ /**
+ * @return Session
+ */
+ virtual EtchSessionData* getSession();
+
+ /**
+ * Sets session
+ */
+ virtual void setSession(EtchSessionData* session);
+
+
+ /**
+ * @see EtchConnection
+ */
+ virtual capu::bool_t isStarted();
+
+ /**
+ * @see capu::thread
+ */
+ virtual void operator()(void * param);
+
+ /**
+ * @see EtchConnection
+ */
+ virtual status_t close();
+
+ /**
+ * @see EtchConnection
+ */
+ virtual status_t setupSocket();
+
+ /**
+ * Sends some data to the remote end. If the connection is buffered, the
+ * data is saved until the buffer is full or the buffer is flushed. If the
+ * connection is not buffered, the data is sent to the operating system
+ * immediately. The operating system may or may not transmit the data
+ * immediately, depending upon the noDelay setting.
+ * @param buf the bytes to be sent.
+ * @param off the offset into buf of the first byte to send.
+ * @param len the number of bytes to send.
+ * @return ETCH_OK if the sent is successful
+ * ETCH_EINVAL if the buffer is NULL or socket is not opened
+ * ETCH_ERROR otherwise
+ */
+ status_t send(capu::int8_t* buf, capu::uint32_t off, capu::uint32_t len);
+
+ /**
+ * Sets the number of connection attempts
+ * @param numAttempts
+ */
+ void setNumAttempts(capu::uint8_t numAttempts);
+
+private:
+ /**
+ * The session for the connection.
+ */
+ EtchSessionData* mSession;
+
+ //PORT
+ capu::uint16_t mPort;
+
+ //HOST ADDR
+ EtchString mHost;
+
+ //SOCKET
+ EtchSocket* mSocket;
+
+ //SOCKET OPTIONS
+ EtchTcpOption mOptions;
+
+ capu::uint8_t mNumAttempts;
+
+protected:
+
+ status_t readSocket();
+
+ status_t openSocket(capu::bool_t reconnect);
+
+};
+
+#endif /* ETCHTCPCONNECTION_H */
+
Added: incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpListener.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpListener.h?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpListener.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpListener.h Fri Mar 2 07:34:18 2012
@@ -0,0 +1,129 @@
+/* $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 __ETCHTCPLISTENER_H__
+#define __ETCHTCPLISTENER_H__
+#include "common/EtchError.h"
+#include "common/EtchObject.h"
+#include "capu/util/SmartPointer.h"
+#include "transport/EtchTransport.h"
+#include "transport/EtchSessionListener.h"
+#include "common/EtchSocket.h"
+#include "transport/EtchConnection.h"
+#include "capu/os/ServerSocket.h"
+#include "capu/os/Socket.h"
+#include "common/EtchServerSocket.h"
+#include "util/EtchURL.h"
+#include "util/EtchResources.h"
+
+/**
+ * Implementation of a connection which handles a socket listener.
+ */
+class EtchTcpListener
+: public EtchConnection<EtchSessionListener<EtchSocket> >,
+public virtual EtchTransport<EtchSessionListener<EtchSocket> > {
+public:
+
+ /**
+ * Default Constructor
+ */
+ EtchTcpListener(EtchURL *url);
+
+ /**
+ * Destructor
+ */
+ ~EtchTcpListener();
+
+ virtual capu::bool_t isStarted();
+
+ virtual status_t close();
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportControl(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value);
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportNotify(capu::SmartPointer<EtchObject> event);
+
+ /**
+ * @see TransportData
+ */
+ virtual status_t transportQuery(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result);
+
+ /**
+ * @return Session
+ */
+ virtual EtchSessionListener<EtchSocket>* getSession();
+
+ /**
+ * Sets session
+ */
+ virtual void setSession(EtchSessionListener<EtchSocket>* session);
+
+ virtual void operator()(void * param);
+
+ /**
+ * @see EtchConnection
+ */
+ virtual status_t setupSocket();
+
+protected:
+
+ /**
+ * @see EtchConnection
+ */
+ virtual status_t openSocket(capu::bool_t reconnect);
+
+ /**
+ * @see EtchConnection
+ */
+ virtual status_t readSocket();
+
+private:
+ /**
+ * The session for the connection.
+ */
+ EtchSessionListener<EtchSocket>* mSession;
+
+ EtchServerSocket* mSocket;
+
+ capu::uint16_t mPort;
+
+ capu::uint8_t mBackLog;
+
+
+public:
+ /**
+ * Query term for URI to specify backlog value to ServerSocket. The value
+ * is "TcpListener.backlog". The backlog must be >= 0 and somewhat
+ * reasonable. The value 0 specifies the system default, the same as if you
+ * leave the term off altogether.
+ *
+ * Example: tcp://host:port?TcpListener.backlog=250
+ *
+ * @see #TcpListener(String, Resources)
+ */
+ const static EtchString BACKLOG;
+};
+
+#endif /* ETCHTCPLISTENER_H */
+
Added: incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpOption.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpOption.h?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpOption.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/transport/EtchTcpOption.h Fri Mar 2 07:34:18 2012
@@ -0,0 +1,105 @@
+/* $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 __ETCHTCPOPTION_H__
+#define __ETCHTCPOPTION_H__
+
+#include "common/EtchString.h"
+#include "util/EtchURL.h"
+
+class EtchTcpOption {
+public:
+
+ EtchTcpOption(EtchURL *url);
+
+ virtual ~EtchTcpOption();
+
+ /**
+ * Term on the uri which specifies the output buffer size in bytes. The term
+ * string is "TcpTransport.bufferSize". The value is an integer between 0
+ * and 65536. The default is 0, which means no output buffering.
+ */
+ const static EtchString BUFFER_SIZE;
+
+ /**
+ * Term on the uri which specifies the keep alive flag. The term string is
+ * "TcpTransport.keepAlive". The value is "true" or "false". The default is
+ * "false". Here keep alive refers to TCP specified keep alive, which is not
+ * the same as any application level keep alive.
+ */
+ const static EtchString KEEP_ALIVE;
+
+ /**
+ * Term on the uri which specifies the linger on close time in seconds. The
+ * term string is "TcpTransport.lingerTime". The value is an integer between
+ * -1 and 65535. The default is 30. The value -1 means "no linger on close".
+ * The value determines how long close will wait for buffered but
+ * unacknowledged data to be delivered. When the time expires, the
+ * connection will be forcefully closed. (The difference between a linger of
+ * -1 and a linger of 0 is subtle, but comes down to a close with a linger
+ * of -1 means perform forceful close while a close with a linger of 0 means
+ * perform a graceful close if there is no buffered data and a forceful
+ * close if there is buffered data. A forceful close is send RST, while a
+ * graceful close means send FIN and wait for FINACK.)
+ */
+ const static EtchString LINGER_TIME;
+
+ /**
+ * Term on the uri which specifies the no delay flag. The term string is
+ * "TcpTransport.noDelay". The value is "true" or "false". The default is
+ * "true". When true, the operating system will make a best effort to
+ * transmit data ASAP. When false, data might be delayed somewhat in order
+ * to allow for more efficient transmission by combining packets (see
+ * Nagle's Algorithm).
+ */
+ const static EtchString NO_DELAY;
+
+ /**
+ * Term on the uri which specifies the reconnect delay in milliseconds. The
+ * term string is "TcpTransport.reconnectDelay". The value is an integer >=
+ * 0. The default is 0. The value 0 means no automatic reconnection is
+ * desired.
+ */
+ const static EtchString RECONNECT_DELAY;
+
+private:
+
+ capu::bool_t isValidNumber(const char * str, capu::int32_t len);
+
+ capu::bool_t checkBufferSize(capu::int32_t size);
+
+ capu::bool_t checkLingerTime(capu::int32_t lingertime);
+
+ capu::bool_t checkReconnectDelay(capu::int32_t delay);
+
+public:
+
+ capu::int32_t mBufferSize;
+
+ capu::int32_t mLingerTime;
+
+ capu::int32_t mReconnectDelay;
+
+ capu::int32_t mKeepAlive;
+
+ capu::int32_t mNoDelay;
+
+};
+
+#endif /* ETCHTCPOPTION_H */
+
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h Fri Mar 2 07:34:18 2012
@@ -38,7 +38,8 @@ namespace capu {
CAPU_SOCKET_EADDR = 10,
CAPU_ENO_MEMORY = 11,
CAPU_TIMEOUT = 12,
- CAPU_ENOT_EXIST = 13
+ CAPU_ENOT_EXIST = 13,
+ CAPU_ENOT_SUPPORTED = 14
};
}
#endif
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt Fri Mar 2 07:34:18 2012
@@ -52,10 +52,14 @@ SET(MAIN_INCLUDES
${PROJECT_SOURCE_DIR}/include/transport/EtchSessionMessage.h
${PROJECT_SOURCE_DIR}/include/transport/EtchSessionPacket.h
${PROJECT_SOURCE_DIR}/include/transport/EtchWho.h
+ ${PROJECT_SOURCE_DIR}/include/transport/EtchConnection.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTransport.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTransportData.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTransportMessage.h
${PROJECT_SOURCE_DIR}/include/transport/EtchTransportPacket.h
+ ${PROJECT_SOURCE_DIR}/include/transport/EtchTcpConnection.h
+ ${PROJECT_SOURCE_DIR}/include/transport/EtchTcpListener.h
+ ${PROJECT_SOURCE_DIR}/include/transport/EtchTcpOption.h
${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
)
@@ -77,6 +81,9 @@ SET(MAIN_SOURCES
util/EtchResources.cpp
transport/EtchFlexBuffer.cpp
transport/EtchSession.cpp
+ transport/EtchTcpConnection.cpp
+ transport/EtchTcpListener.cpp
+ transport/EtchTcpOption.cpp
util/EtchUtil.cpp
)
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp Fri Mar 2 07:34:18 2012
@@ -97,13 +97,6 @@ capu::int32_t EtchString::length() const
}
const char* EtchString::c_str() const{
- capu::int32_t len = strlen(mData);
- char* tmp = new char[len + 1];
- etch_strcpy_s(tmp, len + 1, mData);
- return tmp;
-}
-
-const char* EtchString::c_str() {
return mData;
}
Added: incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpConnection.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpConnection.cpp?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpConnection.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpConnection.cpp Fri Mar 2 07:34:18 2012
@@ -0,0 +1,258 @@
+/* $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 "transport/EtchTcpConnection.h"
+
+EtchTcpConnection::EtchTcpConnection(EtchSocket* socket, EtchURL* uri) : mOptions(uri) {
+ if ((socket == NULL) && (uri != NULL)) {
+ mHost = uri->getHost();
+ mPort = uri->getPort();
+ } else {
+ mHost.set("");
+ mPort = 0;
+ }
+ mThread = NULL;
+ mIsStarted = false;
+ mSession = NULL;
+ mSocket = socket;
+}
+
+EtchTcpConnection::~EtchTcpConnection() {
+
+
+ if (mThread != NULL) {
+ mThread->join();
+ delete mThread;
+ }
+
+ if (mSocket != NULL) {
+ mSocket->close();
+ delete mSocket;
+ mSocket = NULL;
+ mIsStarted = false;
+ }
+}
+
+status_t EtchTcpConnection::send(capu::int8_t* buf, capu::uint32_t off, capu::uint32_t len) {
+ if (mSocket != NULL)
+ return mSocket->send((unsigned char *) &buf[off], len);
+ return ETCH_ERROR;
+}
+
+status_t EtchTcpConnection::readSocket() {
+ capu::SmartPointer<EtchFlexBuffer> buf = new EtchFlexBuffer(new capu::int8_t[8192], 8192);
+
+ while (mIsStarted) {
+ capu::int32_t n;
+ status_t result = mSocket->receive((unsigned char *) buf->getBuffer(), buf->getSize(), n);
+ if (result != ETCH_OK)
+ return result;
+ if (n <= 0)
+ return ETCH_ERROR;
+
+ buf->setLength(n);
+ buf->setIndex(0);
+ mSession->sessionData(NULL, buf);
+ }
+ return ETCH_OK;
+}
+
+status_t EtchTcpConnection::openSocket(capu::bool_t reconnect) {
+
+ mMutexConnection.lock();
+ // if a one time connection from a server socket listener, just
+ // return the existing socket.
+ if (!reconnect && (mSocket != NULL)) {
+ mMutexConnection.unlock();
+ return ETCH_OK;
+ }
+ //temporary socket in a listener
+ if ((reconnect == false) && (mPort == 0) && (mHost.length() == 0)) {
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+ }
+ // if a reconnect but no retries allowed, then bail.
+ if (reconnect && (mOptions.mReconnectDelay == 0)) {
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+ }
+
+ capu::bool_t first = true;
+ while (mIsStarted) {
+ // if reconnect is false and first is true, this is our
+ // very first attempt to connect. otherwise, we are trying
+ // to reconnect a broken link or establish a link where we
+ // have already failed at least once.
+
+ if (reconnect || !first) {
+ if (mOptions.mReconnectDelay == 0) {
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+ }
+ capu::Thread::Sleep(mOptions.mReconnectDelay);
+
+ if (!mIsStarted) {
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+ }
+ }
+ // try to open a socket.
+ mSocket = new EtchSocket();
+ if (mSocket->connect((unsigned char *) mHost.c_str(), mPort) == ETCH_OK) {
+ mMutexConnection.unlock();
+ return ETCH_OK;
+ } else {
+ mSocket->close();
+ delete mSocket;
+ mSocket = NULL;
+ }
+
+ //LOGGER IS NEEDED
+ }
+
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+}
+
+status_t EtchTcpConnection::transportData(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchFlexBuffer> buf) {
+ return this->send(buf->getBuffer(), buf->getIndex(), buf->getAvailableBytes());
+}
+
+status_t EtchTcpConnection::transportNotify(capu::SmartPointer<EtchObject> event) {
+ return ETCH_OK;
+}
+
+status_t EtchTcpConnection::transportQuery(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result) {
+ //Nothing to do
+ return ETCH_EUNIMPL;
+}
+
+status_t EtchTcpConnection::transportControl(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value) {
+
+ if (control->equals(&EtchTcpConnection::START)) {
+ if (mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = true;
+ mMutex.unlock();
+ mThread = new capu::Thread(this, NULL);
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpConnection::START_AND_WAIT_UP)) {
+ if (mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = true;
+ mMutex.unlock();
+ mThread = new capu::Thread(this, NULL);
+ capu::Thread::Sleep(((EtchInt32*) value.get())->get());
+ //TODO: Wait handling in one of the next releases
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpConnection::STOP)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpConnection::STOP_AND_WAIT_DOWN)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ capu::Thread::Sleep(((EtchInt32*) value.get())->get());
+ //TODO: Wait handling in one of the next releases
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpConnection::RESET)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ return ETCH_OK;
+ }
+ return ETCH_ENOT_SUPPORTED;
+}
+
+EtchSessionData* EtchTcpConnection::getSession() {
+ return mSession;
+}
+
+void EtchTcpConnection::setSession(EtchSessionData* session) {
+ mSession = session;
+}
+
+status_t EtchTcpConnection::close() {
+ if (mSocket != NULL)
+ return mSocket->close();
+ else
+ return ETCH_ERROR;
+}
+
+capu::bool_t EtchTcpConnection::isStarted() {
+ return mIsStarted;
+}
+
+void EtchTcpConnection::operator() (void* param) {
+
+ capu::bool_t first = true;
+
+ while (mIsStarted) {
+
+ if (openSocket(!first) != ETCH_OK) {
+ break;
+ }
+ status_t res = setupSocket();
+
+ if (res != ETCH_OK) {
+ close();
+ break;
+ }
+
+ if (readSocket() != ETCH_OK) {
+ close();
+ break;
+ }
+ close();
+ first = false;
+ }
+ mIsStarted = false;
+}
+
+status_t EtchTcpConnection::setupSocket() {
+ if (mSocket->setBufferSize(mOptions.mBufferSize) != ETCH_OK)
+ return ETCH_ERROR;
+ if (mSocket->setKeepAlive((mOptions.mKeepAlive != 0)) != ETCH_OK)
+ return ETCH_ERROR;
+ if (mSocket->setLingerOption((mOptions.mLingerTime >= 0), mOptions.mLingerTime) != ETCH_OK)
+ return ETCH_ERROR;
+ if (mSocket->setNoDelay((mOptions.mNoDelay != 0)) != ETCH_OK)
+ return ETCH_ERROR;
+ return ETCH_OK;
+}
Added: incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpListener.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpListener.cpp?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpListener.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpListener.cpp Fri Mar 2 07:34:18 2012
@@ -0,0 +1,207 @@
+/* $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 "transport/EtchTcpListener.h"
+
+const EtchString EtchTcpListener::BACKLOG("TcpTransportFactory.socket");
+
+EtchTcpListener::EtchTcpListener(EtchURL *url)
+: mPort(url->getPort()) {
+ EtchString str;
+ if (url->getTerms().get(BACKLOG, &str) != ETCH_OK) {
+ mBackLog = 0;
+ } else {
+ const char *c_str = str.c_str();
+ mBackLog = atoi(c_str);
+ if (mBackLog < 0) {
+ mBackLog = 0;
+ }
+ }
+ mSession = NULL;
+ mIsStarted = false;
+ mThread = NULL;
+ mSocket = NULL;
+}
+
+EtchTcpListener::~EtchTcpListener() {
+ if (mThread != NULL) {
+ delete mThread;
+ }
+ if (mSocket != NULL) {
+ mSocket->close();
+ delete mSocket;
+ mSocket = NULL;
+ }
+}
+
+capu::bool_t EtchTcpListener::isStarted() {
+ return mIsStarted;
+}
+
+status_t EtchTcpListener::close() {
+ if (mSocket != NULL)
+ return mSocket->close();
+ return ETCH_ERROR;
+}
+
+status_t EtchTcpListener::openSocket(capu::bool_t reconnect) {
+ // if a one time connection from a server socket listener, just
+ // return the existing socket.
+ mMutexConnection.lock();
+ if (!reconnect && (mSocket != NULL)) {
+ mMutexConnection.unlock();
+ return ETCH_OK;
+ }
+ while (isStarted()) {
+ // if reconnect is false and first is true, this is our
+ // very first attempt to connect. otherwise, we are trying
+ // to reconnect a broken link or establish a link where we
+ // have already failed at least once.
+ // try to open a socket.
+
+ mSocket = new EtchServerSocket();
+ if (mSocket->bind(mPort) == ETCH_OK) {
+ if (mSocket->listen(mBackLog) == ETCH_OK) {
+ mMutexConnection.unlock();
+ return ETCH_OK;
+ } else {
+ mSocket->close();
+ delete mSocket;
+ mSocket = NULL;
+ }
+ }
+ //LOGGER IS NEEDED
+ }
+ mMutexConnection.unlock();
+ return ETCH_ERROR;
+}
+
+status_t EtchTcpListener::readSocket() {
+ EtchSocket* s = NULL;
+ while (isStarted()) {
+ s = mSocket->accept();
+ if (s == NULL)
+ break;
+ if (mSession != NULL) {
+
+ mSession->sessionAccepted(s);
+ } else {
+ delete s;
+ return ETCH_ERROR;
+ }
+ }
+ return ETCH_OK;
+}
+
+status_t EtchTcpListener::transportControl(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value) {
+
+ if (control->equals(&EtchTcpListener::START)) {
+ if (mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = true;
+ mMutex.unlock();
+ mThread = new capu::Thread(this, NULL);
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpListener::START_AND_WAIT_UP)) {
+ if (mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = true;
+ mMutex.unlock();
+ mThread = new capu::Thread(this, NULL);
+ capu::Thread::Sleep(((EtchInt32*) value.get())->get());
+ //TODO: Wait handling in one of the next releases
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpListener::STOP)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpListener::STOP_AND_WAIT_DOWN)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ capu::Thread::Sleep(((EtchInt32*) value.get())->get());
+ //TODO: Wait handling in one of the next releases
+ return ETCH_OK;
+ }
+
+ if (control->equals(&EtchTcpListener::RESET)) {
+ if (!mIsStarted)
+ return ETCH_OK;
+ mMutex.lock();
+ mIsStarted = false;
+ mMutex.unlock();
+ close();
+ return ETCH_OK;
+ }
+ return ETCH_ENOT_SUPPORTED;
+}
+
+status_t EtchTcpListener::transportNotify(capu::SmartPointer<EtchObject> event) {
+ return ETCH_OK;
+}
+
+status_t EtchTcpListener::transportQuery(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result) {
+ //Nothing to do
+ return ETCH_EUNIMPL;
+}
+
+EtchSessionListener<EtchSocket>* EtchTcpListener::getSession() {
+ return mSession;
+}
+
+void EtchTcpListener::setSession(EtchSessionListener<EtchSocket>* session) {
+ mSession = session;
+}
+
+void EtchTcpListener::operator() (void* param) {
+
+ capu::bool_t first = true;
+
+ while (mIsStarted) {
+
+ if (openSocket(!first) != ETCH_OK) {
+ break;
+ }
+ if (readSocket() != ETCH_OK) {
+ close();
+ break;
+ }
+ close();
+ first = false;
+ }
+ mIsStarted = false;
+}
+
+status_t EtchTcpListener::setupSocket() {
+ return ETCH_EUNIMPL;
+}
Added: incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpOption.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpOption.cpp?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpOption.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/transport/EtchTcpOption.cpp Fri Mar 2 07:34:18 2012
@@ -0,0 +1,129 @@
+/* $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 "transport/EtchTcpOption.h"
+
+const EtchString EtchTcpOption::BUFFER_SIZE("TcpTransport.bufferSize");
+
+const EtchString EtchTcpOption::KEEP_ALIVE("TcpTransport.keepAlive");
+
+const EtchString EtchTcpOption::LINGER_TIME("TcpTransport.lingerTime");
+
+const EtchString EtchTcpOption::NO_DELAY("TcpTransport.noDelay");
+
+const EtchString EtchTcpOption::RECONNECT_DELAY("TcpTransport.reconnectDelay");
+
+EtchTcpOption::EtchTcpOption(EtchURL *url)
+: mBufferSize(0), mLingerTime(30), mReconnectDelay(10), mKeepAlive(0), mNoDelay(1) {
+ EtchString result;
+ if (url->getTerms().get((EtchString&) BUFFER_SIZE, &result) != ETCH_ENOT_EXIST) {
+ const char * str = result.c_str();
+
+ if (str != NULL) {
+ capu::int32_t len = capu::StringUtils::Strlen(str);
+ if (isValidNumber(str, len))
+ mBufferSize = atoi(str);
+ str = NULL;
+ }
+ }
+
+ if (url->getTerms().get((EtchString&) KEEP_ALIVE, &result) != ETCH_ENOT_EXIST) {
+ const char * str = result.c_str();
+ if (str != NULL) {
+ capu::int32_t len = capu::StringUtils::Strlen(str);
+ if (isValidNumber(str, len))
+ mKeepAlive = atoi(str);
+ str = NULL;
+ }
+ }
+
+ if (url->getTerms().get((EtchString&) LINGER_TIME, &result) != ETCH_ENOT_EXIST) {
+ const char * str = result.c_str();
+ if (str != NULL) {
+ capu::int32_t len = capu::StringUtils::Strlen(str);
+ //check if the string contains only digits
+ if (isValidNumber(str, len))
+ mLingerTime = atoi(str);
+ }
+ }
+
+ if (url->getTerms().get((EtchString&) NO_DELAY, &result) != ETCH_ENOT_EXIST) {
+ const char * str = result.c_str();
+ if (str != NULL) {
+
+ capu::int32_t len = capu::StringUtils::Strlen(str);
+
+ if (isValidNumber(str, len))
+ mNoDelay = atoi(str);
+ delete[] str;
+ }
+ }
+
+ if (url->getTerms().get((EtchString&) RECONNECT_DELAY, &result) != ETCH_ENOT_EXIST) {
+ const char * str = result.c_str();
+ if (str != NULL) {
+ //check if the string contains only digits
+ capu::int32_t len = capu::StringUtils::Strlen(str);
+
+ if (isValidNumber(str, len))
+ mReconnectDelay = atoi(str);
+ str = NULL;
+ }
+ }
+
+ if (checkBufferSize(mBufferSize) == false)
+ mBufferSize = 0;
+
+ if (checkLingerTime(mLingerTime) == false)
+ mLingerTime = 30;
+
+ if (checkReconnectDelay(mReconnectDelay) == false)
+ mReconnectDelay = 10;
+}
+
+EtchTcpOption::~EtchTcpOption() {
+
+}
+
+capu::bool_t EtchTcpOption::checkBufferSize(capu::int32_t size) {
+ if ((size < 0) || (size > 65536))
+ return false;
+ return true;
+}
+
+capu::bool_t EtchTcpOption::checkLingerTime(capu::int32_t lingertime) {
+ if ((lingertime <= -1) || (lingertime > 240))
+ return false;
+ return true;
+}
+
+capu::bool_t EtchTcpOption::checkReconnectDelay(capu::int32_t delay) {
+
+ if (delay < 0)
+ return false;
+ return true;
+}
+
+capu::bool_t EtchTcpOption::isValidNumber(const char * str, capu::int32_t len) {
+ for (capu::int32_t i = 0; i < len; i++) {
+ if ((str[i] > 57) || (str[i] < 48)) {
+ return false;
+ }
+ }
+ return true;
+}
\ No newline at end of file
Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt?rev=1296073&r1=1296072&r2=1296073&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt Fri Mar 2 07:34:18 2012
@@ -19,6 +19,8 @@
include_directories (${PROJECT_SOURCE_DIR}/include)
include_directories (${GTEST})
include_directories (${GTEST}/include)
+include_directories (${GMOCK})
+include_directories (${GMOCK}/include)
include_directories (${CAPU}/include/capu)
@@ -43,8 +45,11 @@ add_executable (etch-cpp-test
util/EtchURLTest.cpp
util/EtchResourcesTest.cpp
transport/EtchFlexBufferTest.cpp
+ transport/EtchTcpConnectionTest.cpp
+ transport/EtchTcpListenerTest.cpp
util/EtchUtilTest.cpp
${GTEST}/src/gtest-all.cc
+ ${GMOCK}/src/gmock-all.cc
main.cpp
)
Added: incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpConnectionTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpConnectionTest.cpp?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpConnectionTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpConnectionTest.cpp Fri Mar 2 07:34:18 2012
@@ -0,0 +1,135 @@
+/* $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 <gmock/gmock.h>
+#include "transport/EtchTcpConnection.h"
+#include "util/EtchResources.h"
+#include "util/EtchURL.h"
+#include "common/EtchError.h"
+#include "common/EtchSocket.h"
+#include "common/EtchString.h"
+#include "capu/util/SmartPointer.h"
+#include "transport/EtchTcpListener.h"
+#include "transport/EtchSessionData.h"
+#include "transport/EtchSessionListener.h"
+
+class MockPacketizer : public virtual EtchSessionData {
+public:
+
+ //Communication Test Between Peers
+
+ status_t sessionData(capu::SmartPointer<EtchWho> sender, capu::SmartPointer<EtchFlexBuffer> buf) {
+ EXPECT_TRUE(memcmp(buf->getBuffer(), "mock", buf->getLength()) == 0);
+ return ETCH_OK;
+ }
+
+ MOCK_METHOD2(sessionQuery, status_t(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result));
+
+ MOCK_METHOD2(sessionControl, status_t(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value));
+
+ MOCK_METHOD1(sessionNotify, status_t(capu::SmartPointer<EtchObject> event));
+
+};
+
+class MockListener : public virtual EtchSessionListener<EtchSocket> {
+public:
+
+ //Communication Test Between Peers
+
+ EtchResources resources;
+
+ status_t sessionAccepted(EtchSocket* connection) {
+ EtchString _socket("socket");
+ EtchObject *tmp;
+ resources.put(_socket, connection, tmp);
+ connection->send((unsigned char *) "mock", 4);
+ capu::Thread::Sleep(1000);
+ delete connection;
+ return ETCH_OK;
+ }
+
+ MOCK_METHOD2(sessionQuery, status_t(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result));
+
+ MOCK_METHOD2(sessionControl, status_t(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value));
+
+ MOCK_METHOD1(sessionNotify, status_t(capu::SmartPointer<EtchObject> event));
+
+};
+
+TEST(EtchTcpConnection, constructorTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchResources resources;
+ EtchTcpConnection * conn = new EtchTcpConnection(NULL, &url);
+ EXPECT_TRUE(conn != NULL);
+ delete conn;
+}
+
+TEST(EtchTcpConnection, isStartedTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchTcpConnection * conn = new EtchTcpConnection(NULL, &url);
+ EtchTcpListener *listener = new EtchTcpListener(&url);
+ EXPECT_FALSE(conn->isStarted());
+ EtchSessionListener<EtchSocket>* mSessionListener = new MockListener();
+ EtchSessionData* mPacketizer = new MockPacketizer();
+ //START THE LISTENER
+ listener->setSession(mSessionListener);
+ listener->transportControl(new EtchString(EtchTcpListener::START_AND_WAIT_UP), new EtchInt32(1000));
+ //START THE TRANSPORT
+ conn->setSession(mPacketizer);
+ conn->transportControl(new EtchString(EtchTcpConnection::START_AND_WAIT_UP), new EtchInt32(1000));
+ EXPECT_TRUE(conn->isStarted());
+ //STOP THE TRANSPORT
+ conn->transportControl(new EtchString(EtchTcpConnection::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ //STOP THE LISTENER
+ listener->transportControl(new EtchString(EtchTcpListener::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ EXPECT_FALSE(conn->isStarted());
+ listener->setSession(NULL);
+ conn->setSession(NULL);
+
+ delete mSessionListener;
+ delete mPacketizer;
+ delete listener;
+ delete conn;
+}
+
+TEST(EtchTcpConnectionAndListener, SessionAcceptTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchTcpConnection * conn = new EtchTcpConnection(NULL, &url);
+ EtchSessionListener<EtchSocket>* mSessionListener = new MockListener();
+ EtchSessionData* mPacketizer = new MockPacketizer();
+ EtchTcpListener listener(&url);
+
+ //START THE LISTENER
+ listener.setSession(mSessionListener);
+ listener.transportControl(new EtchString(EtchTcpListener::START_AND_WAIT_UP), new EtchInt32(1000));
+
+ //START THE TRANSPORT
+ conn->setSession(mPacketizer);
+ conn->transportControl(new EtchString(EtchTcpConnection::START_AND_WAIT_UP), new EtchInt32(1000));
+
+ conn->transportControl(new EtchString(EtchTcpConnection::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ //STOP THE LISTENER
+ listener.transportControl(new EtchString(EtchTcpListener::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+
+ listener.setSession(NULL);
+ conn->setSession(NULL);
+
+ delete mSessionListener;
+ delete mPacketizer;
+ delete conn;
+}
Added: incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpListenerTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpListenerTest.cpp?rev=1296073&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpListenerTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/transport/EtchTcpListenerTest.cpp Fri Mar 2 07:34:18 2012
@@ -0,0 +1,110 @@
+/* $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 <gmock/gmock.h>
+#include "util/EtchResources.h"
+#include "util/EtchURL.h"
+#include "common/EtchError.h"
+#include "common/EtchSocket.h"
+#include "common/EtchString.h"
+#include "capu/util/SmartPointer.h"
+#include "transport/EtchTcpListener.h"
+#include "transport/EtchSessionData.h"
+#include "transport/EtchSessionListener.h"
+#include "transport/EtchTcpConnection.h"
+
+class MockPacketizer2 : public EtchSessionData {
+public:
+
+ status_t sessionData(capu::SmartPointer<EtchWho> sender, capu::SmartPointer<EtchFlexBuffer> buf) {
+ EXPECT_TRUE(memcmp(buf->getBuffer(), "mock", buf->getLength()) == 0);
+ return ETCH_OK;
+ }
+
+ MOCK_METHOD2(sessionQuery, status_t(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result));
+
+ MOCK_METHOD2(sessionControl, status_t(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value));
+
+ MOCK_METHOD1(sessionNotify, status_t(capu::SmartPointer<EtchObject> event));
+};
+
+class MockListener2 : public virtual EtchSessionListener<EtchSocket> {
+public:
+
+ EtchResources resources;
+
+ status_t sessionAccepted(EtchSocket* connection) {
+ EtchString _socket("socket");
+ EtchObject *tmp;
+ resources.put(_socket, connection, tmp);
+ connection->send((unsigned char *) "mock", 4);
+ delete connection;
+ return ETCH_OK;
+ }
+
+ MOCK_METHOD2(sessionQuery, status_t(capu::SmartPointer<EtchObject> query, capu::SmartPointer<EtchObject> result));
+
+ MOCK_METHOD2(sessionControl, status_t(capu::SmartPointer<EtchObject> control, capu::SmartPointer<EtchObject> value));
+
+ MOCK_METHOD1(sessionNotify, status_t(capu::SmartPointer<EtchObject> event));
+
+};
+
+TEST(EtchTcpListener, constructorTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchTcpListener * tcpListener = new EtchTcpListener(&url);
+ EXPECT_TRUE(tcpListener != NULL);
+ delete tcpListener;
+}
+
+TEST(EtchTcpListener, transportControlTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchTcpListener * tcpListener = new EtchTcpListener(&url);
+ tcpListener->transportControl(new EtchString(EtchTcpListener::START_AND_WAIT_UP), new EtchInt32(1000));
+ EXPECT_TRUE(tcpListener->isStarted());
+ tcpListener->transportControl(new EtchString(EtchTcpListener::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ delete tcpListener;
+}
+
+TEST(EtchTcpListener, isStartedTest) {
+ EtchURL url("tcp://127.0.0.1:4001");
+ EtchTcpConnection * conn = new EtchTcpConnection(NULL, &url);
+ EtchTcpListener *listener = new EtchTcpListener(&url);
+ EtchSessionListener<EtchSocket>* mSessionListener = new MockListener2();
+ EtchSessionData* mPacketizer = new MockPacketizer2();
+ EXPECT_FALSE(listener->isStarted());
+ //START THE LISTENER
+ listener->setSession(mSessionListener);
+ listener->transportControl(new EtchString(EtchTcpListener::START_AND_WAIT_UP), new EtchInt32(1000));
+ //START THE TRANSPORT
+ conn->setSession(mPacketizer);
+ conn->transportControl(new EtchString(EtchTcpConnection::START_AND_WAIT_UP), new EtchInt32(1000));
+ EXPECT_TRUE(listener->isStarted());
+ //STOP THE TRANSPORT
+ conn->transportControl(new EtchString(EtchTcpConnection::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ //STOP THE LISTENER
+ listener->transportControl(new EtchString(EtchTcpListener::STOP_AND_WAIT_DOWN), new EtchInt32(1000));
+ EXPECT_FALSE(listener->isStarted());
+ listener->setSession(NULL);
+ conn->setSession(NULL);
+
+ delete mSessionListener;
+ delete mPacketizer;
+ delete listener;
+ delete conn;
+}
\ No newline at end of file