Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <time.h>
  9. #include <stdlib.h>
  10.  
  11. #define USERS_NR 9 //stała liczba użytkowników
  12.  
  13. struct User { //struktura uzytkownika
  14. char name[20];
  15. char pass[20];
  16. int status; //0-niezalogowany 1-zalogowany
  17. int group[4]; //0-nienalezy 1-nalezy
  18. };
  19.  
  20. struct msgbuf { //struktura wiadomości
  21. long type;
  22. char text[200];
  23. };
  24. struct message { //struktura przekierowywanych wiadomosci
  25. long type; //od kogo
  26. char text[400]; //wiadomość
  27. int address; //do kogo <1,9> - pojedynczy użytkownik
  28. time_t msgtime; //{11, 22, 33} - grupa 1, 2 i 3.
  29. };
  30. //ŁADOWANIE UŻYTKOWNIKÓW Z PLIKU
  31. void load_users(struct User *user) {
  32. FILE *fp;
  33. fp = fopen("test_users", "r");
  34.  
  35. int index;
  36. index = 1;
  37. //ŁADOWANIE NAZW I HASEŁ
  38. while(index <= USERS_NR)
  39. {
  40. fscanf(fp, "%s", user[index].name);
  41. fscanf(fp, "%s", user[index].pass);
  42. user[index].status = 0;
  43. user[index].group[1] = 0;
  44. user[index].group[2] = 0;
  45. user[index].group[3] = 0;
  46. index++;
  47. }
  48. //ŁADOWANIE PRZYNALEŻNOŚCI DO GRUP
  49. index = 1;
  50. int var;
  51. while(index <= USERS_NR)
  52. {
  53. fscanf(fp, "%d", &var);
  54. if(var > 0) {
  55. user[index].group[var] = 1;
  56. }
  57. index++;
  58. }
  59. fclose(fp);
  60. }
  61. //SPRAWDZENIE CZY TAKI UZYTKOWNIK ISTNIEJE
  62. int check_index(char* name, struct User *user, int id) {
  63. int it;
  64. it = 1;
  65. struct msgbuf msg10;
  66. msg10.type = 10;
  67. printf("próba logowania\n");
  68. //PRZEGLĄD PO WSZYSTKICH NAZWACH I POROWNANIE
  69. while(strcmp(name, user[it].name)) {
  70. it++;
  71. //GDY NIE ZNALAZŁ TAKIEJ NAZWY PROSI O POPRAWE
  72. if(it > USERS_NR) {
  73. printf("błędny login\n");
  74. strcpy(msg10.text, "error");
  75. msg10.type = 10;
  76. msgsnd(id, &msg10, sizeof(msg10)-sizeof(long), 0);
  77. msg10.type = 100;
  78. msgrcv(id, &msg10, sizeof(msg10)-sizeof(long), 100, 0);
  79. strcpy(name, msg10.text);
  80. it = 1;
  81. }
  82. }
  83. //POPRAWNA NAZWA UZYTKOWNIKA
  84. msg10.type = 10;
  85. strcpy(msg10.text, "poprawny");
  86. msgsnd(id, &msg10, sizeof(msg10)-sizeof(long), 0);
  87. //ZWRACA NUMER UŻYTKOWNIKA
  88. return it;
  89. }
  90. //OBSŁUGA KLIENTÓW
  91. void comm_service(int id_s, struct User *user) {
  92.  
  93. struct msgbuf msg; //odbieranie wiadomości
  94. int clause; //zmienna pomocnicza - odbiór komunikatu
  95. // 1) //LOGOWANIE
  96. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 100, IPC_NOWAIT);
  97. if(clause != -1) {
  98. //SPRAWDZENIE NAZWY
  99. int index;
  100. index = check_index(msg.text, user, id_s);
  101. printf("uzytkownik %d chce sie zalogowac\n", index);
  102.  
  103. //bufor pomocniczy do kontaku z userem
  104. struct msgbuf msg10;
  105. msg10.type = 10;
  106. //warunek stopu
  107. int var;
  108. var = 1;
  109. //SPRAWDZENIE HASLA
  110. do {
  111. msgrcv(id_s, &msg, sizeof(msg10)-sizeof(long), 100, 0);
  112. //BŁĘDNE HASŁO
  113. if(strcmp(user[index].pass, msg.text)) {
  114. msg10.type = 10;
  115. strcpy(msg10.text, "error");
  116. printf("uzytkownik %d błędne hasło\n", index);
  117. msgsnd(id_s, &msg10, sizeof(msg10)-sizeof(long), 0);
  118. }
  119. //POPRAWNE HASŁO
  120. else {
  121. msg10.type = 10;
  122. strcpy(msg10.text, "poprawny");
  123. msgsnd(id_s, &msg10, sizeof(msg10)-sizeof(long), 0);
  124. var = 0;
  125. }
  126. } while(var);
  127. //UZYTKOWNIK ZALOGOWANY
  128. printf("uzytkownik %d zalogował się\n", index);
  129. sprintf(msg10.text, "%d", index);
  130. //WYSLANIE UZYTKOWNIKOWI SWOJEGO INDEKSU
  131. msg10.type = 10;
  132. msgsnd(id_s, &msg10, sizeof(msg10)-sizeof(long), 0);
  133. user[index].status = 1;
  134. }
  135. // 2) //WYLOGOWANIE
  136. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 12, IPC_NOWAIT);
  137. if(clause != -1) {
  138. printf("użytkownik %s wylogował się\n", msg.text);
  139. int pom;
  140. sscanf(msg.text, "%d", &pom);
  141. user[pom].status = 0;
  142. }
  143. // 3) //PODGLĄD LISTY UŻYTKOWNIKÓW
  144. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 13, IPC_NOWAIT);
  145. if(clause != -1) {
  146. //POBRANIE NUMERU UZYTKOWNIKA
  147. int number;
  148. sscanf(msg.text, "%d", &number);
  149. printf("podgląd listy %d uzytkownika\n", number);
  150. //PRZYGOTOWANIE WIADOMOŚCI
  151. char help[3];
  152. //wklejam do msg.text po kolei nazwy i status
  153. strcpy(msg.text, user[1].name);
  154. strcat(msg.text, " ");
  155. sprintf(help, "%d", user[1].status);
  156. strcat(msg.text, help);
  157. strcat(msg.text, "\n");
  158. msg.type = 133;
  159. //SKLEJANIE WYSYŁKI
  160. int it;
  161. for(it = 2; it <= USERS_NR; it++) {
  162. strcat(msg.text, user[it].name);
  163. strcat(msg.text, " ");
  164. sprintf(help, "%d", user[it].status);
  165. strcat(msg.text, help);
  166. strcat(msg.text, "\n");
  167. }
  168. //wysyłam caly podgląd w jednym stringu
  169. msgsnd(id_s, &msg, sizeof(msg)-sizeof(long), 0);
  170. }
  171. // 4) //ZAPISANIE SIĘ DO GRUPY
  172. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 14, IPC_NOWAIT);
  173. if(clause != -1) {
  174. //POBRANIE NR UZYTKOWNIKA
  175. int number;
  176. sscanf(msg.text, "%d", &number);
  177. printf("uzytkownik %d chce sie zapisac do grupy\n", number);
  178. char help[6];
  179. //PRZYGOTOWANIE WIADOMOŚCI
  180. help[0] = user[number].group[1] + '0';
  181. help[1] = ' ';
  182. help[2] = user[number].group[2] + '0';
  183. help[3] = ' ';
  184. help[4] = user[number].group[3] + '0';
  185. help[5] = '\0';
  186. //WYSŁANIE PRZYNALEŻNOŚCI DO KTÓRYCH GRUP JUŻ NALEŻY
  187. strcpy(msg.text, help);
  188. msg.type = 144;
  189. msgsnd(id_s, &msg, sizeof(msg)-sizeof(long), 0);
  190. msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 14, 0);
  191. //ODEBRANIE NR GRUPY I PRZYPISANIE
  192. int nr_group;
  193. sscanf(msg.text, "%d", &nr_group);
  194. printf("uzytkownik %d zapisał sie do grupy %d\n", number, nr_group);
  195. user[number].group[nr_group] = 1;
  196. }
  197.  
  198. // 5) //WYPISANIE SIĘ Z GRUPY
  199. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 15, IPC_NOWAIT);
  200. if(clause != -1) {
  201. //POBRANIE NR UZYTKOWNIKA
  202. int number;
  203. sscanf(msg.text, "%d", &number);
  204. printf("uzytkownik %d chce sie wypisac z grupy\n", number);
  205. //PRZYGOTOWANIE WIADOMOŚCI
  206. char help[6];
  207. help[0] = user[number].group[1] + '0';
  208. help[1] = ' ';
  209. help[2] = user[number].group[2] + '0';
  210. help[3] = ' ';
  211. help[4] = user[number].group[3] + '0';
  212. help[5] = '\0';
  213. //WYSŁANIE PRZYNALEŻNOŚCI DO KTÓRYCH GRUP JUŻ NALEŻY
  214. strcpy(msg.text, help);
  215. msg.type = 155;
  216. msgsnd(id_s, &msg, sizeof(msg)-sizeof(long), 0);
  217. msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 15, 0);
  218. //ODEBRANIE NR GRUPY I WYPISANIE
  219. int nr_group;
  220. sscanf(msg.text, "%d", &nr_group);
  221. printf("uzytkownik %d wypisał sie z grupy %d\n", number, nr_group);
  222. user[number].group[nr_group] = 0;
  223. }
  224. // 6) //PODGLĄD DANEJ GRUPY
  225. clause = msgrcv(id_s, &msg, sizeof(msg)-sizeof(long), 16, IPC_NOWAIT);
  226. if(clause != -1) {
  227. //POBRANIE NUMERU GRUPY
  228. int gnumber;
  229. sscanf(msg.text, "%d", &gnumber);
  230. printf("podgląd %d grupy\n", gnumber);
  231. //PRZYGOTOWANIE WIADOMOŚCI
  232. char help[3];
  233. //wklejam do msg.text po kolei nazwy i status
  234. strcpy(msg.text, user[1].name);
  235. strcat(msg.text, " ");
  236. sprintf(help, "%d", user[1].group[gnumber]);
  237. strcat(msg.text, help);
  238. strcat(msg.text, "\n");
  239. //SKLEJANIE WYSYŁKI
  240. int it;
  241. for(it = 2; it <= USERS_NR; it++) {
  242. strcat(msg.text, user[it].name);
  243. strcat(msg.text, " ");
  244. sprintf(help, "%d", user[it].group[gnumber]);
  245. strcat(msg.text, help);
  246. strcat(msg.text, "\n");
  247. }
  248. //wysyłam caly podgląd w jednym stringu
  249. msg.type = 166;
  250. msgsnd(id_s, &msg, sizeof(msg)-sizeof(long), 0);
  251. }
  252. // 7) //PRZEKAZANIE WIADOMOŚCI UZYTKOWNIKA
  253. struct message mssg;
  254. clause = msgrcv(id_s, &mssg, sizeof(mssg)-sizeof(long)-sizeof(int)-sizeof(time_t), -9, IPC_NOWAIT);
  255. if(clause != -1) {
  256. printf("Wiadomosc od %ld do %d\n", mssg.type, mssg.address);
  257. int pom;
  258. pom = mssg.type;
  259. mssg.type = mssg.address * 111;
  260. mssg.address = pom;
  261. mssg.msgtime = time(NULL);
  262. msgsnd(id_s, &mssg, sizeof(mssg)-sizeof(long)-sizeof(int)-sizeof(time_t), 0);
  263. }
  264. // 9) //PRZEKAZANIE WIADOMOŚCI GRUPY
  265. clause = msgrcv(id_s, &mssg, sizeof(mssg)-sizeof(long)-sizeof(int)-sizeof(time_t), 19, IPC_NOWAIT);
  266. if(clause != -1) {
  267. printf("Wiadomosc do grupy%d\n", mssg.address);
  268. int it;
  269. for(it = 1; it <= USERS_NR; it++) {
  270. if(user[it].group[mssg.address] == 1) {
  271. mssg.type = it * 111;
  272. mssg.msgtime = time(NULL);
  273. mssg.address = mssg.address * 11;
  274. msgsnd(id_s, &mssg, sizeof(mssg)-sizeof(long)-sizeof(int)-sizeof(time_t), 0);
  275. mssg.address = mssg.address / 11;
  276. }
  277. }
  278.  
  279. }
  280.  
  281. }
  282.  
  283. int main(void) {
  284. printf("-----WITAJ W SERWERZE-----\n");
  285. printf("aby zakończycz dzialanie wpisz: ctrl+c\n");
  286. //tablica struktur użytkowników
  287. struct User test[USERS_NR + 1];
  288. //wczytanie nazw i haseł użytkowników
  289. load_users(test);
  290. //zmienna z kolejką serwera
  291. int ipc_s;
  292. ipc_s = msgget(99999, 0777 | IPC_CREAT);
  293.  
  294. while(1) {
  295. comm_service(ipc_s, test);
  296. }
  297. return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement