|
| 1 | +// |
| 2 | +// Created by ByteDance on 2022/4/12. |
| 3 | +// |
| 4 | + |
| 5 | +// |
| 6 | +// Created by ByteDance on 2022/4/12. |
| 7 | +// |
| 8 | +#include "Factory.h" |
| 9 | +#include "Client.h" |
| 10 | +#include "com.h" |
| 11 | + |
| 12 | +namespace qml { |
| 13 | + Client::Client() { |
| 14 | + try { |
| 15 | + socket_com = new Socket("127.0.0.1", 0); |
| 16 | + } catch (std::bad_alloc &e) { |
| 17 | + std::cerr << e.what() << std::endl; |
| 18 | + } |
| 19 | + |
| 20 | + bzero(cmd, sizeof cmd); |
| 21 | + encodeFactory = new EncodeFactory(); |
| 22 | + decodeFactory = new DecodeFactory(); |
| 23 | + } |
| 24 | + |
| 25 | + void Client::Start() { |
| 26 | + NetWork* nw = new NetWork("127.0.0.1", PORT); |
| 27 | + |
| 28 | + if (socket_com->Connect(nw)) { |
| 29 | + ASSERT(false, "connect error"); |
| 30 | + } |
| 31 | + |
| 32 | + delete nw; |
| 33 | + bzero(res, sizeof res); |
| 34 | + int num = socket_com->Recv(res, sizeof res); |
| 35 | + res[num] = 0; |
| 36 | + PORT2 = atoi(res); |
| 37 | +// std::cout << PORT2 << std::endl; |
| 38 | + handle(); |
| 39 | + } |
| 40 | + |
| 41 | + void Client::split() { |
| 42 | + com.clear(); |
| 43 | + std::string s; |
| 44 | + int len = strlen(cmd); |
| 45 | + for (int i = 0; i < len; i++) { |
| 46 | + if (cmd[i] == ' ') { |
| 47 | + if (!s.empty()) { |
| 48 | + com.push_back(s); |
| 49 | + s.clear(); |
| 50 | + } |
| 51 | + } else if (cmd[i] != '\n') { |
| 52 | + s.push_back(cmd[i]); |
| 53 | + } |
| 54 | + } |
| 55 | + if (!s.empty()) |
| 56 | + com.push_back(s); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + void Client::Send_txt() { |
| 61 | + FILE* fp = fopen(filename, "r"); |
| 62 | + memset(res, 0, sizeof res); |
| 63 | + int num_read; |
| 64 | + bzero(res, sizeof res); |
| 65 | + while ((num_read = fread(res, sizeof(char), sizeof res, fp)) > 0) { |
| 66 | + socket_file->Send(res, num_read); |
| 67 | + bzero(res, sizeof res); |
| 68 | + } |
| 69 | + fclose(fp); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + void Client::SendFile() { |
| 74 | + EncodeType type = qml::check_en(com[1]); |
| 75 | + if (com[0] != "send" || type == NO_EN) { |
| 76 | + puts("error"); |
| 77 | + } else { |
| 78 | + bzero(cmd, sizeof cmd); |
| 79 | + int m = 0; |
| 80 | + for (auto i : com) { |
| 81 | + for (auto j : i) { |
| 82 | + cmd[m++] = j; |
| 83 | + } |
| 84 | + cmd[m++] = ' '; |
| 85 | + } |
| 86 | + cmd[m] = 0; |
| 87 | + socket_com->Send(cmd, sizeof cmd); |
| 88 | + init_file(); |
| 89 | + Encode* en = encodeFactory->CreateEncode(type); |
| 90 | + strcpy(filename, "12123_tmp_12123"); |
| 91 | + en->file_encode(com.back(), true, filename); |
| 92 | + Send_txt(); |
| 93 | + system("rm 12123_tmp_12123"); |
| 94 | + delete socket_file; |
| 95 | + } |
| 96 | + } |
| 97 | + void Client::init_file() { |
| 98 | + try { |
| 99 | + socket_file = new Socket("127.0.0.1", 0); |
| 100 | + } catch (std::bad_alloc &e) { |
| 101 | + std::cerr << e.what() << std::endl; |
| 102 | + } |
| 103 | + NetWork* sock = new NetWork("127.0.0.1", PORT2); |
| 104 | + socket_file->Connect(sock); |
| 105 | + delete sock; |
| 106 | + } |
| 107 | + void Client::SendCom() { |
| 108 | + int num_read; |
| 109 | + socket_com->Send(cmd, sizeof cmd); |
| 110 | + init_file(); |
| 111 | + bzero(res, sizeof res); |
| 112 | + while ((num_read = socket_file->Recv(res, sizeof res)) > 0) { |
| 113 | + std::cout << res; |
| 114 | + bzero(res, sizeof res); |
| 115 | + } |
| 116 | + delete socket_file; |
| 117 | + } |
| 118 | + void Client::handle() { |
| 119 | + bzero(cmd ,sizeof cmd); |
| 120 | + std::cout << "->"; |
| 121 | + while (fgets(cmd, sizeof cmd, stdin) != NULL) { |
| 122 | + split(); |
| 123 | + if (com.empty()) { |
| 124 | + std::cout << "->"; |
| 125 | + continue; |
| 126 | + } else if ((int)com.size() > 3) { |
| 127 | + puts("error"); |
| 128 | + } else if ((int) com.size() == 3) { |
| 129 | + SendFile(); |
| 130 | + } else { |
| 131 | + if (com[0] == "exit") { |
| 132 | + break; |
| 133 | + } else if (com[0] == "cls") { |
| 134 | + system("clear"); |
| 135 | + } else { |
| 136 | + SendCom(); |
| 137 | + } |
| 138 | + } |
| 139 | + bzero(cmd, sizeof cmd); |
| 140 | + std::cout << "->"; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + Client::~Client() { |
| 145 | + delete socket_file; |
| 146 | + delete socket_com; |
| 147 | + delete encodeFactory; |
| 148 | + delete decodeFactory; |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | + |
0 commit comments