Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cn-lab5-client.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <chrono>
- #include <condition_variable>
- #include <iostream>
- #include <map>
- #include <regex>
- #include <string>
- #include <thread>
- #include <winsock2.h>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- using namespace std::chrono;
- typedef unsigned long long ull;
- mutex m;
- condition_variable cvar;
- bool cvar_backup;
- class Network {
- private:
- SOCKET sock;
- sockaddr_in server_addr;
- public:
- Network() {
- WSAData wsadata;
- WSAStartup(0x0202, &wsadata);
- }
- ~Network() {
- WSACleanup();
- }
- int connect(string ip_addr, int port) {
- sock = socket(AF_INET, SOCK_STREAM, 0);
- sockaddr_in server_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.S_un.S_addr = inet_addr(ip_addr.c_str());
- server_addr.sin_port = htons(port);
- int ret = ::connect(sock, (sockaddr *)&server_addr, sizeof(server_addr));
- if (ret == INVALID_SOCKET) {
- cout << "Server is unreachable" << endl;
- cvar_backup = true;
- cvar.notify_one();
- return 1;
- }
- return 0;
- }
- int disconnect() {
- closesocket(sock);
- return 0;
- }
- int send(string s) {
- ::send(sock, s.c_str(), s.length(), 0);
- return 0;
- }
- string receive() {
- char recv_buff[65536];
- int len = recv(sock, &recv_buff[0], sizeof(recv_buff) - 1, 0);
- if (len == -1)
- return "";
- recv_buff[len] = 0;
- return recv_buff;
- }
- }network;
- string tp_to_str(system_clock::time_point tp) {
- return to_string(time_point_cast<milliseconds>(tp).time_since_epoch().count());
- }
- class Commands {
- private:
- map<string, int(Commands::*)(smatch)> command_list;
- int sms(smatch input) {
- auto tp = system_clock::now();
- regex pattern(R"(\-d|(\+?[0-9]+))");
- if (!regex_match(input.str(2), pattern)) {
- cout << "Invalid syntax" << endl;
- return 1;
- }
- network.send(tp_to_str(tp) + " " + input.str(0));
- return 0;
- }
- int del(smatch input) {
- auto tp = system_clock::now();
- regex pattern(R"(\+?[0-9]+)");
- if (!regex_match(input.str(2), pattern)) {
- cout << "Invalid syntax" << endl;
- return 1;
- }
- network.send(tp_to_str(tp) + " " + input.str(0));
- return 0;
- }
- int exit(smatch input) {
- network.send("&client_exit");
- cvar_backup = true;
- cvar.notify_one();
- return 0;
- }
- public:
- Commands() {
- command_list.emplace("exit", &Commands::exit);
- command_list.emplace("quit", &Commands::exit);
- command_list.emplace("sms", &Commands::sms);
- command_list.emplace("del", &Commands::del);
- }
- int exec(smatch input) {
- try {
- (this->*command_list.at(input.str(1)))(input);
- }
- catch (exception e) {
- cout << e.what() << endl;
- }
- return 0;
- }
- }commands;
- int user_input() {
- cout << "Entre your phone number: " << endl;
- string num;
- getline(cin, num);
- network.send(num);
- while (true) {
- string s;
- getline(cin, s);
- regex pattern(R"(^(".+"|[.\S]+)\s*(".*"|[.\S]*)\s*(".*"|[.\S]*)\s*(".*"|[.\S]*)\s*$)");
- smatch match_results;
- regex_match(s, match_results, pattern);
- commands.exec(match_results);
- }
- return 0;
- }
- int port_listen() {
- while (true) {
- string ans = network.receive();
- if (ans == "")
- return 1;
- if (ans == "&sms_success") {
- cout << "Message sent successfully" << endl;
- continue;
- }
- if (ans == "&del_success") {
- cout << "Message deleted successfully" << endl;
- continue;
- }
- if (ans == "&del_fail_too_late") {
- cout << "Message was sent more than a minute ago" << endl;
- continue;
- }
- if (ans == "&del_fail_not_deletable") {
- cout << "Message wasn't marked as deletable" << endl;
- continue;
- }
- if (ans == "&del_fail_no_such_addressee") {
- cout << "You haven't sent any messages to this number yet" << endl;
- continue;
- }
- }
- return 0;
- }
- int main()
- {
- network.connect("127.0.0.1", 5005);
- thread listen(port_listen);
- thread input(user_input);
- unique_lock<mutex> locked(m);
- cvar.wait(locked, [] {return cvar_backup; });
- network.disconnect();
- if (input.joinable())
- input.join();
- if (listen.joinable())
- listen.join();
- network.~network();
- return 0;
- }
Add Comment
Please, Sign In to add comment