From bc924a1a8dc4eecd0f65fd732beeef0bd7d9917b Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 18:31:51 +0300 Subject: [PATCH 1/9] done client --- lab1_client/.gitignore | 1 + lab1_client/CMakeLists.txt | 9 ++ lab1_client/client.cpp | 211 +++++++++++++++++++++++++++++++++++++ lab1_client/client.h | 59 +++++++++++ lab1_client/main.cpp | 16 +++ 5 files changed, 296 insertions(+) create mode 100644 lab1_client/.gitignore create mode 100644 lab1_client/CMakeLists.txt create mode 100644 lab1_client/client.cpp create mode 100644 lab1_client/client.h create mode 100644 lab1_client/main.cpp diff --git a/lab1_client/.gitignore b/lab1_client/.gitignore new file mode 100644 index 0000000..831205f --- /dev/null +++ b/lab1_client/.gitignore @@ -0,0 +1 @@ +lab1_client diff --git a/lab1_client/CMakeLists.txt b/lab1_client/CMakeLists.txt new file mode 100644 index 0000000..a665566 --- /dev/null +++ b/lab1_client/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.8) +project(lab1_client) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") + +set(SOURCE_FILES main.cpp client.cpp client.h) +add_executable(lab1_client ${SOURCE_FILES}) \ No newline at end of file diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp new file mode 100644 index 0000000..3e42306 --- /dev/null +++ b/lab1_client/client.cpp @@ -0,0 +1,211 @@ +#include "client.h" + +Client::Client(string host, int portno) { + /* Create a socket point */ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) { + perror("ERROR opening socket"); + exit(1); + } + + struct hostent *server = gethostbyname(host.c_str()); + struct sockaddr_in serv_addr; + + if (server == NULL) { + fprintf(stderr, "ERROR, no such host\n"); + exit(0); + } + + bzero((char *) &serv_addr, 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(portno); + + /* Now connect to the server */ + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + perror("ERROR connecting"); + exit(1); + } + + pthread_mutex_init(&mutex, NULL); +} + +Client::~Client() { + pthread_mutex_destroy(&mutex); +} + +rw_t read_num(int sockfd) { + rw_t buffer[2]; + bzero(buffer, sizeof(rw_t) * 2); + ssize_t n; + n = read(sockfd, buffer, sizeof(rw_t)); + if (n <= 0) { + throw rw_exception("ERROR reading from socket"); + } + return buffer[0]; +} + +void write_nums(int sockfd, vector nums) { + rw_t buffer[256]; + ssize_t n; + for (int i = 0; i < nums.size();) { + int j = 0; + for (; j < 255 && i + j < nums.size(); j++) { + buffer[j] = *nums[i + j]; + } + n = write(sockfd, buffer, sizeof(rw_t) * j); + if (n < 0) { + throw rw_exception("ERROR writing to socket"); + } + i += j; + } +} + +rw_t get_res(int sockfd, Client* client) { + rw_t t = read_num(sockfd); + while (t == 3) { + rw_t id = read_num(sockfd); + rw_t res = read_num(sockfd); + cout << "Result of query " << id << " "; + type tp = client->get_long_query(id); + if (tp == SQRT) + cout << "(get_sqrt) is " << (calc_t) res; + else if (tp == FACT) + cout << "(get_fact) is " << (fact_t) res; + cout << endl; + t = read_num(sockfd); + } + return read_num(sockfd); +} + +void *listen(void* args) { + Client* client = ((task_args*) args)->client; + int sockfd = ((task_args*) args)->sockfd; + while (true) { + pthread_mutex_lock(client->get_mutex()); + rw_t buf[4]; + bzero(buf, sizeof(rw_t) * 4); + int n = read(sockfd, buf, 3); + if (n > 0) { + while (n < 3) { + int m = read(sockfd, buf + n, 3 - n / 4); + n += m; + } + rw_t id = buf[1]; + rw_t res = buf[2]; + cout << "Result of query " << id << " "; + type tp = client->get_long_query(id); + if (tp == SQRT) + cout << "(get_sqrt) is " << (calc_t) res; + else if (tp == FACT) + cout << "(get_fact) is " << (fact_t) res; + cout << endl; + } + pthread_mutex_unlock(client->get_mutex()); + sleep(5); + } +} + +void Client::run() { + + cout << "List of commands\n" + "List of commands:\n" + "1. get_sum \n" + "2. get_mult \n" + "3. get_diff \n" + "4. get_quot \n" + "5. get_sqrt \n" + "6. get_fact \n" + "7. exit\n" + ; + + pthread_t *thread = (pthread_t*) malloc(sizeof(pthread_t)); + pthread_create(thread, NULL, listen, new task_args(this, sockfd)); + + string command; + while (cin >> command) { + rw_t type; + if (command == "get_sum") { + type = 1; + } else if (command == "get_mult") { + type = 2; + } else if (command == "get_diff") { + type = 3; + } else if (command == "get_quot") { + type = 4; + } else if (command == "get_sqrt") { + type = 5; + } else if (command == "get_fact") { + type = 6; + } else if (command == "exit") { + pthread_kill(*thread, 0); + free(thread); + close(sockfd); + return; + } else { + cerr << "Incorrect command" << endl; + continue; + } + if (type >= 1 && type <= 4) { + calc_t a, b; + cin >> a >> b; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &a, (rw_t*) &b}); + calc_t res = get_res(sockfd, this); + pthread_mutex_unlock(&mutex); + cout << "Result of query " << command << " is " << res << endl; + } else if (type == 5) { + calc_t a; + cin >> a; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &a}); + rw_t id = get_res(sockfd, this); + long_queries[id] = SQRT; + pthread_mutex_unlock(&mutex); + cout << "Query " << command << " is queued with id " << id << endl; + } else if (type == 6) { + fact_t n; + cin >> n; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &n}); + rw_t id = get_res(sockfd, this); + long_queries[id] = FACT; + pthread_mutex_unlock(&mutex); + cout << "Query " << command << " is queued with id " << id << endl; + } + char buffer[256]; + + /* Now ask for a message from the user, this message + * will be read by server + */ + printf("Please enter the message: "); + bzero(buffer, 256); + fgets(buffer, 255, stdin); + + /* Send message to the server */ + int n = write(sockfd, buffer, strlen(buffer)); + + if (n < 0) { + perror("ERROR writing to socket"); + exit(1); + } + + /* Now read server response */ + bzero(buffer, 256); + n = read(sockfd, buffer, 255); + + if (n < 0) { + perror("ERROR reading from socket"); + exit(1); + } + + printf("%s\n", buffer); + } +} + +pthread_mutex_t *Client::get_mutex() { return &mutex; } +type Client::get_long_query(rw_t id) { return long_queries[id];} + +task_args::task_args(Client *client, int sockfd): client(client), sockfd(sockfd) {} +rw_exception::rw_exception(const string &what): runtime_error(what) {} \ No newline at end of file diff --git a/lab1_client/client.h b/lab1_client/client.h new file mode 100644 index 0000000..0a5f862 --- /dev/null +++ b/lab1_client/client.h @@ -0,0 +1,59 @@ +#include +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +using std::string; +using std::set; +using std::vector; +using std::max; +using std::min; +using std::cin; +using std::cout; +using std::cerr; +using std::endl; +using std::runtime_error; +using std::map; + +typedef unsigned long long fact_t; +typedef unsigned long long rw_t; +typedef double calc_t; + +enum type { SQRT, FACT }; + +class Client { +public: + Client(string host, int portno); + ~Client(); + void run(); + pthread_mutex_t *get_mutex(); + type get_long_query(rw_t id); +private: + int sockfd; + pthread_mutex_t mutex; + map long_queries; +}; + +struct rw_exception : runtime_error { + rw_exception(const string &what); +}; + +struct task_args { + task_args(Client *client, int sockfd); + Client *client; + int sockfd; +}; \ No newline at end of file diff --git a/lab1_client/main.cpp b/lab1_client/main.cpp new file mode 100644 index 0000000..1cea0f1 --- /dev/null +++ b/lab1_client/main.cpp @@ -0,0 +1,16 @@ +#include "client.h" + +int main(int argc, char *argv[]) { + if (argc < 3) { + fprintf(stderr, "usage %s hostname port\n", argv[0]); + exit(0); + } + + uint16_t portno = (uint16_t) atoi(argv[2]); + + + Client client(argv[1], portno); + client.run(); + + return 0; +} \ No newline at end of file From 2b6c78644fe14bbd241c0f971a0b1f6254e7f29b Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 20:00:32 +0300 Subject: [PATCH 2/9] removed odd files from master --- lab1_client/.gitignore | 1 - lab1_client/CMakeLists.txt | 9 -- lab1_client/client.cpp | 211 ------------------------------------- lab1_client/client.h | 59 ----------- lab1_client/main.cpp | 16 --- 5 files changed, 296 deletions(-) delete mode 100644 lab1_client/.gitignore delete mode 100644 lab1_client/CMakeLists.txt delete mode 100644 lab1_client/client.cpp delete mode 100644 lab1_client/client.h delete mode 100644 lab1_client/main.cpp diff --git a/lab1_client/.gitignore b/lab1_client/.gitignore deleted file mode 100644 index 831205f..0000000 --- a/lab1_client/.gitignore +++ /dev/null @@ -1 +0,0 @@ -lab1_client diff --git a/lab1_client/CMakeLists.txt b/lab1_client/CMakeLists.txt deleted file mode 100644 index a665566..0000000 --- a/lab1_client/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -cmake_minimum_required(VERSION 2.8) -project(lab1_client) - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") - -set(SOURCE_FILES main.cpp client.cpp client.h) -add_executable(lab1_client ${SOURCE_FILES}) \ No newline at end of file diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp deleted file mode 100644 index 3e42306..0000000 --- a/lab1_client/client.cpp +++ /dev/null @@ -1,211 +0,0 @@ -#include "client.h" - -Client::Client(string host, int portno) { - /* Create a socket point */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); - - if (sockfd < 0) { - perror("ERROR opening socket"); - exit(1); - } - - struct hostent *server = gethostbyname(host.c_str()); - struct sockaddr_in serv_addr; - - if (server == NULL) { - fprintf(stderr, "ERROR, no such host\n"); - exit(0); - } - - bzero((char *) &serv_addr, 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(portno); - - /* Now connect to the server */ - if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - perror("ERROR connecting"); - exit(1); - } - - pthread_mutex_init(&mutex, NULL); -} - -Client::~Client() { - pthread_mutex_destroy(&mutex); -} - -rw_t read_num(int sockfd) { - rw_t buffer[2]; - bzero(buffer, sizeof(rw_t) * 2); - ssize_t n; - n = read(sockfd, buffer, sizeof(rw_t)); - if (n <= 0) { - throw rw_exception("ERROR reading from socket"); - } - return buffer[0]; -} - -void write_nums(int sockfd, vector nums) { - rw_t buffer[256]; - ssize_t n; - for (int i = 0; i < nums.size();) { - int j = 0; - for (; j < 255 && i + j < nums.size(); j++) { - buffer[j] = *nums[i + j]; - } - n = write(sockfd, buffer, sizeof(rw_t) * j); - if (n < 0) { - throw rw_exception("ERROR writing to socket"); - } - i += j; - } -} - -rw_t get_res(int sockfd, Client* client) { - rw_t t = read_num(sockfd); - while (t == 3) { - rw_t id = read_num(sockfd); - rw_t res = read_num(sockfd); - cout << "Result of query " << id << " "; - type tp = client->get_long_query(id); - if (tp == SQRT) - cout << "(get_sqrt) is " << (calc_t) res; - else if (tp == FACT) - cout << "(get_fact) is " << (fact_t) res; - cout << endl; - t = read_num(sockfd); - } - return read_num(sockfd); -} - -void *listen(void* args) { - Client* client = ((task_args*) args)->client; - int sockfd = ((task_args*) args)->sockfd; - while (true) { - pthread_mutex_lock(client->get_mutex()); - rw_t buf[4]; - bzero(buf, sizeof(rw_t) * 4); - int n = read(sockfd, buf, 3); - if (n > 0) { - while (n < 3) { - int m = read(sockfd, buf + n, 3 - n / 4); - n += m; - } - rw_t id = buf[1]; - rw_t res = buf[2]; - cout << "Result of query " << id << " "; - type tp = client->get_long_query(id); - if (tp == SQRT) - cout << "(get_sqrt) is " << (calc_t) res; - else if (tp == FACT) - cout << "(get_fact) is " << (fact_t) res; - cout << endl; - } - pthread_mutex_unlock(client->get_mutex()); - sleep(5); - } -} - -void Client::run() { - - cout << "List of commands\n" - "List of commands:\n" - "1. get_sum \n" - "2. get_mult \n" - "3. get_diff \n" - "4. get_quot \n" - "5. get_sqrt \n" - "6. get_fact \n" - "7. exit\n" - ; - - pthread_t *thread = (pthread_t*) malloc(sizeof(pthread_t)); - pthread_create(thread, NULL, listen, new task_args(this, sockfd)); - - string command; - while (cin >> command) { - rw_t type; - if (command == "get_sum") { - type = 1; - } else if (command == "get_mult") { - type = 2; - } else if (command == "get_diff") { - type = 3; - } else if (command == "get_quot") { - type = 4; - } else if (command == "get_sqrt") { - type = 5; - } else if (command == "get_fact") { - type = 6; - } else if (command == "exit") { - pthread_kill(*thread, 0); - free(thread); - close(sockfd); - return; - } else { - cerr << "Incorrect command" << endl; - continue; - } - if (type >= 1 && type <= 4) { - calc_t a, b; - cin >> a >> b; - pthread_mutex_lock(&mutex); - write_nums(sockfd, {&type, (rw_t*) &a, (rw_t*) &b}); - calc_t res = get_res(sockfd, this); - pthread_mutex_unlock(&mutex); - cout << "Result of query " << command << " is " << res << endl; - } else if (type == 5) { - calc_t a; - cin >> a; - pthread_mutex_lock(&mutex); - write_nums(sockfd, {&type, (rw_t*) &a}); - rw_t id = get_res(sockfd, this); - long_queries[id] = SQRT; - pthread_mutex_unlock(&mutex); - cout << "Query " << command << " is queued with id " << id << endl; - } else if (type == 6) { - fact_t n; - cin >> n; - pthread_mutex_lock(&mutex); - write_nums(sockfd, {&type, (rw_t*) &n}); - rw_t id = get_res(sockfd, this); - long_queries[id] = FACT; - pthread_mutex_unlock(&mutex); - cout << "Query " << command << " is queued with id " << id << endl; - } - char buffer[256]; - - /* Now ask for a message from the user, this message - * will be read by server - */ - printf("Please enter the message: "); - bzero(buffer, 256); - fgets(buffer, 255, stdin); - - /* Send message to the server */ - int n = write(sockfd, buffer, strlen(buffer)); - - if (n < 0) { - perror("ERROR writing to socket"); - exit(1); - } - - /* Now read server response */ - bzero(buffer, 256); - n = read(sockfd, buffer, 255); - - if (n < 0) { - perror("ERROR reading from socket"); - exit(1); - } - - printf("%s\n", buffer); - } -} - -pthread_mutex_t *Client::get_mutex() { return &mutex; } -type Client::get_long_query(rw_t id) { return long_queries[id];} - -task_args::task_args(Client *client, int sockfd): client(client), sockfd(sockfd) {} -rw_exception::rw_exception(const string &what): runtime_error(what) {} \ No newline at end of file diff --git a/lab1_client/client.h b/lab1_client/client.h deleted file mode 100644 index 0a5f862..0000000 --- a/lab1_client/client.h +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include - -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -using std::string; -using std::set; -using std::vector; -using std::max; -using std::min; -using std::cin; -using std::cout; -using std::cerr; -using std::endl; -using std::runtime_error; -using std::map; - -typedef unsigned long long fact_t; -typedef unsigned long long rw_t; -typedef double calc_t; - -enum type { SQRT, FACT }; - -class Client { -public: - Client(string host, int portno); - ~Client(); - void run(); - pthread_mutex_t *get_mutex(); - type get_long_query(rw_t id); -private: - int sockfd; - pthread_mutex_t mutex; - map long_queries; -}; - -struct rw_exception : runtime_error { - rw_exception(const string &what); -}; - -struct task_args { - task_args(Client *client, int sockfd); - Client *client; - int sockfd; -}; \ No newline at end of file diff --git a/lab1_client/main.cpp b/lab1_client/main.cpp deleted file mode 100644 index 1cea0f1..0000000 --- a/lab1_client/main.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "client.h" - -int main(int argc, char *argv[]) { - if (argc < 3) { - fprintf(stderr, "usage %s hostname port\n", argv[0]); - exit(0); - } - - uint16_t portno = (uint16_t) atoi(argv[2]); - - - Client client(argv[1], portno); - client.run(); - - return 0; -} \ No newline at end of file From 7f5daac9909864a40db7dac78f8e4fe7566adcb4 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 21:56:34 +0300 Subject: [PATCH 3/9] done lab1_client --- lab1_client/.gitignore | 1 + lab1_client/CMakeLists.txt | 9 ++ lab1_client/client.cpp | 188 +++++++++++++++++++++++++++++++++++++ lab1_client/client.h | 60 ++++++++++++ lab1_client/main.cpp | 16 ++++ 5 files changed, 274 insertions(+) create mode 100644 lab1_client/.gitignore create mode 100644 lab1_client/CMakeLists.txt create mode 100644 lab1_client/client.cpp create mode 100644 lab1_client/client.h create mode 100644 lab1_client/main.cpp diff --git a/lab1_client/.gitignore b/lab1_client/.gitignore new file mode 100644 index 0000000..831205f --- /dev/null +++ b/lab1_client/.gitignore @@ -0,0 +1 @@ +lab1_client diff --git a/lab1_client/CMakeLists.txt b/lab1_client/CMakeLists.txt new file mode 100644 index 0000000..a665566 --- /dev/null +++ b/lab1_client/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 2.8) +project(lab1_client) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") + +set(SOURCE_FILES main.cpp client.cpp client.h) +add_executable(lab1_client ${SOURCE_FILES}) \ No newline at end of file diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp new file mode 100644 index 0000000..cd16e2e --- /dev/null +++ b/lab1_client/client.cpp @@ -0,0 +1,188 @@ +#include "client.h" + +Client::Client(string host, int portno) { + /* Create a socket point */ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) { + perror("ERROR opening socket"); + exit(1); + } + + struct hostent *server = gethostbyname(host.c_str()); + struct sockaddr_in serv_addr; + + if (server == NULL) { + fprintf(stderr, "ERROR, no such host\n"); + exit(0); + } + + bzero((char *) &serv_addr, 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(portno); + + /* Now connect to the server */ + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + perror("ERROR connecting"); + exit(1); + } + + pthread_mutex_init(&mutex, NULL); +} + +Client::~Client() { + pthread_mutex_destroy(&mutex); +} + +rw_t read_num(int sockfd) { + rw_t buffer[2]; + bzero(buffer, sizeof(rw_t) * 2); + ssize_t n; + n = read(sockfd, buffer, sizeof(rw_t)); + if (n <= 0) { + throw rw_exception("ERROR reading from socket"); + } + return buffer[0]; +} + +void write_nums(int sockfd, vector nums) { + rw_t buffer[256]; + ssize_t n; + for (int i = 0; i < nums.size();) { + int j = 0; + for (; j < 255 && i + j < nums.size(); j++) { + buffer[j] = *nums[i + j]; + } + n = write(sockfd, buffer, sizeof(rw_t) * j); + if (n < 0) { + throw rw_exception("ERROR writing to socket"); + } + i += j; + } +} + +rw_t get_res(int sockfd, Client* client) { + rw_t t = read_num(sockfd); + while (t == 3) { + rw_t id = read_num(sockfd); + rw_t res = read_num(sockfd); + cout << "Result of query " << id << " "; + type tp = client->get_long_query(id); + if (tp == SQRT) + cout << "(get_sqrt) is " << (calc_t) res; + else if (tp == FACT) + cout << "(get_fact) is " << (fact_t) res; + cout << endl; + t = read_num(sockfd); + } + return read_num(sockfd); +} + +void *listen(void* args) { + Client* client = ((task_args*) args)->client; + int sockfd = ((task_args*) args)->sockfd; + while (true) { + pthread_mutex_lock(client->get_mutex()); + rw_t buf[4]; + bzero(buf, sizeof(rw_t) * 4); + int flags = fcntl(sockfd, F_GETFL, 0); + fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); + int n = read(sockfd, buf, 3); + fcntl(sockfd, F_SETFL, flags); + if (n > 0) { + while (n < 3) { + int m = read(sockfd, buf + n, 3 - n / 4); + n += m; + } + rw_t id = buf[1]; + rw_t res = buf[2]; + cout << "Result of query " << id << " "; + type tp = client->get_long_query(id); + if (tp == SQRT) + cout << "(get_sqrt) is " << (calc_t) res; + else if (tp == FACT) + cout << "(get_fact) is " << (fact_t) res; + cout << endl; + } + pthread_mutex_unlock(client->get_mutex()); + sleep(5); + } +} + +void Client::run() { + + cout << "List of commands\n" + "List of commands:\n" + "1. get_sum \n" + "2. get_mult \n" + "3. get_diff \n" + "4. get_quot \n" + "5. get_sqrt \n" + "6. get_fact \n" + "7. exit\n" + ; + + pthread_t *thread = (pthread_t*) malloc(sizeof(pthread_t)); + pthread_create(thread, NULL, listen, new task_args(this, sockfd)); + + string command; + while (cin >> command) { + rw_t type; + if (command == "get_sum") { + type = 1; + } else if (command == "get_mult") { + type = 2; + } else if (command == "get_diff") { + type = 3; + } else if (command == "get_quot") { + type = 4; + } else if (command == "get_sqrt") { + type = 5; + } else if (command == "get_fact") { + type = 6; + } else if (command == "exit") { + pthread_kill(*thread, 0); + free(thread); + close(sockfd); + return; + } else { + cerr << "Incorrect command" << endl; + continue; + } + if (type >= 1 && type <= 4) { + calc_t a, b; + cin >> a >> b; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &a, (rw_t*) &b}); + rw_t store = get_res(sockfd, this); + calc_t res = *((calc_t*) &store); + pthread_mutex_unlock(&mutex); + cout << "Result of query " << command << " is " << res << endl; + } else if (type == 5) { + calc_t a; + cin >> a; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &a}); + rw_t id = get_res(sockfd, this); + long_queries[id] = SQRT; + pthread_mutex_unlock(&mutex); + cout << "Query " << command << " is queued with id " << id << endl; + } else if (type == 6) { + fact_t n; + cin >> n; + pthread_mutex_lock(&mutex); + write_nums(sockfd, {&type, (rw_t*) &n}); + rw_t id = get_res(sockfd, this); + long_queries[id] = FACT; + pthread_mutex_unlock(&mutex); + cout << "Query " << command << " is queued with id " << id << endl; + } + } +} + +pthread_mutex_t *Client::get_mutex() { return &mutex; } +type Client::get_long_query(rw_t id) { return long_queries[id];} + +task_args::task_args(Client *client, int sockfd): client(client), sockfd(sockfd) {} +rw_exception::rw_exception(const string &what): runtime_error(what) {} \ No newline at end of file diff --git a/lab1_client/client.h b/lab1_client/client.h new file mode 100644 index 0000000..844e6aa --- /dev/null +++ b/lab1_client/client.h @@ -0,0 +1,60 @@ +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +using std::string; +using std::set; +using std::vector; +using std::max; +using std::min; +using std::cin; +using std::cout; +using std::cerr; +using std::endl; +using std::runtime_error; +using std::map; + +typedef unsigned long long fact_t; +typedef unsigned long long rw_t; +typedef double calc_t; + +enum type { SQRT, FACT }; + +class Client { +public: + Client(string host, int portno); + ~Client(); + void run(); + pthread_mutex_t *get_mutex(); + type get_long_query(rw_t id); +private: + int sockfd; + pthread_mutex_t mutex; + map long_queries; +}; + +struct rw_exception : runtime_error { + rw_exception(const string &what); +}; + +struct task_args { + task_args(Client *client, int sockfd); + Client *client; + int sockfd; +}; \ No newline at end of file diff --git a/lab1_client/main.cpp b/lab1_client/main.cpp new file mode 100644 index 0000000..1cea0f1 --- /dev/null +++ b/lab1_client/main.cpp @@ -0,0 +1,16 @@ +#include "client.h" + +int main(int argc, char *argv[]) { + if (argc < 3) { + fprintf(stderr, "usage %s hostname port\n", argv[0]); + exit(0); + } + + uint16_t portno = (uint16_t) atoi(argv[2]); + + + Client client(argv[1], portno); + client.run(); + + return 0; +} \ No newline at end of file From a1b6f12e2e5f4b316a21d51e8d98114c235bae69 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 22:06:27 +0300 Subject: [PATCH 4/9] fixed protocol --- lab1_client/client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index cd16e2e..8ca49cc 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -132,9 +132,9 @@ void Client::run() { if (command == "get_sum") { type = 1; } else if (command == "get_mult") { - type = 2; - } else if (command == "get_diff") { type = 3; + } else if (command == "get_diff") { + type = 2; } else if (command == "get_quot") { type = 4; } else if (command == "get_sqrt") { From a45e50f6e44e3fee1fc7198b648c93a8bfb23e08 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 22:09:55 +0300 Subject: [PATCH 5/9] fixed protocol --- lab1_client/client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index 8ca49cc..a2a7d49 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -138,9 +138,9 @@ void Client::run() { } else if (command == "get_quot") { type = 4; } else if (command == "get_sqrt") { - type = 5; - } else if (command == "get_fact") { type = 6; + } else if (command == "get_fact") { + type = 5; } else if (command == "exit") { pthread_kill(*thread, 0); free(thread); From 66c9653f6750b72627d197964d7d43e95cceb184 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 22:12:19 +0300 Subject: [PATCH 6/9] fixed switch types --- lab1_client/client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index a2a7d49..16c879a 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -159,7 +159,7 @@ void Client::run() { calc_t res = *((calc_t*) &store); pthread_mutex_unlock(&mutex); cout << "Result of query " << command << " is " << res << endl; - } else if (type == 5) { + } else if (type == 6) { calc_t a; cin >> a; pthread_mutex_lock(&mutex); @@ -168,7 +168,7 @@ void Client::run() { long_queries[id] = SQRT; pthread_mutex_unlock(&mutex); cout << "Query " << command << " is queued with id " << id << endl; - } else if (type == 6) { + } else if (type == 5) { fact_t n; cin >> n; pthread_mutex_lock(&mutex); From 231f89be108e07214a9145e2fd979e474f1799c5 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 22:54:36 +0300 Subject: [PATCH 7/9] fixed read in listen --- lab1_client/client.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index 16c879a..1bc07ce 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -88,11 +88,11 @@ void *listen(void* args) { bzero(buf, sizeof(rw_t) * 4); int flags = fcntl(sockfd, F_GETFL, 0); fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); - int n = read(sockfd, buf, 3); + int n = read(sockfd, buf, 3 * sizeof(rw_t)); fcntl(sockfd, F_SETFL, flags); if (n > 0) { - while (n < 3) { - int m = read(sockfd, buf + n, 3 - n / 4); + while (n < 3 * sizeof(rw_t)) { + int m = read(sockfd, buf + n, 3 * sizeof(rw_t) - n); n += m; } rw_t id = buf[1]; From 7a2b98f772af85adc63c678d8f992a8ddc82ee20 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 22:59:28 +0300 Subject: [PATCH 8/9] fixed read in listen --- lab1_client/client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index 1bc07ce..457fc72 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -92,7 +92,7 @@ void *listen(void* args) { fcntl(sockfd, F_SETFL, flags); if (n > 0) { while (n < 3 * sizeof(rw_t)) { - int m = read(sockfd, buf + n, 3 * sizeof(rw_t) - n); + int m = read(sockfd, ((char*)buf) + n, 3 * sizeof(rw_t) - n); n += m; } rw_t id = buf[1]; From 0ba1a26853cfced79e2095df5ba85cee317f6b67 Mon Sep 17 00:00:00 2001 From: Mikhail Yutman Date: Tue, 19 Feb 2019 23:09:24 +0300 Subject: [PATCH 9/9] fixed read in listen --- lab1_client/client.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lab1_client/client.cpp b/lab1_client/client.cpp index 457fc72..292ab02 100644 --- a/lab1_client/client.cpp +++ b/lab1_client/client.cpp @@ -70,9 +70,9 @@ rw_t get_res(int sockfd, Client* client) { cout << "Result of query " << id << " "; type tp = client->get_long_query(id); if (tp == SQRT) - cout << "(get_sqrt) is " << (calc_t) res; + cout << "(get_sqrt) is " << *((calc_t*)&res); else if (tp == FACT) - cout << "(get_fact) is " << (fact_t) res; + cout << "(get_fact) is " << *((fact_t*)&res); cout << endl; t = read_num(sockfd); } @@ -100,9 +100,9 @@ void *listen(void* args) { cout << "Result of query " << id << " "; type tp = client->get_long_query(id); if (tp == SQRT) - cout << "(get_sqrt) is " << (calc_t) res; + cout << "(get_sqrt) is " << *((calc_t*)&res); else if (tp == FACT) - cout << "(get_fact) is " << (fact_t) res; + cout << "(get_fact) is " << *((fact_t*)&res); cout << endl; } pthread_mutex_unlock(client->get_mutex());