You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/05/31 13:56:03 UTC
svn commit: r543121 [4/6] - in /activemq/activemq-cpp/trunk/src/decaf: io/
lang/ lang/exceptions/ net/ util/ util/concurrent/ util/logging/
Added: activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,221 @@
+/*
+ * 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 "ServerSocket.h"
+#include "SocketError.h"
+
+#ifdef HAVE_WINSOCK2_H
+ #include <Winsock2.h>
+ #include <Ws2tcpip.h>
+ #include <sys/stat.h>
+ #define stat _stat
+#else
+ #include <unistd.h>
+ #include <netdb.h>
+ #include <fcntl.h>
+ #include <sys/file.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ #include <string.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <assert.h>
+#include <string>
+
+using namespace decaf::net;
+
+#ifdef HAVE_WINSOCK2_H
+
+ // Static socket initializer needed for winsock
+
+ ServerSocket::StaticServerSocketInitializer::StaticServerSocketInitializer () {
+ socketInitError = NULL;
+ const WORD version_needed = MAKEWORD(2,2); // lo-order byte: major version
+ WSAData temp;
+ if( WSAStartup(version_needed, &temp )){
+ clear();
+ socketInitError = new SocketException ( __FILE__, __LINE__,
+ "winsock.dll was not found");
+ }
+ }
+ ServerSocket::StaticServerSocketInitializer::~StaticServerSocketInitializer () {
+ clear();
+ WSACleanup();
+ }
+
+ // Create static instance of the socket initializer.
+ ServerSocket::StaticServerSocketInitializer
+ ServerSocket::staticSocketInitializer;
+
+#endif
+
+
+////////////////////////////////////////////////////////////////////////////////
+ServerSocket::ServerSocket()
+{
+ socketHandle = Socket::INVALID_SOCKET_HANDLE;
+
+#if defined(HAVE_WINSOCK2_H)
+ if( ServerSocket::staticSocketInitializer.getSocketInitError() != NULL ) {
+ throw *ServerSocket::staticSocketInitializer.getSocketInitError();
+ }
+#endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ServerSocket::~ServerSocket()
+{
+ // No shutdown, just close - dont want blocking destructor.
+ close();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::bind( const char* host, int port ) throw ( SocketException )
+{
+ bind (host, port, SOMAXCONN);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::bind( const char* host,
+ int port,
+ int backlog ) throw ( SocketException )
+{
+ if(isBound()) {
+ throw SocketException ( __FILE__, __LINE__,
+ "ServerSocket::bind - Socket already bound" );
+ }
+
+ // Create the socket.
+ socketHandle = ::socket(AF_INET, SOCK_STREAM, 0 );
+ if( socketHandle < 0) {
+ socketHandle = Socket::INVALID_SOCKET_HANDLE;
+ throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str());
+ }
+
+ // Verify the port value.
+ if( port <= 0 || port > 65535 ) {
+ throw SocketException( __FILE__, __LINE__,
+ "ServerSocket::bind - Port out of range: %d", port );
+ }
+
+ sockaddr_in bind_addr;
+ bind_addr.sin_family = AF_INET;
+ bind_addr.sin_port = htons((short)port);
+ bind_addr.sin_addr.s_addr = 0; // To be set later down...
+ memset(&bind_addr.sin_zero, 0, sizeof(bind_addr.sin_zero));
+ int status;
+
+ // Resolve name
+#if defined(HAVE_STRUCT_ADDRINFO)
+ ::addrinfo hints;
+ memset(&hints, 0, sizeof(addrinfo));
+ hints.ai_family = PF_INET;
+ struct addrinfo *res_ptr = NULL;
+ status = ::getaddrinfo(host, NULL, &hints, &res_ptr);
+ if( status != 0 || res_ptr == NULL) {
+ throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
+ }
+ assert(res_ptr->ai_addr->sa_family == AF_INET);
+ // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr is a 32 bit value, not an unsigned long.
+ assert(sizeof(((sockaddr_in*)res_ptr->ai_addr)->sin_addr.s_addr) == 4);
+ bind_addr.sin_addr.s_addr = ((sockaddr_in*)res_ptr->ai_addr)->sin_addr.s_addr;
+ freeaddrinfo(res_ptr);
+#else
+ struct ::hostent *he = ::gethostbyname(host);
+ if( he == NULL ) {
+ throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
+ }
+ bind_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
+#endif
+
+
+ // Set the socket to reuse the address.
+ int value = 1;
+ ::setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) );
+
+ status = ::bind(socketHandle,
+ reinterpret_cast<sockaddr*>(&bind_addr), sizeof( bind_addr ));
+
+ if( status < 0 ){
+ close();
+ throw SocketException ( __FILE__, __LINE__,
+ "ServerSocket::bind - %s", SocketError::getErrorString().c_str() );
+ }
+ status = ::listen( socketHandle, (int)backlog );
+ if( status < 0 ) {
+ close();
+ throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ServerSocket::close() throw ( lang::Exception ){
+
+ if( isBound() ) {
+
+ #if !defined(HAVE_WINSOCK2_H)
+ ::close( socketHandle );
+ #else
+ ::closesocket( socketHandle );
+ #endif
+
+ socketHandle = Socket::INVALID_SOCKET_HANDLE;
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool ServerSocket::isBound() const {
+ return this->socketHandle != Socket::INVALID_SOCKET_HANDLE;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* ServerSocket::accept() throw (SocketException)
+{
+ struct sockaddr_in temp;
+
+ #if !defined(HAVE_WINSOCK2_H)
+ socklen_t temp_len = sizeof( sockaddr_in );
+ #else
+ int temp_len = sizeof( sockaddr_in );
+ #endif
+
+ SocketHandle ss_socket_handle = NULL;
+
+ // Loop to ignore any signal interruptions that occur during the operation.
+ do {
+
+ ss_socket_handle = ::accept( socketHandle,
+ reinterpret_cast<struct sockaddr*>(&temp),
+ &temp_len );
+
+ } while( ss_socket_handle < 0 &&
+ SocketError::getErrorCode() == SocketError::INTERRUPTED );
+
+ if( ss_socket_handle < 0 ) {
+ throw SocketException( __FILE__, __LINE__,
+ "ServerSocket::accept- %s", SocketError::getErrorString().c_str() );
+ }
+
+ return new TcpSocket( ss_socket_handle );
+}
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.h Thu May 31 04:55:59 2007
@@ -0,0 +1,123 @@
+/*
+ * 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 _DECAF_NET_SERVERSOCKETIMPL_H_
+#define _DECAF_NET_SERVERSOCKETIMPL_H_
+
+#include <decaf/net/TcpSocket.h>
+#include <decaf/net/SocketException.h>
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace net{
+
+ /**
+ * A server socket class (for testing purposes).
+ */
+ class ServerSocket
+ {
+ public:
+
+ typedef Socket::SocketHandle SocketHandle;
+
+ private:
+
+ SocketHandle socketHandle;
+
+ public:
+
+ /**
+ * Constructor.
+ * Creates a non-bound server socket.
+ */
+ ServerSocket();
+
+ /**
+ * Destructor.
+ * Releases socket handle if close() hasn't been called.
+ */
+ virtual ~ServerSocket();
+
+ public:
+
+ /**
+ * Bind and listen to given IP/dns and port.
+ * @param host IP address or host name.
+ * @param port TCP port between 1..655535
+ */
+ virtual void bind( const char* host, int port ) throw ( SocketException );
+
+ /**
+ * Bind and listen to given IP/dns and port.
+ * @param host IP address or host name.
+ * @param port TCP port between 1..655535
+ * @param backlog Size of listen backlog.
+ */
+ virtual void bind( const char* host,
+ int port,
+ int backlog ) throw ( SocketException );
+
+ /**
+ * Blocks until a client connects to the bound socket.
+ * @return new socket. Never returns NULL.
+ */
+ virtual Socket* accept () throw ( SocketException );
+
+ /**
+ * Closes the server socket.
+ */
+ virtual void close() throw( lang::Exception );
+
+ /**
+ * @return true of the server socket is bound.
+ */
+ virtual bool isBound() const;
+
+ protected:
+
+ #ifdef HAVE_WINSOCK2_H
+
+ // WINDOWS needs initialization of winsock
+ class StaticServerSocketInitializer {
+ private:
+
+ SocketException* socketInitError;
+
+ void clear(){
+ if( socketInitError != NULL ){
+ delete socketInitError;
+ }
+ socketInitError = NULL;
+ }
+
+ public:
+
+ SocketException* getSocketInitError() {
+ return socketInitError;
+ }
+ StaticServerSocketInitializer();
+ virtual ~StaticServerSocketInitializer();
+
+ };
+ static StaticServerSocketInitializer staticSocketInitializer;
+ #endif
+
+ };
+
+}}
+
+#endif // _DECAF_NET_SERVERSOCKETIMPL_H_
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/Socket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/Socket.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/Socket.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/Socket.h Thu May 31 04:55:59 2007
@@ -0,0 +1,169 @@
+/*
+ * 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 _DECAF_NET_SOCKET_H_
+#define _DECAF_NET_SOCKET_H_
+
+#include <decaf/net/SocketException.h>
+#include <decaf/io/InputStream.h>
+#include <decaf/io/OutputStream.h>
+#include <decaf/io/Closeable.h>
+#include <decaf/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+#include <Winsock2.h> // SOCKET
+#endif
+
+namespace decaf{
+namespace net{
+
+ class Socket : public decaf::io::Closeable
+ {
+ public:
+
+ // Define the SocketHandle type.
+ #if !defined(HAVE_WINSOCK2_H)
+ typedef int SocketHandle;
+ #else
+ typedef SOCKET SocketHandle;
+ #endif
+
+ /**
+ * Defines a constant for an invalid socket handle.
+ */
+ static const SocketHandle INVALID_SOCKET_HANDLE = (SocketHandle) -1;
+
+ public:
+
+ virtual ~Socket() {}
+
+ /**
+ * Connects to the specified destination. Closes this socket if
+ * connected to another destination.
+ * @param host The host of the server to connect to.
+ * @param port The port of the server to connect to.
+ * @throws IOException Thrown if a failure occurred in the connect.
+ */
+ virtual void connect( const char* host, int port )
+ throw(SocketException) = 0;
+
+ /**
+ * Indicates whether or not this socket is connected to a destination.
+ * @returns true if connected
+ */
+ virtual bool isConnected() const = 0;
+
+ /**
+ * Gets the InputStream for this socket.
+ * @return The InputStream for this socket. NULL if not connected.
+ */
+ virtual io::InputStream* getInputStream() = 0;
+
+ /**
+ * Gets the OutputStream for this socket.
+ * @return the OutputStream for this socket. NULL if not connected.
+ */
+ virtual io::OutputStream* getOutputStream() = 0;
+
+ /**
+ * Gets the linger time.
+ * @return The linger time in seconds.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getSoLinger() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the linger time.
+ * @param linger The linger time in seconds. If 0, linger is off.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setSoLinger( int linger ) throw( SocketException ) = 0;
+
+ /**
+ * Gets the keep alive flag.
+ * @return True if keep alive is enabled.
+ * @throws SocketException if the operation fails.
+ */
+ virtual bool getKeepAlive() const throw( SocketException ) = 0;
+
+ /**
+ * Enables/disables the keep alive flag.
+ * @param keepAlive If true, enables the flag.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setKeepAlive( bool keepAlive ) throw( SocketException ) = 0;
+
+ /**
+ * Gets the receive buffer size.
+ * @return the receive buffer size in bytes.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getReceiveBufferSize() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the recieve buffer size.
+ * @param size Number of bytes to set the receive buffer to.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setReceiveBufferSize( int size ) throw( SocketException ) = 0;
+
+ /**
+ * Gets the reuse address flag.
+ * @return True if the address can be reused.
+ * @throws SocketException if the operation fails.
+ */
+ virtual bool getReuseAddress() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the reuse address flag.
+ * @param reuse If true, sets the flag.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setReuseAddress( bool reuse ) throw( SocketException ) = 0;
+
+ /**
+ * Gets the send buffer size.
+ * @return the size in bytes of the send buffer.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getSendBufferSize() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the send buffer size.
+ * @param size The number of bytes to set the send buffer to.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setSendBufferSize( int size ) throw( SocketException ) = 0;
+
+ /**
+ * Gets the timeout for socket operations.
+ * @return The timeout in milliseconds for socket operations.
+ * @throws SocketException Thrown if unable to retrieve the information.
+ */
+ virtual int getSoTimeout() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the timeout for socket operations.
+ * @param timeout The timeout in milliseconds for socket operations.<p>
+ * @throws SocketException Thrown if unable to set the information.
+ */
+ virtual void setSoTimeout( int timeout ) throw( SocketException ) = 0;
+
+ };
+
+}}
+
+#endif /*_DECAF_NET_SOCKET_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketError.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketError.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketError.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketError.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,84 @@
+/*
+ * 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 "SocketError.h"
+#include <activemq/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+ #include <Winsock2.h>
+#else
+ #include <errno.h>
+#endif
+
+using namespace decaf;
+using namespace decaf::net;
+
+// Platform constants.
+#if defined(HAVE_WINSOCK2_H)
+ const int SocketError::INTERRUPTED = WSAEINTR;
+#else
+ const int SocketError::INTERRUPTED = EINTR;
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+int SocketError::getErrorCode() {
+
+ #if defined(HAVE_WINSOCK2_H)
+
+ return ::WSAGetLastError();
+
+ #else
+
+ return errno;
+
+ #endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string SocketError::getErrorString() {
+
+ std::string returnValue;
+
+ // Get the error code.
+ int errorCode = getErrorCode();
+
+ #if defined(HAVE_WINSOCK2_H)
+
+ // Create the error string.
+ static const int errorStringSize = 512;
+ char errorString[errorStringSize];
+ memset( errorString, 0, errorStringSize );
+ ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+ 0,
+ errorCode,
+ 0,
+ errorString,
+ errorStringSize - 1,
+ NULL);
+
+ returnValue = errorString;
+
+ #else
+
+ // Create the error string.
+ returnValue = ::strerror(errorCode);
+
+ #endif
+
+ return returnValue;
+}
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketError.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketError.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketError.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketError.h Thu May 31 04:55:59 2007
@@ -0,0 +1,53 @@
+/*
+ * 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 _DECAF_NET_SOCKETERROR_H_
+#define _DECAF_NET_SOCKETERROR_H_
+
+#include <string>
+
+namespace decaf{
+namespace net{
+
+ /**
+ * Static utility class to simplify handling of error codes
+ * for socket operations.
+ */
+ class SocketError {
+ public:
+
+ /**
+ * Indicates that a socket operation was interrupted by a signal.
+ */
+ static const int INTERRUPTED;
+
+ public:
+
+ /**
+ * Gets the last error appropriate for the platform.
+ */
+ static int getErrorCode();
+
+ /**
+ * Gets the string description for the last error.
+ */
+ static std::string getErrorString();
+ };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETERROR_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketException.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketException.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketException.h Thu May 31 04:55:59 2007
@@ -0,0 +1,72 @@
+/*
+ * 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 _DECAF_NET_SOCKETEXCEPTION_H_
+#define _DECAF_NET_SOCKETEXCEPTION_H_
+
+#include <decaf/io/IOException.h>
+
+namespace decaf{
+namespace net{
+
+ /**
+ * Exception for errors when manipulating sockets.
+ */
+ class SocketException : public io::IOException
+ {
+ public:
+
+ SocketException() throw() {}
+ SocketException( const lang::Exception& ex ) throw()
+ : io::IOException()
+ {
+ *(lang::Exception*)this = ex;
+ }
+ SocketException( const SocketException& ex ) throw()
+ : io::IOException()
+ {
+ *(lang::Exception*)this = ex;
+ }
+ SocketException( const char* file, const int lineNumber,
+ const char* msg, ...) throw()
+ : io::IOException()
+ {
+ va_list vargs;
+ va_start(vargs, msg);
+ buildMessage(msg, vargs);
+
+ // Set the first mark for this exception.
+ setMark( file, lineNumber );
+ }
+
+ /**
+ * Clones this exception. This is useful for cases where you need
+ * to preserve the type of the original exception as well as the message.
+ * All subclasses should override.
+ */
+ virtual SocketException* clone() const{
+ return new SocketException( *this );
+ }
+
+ virtual ~SocketException() throw() {}
+
+ };
+
+}}
+
+
+#endif // _DECAF_NET_SOCKETEXCEPTION_H_
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,171 @@
+/*
+ * 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 <decaf/util/Config.h>
+
+#if !defined(HAVE_WINSOCK2_H)
+ #include <sys/select.h>
+ #include <sys/socket.h>
+#else
+ #include <Winsock2.h>
+#endif
+
+#ifdef HAVE_SYS_IOCTL_H
+#define BSD_COMP /* Get FIONREAD on Solaris2. */
+#include <sys/ioctl.h>
+#endif
+
+// Pick up FIONREAD on Solaris 2.5.
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
+
+#include <decaf/net/SocketInputStream.h>
+#include <decaf/net/SocketError.h>
+#include <decaf/io/IOException.h>
+#include <decaf/lang/Character.h>
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+#include <stdlib.h>
+#include <string>
+#include <stdio.h>
+#include <iostream>
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::util;
+using namespace decaf::lang;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+SocketInputStream::SocketInputStream( net::Socket::SocketHandle socket )
+{
+ this->socket = socket;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SocketInputStream::~SocketInputStream()
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::available() const throw ( io::IOException ){
+
+// The windows version
+#if defined(HAVE_WINSOCK2_H)
+
+ unsigned long numBytes = 0;
+
+ if (::ioctlsocket (socket, FIONREAD, &numBytes) == SOCKET_ERROR){
+ throw SocketException( __FILE__, __LINE__, "ioctlsocket failed" );
+ }
+
+ return (std::size_t)numBytes;
+
+#else // !defined(HAVE_WINSOCK2_H)
+
+ // If FIONREAD is defined - use ioctl to find out how many bytes
+ // are available.
+ #if defined(FIONREAD)
+
+ std::size_t numBytes = 0;
+ if( ::ioctl (socket, FIONREAD, &numBytes) != -1 ){
+ return numBytes;
+ }
+
+ #endif
+
+ // If we didn't get anything we can use select. This is a little
+ // less functional. We will poll on the socket - if there is data
+ // available, we'll return 1, otherwise we'll return zero.
+ #if defined(HAVE_SELECT)
+
+ fd_set rd;
+ FD_ZERO(&rd);
+ FD_SET( socket, &rd );
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ int returnCode = ::select(socket+1, &rd, NULL, NULL, &tv);
+ if(returnCode == -1){
+ throw IOException(__FILE__, __LINE__, SocketError::getErrorString().c_str() );
+ }
+ return (returnCode == 0)? 0 : 1;
+
+ #else
+
+ return 0;
+
+ #endif /* HAVE_SELECT */
+
+
+#endif // !defined(HAVE_WINSOCK2_H)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+unsigned char SocketInputStream::read() throw ( IOException ){
+
+ unsigned char c;
+ std::size_t len = read( &c, 1 );
+ if( len != sizeof(c) ){
+ throw IOException( __FILE__, __LINE__,
+ "activemq::io::SocketInputStream::read - failed reading a byte");
+ }
+
+ return c;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::read( unsigned char* buffer,
+ std::size_t bufferSize ) throw ( IOException )
+{
+ int len = 0;
+
+ // Loop to ignore any signal interruptions that occur during the read.
+ do {
+
+ // Read data from the socket.
+ len = ::recv(socket, (char*)buffer, (int)bufferSize, 0);
+
+ // Check for a closed socket.
+ if( len == 0 ){
+ throw IOException( __FILE__, __LINE__,
+ "activemq::io::SocketInputStream::read - The connection is broken" );
+ }
+
+ } while( len == -1 &&
+ SocketError::getErrorCode() == SocketError::INTERRUPTED );
+
+ // Check for error.
+ if( len == -1 ){
+
+ // Otherwise, this was a bad error - throw an exception.
+ throw IOException( __FILE__, __LINE__,
+ "activemq::io::SocketInputStream::read - %s", SocketError::getErrorString().c_str() );
+ }
+
+ return len;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t SocketInputStream::skip( std::size_t num AMQCPP_UNUSED )
+throw ( io::IOException, lang::exceptions::UnsupportedOperationException ) {
+ throw lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__,
+ "skip() method is not supported");
+}
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.h Thu May 31 04:55:59 2007
@@ -0,0 +1,161 @@
+/*
+ * 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 _DECAF_NET_SOCKETINPUTSTREAM_H_
+#define _DECAF_NET_SOCKETINPUTSTREAM_H_
+
+#include <decaf/io/InputStream.h>
+#include <decaf/net/Socket.h>
+#include <decaf/util/concurrent/Mutex.h>
+#include <decaf/lang/Exception.h>
+
+namespace decaf{
+namespace net{
+
+ /**
+ * Input stream for performing reads on a socket. This
+ * class will only work properly for blocking sockets.
+ */
+ class SocketInputStream : public io::InputStream
+ {
+ private:
+
+ // The socket handle.
+ Socket::SocketHandle socket;
+ util::concurrent::Mutex mutex;
+
+ public:
+
+ /**
+ * Constructor.
+ * @param socket the socket handle.
+ */
+ SocketInputStream( Socket::SocketHandle socket );
+
+ /**
+ * Destructor.
+ */
+ virtual ~SocketInputStream();
+
+ /**
+ * Locks the object.
+ * @throws Exception
+ */
+ virtual void lock() throw( lang::Exception ){
+ mutex.lock();
+ }
+
+ /**
+ * Unlocks the object.
+ * @throws Exception
+ */
+ virtual void unlock() throw( lang::Exception ){
+ mutex.unlock();
+ }
+
+ /**
+ * Waits on a signal from this object, which is generated
+ * by a call to Notify. Must have this object locked before
+ * calling.
+ * @throws Exception
+ */
+ virtual void wait() throw( lang::Exception ){
+ mutex.wait();
+ }
+
+ /**
+ * Waits on a signal from this object, which is generated
+ * by a call to Notify. Must have this object locked before
+ * calling. This wait will timeout after the specified time
+ * interval.
+ * @param millisecs time in millisecsonds to wait, or WAIT_INIFINITE
+ * @throws Exception
+ */
+ virtual void wait( unsigned long millisecs )
+ throw( lang::Exception ) {
+
+ mutex.wait( millisecs );
+ }
+
+ /**
+ * Signals a waiter on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws Exception
+ */
+ virtual void notify() throw( lang::Exception ){
+ mutex.notify();
+ }
+
+ /**
+ * Signals the waiters on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws Exception
+ */
+ virtual void notifyAll() throw( lang::Exception ){
+ mutex.notifyAll();
+ }
+
+ /**
+ * Returns the number of bytes available on the socket to
+ * be read right now.
+ * @return The number of bytes currently available to
+ * be read on the socket.
+ */
+ virtual std::size_t available() const throw ( io::IOException );
+
+ /**
+ * Reads a single byte from the buffer. If no data
+ * is available, blocks until their is.
+ * @return The next byte.
+ * @throws IOException thrown if an error occurs.
+ */
+ virtual unsigned char read() throw ( io::IOException );
+
+ /**
+ * Reads an array of bytes from the buffer. If no data
+ * is available, blocks until there is.
+ * @param buffer (out) the target buffer.
+ * @param bufferSize the size of the output buffer.
+ * @return The number of bytes read.
+ * @throws IOException thrown if an error occurs.
+ */
+ virtual std::size_t read( unsigned char* buffer,
+ std::size_t bufferSize )
+ throw ( io::IOException );
+
+ /**
+ * Close - does nothing. It is the responsibility of the owner
+ * of the socket object to close it.
+ * @throws CMSException
+ */
+ virtual void close() throw( lang::Exception ){}
+
+ /**
+ * Not supported.
+ * @throws an UnsupportedOperationException.
+ */
+ virtual std::size_t skip( std::size_t num )
+ throw ( io::IOException,
+ lang::exceptions::UnsupportedOperationException );
+
+ };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETINPUTSTREAM_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,84 @@
+/*
+ * 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 "SocketOutputStream.h"
+#include <decaf/util/Config.h>
+#include <decaf/lang/Character.h>
+#include "SocketError.h"
+
+#ifdef HAVE_WINSOCK2_H
+ #include <Winsock2.h>
+#else
+ #include <sys/socket.h>
+#endif
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+
+#if defined(SOCKET_NOSIGNAL)
+ #define AMQ_SEND_OPTS SOCKET_NOSIGNAL
+#elif defined(MSG_NOSIGNAL)
+ #define AMQ_SEND_OPTS MSG_NOSIGNAL
+#else
+ #define AMQ_SEND_OPTS 0
+#endif
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::util;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+SocketOutputStream::SocketOutputStream( Socket::SocketHandle socket )
+{
+ this->socket = socket;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+SocketOutputStream::~SocketOutputStream()
+{
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketOutputStream::write( unsigned char c ) throw (IOException)
+{
+ write( &c, 1 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketOutputStream::write( const unsigned char* buffer, std::size_t len )
+ throw (IOException)
+{
+ std::size_t remaining = len;
+ int sendOpts = AMQ_SEND_OPTS;
+
+ while( remaining > 0 )
+ {
+ int length = ::send( socket, (const char*)buffer, (int)remaining, sendOpts );
+ if( length == -1 ){
+ throw IOException( __FILE__, __LINE__,
+ "activemq::io::SocketOutputStream::write - %s", SocketError::getErrorString().c_str() );
+ }
+
+ buffer+=length;
+ remaining -= length;
+ }
+}
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.h Thu May 31 04:55:59 2007
@@ -0,0 +1,142 @@
+/*
+ * 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 _DECAF_NET_SOCKETOUTPUTSTREAM_H_
+#define _DECAF_NET_SOCKETOUTPUTSTREAM_H_
+
+#include <decaf/io/OutputStream.h>
+#include <decaf/net/Socket.h>
+#include <decaf/util/concurrent/Mutex.h>
+
+namespace decaf{
+namespace net{
+
+ /**
+ * Output stream for performing write operations
+ * on a socket.
+ */
+ class SocketOutputStream : public io::OutputStream
+ {
+ private:
+
+ // The socket.
+ Socket::SocketHandle socket;
+ util::concurrent::Mutex mutex;
+
+ public:
+
+ /**
+ * Constructor.
+ * @param socket the socket handle.
+ */
+ SocketOutputStream( Socket::SocketHandle socket );
+
+ virtual ~SocketOutputStream();
+
+ /**
+ * Locks the object.
+ * @throws Exception
+ */
+ virtual void lock() throw( lang::Exception ){
+ mutex.lock();
+ }
+
+ /**
+ * Unlocks the object.
+ * @throws Exception
+ */
+ virtual void unlock() throw( lang::Exception ){
+ mutex.unlock();
+ }
+
+ /**
+ * Waits on a signal from this object, which is generated
+ * by a call to Notify. Must have this object locked before
+ * calling.
+ * @throws Exception
+ */
+ virtual void wait() throw( lang::Exception ){
+ mutex.wait();
+ }
+
+ /**
+ * Waits on a signal from this object, which is generated
+ * by a call to Notify. Must have this object locked before
+ * calling. This wait will timeout after the specified time
+ * interval.
+ * @param millisecs time in millisecsonds to wait, or WAIT_INIFINITE
+ * @throws Exception
+ */
+ virtual void wait( unsigned long millisecs )
+ throw( lang::Exception ) {
+
+ mutex.wait( millisecs );
+ }
+
+ /**
+ * Signals a waiter on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws Exception
+ */
+ virtual void notify() throw( lang::Exception ){
+ mutex.notify();
+ }
+
+ /**
+ * Signals the waiters on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ */
+ virtual void notifyAll() throw( lang::Exception ){
+ mutex.notifyAll();
+ }
+
+ /**
+ * Writes a single byte to the output stream.
+ * @param c the byte.
+ * @throws IOException thrown if an error occurs.
+ */
+ virtual void write( unsigned char c ) throw ( io::IOException );
+
+ /**
+ * Writes an array of bytes to the output stream.
+ * @param buffer The array of bytes to write.
+ * @param len The number of bytes from the buffer to be written.
+ * @throws IOException thrown if an error occurs.
+ */
+ virtual void write( const unsigned char* buffer,
+ std::size_t len ) throw ( io::IOException );
+
+ /**
+ * Flush - does nothing.
+ * @throws IOException
+ */
+ virtual void flush() throw ( io::IOException ){};
+
+ /**
+ * Close - does nothing. It is the responsibility of the owner
+ * of the socket object to close it.
+ * @throws CMSException
+ */
+ virtual void close() throw( lang::Exception ){}
+
+ };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETOUTPUTSTREAM_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,449 @@
+/*
+ * 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 <decaf/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+ #include <Winsock2.h>
+ #include <Ws2tcpip.h>
+ #include <sys/stat.h>
+ #define stat _stat
+#else
+ #include <unistd.h>
+ #include <netdb.h>
+ #include <fcntl.h>
+ #include <sys/file.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ #include <string.h>
+ #include <netinet/tcp.h>
+#endif
+
+#ifndef SHUT_RDWR
+ #define SHUT_RDWR 2 // Winsock2 doesn't seem to define this
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <sys/types.h>
+
+#include "TcpSocket.h"
+#include "SocketInputStream.h"
+#include "SocketOutputStream.h"
+#include "SocketError.h"
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::io;
+using namespace decaf::lang;
+
+#if defined(HAVE_WINSOCK2_H)
+
+ // Static socket initializer needed for winsock
+
+ TcpSocket::StaticSocketInitializer::StaticSocketInitializer() {
+ socketInitError = NULL;
+ const WORD version_needed = MAKEWORD(2,2); // lo-order byte: major version
+ WSAData temp;
+ if( WSAStartup( version_needed, &temp ) ){
+ clear();
+ socketInitError = new SocketException ( __FILE__, __LINE__,
+ "winsock.dll was not found");
+ }
+ }
+ TcpSocket::StaticSocketInitializer::~StaticSocketInitializer() {
+ clear();
+ WSACleanup();
+ }
+
+ // Create static instance of the socket initializer.
+ TcpSocket::StaticSocketInitializer TcpSocket::staticSocketInitializer;
+
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::TcpSocket() throw (SocketException)
+:
+ socketHandle( INVALID_SOCKET_HANDLE ),
+ inputStream( NULL ),
+ outputStream( NULL )
+{
+
+ try {
+
+#if defined(HAVE_WINSOCK2_H)
+ if( staticSocketInitializer.getSocketInitError() != NULL ) {
+ throw *staticSocketInitializer.getSocketInitError();
+ }
+#endif
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::TcpSocket( SocketHandle socketHandle )
+:
+ socketHandle( INVALID_SOCKET_HANDLE ),
+ inputStream( NULL ),
+ outputStream( NULL )
+{
+ try {
+
+#if defined(HAVE_WINSOCK2_H)
+ if( staticSocketInitializer.getSocketInitError() != NULL ) {
+ throw *staticSocketInitializer.getSocketInitError();
+ }
+#endif
+
+ this->socketHandle = socketHandle;
+ this->inputStream = new SocketInputStream( socketHandle );
+ this->outputStream = new SocketOutputStream( socketHandle );
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+TcpSocket::~TcpSocket()
+{
+ // No shutdown, just close - dont want blocking destructor.
+ close();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+InputStream* TcpSocket::getInputStream(){
+ return inputStream;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+OutputStream* TcpSocket::getOutputStream(){
+ return outputStream;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::connect(const char* host, int port) throw ( SocketException )
+{
+ try{
+
+ if( isConnected() ) {
+ throw SocketException( __FILE__, __LINE__,
+ "Socket::connect - Socket already connected. host: %s, port: %d", host, port );
+ }
+
+ // Create the socket.
+ checkResult( (int)(socketHandle = ::socket(AF_INET, SOCK_STREAM, 0)) );
+
+ // Check port value.
+ if (port <= 0 || port > 65535) {
+ close();
+ throw SocketException ( __FILE__, __LINE__,
+ "Socket::connect- Port out of range: %d", port );
+ }
+
+#ifdef SO_NOSIGPIPE // Don't want to get a SIGPIPE on FreeBSD and Mac OS X
+
+ int optval = 1;
+ checkResult( ::setsockopt( socketHandle, SOL_SOCKET, SO_NOSIGPIPE, (char*)&optval, sizeof(optval)) );
+
+#endif
+
+ sockaddr_in target_addr;
+ target_addr.sin_family = AF_INET;
+ target_addr.sin_port = htons( ( short ) port );
+ target_addr.sin_addr.s_addr = 0; // To be set later down...
+ memset( &target_addr.sin_zero, 0, sizeof( target_addr.sin_zero ) );
+
+ // Resolve name
+#if defined(HAVE_STRUCT_ADDRINFO)
+ addrinfo hints;
+ memset(&hints, 0, sizeof(addrinfo));
+ hints.ai_family = PF_INET;
+ struct addrinfo *res_ptr = NULL;
+
+ checkResult( ::getaddrinfo( host, NULL, &hints, &res_ptr ) );
+
+ assert(res_ptr->ai_addr->sa_family == AF_INET);
+ // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr
+ // is a 32 bit value, not an unsigned long.
+ assert( sizeof( ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr ) == 4 );
+ target_addr.sin_addr.s_addr = ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr;
+ freeaddrinfo( res_ptr );
+#else
+ struct ::hostent *he = ::gethostbyname(host);
+ if( he == NULL ) {
+ throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
+ }
+ target_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
+#endif
+
+ // Attempt the connection to the server.
+ checkResult( ::connect( socketHandle,
+ ( const sockaddr * )&target_addr,
+ sizeof( target_addr ) ) );
+
+ // Create an input/output stream for this socket.
+ inputStream = new SocketInputStream( socketHandle );
+ outputStream = new SocketOutputStream( socketHandle );
+
+ }
+ catch( SocketException& ex ) {
+ ex.setMark( __FILE__, __LINE__);
+ try{ close(); } catch( lang::Exception& cx){ /* Absorb */ }
+ throw ex;
+ }
+ catch( ... ){
+ try{ close(); } catch( lang::Exception& cx){ /* Absorb */ }
+ throw SocketException( __FILE__, __LINE__, "connect() caught unknown exception");
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::close() throw( lang::Exception )
+{
+ // Destroy the input stream.
+ if( inputStream != NULL ){
+ delete inputStream;
+ inputStream = NULL;
+ }
+
+ // Destroy the output stream.
+ if( outputStream != NULL ){
+ delete outputStream;
+ outputStream = NULL;
+ }
+
+ if( isConnected() )
+ {
+ ::shutdown( socketHandle, SHUT_RDWR );
+
+ #if !defined(HAVE_WINSOCK2_H)
+ ::close( socketHandle );
+ #else
+ ::closesocket( socketHandle );
+ #endif
+
+ socketHandle = INVALID_SOCKET_HANDLE;
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSoLinger() const throw( SocketException ){
+
+ try{
+ linger value;
+ socklen_t length = sizeof( value );
+ checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, &length ));
+
+ return value.l_onoff? value.l_linger : 0;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSoLinger( int dolinger ) throw( SocketException ){
+
+ try{
+ linger value;
+ value.l_onoff = dolinger != 0;
+ value.l_linger = dolinger;
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, sizeof(value) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getKeepAlive() const throw( SocketException ){
+
+ try{
+ int value;
+ socklen_t length = sizeof( int );
+ checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, &length ));
+ return value != 0;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setKeepAlive( const bool keepAlive ) throw( SocketException ){
+
+ try{
+ int value = keepAlive? 1 : 0;
+ checkResult(::setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, sizeof(int)) );
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getReceiveBufferSize() const throw( SocketException ){
+
+ try{
+ int value;
+ socklen_t length = sizeof( value );
+ checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&value, &length ));
+ return value;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setReceiveBufferSize( int size ) throw( SocketException ){
+
+ try{
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getReuseAddress() const throw( SocketException ){
+
+ try{
+ int value;
+ socklen_t length = sizeof( int );
+ checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, &length ));
+ return value != 0;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setReuseAddress( bool reuse ) throw( SocketException ){
+
+ try{
+ int value = reuse? 1 : 0;
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSendBufferSize() const throw( SocketException ){
+
+ try{
+ int value;
+ socklen_t length = sizeof( value );
+ checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&value, &length ));
+ return value;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSendBufferSize( int size ) throw( SocketException ){
+
+ try{
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(size) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setSoTimeout ( const int millisecs ) throw ( SocketException )
+{
+ try{
+
+#if !defined(HAVE_WINSOCK2_H)
+ timeval timot;
+ timot.tv_sec = millisecs / 1000;
+ timot.tv_usec = (millisecs % 1000) * 1000;
+#else
+ int timot = millisecs;
+#endif
+
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVTIMEO, (const char*) &timot, sizeof (timot) ));
+ checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDTIMEO, (const char*) &timot, sizeof (timot) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getSoTimeout() const throw( SocketException )
+{
+ try{
+
+#if !defined(HAVE_WINSOCK2_H)
+ timeval timot;
+ timot.tv_sec = 0;
+ timot.tv_usec = 0;
+ socklen_t size = sizeof(timot);
+#else
+ int timot = 0;
+ int size = sizeof(timot);
+#endif
+
+ checkResult(::getsockopt(socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char*) &timot, &size));
+
+#if !defined(HAVE_WINSOCK2_H)
+ return (timot.tv_sec * 1000) + (timot.tv_usec / 1000);
+#else
+ return timot;
+#endif
+
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool TcpSocket::getTcpNoDelay() const throw ( lang::Exception ) {
+
+ try{
+ int value;
+ socklen_t length = sizeof( int );
+ checkResult(::getsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&value, &length ));
+ return value != 0;
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setTcpNoDelay( bool value ) throw ( lang::Exception ) {
+
+ try{
+ int ivalue = value ? 1 : 0;
+ checkResult(::setsockopt( socketHandle, IPPROTO_TCP, TCP_NODELAY, (char*)&ivalue, sizeof(int) ));
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::checkResult( int value ) const throw (SocketException) {
+
+ if( value < 0 ){
+ throw SocketException( __FILE__, __LINE__,
+ SocketError::getErrorString().c_str() );
+ }
+}
+
+
Added: activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.h Thu May 31 04:55:59 2007
@@ -0,0 +1,259 @@
+/*
+ * 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 _DECAF_NET_TCPSOCKET_H_
+#define _DECAF_NET_TCPSOCKET_H_
+
+#include <decaf/net/SocketException.h>
+#include <decaf/net/Socket.h>
+#include <decaf/io/InputStream.h>
+#include <decaf/io/OutputStream.h>
+
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace net{
+
+ // Forward declarations
+ class SocketInputStream;
+ class SocketOutputStream;
+
+ /**
+ * Platform-independent implementation of the socket interface.
+ */
+ class TcpSocket : public Socket
+ {
+ private:
+
+ /**
+ * The handle for this socket.
+ */
+ SocketHandle socketHandle;
+
+ /**
+ * The input stream for reading this socket.
+ */
+ SocketInputStream* inputStream;
+
+ /**
+ * The output stream for writing to this socket.
+ */
+ SocketOutputStream* outputStream;
+
+ public:
+
+ /**
+ * Construct a non-connected socket.
+ * @throws SocketException thrown one windows if the static initialization
+ * call to WSAStartup was not successful.
+ */
+ TcpSocket() throw (SocketException);
+
+ /**
+ * Construct a connected or bound socket based on given
+ * socket handle.
+ * @param socketHandle a socket handle to wrap in the object
+ */
+ TcpSocket( SocketHandle socketHandle );
+
+ /**
+ * Destruct.
+ * Releases the socket handle but not
+ * gracefully shut down the connection.
+ */
+ virtual ~TcpSocket();
+
+ /**
+ * Gets the handle for the socket.
+ * @return SocketHabler for this Socket, can be NULL
+ */
+ SocketHandle getSocketHandle () {
+ return socketHandle;
+ }
+
+ /**
+ * Connects to the specified destination. Closes this socket if
+ * connected to another destination.
+ * @param host The host of the server to connect to.
+ * @param port The port of the server to connect to.
+ * @throws IOException Thrown if a failure occurred in the connect.
+ */
+ virtual void connect( const char* host, int port ) throw( SocketException );
+
+ /**
+ * Indicates whether or not this socket is connected to a destination.
+ * @return true if connected
+ */
+ virtual bool isConnected() const{
+ return socketHandle != INVALID_SOCKET_HANDLE;
+ }
+
+ /**
+ * Gets the InputStream for this socket.
+ * @return The InputStream for this socket. NULL if not connected.
+ */
+ virtual io::InputStream* getInputStream();
+
+ /**
+ * Gets the OutputStream for this socket.
+ * @return the OutputStream for this socket. NULL if not connected.
+ */
+ virtual io::OutputStream* getOutputStream();
+
+ /**
+ * Gets the linger time.
+ * @return The linger time in seconds.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getSoLinger() const throw( SocketException );
+
+ /**
+ * Sets the linger time.
+ * @param linger The linger time in seconds. If 0, linger is off.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setSoLinger( int linger ) throw( SocketException );
+
+ /**
+ * Gets the keep alive flag.
+ * @return True if keep alive is enabled.
+ * @throws SocketException if the operation fails.
+ */
+ virtual bool getKeepAlive() const throw( SocketException );
+
+ /**
+ * Enables/disables the keep alive flag.
+ * @param keepAlive If true, enables the flag.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setKeepAlive( bool keepAlive ) throw( SocketException );
+
+ /**
+ * Gets the receive buffer size.
+ * @return the receive buffer size in bytes.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getReceiveBufferSize() const throw( SocketException );
+
+ /**
+ * Sets the recieve buffer size.
+ * @param size Number of bytes to set the receive buffer to.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setReceiveBufferSize( int size ) throw( SocketException );
+
+ /**
+ * Gets the reuse address flag.
+ * @return True if the address can be reused.
+ * @throws SocketException if the operation fails.
+ */
+ virtual bool getReuseAddress() const throw( SocketException );
+
+ /**
+ * Sets the reuse address flag.
+ * @param reuse If true, sets the flag.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setReuseAddress( bool reuse ) throw( SocketException );
+
+ /**
+ * Gets the send buffer size.
+ * @return the size in bytes of the send buffer.
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getSendBufferSize() const throw( SocketException );
+
+ /**
+ * Sets the send buffer size.
+ * @param size The number of bytes to set the send buffer to.
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setSendBufferSize( int size ) throw( SocketException );
+
+ /**
+ * Gets the timeout for socket operations.
+ * @return The timeout in milliseconds for socket operations.
+ * @throws SocketException Thrown if unable to retrieve the information.
+ */
+ virtual int getSoTimeout() const throw( SocketException );
+
+ /**
+ * Sets the timeout for socket operations.
+ * @param timeout The timeout in milliseconds for socket operations.<p>
+ * @throws SocketException Thrown if unable to set the information.
+ */
+ virtual void setSoTimeout( int timeout ) throw(SocketException);
+
+ /**
+ * Closes this object and deallocates the appropriate resources.
+ * @throws CMSException
+ */
+ virtual void close() throw( lang::Exception );
+
+ public:
+
+ /**
+ * Gets the Status of the TCP_NODELAY param for this socket as a Bool
+ * @returns true if TCP_NODELAY is enabled
+ * @throws CMSException
+ */
+ virtual bool getTcpNoDelay() const throw ( lang::Exception );
+
+ /**
+ * Sets the Status of the TCP_NODELAY param for this socket as a Bool
+ * @param value - true if TCP_NODELAY is to be enabled
+ * @throws CMSException
+ */
+ virtual void setTcpNoDelay( bool value ) throw ( lang::Exception );
+
+ protected:
+
+ #if defined(HAVE_WINSOCK2_H)
+
+ // WINDOWS needs initialization of winsock
+ class StaticSocketInitializer {
+ private:
+
+ SocketException* socketInitError;
+
+ void clear(){
+ if( socketInitError != NULL ){
+ delete socketInitError;
+ }
+ socketInitError = NULL;
+ }
+
+ public:
+
+ SocketException* getSocketInitError() {
+ return socketInitError;
+ }
+
+ StaticSocketInitializer();
+ virtual ~StaticSocketInitializer();
+
+ };
+
+ static StaticSocketInitializer staticSocketInitializer;
+ #endif
+
+ void checkResult( int value ) const throw (SocketException);
+
+ };
+
+}}
+
+#endif /*_DECAF_NET_TCPSOCKET_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/util/Config.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/util/Config.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/util/Config.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/util/Config.h Thu May 31 04:55:59 2007
@@ -0,0 +1,90 @@
+/*
+ * 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 ACTIVEMQ_UTIL_CONFIG_H_
+#define ACTIVEMQ_UTIL_CONFIG_H_
+
+#ifdef AMQCPP_DLL
+#ifdef AMQCPP_EXPORTS
+#define AMQCPP_API __declspec(dllexport)
+#else
+#define AMQCPP_API __declspec(dllimport)
+#endif
+#else
+#define AMQCPP_API
+#endif
+
+//
+// The purpose of this header is to try to detect the supported headers
+// of the platform when the ./configure script is not being used to generate
+// the config.h file.
+//
+#if defined(HAVE_CONFIG_H)
+
+ // config.h is generated by the ./configure script and it only
+ // used by unix like systems (including cygwin)
+ #include <config.h>
+
+#else /* !defined(HAVE_CONFIG_H) */
+
+ // Not using ./configure script and make system.. chances are your using the native build tools
+ // of Windows or OS X to do this build
+
+ #if defined(_WIN32)
+ #ifndef HAVE_OBJBASE_H
+ #define HAVE_OBJBASE_H
+ #endif
+ #ifndef HAVE_RPCDCE_H
+ #define HAVE_RPCDCE_H
+ #endif
+ #ifndef HAVE_WINSOCK2_H
+ #define HAVE_WINSOCK2_H
+ #endif
+ #ifndef HAVE_STRUCT_ADDRINFO
+ #define HAVE_STRUCT_ADDRINFO
+ #endif
+ #ifndef HAVE_SYS_TIMEB_H
+ #define HAVE_SYS_TIMEB_H
+ #endif
+ #ifndef HAVE_FTIME
+ #define HAVE_FTIME
+ #endif
+ #ifndef HAVE_WINDOWS_H
+ #define HAVE_WINDOWS_H
+ #endif
+ #else
+ #ifndef HAVE_UUID_UUID_H
+ #define HAVE_UUID_UUID_H
+ #endif
+ #ifndef HAVE_UUID_T
+ #define HAVE_UUID_T
+ #endif
+ #ifndef HAVE_PTHREAD_H
+ #define HAVE_PTHREAD_H
+ #endif
+ #endif
+
+#endif /* !defined(HAVE_CONFIG_H) */
+
+// Macro to mark attributes as unused
+#ifdef __GNUC__
+ #define AMQCPP_UNUSED __attribute__ ((__unused__))
+#else
+ #define AMQCPP_UNUSED
+#endif
+
+
+#endif /*ACTIVEMQ_UTIL_CONFIG_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/util/Date.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/util/Date.cpp?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/util/Date.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/util/Date.cpp Thu May 31 04:55:59 2007
@@ -0,0 +1,57 @@
+/*
+ * 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 <decaf/util/Date.h>
+#include <decaf/util/Config.h>
+
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#ifdef HAVE_SYS_TIMEB_H
+#include <sys/timeb.h>
+#endif
+
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+
+using namespace stl;
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+long long Date::getCurrentTimeMilliseconds(){
+
+#if defined (HAVE_GETTIMEOFDAY)
+ timeval tv;
+ gettimeofday (&tv, NULL);
+ return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
+#elif defined (HAVE_TIME)
+ return time (NULL) * 1000LL;
+#elif defined (HAVE_FTIME)
+ struct timeb t;
+ ftime (&t);
+ return (t.time * 1000LL) + t.millitm;
+#else
+
+ // This platform doesn't support any of the standard time methods
+ // ... should never get here.
+ #error "No current time function available on the local platform";
+
+#endif
+}
+
Added: activemq/activemq-cpp/trunk/src/decaf/util/Date.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/util/Date.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/util/Date.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/util/Date.h Thu May 31 04:55:59 2007
@@ -0,0 +1,128 @@
+/*
+ * 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 _DECAF_UTIL_DATE_H_
+#define _DECAF_UTIL_DATE_H_
+
+namespace activemq{
+namespace util{
+
+ /**
+ * Wrapper class around a time value in milliseconds. This
+ * class is comparable to Java's java.util.Date class.
+ */
+ class Date
+ {
+ private:
+
+ /**
+ * The underlying time value in milliseconds�
+ */
+ long long time;
+
+ public:
+
+ /**
+ * Default constructor - sets time to now.
+ */
+ Date(){
+ time = getCurrentTimeMilliseconds();
+ }
+
+ /**
+ * Constructs the date with a given time value.
+ * @param milliseconds The time in milliseconds;
+ */
+ Date( long long milliseconds ){
+ this->time = milliseconds;
+ }
+
+ /**
+ * Copy constructor.
+ */
+ Date( const Date& source ){
+ (*this) = source;
+ }
+
+ virtual ~Date(){}
+
+ /**
+ * Gets the underlying time.
+ * @return The underlying time value in milliseconds.
+ */
+ long long getTime() const{
+ return time;
+ }
+
+ /**
+ * Sets the underlying time.
+ * @param milliseconds The underlying time value in
+ * milliseconds.
+ */
+ void setTime( long long milliseconds ){
+ this->time = milliseconds;
+ }
+
+ /**
+ * Determines wether or not this date falls after the
+ * specified time.
+ * @param when The date to compare
+ * @return true if this date falls after when.
+ */
+ bool after( Date& when ) const{
+ return time > when.time;
+ }
+
+ /**
+ * Determines wether or not this date falls before the
+ * specified time.
+ * @param when The date to compare
+ * @return true if this date falls before when.
+ */
+ bool before( Date& when ) const{
+ return time < when.time;
+ }
+
+ /**
+ * Determines wether or not this date is equal to the
+ * specified time.
+ * @param when The date to compare
+ * @return true if this date is equal to when.
+ */
+ bool equals( Date& when ) const{
+ return time == when.time;
+ }
+
+ /**
+ * Assignment operator.
+ */
+ Date& operator =( const Date& source ){
+ this->time = source.time;
+ return *this;
+ }
+
+ /**
+ * Returns the current time in milliseconds. Comparable
+ * to Java's System.currentTimeMillis method.
+ * @return The current time in milliseconds.
+ */
+ static long long getCurrentTimeMilliseconds();
+ };
+
+}}
+
+#endif /*_DECAF_UTIL_DATE_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Concurrent.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Concurrent.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Concurrent.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Concurrent.h Thu May 31 04:55:59 2007
@@ -0,0 +1,63 @@
+/*
+ * 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 _DECAF_UTIL_CONCURRENT_CONCURRENT_H_
+#define _DECAF_UTIL_CONCURRENT_CONCURRENT_H_
+
+#include <decaf/util/concurrent/Lock.h>
+
+namespace decaf{
+namespace util{
+namespace concurrent{
+
+/**
+ * The synchronized macro defines a mechanism for snycronizing
+ * a scetion of code. The macro must be passed an object that
+ * implements the Syncronizable interface.
+ *
+ * The macro works by creating a for loop that will loop exactly
+ * once, creating a Lock object that is scoped to the loop. Once
+ * the loop conpletes and exits the Lock object goes out of scope
+ * releasing the lock on object W. For added safety the if else
+ * is used because not all compiles restrict the lifetime of
+ * loop variables to the loop, they will however restrict them
+ * to the scope of the else.
+ *
+ * The macro would be used as follows.
+ *
+ * <Syncronizable> X;
+ *
+ * somefunction()
+ * {
+ * syncronized(X)
+ * {
+ * // Do something that needs syncronizing.
+ * }
+ * }
+ */
+
+#define WAIT_INFINITE 0xFFFFFFFF
+
+#define synchronized(W) \
+ if(false){} \
+ else \
+ for( decaf::util::concurrent::Lock lock_W(W); \
+ lock_W.isLocked(); lock_W.unlock() )
+
+}}}
+
+#endif /*_DECAF_UTIL_CONCURRENT_CONCURRENT_H_*/
Added: activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Lock.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Lock.h?view=auto&rev=543121
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Lock.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Lock.h Thu May 31 04:55:59 2007
@@ -0,0 +1,125 @@
+/*
+ * 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 _DECAF_UTIL_CONCURRENT_LOCK_H_
+#define _DECAF_UTIL_CONCURRENT_LOCK_H_
+
+#include <decaf/lang/Exception.h>
+#include <decaf/util/concurrent/Synchronizable.h>
+
+namespace decaf{
+namespace util{
+namespace concurrent{
+
+ /**
+ * A wrapper class around a given synchronization mechanism that
+ * provides automatic release upon destruction.
+ * @author Nathan Mittler
+ */
+ class Lock
+ {
+ private:
+
+ /**
+ * Flag to indicate whether or not this object has locked the
+ * sync object.
+ */
+ bool locked;
+
+ /**
+ * The synchronizable object to lock/unlock.
+ */
+ Synchronizable* syncObject;
+
+ public:
+
+ /**
+ * Constructor - initializes the object member and locks
+ * the object if desired.
+ * @param object The sync object to control
+ * @param intiallyLocked If true, the object will automatically
+ * be locked.
+ */
+ Lock( Synchronizable* object, const bool intiallyLocked = true )
+ {
+ try{
+ syncObject = object;
+ locked = false;
+
+ if( intiallyLocked )
+ {
+ lock();
+ }
+ }
+ DECAF_CATCH_RETHROW( lang::Exception )
+ DECAF_CATCHALL_THROW( lang::Exception )
+ }
+
+ /**
+ * Destructor - Unlocks the object if it is locked.
+ */
+ virtual ~Lock()
+ {
+ try{
+ if( locked )
+ {
+ syncObject->unlock();
+ }
+ }
+ DECAF_CATCH_RETHROW( lang::Exception )
+ DECAF_CATCHALL_THROW( lang::Exception )
+ }
+
+ /**
+ * Locks the object.
+ */
+ void lock()
+ {
+ try{
+ syncObject->lock();
+ locked = true;
+ }
+ DECAF_CATCH_RETHROW( lang::Exception )
+ DECAF_CATCHALL_THROW( lang::Exception )
+ }
+
+ /**
+ * Unlocks the object.
+ */
+ void unlock()
+ {
+ try{
+ if(locked)
+ {
+ syncObject->unlock();
+ locked = false;
+ }
+ }
+ DECAF_CATCH_RETHROW( lang::Exception )
+ DECAF_CATCHALL_THROW( lang::Exception )
+ }
+
+ /**
+ * Indicates whether or not the object is locked.
+ * @return true if the object is locked, otherwise false.
+ */
+ bool isLocked() const{ return locked; }
+ };
+
+}}}
+
+#endif // _DECAF_UTIL_CONCURRENT_LOCK_H_