Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <unistd.h>
  7. #include <map>
  8. #include <algorithm>
  9. #include "whatsappio.h"
  10.  
  11. #define FAILED "failed"
  12. #define CLIENT_EXIT "client_exit"
  13. #define SUCCESS "success"
  14. using namespace std;
  15. map<int, string> clients; //sd,name
  16. map<string, vector<string>> groups; //group: members
  17.  
  18. bool createGroup(string basic_string, vector<string> client_list);
  19.  
  20. string get_clients()
  21. {
  22. string result;
  23. for (auto x: clients)
  24. {
  25. result += x.second + ",";
  26. }
  27. if (!result.empty())
  28. {
  29. result.pop_back(); //remove last ','
  30. }
  31. return result;
  32. }
  33.  
  34. int get_client_socket(string name)
  35. {
  36. int result = -1;
  37. for (auto x: clients)
  38. {
  39. if (x.second == name)
  40. {
  41. result = x.first;
  42. }
  43. }
  44. return result;
  45. }
  46.  
  47. bool send_to_group(string group_name, string sender_name, string message)
  48. {
  49. // client trying to send to group he is not in it.
  50. if (!(find(groups[group_name].begin(), groups[group_name].end(), sender_name) !=
  51. groups[group_name].end()))
  52. {
  53. return false;
  54. }
  55. for (string name : groups[group_name])
  56. {
  57. int s = get_client_socket(name);
  58. if (write(s, message.c_str(), strlen(message.c_str())) < 0)
  59. {
  60. return false;
  61. }
  62. }
  63.  
  64. return true;
  65. }
  66.  
  67. /** server side */
  68. int server(unsigned short port_num)
  69. {
  70. char myname[255] = "";
  71. struct hostent *hp;
  72. int server_socket;
  73. struct sockaddr_in sa;
  74. fd_set readfds;
  75. gethostname(myname, 255);
  76. hp = gethostbyname(myname);
  77. if (hp == nullptr)
  78. {
  79. exit(1);
  80. }
  81. cout << inet_ntoa(*((struct in_addr *) hp->h_addr)) << "\n";
  82. //sockaddrr_in initlization
  83. memset(&sa, 0, sizeof(struct sockaddr_in));
  84. sa.sin_family = hp->h_addrtype;
  85. cout << sa.sin_addr.s_addr << "\n";
  86. memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
  87. cout << sa.sin_addr.s_addr << "\n";
  88. sa.sin_port = htons(port_num);
  89. //create socket
  90. if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  91. {
  92. perror("socket");
  93. exit(1);
  94. }
  95.  
  96. if (bind(server_socket, (struct sockaddr *) &sa, sizeof(sa)) < 0)
  97. {
  98. close(server_socket);
  99. perror("bind");
  100. exit(1);
  101. }
  102.  
  103. if (listen(server_socket, 10) < 0)
  104. {
  105. perror("listen");
  106.  
  107. exit(1);
  108. }
  109. while (true)
  110. {
  111. FD_ZERO(&readfds);
  112. FD_SET(server_socket, &readfds);
  113. FD_SET(STDIN_FILENO, &readfds);
  114. int max_fd = server_socket;
  115. for (auto x : clients)
  116. {
  117. FD_SET(x.first, &readfds);
  118. if (x.first > max_fd)
  119. {
  120. max_fd = x.first;
  121. }
  122. }
  123. if (select(max_fd + 1, &readfds, nullptr, nullptr, nullptr) < 0)
  124. {
  125. exit(1);
  126. }
  127. if (FD_ISSET(server_socket, &readfds))
  128. { //new client connection
  129. int new_socket;
  130. if ((new_socket = accept(server_socket, nullptr, nullptr)) < 0)
  131. {
  132. exit(1);
  133. }
  134. char new_name[WA_MAX_NAME] = "";
  135. read(new_socket, new_name, WA_MAX_NAME);
  136. string new_name_s(new_name);
  137. int x = get_client_socket(new_name_s);
  138. if (x != -1)
  139. { //existing name
  140. write(new_socket, FAILED, strlen(FAILED));
  141. }
  142. else
  143. {
  144. write(new_socket, SUCCESS, strlen(SUCCESS));
  145. cout << new_name_s << " connected.\n";
  146. clients[new_socket] = new_name_s;
  147. }
  148. }
  149. if (FD_ISSET(STDIN_FILENO, &readfds))
  150. { // server input
  151. char buffer[WA_MAX_MESSAGE] = "";
  152. int b = (int) read(STDIN_FILENO, buffer, WA_MAX_MESSAGE);
  153. buffer[b - 1] = '\0';
  154. string string_buffer(buffer);
  155. if (string_buffer == "EXIT")
  156. {
  157. for (auto x: clients)
  158. {
  159. write(x.first, CLIENT_EXIT, strlen(CLIENT_EXIT));
  160. close(x.first);
  161. clients.erase(x.first);
  162. }
  163. print_exit();
  164. exit(0);
  165. }
  166. }
  167. else
  168. { //client request
  169. for (auto x : clients)
  170. {
  171. if (FD_ISSET(x.first, &readfds))
  172. { //check client command
  173. char buffer[WA_MAX_MESSAGE] = "";
  174. read(x.first, buffer, WA_MAX_MESSAGE);
  175. command_type command_t;
  176. string name;
  177. string message;
  178. vector<string> g_clients;
  179. parse_command(buffer, command_t, name, message, g_clients);
  180. if (command_t == CREATE_GROUP)
  181. {
  182. g_clients.push_back(x.second);
  183. if (createGroup(name, g_clients))
  184. {
  185. write(x.first, SUCCESS, strlen(SUCCESS));
  186. print_create_group(true, true, x.second, name);
  187. }
  188. else
  189. {
  190.  
  191. write(x.first, FAILED, strlen(FAILED));
  192. print_create_group(true, false, x.second, name);
  193.  
  194. }
  195.  
  196. }
  197. else if (command_t == SEND)
  198. {
  199. if (groups.count(name))
  200. { //to group
  201. bool send_success = send_to_group(name, x.second, x.second + ": " +
  202. message);
  203. write(x.first, send_success ? SUCCESS : FAILED, send_success ? strlen
  204. (SUCCESS) : strlen(FAILED));
  205. print_send(true, send_success, x.second, name, message);
  206. }
  207. else
  208. { //to client
  209. int receiver = get_client_socket(name);
  210. if (receiver == -1 || name == x.second)
  211. { // does not exist, cant send to myself
  212. write(x.first, FAILED, strlen(FAILED));
  213. print_send(true, false, x.second, name, message);
  214. }
  215. else
  216. {
  217. write(x.first, SUCCESS, strlen(SUCCESS));
  218. string sent_message = x.second + ": " + message;
  219. write(receiver, sent_message.c_str(), strlen(sent_message.c_str()));
  220. print_send(true, true, x.second, name, message);
  221. }
  222. }
  223. }
  224. else if (command_t == WHO)
  225. {
  226. string clients_names = get_clients();
  227. write(x.first, clients_names.c_str(), strlen(clients_names.c_str()));
  228. print_who_server(x.second);
  229. }
  230. else if (command_t == EXIT)
  231. {
  232. print_exit(true, x.second);
  233. close(x.first);
  234. clients.erase(x.first);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241.  
  242. bool createGroup(string group_name, vector<string> client_list)
  243. {
  244.  
  245. for (auto c:group_name)
  246. {
  247. if (!isalpha(c) && !isdigit(c))
  248. {
  249.  
  250. return false;
  251. }
  252. }
  253.  
  254. for (auto group :groups)
  255. {
  256. if (group.first == group_name)
  257. {
  258. return false;
  259. }
  260. }
  261. for (auto client: clients)
  262. {
  263. if (client.second == group_name)
  264. {
  265.  
  266. return false;
  267. }
  268.  
  269. }
  270.  
  271. sort(client_list.begin(), client_list.end());
  272. client_list.erase(unique(client_list.begin(), client_list.end()), client_list.end());
  273. for (auto client : client_list)
  274. {
  275. bool found = false;
  276. for (auto connected_clients : clients)
  277. {
  278. if (client == connected_clients.second)
  279. {
  280. cout<<client<<" was found"<<endl;
  281. found = true;
  282. }
  283.  
  284. }
  285. // user in client list that is not connected to server at all
  286.  
  287. if (!found)
  288. {
  289. return false;
  290. }
  291. }
  292. if (client_list.size() < 2)
  293. {
  294. return false;
  295. }
  296.  
  297. groups[group_name] = client_list;
  298. return true;
  299. }
  300.  
  301. int main(int argc, char *argv[])
  302. {
  303. if (argc != 2)
  304. {
  305. print_server_usage();
  306. return -1;
  307. }
  308. server(atoi(argv[1]));
  309. return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement