Skip to content

Commit

Permalink
Merge pull request #50 from Octopus773/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Octopus773 authored Oct 17, 2021
2 parents 3101259 + d36ac0c commit dc0068d
Show file tree
Hide file tree
Showing 36 changed files with 229 additions and 1,572 deletions.
3 changes: 2 additions & 1 deletion Client/src/Network/QtTCPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

namespace Babel {

QtTCPConnection::QtTCPConnection(QWidget *parent)
QtTCPConnection::QtTCPConnection(QWidget *parent, std::function<void(void)> onConnect)
: QObject(parent),
_socket(new QTcpSocket(parent)),
_bytesRead(0),
_connectionId(0) {
QObject::connect(this->_socket, &QTcpSocket::readyRead, this, &QtTCPConnection::readMessage);
QObject::connect(this->_socket, &QTcpSocket::connected, onConnect);
QObject::connect(this->_socket, &QTcpSocket::errorOccurred, [parent, this](int socketError) {
switch (socketError) {
case QAbstractSocket::HostNotFoundError:
Expand Down
3 changes: 2 additions & 1 deletion Client/src/Network/QtTCPConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Network/RFCCodes.hpp"
#include <QTcpSocket>
#include <QtWidgets>
#include <functional>
#include <QString>
#include <QObject>

Expand Down Expand Up @@ -51,7 +52,7 @@ namespace Babel {
uint16_t getPeerPort() const override;

//! @brief ctor
explicit QtTCPConnection(QWidget *parent = nullptr);
explicit QtTCPConnection(QWidget *parent = nullptr, std::function<void(void)> onConnect = [](){});

private:

Expand Down
13 changes: 9 additions & 4 deletions Client/src/Network/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ void Babel::UDPSocket::readPending() {
if (this->_socket->hasPendingDatagrams()) {
QNetworkDatagram datagram = this->_socket->receiveDatagram();

auto *packetRecu = reinterpret_cast<Babel::AudioPacket *> (datagram.data().data());
std::uint64_t timestamp = packetRecu->timestamp;
std::int32_t sizeRecu = packetRecu->size;
auto *receivedPacket = reinterpret_cast<Babel::AudioPacket *> (datagram.data().data());
//std::uint64_t timestamp = receivedPacket->timestamp;
std::int32_t sizeRecu = receivedPacket->size;
std::vector<unsigned char> encodedReceived(sizeRecu);
std::memcpy(encodedReceived.data(), packetRecu->data, sizeRecu);
std::memcpy(encodedReceived.data(), receivedPacket->data, sizeRecu);

std::vector<std::int16_t> decodedData(_audio->getFramesPerBuffer(), 0);
_codec->decode(encodedReceived.data(), decodedData.data(), sizeRecu);
Expand All @@ -60,4 +60,9 @@ Babel::UDPSocket::~UDPSocket() {
void Babel::UDPSocket::close() {
std::lock_guard<std::mutex> lockGuard(_mutex);
this->_socket->close();
}

uint16_t Babel::UDPSocket::getLocalPort() const
{
return this->_socket->localPort();
}
5 changes: 4 additions & 1 deletion Client/src/Network/UDPSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "Audio/IAudioManager.hpp"

namespace Babel {
//! @class wrappers around Qt UDP socket, ICodec and Portaudio
//! @brief wrapper around Qt UDP socket, ICodec and Portaudio
class UDPSocket : public QObject {
Q_OBJECT
public:
Expand All @@ -32,6 +32,9 @@ namespace Babel {
//! @brief reads the incoming datagrams when they arrive
void readPending();

//! @brief get the port that the UDPSocket is listening
uint16_t getLocalPort() const;

private:
//! @brief mutex for closing the socket
std::mutex _mutex;
Expand Down
5 changes: 5 additions & 0 deletions Client/src/SoundHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ void Babel::SoundHandler::addClient(const std::string &userid, const std::string
void Babel::SoundHandler::removeClient(const std::string &userid) {
std::scoped_lock lock(this->_userlist_mtx);
this->_userlist.erase(userid);
}

uint16_t Babel::SoundHandler::getLocalPort() const
{
return this->_socket->getLocalPort();
}
3 changes: 3 additions & 0 deletions Client/src/SoundHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace Babel {
//! @brief remove a client to the current call
void removeClient(const std::string &userid);

//! @brief get the port that the soundHandler is listening
uint16_t getLocalPort() const;

private:
//! @brief pointer to audio input and outputs
std::shared_ptr<Babel::IAudioManager> _audio;
Expand Down
27 changes: 15 additions & 12 deletions Client/src/Windows/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "Network/Message.hpp"
#include "Network/RFCCodes.hpp"
#include <iostream>
#include <memory>
#include <QMainWindow>
#include "Utilities/Utilities.hpp"
#include <QPushButton>
Expand All @@ -17,7 +16,11 @@ namespace Babel {

HomePage::HomePage()
: _window(new QMainWindow()),
_audio(this->udpPort),
_connection(nullptr, [this]() {
this->_ui.page_login->setDisabled(false);
this->changeCurrentUITab(this->_ui.page_login);
}),
_audio(this->startUdpPort),
_messageHandlers({
{RFCCodes::Login,
MessageHandler{[this](const Message<RFCCodes> &m) {
Expand Down Expand Up @@ -65,7 +68,8 @@ namespace Babel {
}}
}
}),
_currentCallId(CurrentlyNotInCall) {
_currentCallId(CurrentlyNotInCall)
{
this->_ui.setupUi(this->_window);

this->_connection.setCallbackOnMessage([this](Message<RFCCodes> m) {
Expand All @@ -92,11 +96,11 @@ namespace Babel {
this->_ui.page_login->setDisabled(true);
this->_ui.page_call->setDisabled(true);
this->_ui.page_server->setDisabled(true);
std::cout << this->_audio.getLocalPort() << std::endl;
}

void HomePage::doConnect() {
this->_connection.connect(this->_ui.input_address->text().toStdString(), this->_ui.input_port->value());
this->_ui.page_login->setDisabled(false);
}

void HomePage::doLogin() {
Expand Down Expand Up @@ -152,7 +156,7 @@ namespace Babel {
}
this->_ui.page_server->setDisabled(false);
this->doListUsers();
this->changeCurrentUITab("page_server");
this->changeCurrentUITab(this->_ui.page_server);
}

void HomePage::sendHandler(const Message<RFCCodes> &m) {
Expand Down Expand Up @@ -241,6 +245,7 @@ namespace Babel {

if (codeId != 1) {
std::cout << "error: callUser " << desc << std::endl;
QMessageBox::warning(nullptr, tr("Babel"), tr(desc.data()));
return;
}

Expand All @@ -254,9 +259,7 @@ namespace Babel {
message.header.codeId = RFCCodes::JoinCall;
message << callId;

std::string address = this->_address;
uint16_t port = this->udpPort;
this->doJoinCall(callId, address, port);
this->doJoinCall(callId, this->_address, this->_audio.getLocalPort());
}

void HomePage::onJoinCall(const Message<RFCCodes> &m) {
Expand Down Expand Up @@ -295,7 +298,7 @@ namespace Babel {
}

this->_ui.page_call->setDisabled(false);
this->changeCurrentUITab("page_call");
this->changeCurrentUITab(this->_ui.page_call);
this->_audio.startCall();
// switch to call tab
}
Expand Down Expand Up @@ -323,7 +326,7 @@ namespace Babel {
this->_currentCallId = CurrentlyNotInCall;
this->sendHandler(m);
this->_ui.page_call->setDisabled(true);
this->changeCurrentUITab("page_server");
this->changeCurrentUITab(this->_ui.page_server);
}

void HomePage::onBasicResponse(const Message<RFCCodes> &m) {
Expand Down Expand Up @@ -368,14 +371,14 @@ namespace Babel {

QMessageBox msgBox;
msgBox.setText(
QString::fromStdString("You're receiving an incoming from " + invitator + "\nDo you accept it ?"));
QString::fromStdString("You're receiving an incoming call from " + invitator + "\nDo you accept it ?"));
msgBox.setWindowTitle("Babel");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
msgBox.setIcon(QMessageBox::Question);
switch (msgBox.exec()) {
case QMessageBox::Yes:
this->doJoinCall(callId, this->_address, this->udpPort);
this->doJoinCall(callId, this->_address, this->_audio.getLocalPort());
break;
case QMessageBox::No:
default:
Expand Down
13 changes: 8 additions & 5 deletions Client/src/Windows/HomePage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ namespace Babel
//! @brief ctor
HomePage();

//! @brief dtor
~HomePage() override= default;

//! @brief The port to receive udp audio packets
const uint16_t udpPort = 23476;
//! @note setting it to zero will let the OS choose a random available port
const uint16_t startUdpPort = 0;

private:
//! @brief The display window
QMainWindow *_window;

//! @brief The UI Class
Ui_babelHome _ui{};
Ui::babelHome _ui{};

//! @brief tcp connection handler
QtTCPConnection _connection;
Expand Down Expand Up @@ -97,10 +101,9 @@ namespace Babel
void updateDisplaySelectedUser();

//! @brief Change the currently selected tab
inline void changeCurrentUITab(const std::string &tabName)
inline void changeCurrentUITab(QWidget *tab)
{
this->_ui.tab_handler->setCurrentWidget(
this->_ui.tab_handler->findChild<QWidget *>(QString::fromStdString(tabName)));
this->_ui.tab_handler->setCurrentWidget(tab);
};

//! @brief All the necessary information to handle a response
Expand Down
10 changes: 0 additions & 10 deletions Client/src/gui/QtBabelGui.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions Client/src/gui/QtBabelGui.hpp

This file was deleted.

48 changes: 0 additions & 48 deletions Client/src/gui/QtHomePage.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions Client/src/gui/QtHomePage.hpp

This file was deleted.

Loading

0 comments on commit dc0068d

Please sign in to comment.