Skip to content

Commit

Permalink
started simple file sending
Browse files Browse the repository at this point in the history
  • Loading branch information
czimbortibor committed Jan 9, 2017
1 parent ae6ea8f commit a831522
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 24 deletions.
11 changes: 11 additions & 0 deletions Client/Client.cpp
Expand Up @@ -105,3 +105,14 @@ void Client::onSendPrivateMessage(QString receiver, QString message, QString sen
std::string package = packaging.createPivatePackage(receiver.toStdString(), message.toStdString(), sender.toStdString());
sendPackage(QString::fromStdString(package));
}

void Client::onSendFile(QByteArray blob, int fileSize, QString receiver, QString sender) {
// send over the file_size package
std::string package = packaging.createFileSizePackage(fileSize);
sendPackage(QString::fromStdString(package));

tcpSocket->waitForBytesWritten();
tcpSocket->flush();
package = blob.toStdString();
sendPackage(QString::fromStdString(package));
}
2 changes: 2 additions & 0 deletions Client/Client.h
Expand Up @@ -7,6 +7,7 @@
#include <QIODevice>
#include <QTcpSocket>
#include <QDataStream>
#include <QThread>

#include "../util/Packaging.h"

Expand Down Expand Up @@ -47,6 +48,7 @@ public slots:
void onLogoutRequest();
void onGlobalPackage(QString message);
void onSendPrivateMessage(QString receiver, QString message, QString sender);
void onSendFile(QByteArray blob, int fileSize, QString receiver, QString sender);
};

#endif // CLIENT_H
2 changes: 1 addition & 1 deletion Client/LoginDialog.cpp
@@ -1,5 +1,5 @@
#include "LoginDialog.h"
#include "ui_logindialog.h"
#include "ui_LoginDialog.h"

#include <QDebug>

Expand Down
4 changes: 2 additions & 2 deletions Client/LoginDialog.ui
Expand Up @@ -9,8 +9,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>210</height>
<width>250</width>
<height>197</height>
</rect>
</property>
<property name="acceptDrops">
Expand Down
3 changes: 2 additions & 1 deletion Client/MainWindow.cpp
@@ -1,5 +1,5 @@
#include "MainWindow.h"
#include "ui_mainwindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
Expand All @@ -26,6 +26,7 @@ void MainWindow::onSignIn(QString username, QString password, QString serverAddr
this->password = password;
this->serverAddr = serverAddr;
this->port = port;
this->setWindowTitle(username);
initClient();
}

Expand Down
21 changes: 21 additions & 0 deletions Client/PrivateMessageDialog.cpp
Expand Up @@ -9,6 +9,9 @@ PrivateMessageDialog::PrivateMessageDialog(QWidget* parent, Client* client, QStr
connect(client, &Client::receivedPrivateMessage, this, &PrivateMessageDialog::onReceivedPrivateMessage);
connect(ui->btnSendMessage, &QPushButton::clicked, this, &PrivateMessageDialog::onSendMessageButtonClicked);
connect(this, &PrivateMessageDialog::sendPrivateMessage, client, &Client::onSendPrivateMessage);

connect(ui->btnSendFile, &QPushButton::clicked, this, &PrivateMessageDialog::onSendFile);
connect(this, &PrivateMessageDialog::sendFile, client, &Client::onSendFile);
}

PrivateMessageDialog::~PrivateMessageDialog() {
Expand All @@ -30,3 +33,21 @@ void PrivateMessageDialog::onSendMessageButtonClicked() {
QString output = sender + ": " + message;
ui->editRecvMessage->append(output);
}

void PrivateMessageDialog::onSendFile() {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"), "/home",
tr("All files (*)"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, "File open error",
tr("There was a problem with opening the file!"));
return;
}

QByteArray blob = file.readAll();
int fileSize = blob.size();
QString receiver = chatPartner;
QString sender = client->getUsername();

emit sendFile(blob, fileSize, receiver, sender);
}
6 changes: 6 additions & 0 deletions Client/PrivateMessageDialog.h
Expand Up @@ -2,6 +2,10 @@
#define PRIVATEMESSAGEDIALOG_HPP

#include <QDialog>
#include <QFileDialog>
#include <QFile>
#include <QByteArray>
#include <QMessageBox>

#include "Client.h"

Expand All @@ -27,10 +31,12 @@ class PrivateMessageDialog : public QDialog {
signals:
void closeDialog(QString username);
void sendPrivateMessage(QString receiver, QString message, QString sender);
void sendFile(QByteArray blob, int fileSize, QString receiver, QString sender);

public slots:
void onReceivedPrivateMessage(QString receiver, QString message, QString sender);
void onSendMessageButtonClicked();
void onSendFile();
};

