passing socket as int instead of &int (why did I do that on the first place. Lifting up SimpleMessage to test/SimpleMessage.hpp

master
Denes Matetelki 12 years ago
parent 518accb0c8
commit ef31a5f571

@ -25,7 +25,7 @@ public:
bool connect(addrinfo *servinfo); bool connect(addrinfo *servinfo);
bool bind(addrinfo *servinfo); bool bind(addrinfo *servinfo);
bool listen( const int maxPendingQueueLen = 64 ); bool listen( const int maxPendingQueueLen = 64 );
bool accept( int &client_socket ); bool accept( int client_socket );
bool send( const void *message, const int lenght ); bool send( const void *message, const int lenght );
bool receive ( void* buffer, const int bufferLen, ssize_t *msgLen ); bool receive ( void* buffer, const int bufferLen, ssize_t *msgLen );

@ -39,7 +39,7 @@ public:
bool bind(); bool bind();
bool listen( const int maxPendingQueueLen = 64 ); bool listen( const int maxPendingQueueLen = 64 );
bool accept(int &client_socket); bool accept(int client_socket);
bool closed() const; bool closed() const;
int getSocket() const; int getSocket() const;

@ -19,7 +19,7 @@ public:
virtual bool listen( const int maxPendingQueueLen = 64 ) = 0; virtual bool listen( const int maxPendingQueueLen = 64 ) = 0;
/// @todo move accept and poll here /// @todo move accept and poll here
virtual int accept() = 0; virtual bool accept(int socket) = 0;
// virtual bool poll() = 0; // virtual bool poll() = 0;

@ -36,7 +36,7 @@ public:
bool bind(); bool bind();
bool listen( const int maxPendingQueueLen = 64 ); bool listen( const int maxPendingQueueLen = 64 );
bool accept(int &client_socket); bool accept(int client_socket);
int getSocket() const; int getSocket() const;
void setState(const State state); void setState(const State state);

@ -48,7 +48,7 @@ public:
bool bind(); bool bind();
bool listen( const int maxPendingQueueLen = 64 ); bool listen( const int maxPendingQueueLen = 64 );
bool accept(int &client_socket); bool accept(int client_socket);
bool closed() const; bool closed() const;

@ -80,8 +80,8 @@ void Poll::acceptClient()
{ {
TRACE; TRACE;
int client_socket(-1); int client_socket = -1;
if (!m_connection->accept(client_socket)) if ( !m_connection->accept( client_socket ) )
return; return;
StreamConnection *streamConnection = dynamic_cast<StreamConnection*>( StreamConnection *streamConnection = dynamic_cast<StreamConnection*>(
@ -124,7 +124,7 @@ void Poll::removeTimeoutedConnections()
ConnectionMap::iterator it; ConnectionMap::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ) for (it = m_connections.begin(); it != m_connections.end(); )
if (it->second->closed()) { if (it->second->disconnect()) {
it = removeConnection(it->second->getSocket(), it++); it = removeConnection(it->second->getSocket(), it++);
} else { } else {
++it; ++it;

@ -132,7 +132,7 @@ bool Socket::listen(const int maxPendingQueueLen)
} }
bool Socket::accept(int &client_socket) bool Socket::accept(int client_socket)
{ {
TRACE; TRACE;
sockaddr clientAddr; sockaddr clientAddr;

@ -106,7 +106,7 @@ bool SslConnection::listen( const int maxPendingQueueLen )
} }
bool SslConnection::accept(int &client_socket) bool SslConnection::accept(int client_socket)
{ {
TRACE; TRACE;

@ -109,7 +109,7 @@ bool TcpConnection::listen( const int maxPendingQueueLen )
} }
bool TcpConnection::accept(int &client_socket) bool TcpConnection::accept(int client_socket)
{ {
TRACE; TRACE;
return m_socket.accept(client_socket); return m_socket.accept(client_socket);

@ -66,7 +66,7 @@ bool TimedTcpConnection::listen( const int maxPendingQueueLen )
} }
bool TimedTcpConnection::accept(int &client_socket) bool TimedTcpConnection::accept(int client_socket)
{ {
TRACE; TRACE;
return m_tcpConnection->accept(client_socket); return m_tcpConnection->accept(client_socket);

@ -0,0 +1,63 @@
#ifndef SIMPLEMESSAGE_HPP
#define SIMPLEMESSAGE_HPP
#include "Message.hpp"
class SimpleMessage : public Message
{
public:
SimpleMessage( void *msgParam = 0)
: Message(msgParam)
{
TRACE;
}
bool buildMessage( const void *msgPart,
const size_t msgLen )
{
TRACE;
m_buffer = std::string( (const char*) msgPart, msgLen );
onMessageReady();
return true;
}
void onMessageReady()
{
TRACE;
LOG_BEGIN(Logger::INFO)
LOG_PROP("message", m_buffer)
LOG_END("Got reply from server.");
*( static_cast<bool*>(m_param) ) = true;
if (m_connection != 0)
m_connection->send(m_buffer.c_str(), m_buffer.length());
m_buffer.clear();
}
Message* clone()
{
TRACE;
return new SimpleMessage(m_param);
}
std::string getBuffer()
{
TRACE;
return m_buffer;
}
protected:
size_t getExpectedLength()
{
TRACE;
return 0;
}
};
#endif // SIMPLEMESSAGE_HPP

@ -3,67 +3,10 @@
#include "Fixture.hpp" #include "Fixture.hpp"
#include "Message.hpp" #include "Message.hpp"
#include "SimpleMessage.hpp"
#include "Connection.hpp" #include "Connection.hpp"
class SimpleMessage : public Message
{
public:
SimpleMessage( void *msgParam = 0)
: Message(msgParam)
{
TRACE;
}
bool buildMessage( const void *msgPart,
const size_t msgLen )
{
TRACE;
m_buffer = std::string( (const char*) msgPart, msgLen );
onMessageReady();
return true;
}
void onMessageReady()
{
TRACE;
LOG_BEGIN(Logger::INFO)
LOG_PROP("message", m_buffer)
LOG_END("Got reply from server.");
*( static_cast<bool*>(m_param) ) = true;
if (m_connection != 0)
m_connection->send(m_buffer.c_str(), m_buffer.length());
m_buffer.clear();
}
Message* clone()
{
TRACE;
return new SimpleMessage(m_param);
}
std::string getBuffer()
{
TRACE;
return m_buffer;
}
protected:
size_t getExpectedLength()
{
TRACE;
return 0;
}
}; // SimpleMessage
class TestMessage : public CxxTest::TestSuite class TestMessage : public CxxTest::TestSuite
{ {
private: private:

@ -27,7 +27,7 @@ public:
void testErrorCheck( void ) void testErrorCheck( void )
{ {
TEST_HEADER; TEST_HEADER;
Mutex m(PTHREAD_MUTEX_ERRORCHECK); Mutex m(Mutex::ErrorCheck);
TS_ASSERT_EQUALS ( m.lock() , 0 ); TS_ASSERT_EQUALS ( m.lock() , 0 );
TS_ASSERT_EQUALS ( m.lock(), EDEADLK ); TS_ASSERT_EQUALS ( m.lock(), EDEADLK );
@ -38,7 +38,7 @@ public:
void testRecursive( void ) void testRecursive( void )
{ {
TEST_HEADER; TEST_HEADER;
Mutex m(PTHREAD_MUTEX_RECURSIVE); Mutex m(Mutex::Recursive);
TS_ASSERT_EQUALS ( m.lock() , 0 ); TS_ASSERT_EQUALS ( m.lock() , 0 );
TS_ASSERT_EQUALS ( m.lock() , 0 ); TS_ASSERT_EQUALS ( m.lock() , 0 );

@ -1,5 +1,7 @@
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include <unistd.h>
#include "Common.hpp" #include "Common.hpp"
#include "Fixture.hpp" #include "Fixture.hpp"

@ -32,7 +32,7 @@ private:
bool connect() { return true; } bool connect() { return true; }
bool disconnect() { return true; } bool disconnect() { return true; }
bool listen(const int) { return true; } bool listen(const int) { return true; }
bool accept(int &) { return true; } bool accept(int) { return true; }
bool closed() const { return true; } bool closed() const { return true; }
}; // StreamConnection }; // StreamConnection

@ -1,10 +1,12 @@
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include <unistd.h>
#include "Fixture.hpp" #include "Fixture.hpp"
#include "Thread.hpp" #include "Thread.hpp"
#include "TcpConnection.hpp" #include "TcpConnection.hpp"
#include "test_Message.hpp" // SimpleMessage #include "SimpleMessage.hpp"
#include "SocketClient.hpp" #include "SocketClient.hpp"
@ -82,7 +84,8 @@ public:
{ {
TEST_HEADER; TEST_HEADER;
SimpleMessage message; bool finished = false;
SimpleMessage message(&finished);
std::string host("localhost"); std::string host("localhost");
std::string service("1234"); std::string service("1234");
@ -104,16 +107,18 @@ public:
TS_ASSERT_EQUALS(socketClient.send( msg1.c_str(), msg1.length()), true); TS_ASSERT_EQUALS(socketClient.send( msg1.c_str(), msg1.length()), true);
// wait for the complate &handled reply // wait for the complate &handled reply
struct timespec tm = {0,1000}; // struct timespec tm = {0,1000};
while ( !finished && socketClient.isPolling() ) while ( !finished && socketClient.isPolling() )
nanosleep(&tm, &tm) ; sleep(1);
// nanosleep(&tm, &tm) ;
socketClient.disconnect(); socketClient.disconnect();
TS_ASSERT_EQUALS(tcpServerThread2.bind(), false); // TS_ASSERT_EQUALS(tcpServerThread2.bind(), false);
TS_ASSERT_EQUALS(tcpServerThread1.disconnect(), false); TS_ASSERT_EQUALS(tcpServerThread1.disconnect(), false);
TS_ASSERT_EQUALS(tcpServerThread2.disconnect(), false); // TS_ASSERT_EQUALS(tcpServerThread2.disconnect(), false);
} }
}; };

Loading…
Cancel
Save