From a99bfcead9a26af11e475e1e796e83561e73cd79 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Feb 2020 22:37:49 +0700 Subject: [PATCH 1/5] Changed function name --- src/Receiver.cpp | 8 ++++---- src/Sender.cpp | 8 ++++---- src/Utils.hpp | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Receiver.cpp b/src/Receiver.cpp index c23e143..b475f14 100644 --- a/src/Receiver.cpp +++ b/src/Receiver.cpp @@ -33,7 +33,7 @@ void checkParts() { while (ttl && emptyParts.size() > 0) { for (auto index : emptyParts) { snprintf(buffer, 7, "RESEND"); // - Utils::writeBytesFromInt(buffer + 6, index, 4); // Create request packet + Utils::writeBytesFromNumber(buffer + 6, index, 4); // Create request packet sendto(_socket, buffer, 10, 0, (sockaddr*) &broadcast_address,// sizeof(broadcast_address)); // @@ -69,7 +69,7 @@ void run(cxxopts::ParseResult &options) { ttl = ttl_max; // Update ttl if (strncmp(buffer, "NEW_PACKET", 10) == 0) { - file_length = Utils::getIntFromBytes(buffer + 10, 4); // Read section "file length" + file_length = Utils::getNumberFromBytes(buffer + 10, 4); // Read section "file length" file = new char[file_length]; memset(file, 0, file_length); @@ -77,8 +77,8 @@ void run(cxxopts::ParseResult &options) { std::cout << "Receive information about new file size: " << file_length << std::endl; std::cout << "Number of parts: " << int((float)file_length / (float)mtu + 0.5) << std::endl; } else if (strncmp(buffer, "TRANSFER", 8) == 0) { - int part = Utils::getIntFromBytes(buffer + 8, 4); // Read section "index" - int size = Utils::getIntFromBytes(buffer + 12, 4); // Read section "size" + int part = Utils::getNumberFromBytes(buffer + 8, 4); // Read section "index" + int size = Utils::getNumberFromBytes(buffer + 12, 4); // Read section "size" parts.insert(part); std::cout << "Receive " << part << " part with size " << size << std::endl; diff --git a/src/Sender.cpp b/src/Sender.cpp index 9f20036..02aa15c 100644 --- a/src/Sender.cpp +++ b/src/Sender.cpp @@ -16,8 +16,8 @@ void sendPart(int part_index) { if (packet_length > mtu) packet_length = mtu; // snprintf(buffer, 9, "TRANSFER"); - Utils::writeBytesFromInt(buffer + 8, (size_t)part_index, 4); // Write section "number" - Utils::writeBytesFromInt(buffer + 12, (size_t)packet_length, 4); // Write section "length" + Utils::writeBytesFromNumber(buffer + 8, (size_t)part_index, 4); // Write section "number" + Utils::writeBytesFromNumber(buffer + 12, (size_t)packet_length, 4); // Write section "length" memcpy(buffer + 16, (void *)(intptr_t)(file + part_index * mtu), packet_length); // Write section "data" // Sending part to the broadcast address @@ -49,7 +49,7 @@ void run(cxxopts::ParseResult &options) { std::cout << "Ok: File successfully copied to RAM" << std::endl; snprintf(buffer, 11, "NEW_PACKET"); // - Utils::writeBytesFromInt(buffer + 10, file_length, 4); // Sending information + Utils::writeBytesFromNumber(buffer + 10, file_length, 4); // Sending information sendto(_socket, buffer, 14, 0, (sockaddr*) &broadcast_address, // about size of new file sizeof(broadcast_address)); // @@ -85,7 +85,7 @@ void run(cxxopts::ParseResult &options) { } if (strncmp(buffer, "RESEND", 6) == 0) { - int part = Utils::getIntFromBytes(buffer + 6, 4); + int part = Utils::getNumberFromBytes(buffer + 6, 4); auto now = std::chrono::system_clock::now(); auto now_ms = std::chrono::time_point_cast(now); diff --git a/src/Utils.hpp b/src/Utils.hpp index 61d40df..c83edba 100644 --- a/src/Utils.hpp +++ b/src/Utils.hpp @@ -37,13 +37,13 @@ namespace Utils { * @param buffer - bytes array * @param count - bytes count */ - int getIntFromBytes(char* buffer, int count) { - int value = 0; + size_t getNumberFromBytes(char* buffer, int count) { + size_t number = 0; for (int i = 0; i < count; i++) { - value = value << 8; - value = value | (buffer[i] & 0xFF); + number = number << 8; + number = number | (buffer[i] & 0xFF); } - return value; + return number; } /** @@ -52,9 +52,9 @@ namespace Utils { * @param value - number * @param count - count bytes */ - void writeBytesFromInt(char* buffer, size_t value, int count) { + void writeBytesFromNumber(char* buffer, size_t number, int count) { for (int i = 0; i < count; i++) { - buffer[count - i - 1] = (char) (value >> (i * 8)); + buffer[count - i - 1] = (char) (number >> (i * 8)); } } } From f2529d4329a651ffa8507c4376d3c3a855337172 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Feb 2020 22:40:26 +0700 Subject: [PATCH 2/5] Added tests with the support for Linux and Windows --- .gitignore | 2 +- FileBroadcaster.sln | 10 ++++ Makefile | 7 +++ tests/Tests.cpp | 35 +++++++++++ tests/packages.config | 4 ++ tests/tests.vcxproj | 135 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tests/Tests.cpp create mode 100644 tests/packages.config create mode 100644 tests/tests.vcxproj diff --git a/.gitignore b/.gitignore index 53ea91f..91ed369 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,4 @@ x64/ x86/ Debug/ Release/ -FileBroadcaster \ No newline at end of file +packages/ \ No newline at end of file diff --git a/FileBroadcaster.sln b/FileBroadcaster.sln index e8cc356..3e73aea 100644 --- a/FileBroadcaster.sln +++ b/FileBroadcaster.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2047 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FileBroadcaster", "src\FileBroadcaster.vcxproj", "{CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "tests\tests.vcxproj", "{6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -21,6 +23,14 @@ Global {CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x64.Build.0 = Release|x64 {CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.ActiveCfg = Release|Win32 {CB24E1BF-BA1E-405A-809C-7EA285F3AF1E}.Release|x86.Build.0 = Release|Win32 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.ActiveCfg = Debug|x64 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x64.Build.0 = Debug|x64 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.ActiveCfg = Debug|Win32 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Debug|x86.Build.0 = Debug|Win32 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.ActiveCfg = Release|x64 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x64.Build.0 = Release|x64 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.ActiveCfg = Release|Win32 + {6E714D4E-EAB6-43B3-AD29-FB75743CA1BD}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Makefile b/Makefile index f5fa8ff..95054a6 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,10 @@ all: -std=c++14 -pthread \ -Ilib/cxxopts/include \ -o FileBroadcaster + +tests: + g++ tests/Tests.cpp \ + -std=c++14 -pthread \ + -Ilib/cxxopts/include \ + -lgtest \ + -o GTests diff --git a/tests/Tests.cpp b/tests/Tests.cpp new file mode 100644 index 0000000..942148e --- /dev/null +++ b/tests/Tests.cpp @@ -0,0 +1,35 @@ +#include + +#include "../src/Utils.hpp" + +class BytesConverter : public ::testing::TestWithParam> { +public: + size_t value; + int count; + +protected: + void SetUp() override { + std::tie(value, count) = GetParam(); + } +}; + +INSTANTIATE_TEST_CASE_P( + CombinationsTest, BytesConverter, + ::testing::Combine( + ::testing::Values(0U, 1U, 2U, 1000U, 65535U, 2147483647U), + ::testing::Values(4, 8, 16))); + +TEST_P(BytesConverter, getIntFromBytes) { + char buffer[16] = { 0 }; + + Utils::writeBytesFromNumber(buffer, value, count); + + size_t result = Utils::getNumberFromBytes(buffer, count); + + EXPECT_EQ(result, value); +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/tests/packages.config b/tests/packages.config new file mode 100644 index 0000000..0acd30a --- /dev/null +++ b/tests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj new file mode 100644 index 0000000..6106a9c --- /dev/null +++ b/tests/tests.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {6e714d4e-eab6-43b3-ad29-fb75743ca1bd} + Win32Proj + 10.0.17763.0 + Application + v141 + Unicode + + + + + + + + + true + + + true + + + + + + + {cb24e1bf-ba1e-405a-809c-7ea285f3af1e} + + + + + Designer + + + + + + + + + + Use + pch.h + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + Level3 + + + true + Console + + + + + NotUsing + + + Disabled + X64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + Level3 + + $(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories);$(SolutionDir)/lib/cxxopts/include/ + + + true + Console + + + + + Use + pch.h + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + + + true + Console + true + true + + + + + NotUsing + + + X64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + + $(MSBuildThisFileDirectory)include;%(AdditionalIncludeDirectories);$(SolutionDir)/lib/cxxopts/include/ + + + true + Console + true + true + + + + + Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}. + + + + \ No newline at end of file From b356984bf1fc57cb444d740fcda402b088566e49 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 7 Feb 2020 22:58:03 +0700 Subject: [PATCH 3/5] Added CircleCI --- .circleci/config.yml | 43 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 4 ++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..66223e2 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,43 @@ +version: 2 +jobs: + gtest: + working_directory: ~/root + docker: + - image: gcc:latest + steps: + - run: + name: Downloading dependencies + command: | + apt-get update + apt-get install -y cmake + apt-get install -y libgtest-dev + - run: + name: Installing dependencies + working_directory: /usr/src/gtest/ + command: | + cmake CMakeLists.txt + make + cp /usr/src/gtest/*.a /usr/lib + - checkout + - run: + name: "Pull Submodules" + command: | + git submodule init + git submodule update --recursive --remote + - run: + name: Building tests + command: make gtests + - run: + name: Running tests + command: ./GTests --gtest_filter=* +workflows: + version: 2 + build_and_test: + jobs: + - gtest: + filters: + branches: + only: + - master + - develop + - tests \ No newline at end of file diff --git a/Makefile b/Makefile index 95054a6..530db0e 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ -all: +program: g++ src/Main.cpp \ -std=c++14 -pthread \ -Ilib/cxxopts/include \ -o FileBroadcaster -tests: +gtests: g++ tests/Tests.cpp \ -std=c++14 -pthread \ -Ilib/cxxopts/include \ From dc1763ac30f05419344e4962d10be387cbc1def9 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Feb 2020 00:10:13 +0700 Subject: [PATCH 4/5] Update README.md --- README.md | 62 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index da6c0bf..983f0b1 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,63 @@ # File-Broadcaster +

+ + Build status + + Code quality + + Release + + License +

+ UDP File sender and receiver Can use broadcast address to send file on all computers in LAN - ## Features - - Send file to one or all computers in LAN - - Reliability of data transmission - - Server timeout detection - - Change MTU - +- Send file to one or all computers in LAN +- Reliability of data transmission +- Server timeout detection +- Change MTU ## Overview - - [Requirements](#requirements) - - [Download](#download) - - [Installation](#installation) - - [Script Parameters](#script-parameters) - - [Packets Specification](#packets-specification) - - [Script Specification](#script-specification) +- [Requirements](#requirements) +- [Download](#download) +- [Installation](#installation) +- [Script Parameters](#script-parameters) +- [Packets Specification](#packets-specification) +- [Script Specification](#script-specification) -# Download +## Download Clone the [source repository](http://github.com/gistrec/File-Broadcaster) from Github. - * On the command line, enter: +* On the command line, enter: ```` git clone https://github.com/gistrec/File-Broadcaster.git git submodule init git submodule update --recursive --remote ```` - * You can probably use [Github for Windows](http://windows.github.com/) or [Github for Mac](http://mac.github.com/) instead of the command line, however these aren't tested/supported and we only use the command line for development. Use [this link](https://git-scm.com/downloads) to download the command line version. + +* You can probably use [Github for Windows](http://windows.github.com/) or [Github for Mac](http://mac.github.com/) instead of the command line, however these aren't tested/supported and we only use the command line for development. Use [this link](https://git-scm.com/downloads) to download the command line version. ## Requirements - * Windows: - * Visual Studio 2015 or 2017 - * Linux: - * g++ - * pthread - * arpa +* Windows: + * Visual Studio 2015 or 2017 +* Linux: + * g++ + * pthread + * arpa ## Installation - * Windows - * Open FileBroadcaster.sln via Visual Studio - * Build project - * Linux - * Open a terminal/console/command prompt, change to the directory where you cloned project, and type: +* Windows + * Open FileBroadcaster.sln via Visual Studio + * Build project +* Linux + * Open a terminal/console/command prompt, change to the directory where you cloned project, and type: ```` make all ```` From 7ce4b6d1237e34e04b3ec1094cae671d3561c29c Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Feb 2020 00:25:38 +0700 Subject: [PATCH 5/5] Updated README: supported platforms added --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 983f0b1..a1d4e87 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,10 @@ Code quality Release + + Code quality - License + License

UDP File sender and receiver