#endif // PRIVATEMESSAGEDIALOG_HPP
23 changes: 17 additions & 6 deletions Client/PrivateMessageDialog.ui
Expand Up @@ -6,17 +6,17 @@
<rect>
<x>0</x>
<y>0</y>
<width>241</width>
<height>308</height>
<width>282</width>
<height>334</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<layout class="QGridLayout" name="gridLayout" rowstretch="0,1,0,0">
<item row="0" column="0" colspan="4">
<widget class="QTextEdit" name="editRecvMessage">
<property name="frameShape">
<enum>QFrame::Panel</enum>
Expand All @@ -29,7 +29,7 @@
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<item row="1" column="0" colspan="4">
<widget class="QTextEdit" name="editSendMessage">
<property name="frameShape">
<enum>QFrame::Panel</enum>
Expand Down Expand Up @@ -76,7 +76,7 @@ background-color: rgb(85, 255, 127);</string>
</property>
</widget>
</item>
<item row="2" column="2">
<item row="2" column="3">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -89,6 +89,17 @@ background-color: rgb(85, 255, 127);</string>
</property>
</spacer>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="btnSendFile">
<property name="styleSheet">
<string notr="true">
background-color: rgb(85, 255, 127);</string>
</property>
<property name="text">
<string>Attach file</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down
46 changes: 41 additions & 5 deletions Server/ClientThread.cpp
Expand Up @@ -52,9 +52,12 @@ void* ClientThread::run() {
} else if (request.compare("private_package") == 0) {
std::cout << "request: " << "private_package" << "\n";
onPrivateMessageRequest(package);
} else if (request.compare("file_size_package") == 0) {
std::cout << "request: " << "file transfer" << "\n";
onFileTransfer(package);
}

std::this_thread::sleep_for(std::chrono::milliseconds(5000));
//std::this_thread::sleep_for(std::chrono::milliseconds(5000));
}
}

Expand Down Expand Up @@ -145,6 +148,20 @@ void ClientThread::onPrivateMessageRequest(const std::string& package) {
sendPackageToTarget(package, receiver);
}

void ClientThread::onFileTransfer(const std::string& package) {
packaging.parsePackage(package);
int fileSize = stoi(packaging.getMessage());
std::cout << "file size: " << fileSize << "\n";

std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::string readPackage = readFilePackage(fileSize);
//std::cout << readPackage;
std::ofstream file;
file.open("../output.txt", std::ios::out);
file << readPackage;
file.close();
}

