Skip to content

Commit

Permalink
Added library and tests for using the stomp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxcucus committed Nov 19, 2023
1 parent 03cb0fb commit 6165de2
Show file tree
Hide file tree
Showing 12 changed files with 813 additions and 2 deletions.
5 changes: 3 additions & 2 deletions c_plus_plus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)

find_package(Qt6 COMPONENTS Core Widgets Quick Network Test)
find_package(Qt6 COMPONENTS Core Widgets Quick Network Test WebSockets)

option(With_Asan "Compile with Address Sanitizer support" OFF)

Expand All @@ -26,9 +26,10 @@ add_subdirectory(PlanesQML)
add_subdirectory(singleround)
add_subdirectory(multiround)
add_subdirectory(bcrypt)
add_subdirectory(stomp)
add_subdirectory(tests/commobjtests)
add_subdirectory(tests/singleroundtests)

add_subdirectory(tests/stomptests)

set(ROOT_PATH ${Qt6_DIR}/../../../)

Expand Down
38 changes: 38 additions & 0 deletions c_plus_plus/stomp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required (VERSION 3.10)
project (libStomp)

cmake_policy(SET CMP0020 NEW)
cmake_policy(SET CMP0043 NEW)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/
${Qt6Core_INCLUDE_DIRS}
${Qt6Network_INCLUDE_DIRS}
${Qt6WebSockets_INCLUDE_DIRS})


set(STOMP_HEADR
stompframe.h
stompclient.h
stompframecreator.h
)

set(STOMP_SRCS
stompframe.cpp
stompclient.cpp
stompframecreator.cpp
)


add_library(libStomp SHARED ${STOMP_HEADR} ${STOMP_SRCS})

target_link_libraries(libStomp
Qt6::Widgets
Qt6::Network
Qt6::WebSockets)

if (UNIX)
install(TARGETS libStomp DESTINATION lib)
else (UNIX)
install(TARGETS libStomp DESTINATION bin)
endif (UNIX)
108 changes: 108 additions & 0 deletions c_plus_plus/stomp/stompclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "stompclient.h"

StompClient::StompClient() {
QObject::connect(&m_Socket, &QWebSocket::connected, this, &StompClient::socketConnected);
QObject::connect(&m_Socket, &QWebSocket::disconnected, this, &StompClient::socketDisconnected);
QObject::connect(&m_Socket, &QWebSocket::binaryMessageReceived, this, &StompClient::binaryMessageReceived);
QObject::connect(&m_Socket, &QWebSocket::textMessageReceived, this, &StompClient::textMessageReceived);
QObject::connect(&m_Socket, &QWebSocket::binaryFrameReceived, this, &StompClient::binaryFrameReceived);
QObject::connect(&m_Socket, &QWebSocket::textFrameReceived, this, &StompClient::textFrameReceived);
QObject::connect(&m_Socket, &QWebSocket::errorOccurred, this, &StompClient::errorOccurred);
QObject::connect(&m_Socket, &QWebSocket::alertReceived, this, &StompClient::alertReceived);
QObject::connect(&m_Socket, &QWebSocket::authenticationRequired, this, &StompClient::authenticationRequired);
QObject::connect(&m_Socket, &QWebSocket::handshakeInterruptedOnError, this, &StompClient::handshakeInterruptedOnError);
QObject::connect(&m_Socket, &QWebSocket::peerVerifyError, this, &StompClient::peerVerifyError);
QObject::connect(&m_Socket, &QWebSocket::proxyAuthenticationRequired, this, &StompClient::proxyAuthenticationRequired);
QObject::connect(&m_Socket, &QWebSocket::sslErrors, this, &StompClient::sslErrors);
QObject::connect(&m_Socket, &QWebSocket::stateChanged, this, &StompClient::stateChanged);

}

int StompClient::getState() {
return m_Socket.state();
}

void StompClient::setUrl(const QString& url) {
m_Url = url;
}

void StompClient::socketConnected() {
qDebug() << "Connected to socket";
qDebug() << "Socket version " << m_Socket.version();
qDebug() << "Max incoming message size " << QWebSocket::maxIncomingMessageSize();
emit clientConnected();
}

void StompClient::socketDisconnected() {
qDebug() << "Disconnected from socket";
}


void StompClient::connectToServer() {
if (m_Url.isEmpty())
return;

qDebug() << "Connect to " << m_Url;
m_Socket.open(m_Url);
}

bool StompClient::sendFrame(std::shared_ptr<StompFrame> stompFrame) {
if (!stompFrame->isBinaryBody()) {
QString str = stompFrame->convertToQString();
m_Socket.sendTextMessage(str);
qDebug() << "Send stomp frame " << str;
return true;
}

QByteArray ba = stompFrame->convertToByteArray();
m_Socket.sendBinaryMessage(ba);
return true;
}

void StompClient::binaryMessageReceived(const QByteArray& ba) {
qDebug() << "Received stomp binary message " << ba.size() << " bytes ";
}

void StompClient::textMessageReceived(const QString& message) {
qDebug() << "Received stomp text message " << message;
}

void StompClient::textFrameReceived(const QString &frame, bool isLastFrame) {
qDebug() << "Received stomp text frame";
}

void StompClient::binaryFrameReceived(const QByteArray &frame, bool isLastFrame) {
qDebug() << "Received stomp binary frame";
}

void StompClient::errorOccurred(QAbstractSocket::SocketError error) {
qDebug() << "Error occured " << error;
}

void StompClient::alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description) {
qDebug() << "Alert reveived";
}

void StompClient::authenticationRequired(QAuthenticator* authenticator) {
qDebug() << "Authentication required";
}

void StompClient::handshakeInterruptedOnError(const QSslError &error) {
qDebug() << "Handshkage Interrupted on Error";
}

void StompClient::peerVerifyError(const QSslError& error) {
qDebug() << "Peer verify error";
}

void StompClient::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) {
qDebug() << "proxyAuthenticationRequired";
}

void StompClient::sslErrors(const QList<QSslError> &errors) {
qDebug() << "sslErrors";
}

void StompClient::stateChanged(QAbstractSocket::SocketState state) {
qDebug() << "Socket state: " << state;
}
62 changes: 62 additions & 0 deletions c_plus_plus/stomp/stompclient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef __STOMPCLIENT_H_
#define __STOMPCLIENT_H_

#include <QWebSocket>
#include <QString>
#include <QObject>

#include "stompframe.h"

class StompClient : public QObject {
Q_OBJECT

public:
StompClient();
void connectToServer();
void setUrl(const QString& url);

bool sendFrame(std::shared_ptr<StompFrame>);

signals:
void clientConnected();

private slots:
void socketConnected();
void socketDisconnected();
void binaryMessageReceived(const QByteArray& ba);
void textMessageReceived(const QString& message);
void textFrameReceived(const QString &frame, bool isLastFrame);
void binaryFrameReceived(const QByteArray &frame, bool isLastFrame);
void errorOccurred(QAbstractSocket::SocketError error);
void alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description);
void authenticationRequired(QAuthenticator *authenticator);
void handshakeInterruptedOnError(const QSslError &error);
void peerVerifyError(const QSslError &error);
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
void sslErrors(const QList<QSslError> &errors);
void stateChanged(QAbstractSocket::SocketState state);

private:
int getState();

private:
QWebSocket m_Socket;
QString m_Url;

};















#endif
Loading

0 comments on commit 6165de2

Please sign in to comment.