From 4b5a75e4c7bb166d33870b7637cc346bcf559ec3 Mon Sep 17 00:00:00 2001 From: Grigory Bartosh Date: Tue, 12 Feb 2019 15:11:37 +0300 Subject: [PATCH 1/5] lab #01 Client done --- 01_Client/.gitignore | 1 + 01_Client/include/Client.h | 29 ++++++++++++ 01_Client/include/Network.h | 45 ++++++++++++++++++ 01_Client/include/SimpleChecker.h | 13 +++++ 01_Client/src/Client.cpp | 38 +++++++++++++++ 01_Client/src/Network.cpp | 64 +++++++++++++++++++++++++ 01_Client/src/SimpleChecker.cpp | 27 +++++++++++ 01_Client/src/main.cpp | 79 +++++++++++++++++++++++++++++++ 8 files changed, 296 insertions(+) create mode 100644 01_Client/.gitignore create mode 100644 01_Client/include/Client.h create mode 100644 01_Client/include/Network.h create mode 100644 01_Client/include/SimpleChecker.h create mode 100644 01_Client/src/Client.cpp create mode 100644 01_Client/src/Network.cpp create mode 100644 01_Client/src/SimpleChecker.cpp create mode 100644 01_Client/src/main.cpp diff --git a/01_Client/.gitignore b/01_Client/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/01_Client/.gitignore @@ -0,0 +1 @@ +main diff --git a/01_Client/include/Client.h b/01_Client/include/Client.h new file mode 100644 index 0000000..6dc3652 --- /dev/null +++ b/01_Client/include/Client.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include "Network.h" +#include "SimpleChecker.h" + +typedef uint64_t calc_t; +typedef uint64_t count_t; + +class Client +{ +private: + typedef uint64_t code_t; + + static const code_t CODE_GET_MAX_SIMPLE = 1; + static const code_t CODE_GET_LAST_N = 2; + static const code_t CODE_CALCULATE = 3; + + Network _network; + +public: + Client(std::string host_name, uint16_t port); + + calc_t get_max_simple() const; + std::vector get_last_n(count_t n) const; + void calculate(count_t n) const; +}; \ No newline at end of file diff --git a/01_Client/include/Network.h b/01_Client/include/Network.h new file mode 100644 index 0000000..380821b --- /dev/null +++ b/01_Client/include/Network.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +struct NetworkException +{ +private: + std::string message; + +public: + NetworkException(std::string message) + : message(message) + { + } + + std::string get_message() const + { + return message; + } +}; + +class Network +{ +private: + int _sockfd; + +public: + Network(std::string host_name, uint16_t port); + + void send_int(uint64_t x, size_t size) const; + uint64_t read_int(size_t size) const; + + void send_vector(const std::vector &v, size_t size_length, size_t size_num) const; + std::vector read_vector(size_t size_length, size_t size_num) const; +}; \ No newline at end of file diff --git a/01_Client/include/SimpleChecker.h b/01_Client/include/SimpleChecker.h new file mode 100644 index 0000000..dfffd65 --- /dev/null +++ b/01_Client/include/SimpleChecker.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +class SimpleChecker +{ +public: + SimpleChecker() = delete; + + static bool is_simple(uint64_t n); + static std::vector check_interval(uint64_t start_num, size_t n); +}; \ No newline at end of file diff --git a/01_Client/src/Client.cpp b/01_Client/src/Client.cpp new file mode 100644 index 0000000..db5ae07 --- /dev/null +++ b/01_Client/src/Client.cpp @@ -0,0 +1,38 @@ +#include "Client.h" + +using std::string; +using std::vector; + +Client::Client(string host_name, uint16_t port) +: _network(host_name, port) +{ +} + +calc_t Client::get_max_simple() const +{ + _network.send_int(CODE_GET_MAX_SIMPLE, sizeof(CODE_GET_MAX_SIMPLE)); + return (calc_t)_network.read_int(sizeof(calc_t)); +} + +vector Client::get_last_n(count_t n) const +{ + _network.send_int(CODE_GET_LAST_N, sizeof(CODE_GET_LAST_N)); + _network.send_int(n, sizeof(n)); + + vector res; + vector response = _network.read_vector(sizeof(count_t), sizeof(calc_t)); + for (uint64_t x : response) + res.push_back((calc_t)x); + return res; +} + +void Client::calculate(count_t n) const +{ + _network.send_int(CODE_CALCULATE, sizeof(CODE_CALCULATE)); + _network.send_int(n, sizeof(n)); + + calc_t start_num = (calc_t)_network.read_int(sizeof(calc_t)); + vector simple_nums = SimpleChecker::check_interval(start_num, n); + + _network.send_vector(simple_nums, sizeof(count_t), sizeof(calc_t)); +} \ No newline at end of file diff --git a/01_Client/src/Network.cpp b/01_Client/src/Network.cpp new file mode 100644 index 0000000..4bf7757 --- /dev/null +++ b/01_Client/src/Network.cpp @@ -0,0 +1,64 @@ +#include "Network.h" + +using std::vector; +using std::string; + +Network::Network(string host_name, uint16_t port) +{ + _sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (_sockfd < 0) { + throw NetworkException("Could not open socket"); + } + + struct hostent *server = gethostbyname(host_name.c_str()); + + if (server == NULL) { + throw NetworkException("Could not find host"); + } + + struct sockaddr_in serv_addr; + memset((char*)&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy(server->h_addr, (char*) &serv_addr.sin_addr.s_addr, (size_t) server->h_length); + serv_addr.sin_port = htons(port); + + if (connect(_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + throw NetworkException("Could not connect to server"); + } +} + +void Network::send_int(uint64_t x, size_t size) const +{ + if (write(_sockfd, (char*)&x, size) < 0) { + throw NetworkException("Could not send number"); + } +} + +uint64_t Network::read_int(size_t size) const +{ + uint64_t x = 0; + if (read(_sockfd, (char*)&x, size) < 0) { + throw NetworkException("Could not read number"); + } + return x; +} + +void Network::send_vector(const vector &v, size_t size_length, size_t size_num) const +{ + send_int(v.size(), size_length); + for (uint64_t x : v) { + send_int(x, size_num); + } +} + +vector Network::read_vector(size_t size_length, size_t size_num) const +{ + vector v; + + size_t n = read_int(size_length); + for (size_t i = 0; i < n; i++) { + v.push_back(read_int(size_num)); + } + + return v; +} \ No newline at end of file diff --git a/01_Client/src/SimpleChecker.cpp b/01_Client/src/SimpleChecker.cpp new file mode 100644 index 0000000..f3bbb0b --- /dev/null +++ b/01_Client/src/SimpleChecker.cpp @@ -0,0 +1,27 @@ +#include "SimpleChecker.h" + +using std::vector; + +bool SimpleChecker::is_simple(uint64_t n) +{ + if (n < 2) + return false; + + for (uint64_t d = 2; d*d <= n; d++) + if ((n / d) * d == n) + return false; + return true; +} + +vector SimpleChecker::check_interval(uint64_t start_num, size_t n) +{ + vector res; + + for (size_t dx = 0; dx < n; dx++) { + size_t x = start_num + dx; + if (is_simple(x)) + res.push_back(x); + } + + return res; +} \ No newline at end of file diff --git a/01_Client/src/main.cpp b/01_Client/src/main.cpp new file mode 100644 index 0000000..8517ba7 --- /dev/null +++ b/01_Client/src/main.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include "Client.h" + +using namespace std; + +tuple read_addres() +{ + string host_name; + uint16_t port; + + cout << "Enter host name and port" << endl; + cin >> host_name >> port; + + return make_tuple(host_name, port); +} + +void print_commands() +{ + cout << endl; + cout << "List of commands:" << endl; + cout << "1. get_max_simple" << endl; + cout << "2. get_last_n " << endl; + cout << "3. calculate " << endl; + cout << "4. exit" << endl; + cout << endl; +} + +string read_command() +{ + cout << endl; + cout << "Enter command" << endl; + string command; + cin >> command; + return command; +} + +int main() +{ + try { + string host_name; + uint16_t port; + tie(host_name, port) = read_addres(); + Client client(host_name, port); + + print_commands(); + + while (true) + { + string command = read_command(); + if (command == "get_max_simple") { + calc_t x = client.get_max_simple(); + cout << x << endl; + } else if (command == "get_last_n") { + count_t n; + cin >> n; + vector v = client.get_last_n(n); + for (calc_t x : v) + cout << x << " "; + cout << endl; + } else if (command == "calculate") { + count_t n; + cin >> n; + client.calculate(n); + cout << "Calculations completed" << endl; + } else if (command == "exit") { + break; + } else { + cout << "This is an unknown command" << endl; + } + } + } catch (NetworkException &e) { + cout << e.get_message() << endl; + } + + return 0; +} \ No newline at end of file From 3282209c6be2ab34270c8100cd8f5a6cd4d7b190 Mon Sep 17 00:00:00 2001 From: Grigory Bartosh Date: Tue, 12 Feb 2019 18:24:31 +0300 Subject: [PATCH 2/5] added stddef.h --- 01_Client/include/Client.h | 1 + 01_Client/include/Network.h | 1 + 01_Client/include/SimpleChecker.h | 1 + 3 files changed, 3 insertions(+) diff --git a/01_Client/include/Client.h b/01_Client/include/Client.h index 6dc3652..ac7d92d 100644 --- a/01_Client/include/Client.h +++ b/01_Client/include/Client.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/01_Client/include/Network.h b/01_Client/include/Network.h index 380821b..cb6f5dc 100644 --- a/01_Client/include/Network.h +++ b/01_Client/include/Network.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/01_Client/include/SimpleChecker.h b/01_Client/include/SimpleChecker.h index dfffd65..90e8e25 100644 --- a/01_Client/include/SimpleChecker.h +++ b/01_Client/include/SimpleChecker.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include From e642b58ca6e9b891477bf4afa0e42226f009bad6 Mon Sep 17 00:00:00 2001 From: Grigory Bartosh Date: Tue, 12 Feb 2019 22:40:52 +0300 Subject: [PATCH 3/5] refactoring: simple -> prime --- 01_Client/include/Client.h | 2 +- 01_Client/include/SimpleChecker.h | 2 +- 01_Client/src/Client.cpp | 6 +++--- 01_Client/src/SimpleChecker.cpp | 4 ++-- 01_Client/src/main.cpp | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/01_Client/include/Client.h b/01_Client/include/Client.h index ac7d92d..32619c7 100644 --- a/01_Client/include/Client.h +++ b/01_Client/include/Client.h @@ -24,7 +24,7 @@ class Client public: Client(std::string host_name, uint16_t port); - calc_t get_max_simple() const; + calc_t get_max_prime() const; std::vector get_last_n(count_t n) const; void calculate(count_t n) const; }; \ No newline at end of file diff --git a/01_Client/include/SimpleChecker.h b/01_Client/include/SimpleChecker.h index 90e8e25..4e68242 100644 --- a/01_Client/include/SimpleChecker.h +++ b/01_Client/include/SimpleChecker.h @@ -9,6 +9,6 @@ class SimpleChecker public: SimpleChecker() = delete; - static bool is_simple(uint64_t n); + static bool is_prime(uint64_t n); static std::vector check_interval(uint64_t start_num, size_t n); }; \ No newline at end of file diff --git a/01_Client/src/Client.cpp b/01_Client/src/Client.cpp index db5ae07..374d75f 100644 --- a/01_Client/src/Client.cpp +++ b/01_Client/src/Client.cpp @@ -8,7 +8,7 @@ Client::Client(string host_name, uint16_t port) { } -calc_t Client::get_max_simple() const +calc_t Client::get_max_prime() const { _network.send_int(CODE_GET_MAX_SIMPLE, sizeof(CODE_GET_MAX_SIMPLE)); return (calc_t)_network.read_int(sizeof(calc_t)); @@ -32,7 +32,7 @@ void Client::calculate(count_t n) const _network.send_int(n, sizeof(n)); calc_t start_num = (calc_t)_network.read_int(sizeof(calc_t)); - vector simple_nums = SimpleChecker::check_interval(start_num, n); + vector prime_nums = SimpleChecker::check_interval(start_num, n); - _network.send_vector(simple_nums, sizeof(count_t), sizeof(calc_t)); + _network.send_vector(prime_nums, sizeof(count_t), sizeof(calc_t)); } \ No newline at end of file diff --git a/01_Client/src/SimpleChecker.cpp b/01_Client/src/SimpleChecker.cpp index f3bbb0b..c351580 100644 --- a/01_Client/src/SimpleChecker.cpp +++ b/01_Client/src/SimpleChecker.cpp @@ -2,7 +2,7 @@ using std::vector; -bool SimpleChecker::is_simple(uint64_t n) +bool SimpleChecker::is_prime(uint64_t n) { if (n < 2) return false; @@ -19,7 +19,7 @@ vector SimpleChecker::check_interval(uint64_t start_num, size_t n) for (size_t dx = 0; dx < n; dx++) { size_t x = start_num + dx; - if (is_simple(x)) + if (is_prime(x)) res.push_back(x); } diff --git a/01_Client/src/main.cpp b/01_Client/src/main.cpp index 8517ba7..bcb7ca3 100644 --- a/01_Client/src/main.cpp +++ b/01_Client/src/main.cpp @@ -21,7 +21,7 @@ void print_commands() { cout << endl; cout << "List of commands:" << endl; - cout << "1. get_max_simple" << endl; + cout << "1. get_max_prime" << endl; cout << "2. get_last_n " << endl; cout << "3. calculate " << endl; cout << "4. exit" << endl; @@ -50,8 +50,8 @@ int main() while (true) { string command = read_command(); - if (command == "get_max_simple") { - calc_t x = client.get_max_simple(); + if (command == "get_max_prime") { + calc_t x = client.get_max_prime(); cout << x << endl; } else if (command == "get_last_n") { count_t n; From bebf575bf611f831e374e200e4667be96c214388 Mon Sep 17 00:00:00 2001 From: Grigory Bartosh Date: Tue, 19 Feb 2019 23:43:38 +0300 Subject: [PATCH 4/5] lab #02 server --- 02_Server/.gitignore | 1 + 02_Server/include/Calculator.h | 19 +++ 02_Server/include/Server.h | 182 ++++++++++++++++++++++++ 02_Server/src/Calculator.cpp | 36 +++++ 02_Server/src/Server.cpp | 249 +++++++++++++++++++++++++++++++++ 02_Server/src/main.cpp | 28 ++++ 6 files changed, 515 insertions(+) create mode 100644 02_Server/.gitignore create mode 100644 02_Server/include/Calculator.h create mode 100644 02_Server/include/Server.h create mode 100644 02_Server/src/Calculator.cpp create mode 100644 02_Server/src/Server.cpp create mode 100644 02_Server/src/main.cpp diff --git a/02_Server/.gitignore b/02_Server/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/02_Server/.gitignore @@ -0,0 +1 @@ +main diff --git a/02_Server/include/Calculator.h b/02_Server/include/Calculator.h new file mode 100644 index 0000000..974c7b5 --- /dev/null +++ b/02_Server/include/Calculator.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include +#include + +class Calculator +{ +public: + Calculator() = delete; + + static double get_sum(double a, double b); + static double get_diff(double a, double b); + static double get_mul(double a, double b); + static double get_quot(double a, double b); + static uint64_t get_fact(uint64_t a); + static double get_sqrt(double a); +}; \ No newline at end of file diff --git a/02_Server/include/Server.h b/02_Server/include/Server.h new file mode 100644 index 0000000..c629747 --- /dev/null +++ b/02_Server/include/Server.h @@ -0,0 +1,182 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include "Calculator.h" + +struct NetworkException +{ +private: + std::string message; + +public: + NetworkException(std::string message) + : message(message) + { + } + + std::string get_message() const + { + return message; + } +}; + +class Server +{ +private: + int _sockfd; + + void init_network(uint16_t port); + int accept_client(); + +public: + typedef uint64_t code_t; + typedef uint64_t uint_t; + typedef double float_t; + + static const code_t CODE_FAST = 1; + static const code_t CODE_LONG_ID = 2; + static const code_t CODE_LONG = 3; + + static const code_t CODE_GET_SUM = 1; + static const code_t CODE_GET_DIFF = 2; + static const code_t CODE_GET_MUL = 3; + static const code_t CODE_GET_QUOT = 4; + static const code_t CODE_GET_FACT = 5; + static const code_t CODE_GET_SQRT = 6; + + Server(uint16_t port); + ~Server(); + + static void read_message(int sockfd, char* x, size_t size); + static void send_message(int sockfd, char* x, size_t size); + + void run(); +}; + +struct TaskData +{ +private: + bool finished; + + void init_state() + { + finished = false; + mutex_is_finished = new pthread_mutex_t; + pthread_mutex_init(mutex_is_finished, NULL); + } + +public: + pthread_t *thread; + int sockfd; + + Server::code_t command_type; + Server::uint_t id; + char* param = NULL; + + pthread_mutex_t *mutex_sockfd; + pthread_mutex_t *mutex_is_finished; + + TaskData(pthread_t *thread, int sockfd) + : thread(thread), sockfd(sockfd) + { + init_state(); + + mutex_sockfd = new pthread_mutex_t; + pthread_mutex_init(mutex_sockfd, NULL); + } + + TaskData(TaskData &other, pthread_t *thread, Server::code_t command_type, Server::uint_t id, char* param) + : thread(thread), command_type(command_type), id(id), param(param) + { + std::cout << "TackData id: " << id << std::endl; + + init_state(); + + sockfd = other.sockfd; + mutex_sockfd = other.mutex_sockfd; + } + + ~TaskData() + { + pthread_join(*thread, NULL); + + if (param != NULL) { + delete[] param; + } + free(thread); + close(sockfd); + + pthread_mutex_destroy(mutex_sockfd); + pthread_mutex_destroy(mutex_is_finished); + } + + void mark_finished() + { + pthread_mutex_lock(mutex_is_finished); + finished = true; + pthread_mutex_unlock(mutex_is_finished); + } + + bool is_finished() + { + pthread_mutex_lock(mutex_is_finished); + bool ans = finished; + pthread_mutex_unlock(mutex_is_finished); + return ans; + } +}; + +class TaskController +{ +private: + std::vector tasks; + +public: + TaskController() + {} + + void add(TaskData *task) + { + tasks.push_back(task); + } + + void tryFinish() + { + std::vector remaining_tasks; + for (TaskData *task : tasks) + { + if (task->is_finished()) + { + delete task; + } else { + remaining_tasks.push_back(task); + } + } + + tasks = remaining_tasks; + } + + void finish() + { + while (tasks.size() > 0) { + tryFinish(); + } + } +}; \ No newline at end of file diff --git a/02_Server/src/Calculator.cpp b/02_Server/src/Calculator.cpp new file mode 100644 index 0000000..2bf2b43 --- /dev/null +++ b/02_Server/src/Calculator.cpp @@ -0,0 +1,36 @@ +#include "Calculator.h" + +double Calculator::get_sum(double a, double b) +{ + return a + b; +} + +double Calculator::get_diff(double a, double b) +{ + return a - b; +} + +double Calculator::get_mul(double a, double b) +{ + return a * b; +} + +double Calculator::get_quot(double a, double b) +{ + return a / b; +} + +uint64_t Calculator::get_fact(uint64_t a) +{ + uint64_t res = 1; + for (uint64_t i = 1; i <= a; i++) { + res *= i; + } + return res; +} + +double Calculator::get_sqrt(double a) +{ + std::cout << a << " " << sqrt(a) << std::endl; + return sqrt(a); +} \ No newline at end of file diff --git a/02_Server/src/Server.cpp b/02_Server/src/Server.cpp new file mode 100644 index 0000000..0fdb190 --- /dev/null +++ b/02_Server/src/Server.cpp @@ -0,0 +1,249 @@ +#include "Server.h" + +void Server::init_network(uint16_t port) +{ + _sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (_sockfd < 0) { + throw NetworkException("Could not open socket"); + } + + struct sockaddr_in serv_addr; + + bzero((char *) &serv_addr, sizeof(serv_addr)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(port); + + if (bind(_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + throw NetworkException("Could not bind"); + } +} + +Server::Server(uint16_t port) +{ + init_network(port); +} + +int Server::accept_client() +{ + int sockfd; + unsigned int clilen; + struct sockaddr_in cli_addr; + + listen(_sockfd, 5); + clilen = sizeof(cli_addr); + + sockfd = accept(_sockfd, (struct sockaddr *) &cli_addr, &clilen); + + if (sockfd < 0) { + throw NetworkException("Could not accept"); + } + + return sockfd; +} + +void Server::send_message(int sockfd, char* x, size_t size) +{ + if (write(sockfd, x, size) < 0) { + throw NetworkException("Could not write message"); + } + + std::cout << "send: float: " << (*((float_t*)x)) << " int: " << (*((uint_t*)x)) << std::endl; +} + +void Server::read_message(int sockfd, char* x, size_t size) +{ + if (read(sockfd, x, size) < 0) { + throw NetworkException("Could not read message"); + } + + std::cout << "read: float: " << (*((float_t*)x)) << " int: " << (*((uint_t*)x)) << std::endl; +} + +void* long_operation(void *void_params) +{ + TaskData *task_data = (TaskData*)void_params; + int sockfd = task_data->sockfd; + + Server::code_t command_type = task_data->command_type; + + try { + std::cout << "------ id: " << (task_data->id) << std::endl; + std::cout << "------ command_type: " << (command_type) << std::endl; + + pthread_mutex_lock(task_data->mutex_sockfd); + std::cout << "------ a" << std::endl; + Server::uint_t code = Server::CODE_LONG_ID; + std::cout << "------ b" << std::endl; + std::cout << "------ code: " << code << std::endl; + Server::send_message(sockfd, (char*)&code, sizeof(code)); + std::cout << "------ c" << std::endl; + Server::send_message(sockfd, (char*)(&(task_data->id)), sizeof(task_data->id)); + std::cout << "------ d" << std::endl; + pthread_mutex_unlock(task_data->mutex_sockfd); + std::cout << "------ e" << std::endl; + + Server::uint_t tmp_uint; + Server::float_t tmp_float; + char* res_buff = NULL; + size_t res_size; + std::cout << "------ f" << std::endl; + switch(command_type) { + case Server::CODE_GET_FACT: + tmp_uint = *((Server::uint_t*)(task_data->param)); + tmp_uint = Calculator::get_fact(tmp_uint); + res_buff = (char*)&tmp_uint; + res_size = sizeof(tmp_uint); + break; + + case Server::CODE_GET_SQRT: + tmp_float = *((Server::float_t*)(task_data->param)); + std::cout << "------ tmp_float: " << tmp_float << std::endl; + tmp_float = Calculator::get_sqrt(tmp_float); + std::cout << "------ tmp_float res: " << tmp_float << std::endl; + res_buff = (char*)&tmp_float; + res_size = sizeof(tmp_float); + break; + } + + std::cout << "------ g" << std::endl; + pthread_mutex_lock(task_data->mutex_sockfd); + code = Server::CODE_LONG; + std::cout << "------ h" << std::endl; + Server::send_message(sockfd, (char*)&code, sizeof(code)); + std::cout << "------ i" << std::endl; + Server::send_message(sockfd, (char*)(&(task_data->id)), sizeof(task_data->id)); + std::cout << "------ j" << std::endl; + Server::send_message(sockfd, res_buff, res_size); + std::cout << "------ k" << std::endl; + pthread_mutex_unlock(task_data->mutex_sockfd); + std::cout << "------ l" << std::endl; + } catch (NetworkException &e) { + std::cout << "------ fuuuuuuuuuuuck" << std::endl; + pthread_mutex_unlock(task_data->mutex_sockfd); + } + + task_data->mark_finished(); +} + +void* client_service(void *void_params) +{ + std::cout << "started thread" << std::endl; + TaskData *task_data = (TaskData*)void_params; + int sockfd = task_data->sockfd; + + TaskController task_controller; + Server::uint_t id = 1; + + std::cout << 0 << std::endl; + //while (true) { + for (int itr = 0; itr < 2; itr++) { + try { + Server::code_t command_type; + std::cout << 1 << std::endl; + Server::read_message(sockfd, (char*)&command_type, sizeof(command_type)); + std::cout << 2 << std::endl; + + if (command_type == Server::CODE_GET_FACT || command_type == Server::CODE_GET_SQRT) { + char* buff; + if (command_type == Server::CODE_GET_FACT) { + buff = new char[sizeof(Server::uint_t)]; + Server::read_message(sockfd, buff, sizeof(Server::uint_t)); + } else { + buff = new char[sizeof(Server::float_t)]; + Server::read_message(sockfd, buff, sizeof(Server::float_t)); + } + + pthread_t *thread = (pthread_t*)malloc(sizeof(pthread_t)); + TaskData *long_task_data = new TaskData(*task_data, thread, command_type, id++, buff); + pthread_create(thread, NULL, long_operation, (void*)long_task_data); + + task_controller.add(long_task_data); + } else { + std::cout << 3 << std::endl; + Server::float_t a, b, c; + Server::read_message(sockfd, (char*)&a, sizeof(a)); + Server::read_message(sockfd, (char*)&b, sizeof(b)); + std::cout << 4 << std::endl; + + switch(command_type) { + case Server::CODE_GET_SUM: + c = Calculator::get_sum(a, b); + break; + + case Server::CODE_GET_DIFF: + c = Calculator::get_diff(a, b); + break; + + case Server::CODE_GET_MUL: + c = Calculator::get_mul(a, b); + break; + + case Server::CODE_GET_QUOT: + c = Calculator::get_quot(a, b); + break; + + default: + throw NetworkException("Unknown command"); + break; + } + std::cout << 5 << std::endl; + + try { + pthread_mutex_lock(task_data->mutex_sockfd); + Server::uint_t code = Server::CODE_FAST; + Server::send_message(sockfd, (char*)&code, sizeof(code)); + Server::send_message(sockfd, (char*)&c, sizeof(c)); + pthread_mutex_unlock(task_data->mutex_sockfd); + std::cout << 6 << std::endl; + } catch (NetworkException &e) { + pthread_mutex_unlock(task_data->mutex_sockfd); + throw e; + } + std::cout << 8 << std::endl; + } + } catch (NetworkException &e) { + std::cout << "break" << std::endl; + break; + } + std::cout << 9 << std::endl; + + task_controller.tryFinish(); + + std::cout << 10 << std::endl; + } + std::cout << 11 << std::endl; + + task_controller.finish(); + task_data->mark_finished(); +} + +void Server::run() +{ + TaskController task_controller; + + while (true) { + int sockfd; + try { + sockfd = accept_client(); + } catch (NetworkException &e) { + continue; + } + + pthread_t *thread = (pthread_t*)malloc(sizeof(pthread_t)); + TaskData *task_data = new TaskData(thread, sockfd); + pthread_create(thread, NULL, client_service, (void*)task_data); + + task_controller.add(task_data); + task_controller.tryFinish(); + } + + task_controller.finish(); +} + +Server::~Server() +{ + close(_sockfd); +} \ No newline at end of file diff --git a/02_Server/src/main.cpp b/02_Server/src/main.cpp new file mode 100644 index 0000000..0c2a9bb --- /dev/null +++ b/02_Server/src/main.cpp @@ -0,0 +1,28 @@ +#include +#include "Server.h" + +using namespace std; + +uint16_t read_port() +{ + uint16_t port; + + cout << "Enter port" << endl; + cin >> port >> port; + + return port; +} + +int main() +{ + try { + //uint16_t port = read_port(); + uint16_t port = 5001; + Server server(port); + server.run(); + } catch (...) { + cout << "AAAAAAAAAAAAAAA" << endl; + } + + return 0; +} \ No newline at end of file From 33bf05da145ce1dedc1845c8b32a98f193d578f7 Mon Sep 17 00:00:00 2001 From: Grigory Bartosh Date: Wed, 20 Feb 2019 00:19:34 +0300 Subject: [PATCH 5/5] lab #2 cout removed --- 02_Server/include/Calculator.h | 1 - 02_Server/include/Server.h | 13 ++++++---- 02_Server/src/Calculator.cpp | 1 - 02_Server/src/Server.cpp | 44 +++------------------------------- 4 files changed, 12 insertions(+), 47 deletions(-) diff --git a/02_Server/include/Calculator.h b/02_Server/include/Calculator.h index 974c7b5..b8bdf27 100644 --- a/02_Server/include/Calculator.h +++ b/02_Server/include/Calculator.h @@ -3,7 +3,6 @@ #include #include #include -#include class Calculator { diff --git a/02_Server/include/Server.h b/02_Server/include/Server.h index c629747..37db3d0 100644 --- a/02_Server/include/Server.h +++ b/02_Server/include/Server.h @@ -12,7 +12,6 @@ #include #include -#include #include #include @@ -73,6 +72,7 @@ class Server struct TaskData { private: + bool is_long_operation; bool finished; void init_state() @@ -96,6 +96,8 @@ struct TaskData TaskData(pthread_t *thread, int sockfd) : thread(thread), sockfd(sockfd) { + is_long_operation = false; + init_state(); mutex_sockfd = new pthread_mutex_t; @@ -105,7 +107,7 @@ struct TaskData TaskData(TaskData &other, pthread_t *thread, Server::code_t command_type, Server::uint_t id, char* param) : thread(thread), command_type(command_type), id(id), param(param) { - std::cout << "TackData id: " << id << std::endl; + is_long_operation = true; init_state(); @@ -121,10 +123,13 @@ struct TaskData delete[] param; } free(thread); - close(sockfd); - pthread_mutex_destroy(mutex_sockfd); pthread_mutex_destroy(mutex_is_finished); + + if (!is_long_operation) { + close(sockfd); + pthread_mutex_destroy(mutex_sockfd); + } } void mark_finished() diff --git a/02_Server/src/Calculator.cpp b/02_Server/src/Calculator.cpp index 2bf2b43..d10107d 100644 --- a/02_Server/src/Calculator.cpp +++ b/02_Server/src/Calculator.cpp @@ -31,6 +31,5 @@ uint64_t Calculator::get_fact(uint64_t a) double Calculator::get_sqrt(double a) { - std::cout << a << " " << sqrt(a) << std::endl; return sqrt(a); } \ No newline at end of file diff --git a/02_Server/src/Server.cpp b/02_Server/src/Server.cpp index 0fdb190..67544ce 100644 --- a/02_Server/src/Server.cpp +++ b/02_Server/src/Server.cpp @@ -46,20 +46,16 @@ int Server::accept_client() void Server::send_message(int sockfd, char* x, size_t size) { - if (write(sockfd, x, size) < 0) { + if (write(sockfd, x, size) <= 0) { throw NetworkException("Could not write message"); } - - std::cout << "send: float: " << (*((float_t*)x)) << " int: " << (*((uint_t*)x)) << std::endl; } void Server::read_message(int sockfd, char* x, size_t size) { - if (read(sockfd, x, size) < 0) { + if (read(sockfd, x, size) <= 0) { throw NetworkException("Could not read message"); } - - std::cout << "read: float: " << (*((float_t*)x)) << " int: " << (*((uint_t*)x)) << std::endl; } void* long_operation(void *void_params) @@ -70,26 +66,16 @@ void* long_operation(void *void_params) Server::code_t command_type = task_data->command_type; try { - std::cout << "------ id: " << (task_data->id) << std::endl; - std::cout << "------ command_type: " << (command_type) << std::endl; - pthread_mutex_lock(task_data->mutex_sockfd); - std::cout << "------ a" << std::endl; Server::uint_t code = Server::CODE_LONG_ID; - std::cout << "------ b" << std::endl; - std::cout << "------ code: " << code << std::endl; Server::send_message(sockfd, (char*)&code, sizeof(code)); - std::cout << "------ c" << std::endl; Server::send_message(sockfd, (char*)(&(task_data->id)), sizeof(task_data->id)); - std::cout << "------ d" << std::endl; pthread_mutex_unlock(task_data->mutex_sockfd); - std::cout << "------ e" << std::endl; Server::uint_t tmp_uint; Server::float_t tmp_float; char* res_buff = NULL; size_t res_size; - std::cout << "------ f" << std::endl; switch(command_type) { case Server::CODE_GET_FACT: tmp_uint = *((Server::uint_t*)(task_data->param)); @@ -100,28 +86,19 @@ void* long_operation(void *void_params) case Server::CODE_GET_SQRT: tmp_float = *((Server::float_t*)(task_data->param)); - std::cout << "------ tmp_float: " << tmp_float << std::endl; tmp_float = Calculator::get_sqrt(tmp_float); - std::cout << "------ tmp_float res: " << tmp_float << std::endl; res_buff = (char*)&tmp_float; res_size = sizeof(tmp_float); break; } - std::cout << "------ g" << std::endl; pthread_mutex_lock(task_data->mutex_sockfd); code = Server::CODE_LONG; - std::cout << "------ h" << std::endl; Server::send_message(sockfd, (char*)&code, sizeof(code)); - std::cout << "------ i" << std::endl; Server::send_message(sockfd, (char*)(&(task_data->id)), sizeof(task_data->id)); - std::cout << "------ j" << std::endl; Server::send_message(sockfd, res_buff, res_size); - std::cout << "------ k" << std::endl; pthread_mutex_unlock(task_data->mutex_sockfd); - std::cout << "------ l" << std::endl; } catch (NetworkException &e) { - std::cout << "------ fuuuuuuuuuuuck" << std::endl; pthread_mutex_unlock(task_data->mutex_sockfd); } @@ -130,21 +107,16 @@ void* long_operation(void *void_params) void* client_service(void *void_params) { - std::cout << "started thread" << std::endl; TaskData *task_data = (TaskData*)void_params; int sockfd = task_data->sockfd; TaskController task_controller; Server::uint_t id = 1; - std::cout << 0 << std::endl; - //while (true) { - for (int itr = 0; itr < 2; itr++) { + while (true) { try { Server::code_t command_type; - std::cout << 1 << std::endl; Server::read_message(sockfd, (char*)&command_type, sizeof(command_type)); - std::cout << 2 << std::endl; if (command_type == Server::CODE_GET_FACT || command_type == Server::CODE_GET_SQRT) { char* buff; @@ -162,11 +134,9 @@ void* client_service(void *void_params) task_controller.add(long_task_data); } else { - std::cout << 3 << std::endl; Server::float_t a, b, c; Server::read_message(sockfd, (char*)&a, sizeof(a)); Server::read_message(sockfd, (char*)&b, sizeof(b)); - std::cout << 4 << std::endl; switch(command_type) { case Server::CODE_GET_SUM: @@ -189,7 +159,6 @@ void* client_service(void *void_params) throw NetworkException("Unknown command"); break; } - std::cout << 5 << std::endl; try { pthread_mutex_lock(task_data->mutex_sockfd); @@ -197,24 +166,17 @@ void* client_service(void *void_params) Server::send_message(sockfd, (char*)&code, sizeof(code)); Server::send_message(sockfd, (char*)&c, sizeof(c)); pthread_mutex_unlock(task_data->mutex_sockfd); - std::cout << 6 << std::endl; } catch (NetworkException &e) { pthread_mutex_unlock(task_data->mutex_sockfd); throw e; } - std::cout << 8 << std::endl; } } catch (NetworkException &e) { - std::cout << "break" << std::endl; break; } - std::cout << 9 << std::endl; task_controller.tryFinish(); - - std::cout << 10 << std::endl; } - std::cout << 11 << std::endl; task_controller.finish(); task_data->mark_finished();