void ClientThread::onLogoutRequest() {
std::cout << "Disconnecting user..\n";
// TODO: remove the disconnected user from the online user's list
Expand All @@ -157,13 +174,32 @@ void ClientThread::onLogoutRequest() {

std::string ClientThread::readPackage() {
int buffSize = sizeof(messageBuff);
char msgBuff[1024];

int res = recv(acceptSocket, msgBuff, buffSize, 0);
if (res != 0) {
int res = recv(acceptSocket, messageBuff, buffSize, 0);
if (res > 0) {
std::cout << " bytes: " << res << "\n";
}
if (res < 0) {
else if (res == 0) {
tcpserver->removeClient(user.getUsername());
pthread_exit(NULL);
}
else {
errorMsg = "error when receiving the message from the client!\n";
error(errorMsg.c_str());
}
messageBuff[res] = '\0';
return messageBuff;
}

std::string ClientThread::readFilePackage(int fileSize) {
int buffSize = sizeof(messageBuff);
char msgBuff[fileSize];

int res = recv(acceptSocket, msgBuff, fileSize, 0);
if (res > 0) {
std::cout << "\n file bytes read: " << res << "\n";
}
else {
errorMsg = "error when receiving the message from the client!\n";
error(errorMsg.c_str());
}
Expand Down
8 changes: 7 additions & 1 deletion Server/ClientThread.h
Expand Up @@ -26,6 +26,8 @@
#include <list>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream>

#include "../util/Packaging.h"
#include "MyThread.h"
Expand All @@ -46,6 +48,7 @@ class ClientThread : public MyThread {
void onLogoutRequest();
void onGlobalMessageRequest(const std::string& package);
void onPrivateMessageRequest(const std::string& package);
void onFileTransfer(const std::string& package);

bool logoutRequest = false;

Expand All @@ -57,13 +60,16 @@ class ClientThread : public MyThread {

private:
int acceptSocket;
char messageBuff[1024];
char messageBuff[2048];

/** basic information about a user */
User user;
void sendPackage(const std::string& package);
/** reads the incoming package from the user */
std::string readPackage();
/** reads a file package which comes right after the file's size package and saves into an output file */
std::string readFilePackage(int fileSize);

/** sends the package to every online user*/
void sendPackageToAll(const std::string& package);
void sendPackageToTarget(const std::string& package, const std::string& target);
Expand Down
16 changes: 13 additions & 3 deletions Server/TCPServer.cpp
Expand Up @@ -75,9 +75,19 @@ void TCPServer::startServer() {
}
}

void TCPServer::removeClient(ClientThread& clientThread) {
//std::unique_ptr<ClientThread> clientPtr = static_cast<std::unique_ptr<ClientThread>>(&clientThread);
//usersPtr->remove(clientPtr);
// TODO: remove client
void TCPServer::removeClient(const std::string& username) {
/* std::cout << "nr of users: " << usersPtr->size() << "\n";
auto list = usersPtr.get();
std::list<std::unique_ptr<ClientThread>>::const_iterator iterator;
for (iterator = list->begin(); iterator != list->end(); ++iterator) {
std::string tmp = (**iterator).getUsername();
if (tmp.compare(username) == 0) {
break;
}
}
usersPtr->erase(iterator);
std::cout << "nr of users after delete: " << usersPtr->size() << "\n"; */
}

std::vector<std::string> TCPServer::getOnlineUsers() {
Expand Down
2 changes: 1 addition & 1 deletion Server/TCPServer.h
Expand Up @@ -28,7 +28,7 @@ class TCPServer {
virtual ~TCPServer();

void startServer();
void removeClient(ClientThread& clientThread);
void removeClient(const std::string& username);
std::vector<std::string> getOnlineUsers();

void lockMutex() { pthread_mutex_lock(&mutex); }
Expand Down
21 changes: 17 additions & 4 deletions util/Packaging.cpp
Expand Up @@ -66,16 +66,26 @@ std::string Packaging::createOnlineUsersPackage(const std::vector<std::string>&
// inserts the elements into the "joinedFields" stream, with the "," delimiter
std::copy(onlineUsers.begin(), onlineUsers.end(), std::ostream_iterator<std::string>(joinedFields, ","));
std::string usersList = joinedFields.str();
// unnecessary delimiter on the end of the string
//usersList[usersList.length() - 1] = '\0';
/*std::string usersList;
usersList.assign(tmpStr, 0, tmpStr.length() - 1);*/
this->receiver = "list_users";
this->message = usersList;
this->sender = "server";
return constructPackage();
}

std::string Packaging::createFileSizePackage(int fileSize) {
this->receiver = "file_size";
this->message = std::to_string(fileSize);
this->sender = sender;
return constructPackage();
}

std::string Packaging::createFilePackage(const std::string& blob, const std::string& receiver, const std::string& sender) {
this->receiver = receiver;
this->message = blob;
this->sender = sender;
return constructPackage();
}

std::string Packaging::constructPackage() {
std::string pckg = receiver + glue + glue + message + glue + sender + glue;
size_t len = pckg.length();
Expand Down Expand Up @@ -127,6 +137,9 @@ std::string Packaging::identifyRequest(std::string package) {
if (token.compare("list_users") == 0) {
return "online_users_package";
}
if (token.compare("file_size") == 0) {
return "file_size_package";
}
// erase token + glue
package.erase(0, pos + 1);
}
Expand Down
5 changes: 5 additions & 0 deletions util/Packaging.h
Expand Up @@ -33,6 +33,7 @@
* - disconnect: message = "disconnect"
* - global: receiver = "global"
* - list_users: receiver = "list_users"
* - file_size: receiver = "file_size"
*/
class Packaging {
public:
Expand All @@ -53,6 +54,10 @@ class Packaging {
std::string createPivatePackage(const std::string& receiver, const std::string& message, const std::string& sender);
/** package used to send the online user's list to a user */
std::string createOnlineUsersPackage(const std::vector<std::string>& onlineUsers);
/** package used to signal the incoming file's size */
std::string createFileSizePackage(int fileSize);
/** package used to send a binary stream */
std::string createFilePackage(const std::string& blob, const std::string& receiver, const std::string& sender);

void parsePackage(std::string package);

Expand Down

0 comments on commit a831522

Please sign in to comment.