Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 01_Client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
30 changes: 30 additions & 0 deletions 01_Client/include/Client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <stddef.h>
#include <cstdint>
#include <vector>
#include <string>
#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_prime() const;
std::vector<calc_t> get_last_n(count_t n) const;
void calculate(count_t n) const;
};
46 changes: 46 additions & 0 deletions 01_Client/include/Network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <stddef.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>

#include <cstdint>
#include <vector>
#include <string>

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<uint64_t> &v, size_t size_length, size_t size_num) const;
std::vector<uint64_t> read_vector(size_t size_length, size_t size_num) const;
};
14 changes: 14 additions & 0 deletions 01_Client/include/SimpleChecker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <stddef.h>
#include <cstdint>
#include <vector>

class SimpleChecker
{
public:
SimpleChecker() = delete;

static bool is_prime(uint64_t n);
static std::vector<uint64_t> check_interval(uint64_t start_num, size_t n);
};
38 changes: 38 additions & 0 deletions 01_Client/src/Client.cpp
Original file line number Diff line number Diff line change
@@ -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_prime() const
{
_network.send_int(CODE_GET_MAX_SIMPLE, sizeof(CODE_GET_MAX_SIMPLE));
return (calc_t)_network.read_int(sizeof(calc_t));
}

vector<calc_t> 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<calc_t> res;
vector<uint64_t> 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<uint64_t> prime_nums = SimpleChecker::check_interval(start_num, n);

_network.send_vector(prime_nums, sizeof(count_t), sizeof(calc_t));
}
64 changes: 64 additions & 0 deletions 01_Client/src/Network.cpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t> &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<uint64_t> Network::read_vector(size_t size_length, size_t size_num) const
{
vector<uint64_t> 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;
}
27 changes: 27 additions & 0 deletions 01_Client/src/SimpleChecker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "SimpleChecker.h"

using std::vector;

bool SimpleChecker::is_prime(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<uint64_t> SimpleChecker::check_interval(uint64_t start_num, size_t n)
{
vector<uint64_t> res;

for (size_t dx = 0; dx < n; dx++) {
size_t x = start_num + dx;
if (is_prime(x))
res.push_back(x);
}

return res;
}
79 changes: 79 additions & 0 deletions 01_Client/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <tuple>
#include <vector>
#include <string>
#include "Client.h"

using namespace std;

tuple<string, uint16_t> 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_prime" << endl;
cout << "2. get_last_n <n>" << endl;
cout << "3. calculate <n>" << 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_prime") {
calc_t x = client.get_max_prime();
cout << x << endl;
} else if (command == "get_last_n") {
count_t n;
cin >> n;
vector<calc_t> 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;
}
1 change: 1 addition & 0 deletions 02_Server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
18 changes: 18 additions & 0 deletions 02_Server/include/Calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <stddef.h>
#include <cstdint>
#include <cmath>

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);
};
Loading