ClientMain.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <string.h>
  6. #include "Client.h"
  7. #include "String.h"
  8. Client client;
  9. void interruptHandler(int signal)
  10. {
  11. clientRemove(&client);
  12. exit(EXIT_SUCCESS);
  13. }
  14. int readNumber(const char* text)
  15. {
  16. while(1)
  17. {
  18. int number;
  19. printf("%s", text);
  20. int result = scanf("%d", &number);
  21. char c = getchar();
  22. while(c != EOF && c != '\n')
  23. {
  24. c = getchar();
  25. }
  26. if(result != 0)
  27. {
  28. return number;
  29. }
  30. }
  31. return -1;
  32. }
  33. void waitForData()
  34. {
  35. while(1)
  36. {
  37. printf("Type a command: ");
  38. String s;
  39. stringInit(&s);
  40. stringRead(&s);
  41. if(stringCompare(&s, "SEND"))
  42. {
  43. printf("Sending a mail to the server...\n");
  44. //outgoing stream
  45. Stream out;
  46. streamInit(&out, 128);
  47. streamWriteInt(&out, 0);
  48. // sender
  49. printf("Type a sender with at most 8 characters: ");
  50. String sender;
  51. stringInit(&sender);
  52. stringRead(&sender);
  53. int l = stringGetLength(&sender);
  54. while(l < 1 || l > 8)
  55. {
  56. printf("Please type a valid sender: ");
  57. stringRemove(&sender);
  58. stringInit(&sender);
  59. stringRead(&sender);
  60. l = stringGetLength(&sender);
  61. }
  62. streamWriteChars(&out, sender.data);
  63. streamWriteChar(&out, '\n');
  64. stringRemove(&sender);
  65. // receiver
  66. printf("Type a receiver with at most 8 characters: ");
  67. String receiver;
  68. stringInit(&receiver);
  69. stringRead(&receiver);
  70. l = stringGetLength(&receiver);
  71. while(l < 1 || l > 8)
  72. {
  73. printf("Please type a valid receiver: ");
  74. stringRemove(&receiver);
  75. stringInit(&receiver);
  76. stringRead(&receiver);
  77. l = stringGetLength(&receiver);
  78. }
  79. streamWriteChars(&out, receiver.data);
  80. streamWriteChar(&out, '\n');
  81. stringRemove(&receiver);
  82. // subject
  83. printf("Type a subject with at most 80 characters: ");
  84. String subject;
  85. stringInit(&subject);
  86. stringRead(&subject);
  87. l = stringGetLength(&subject);
  88. while(l < 1 || l > 80)
  89. {
  90. printf("Please type a valid receiver: ");
  91. stringRemove(&subject);
  92. stringInit(&subject);
  93. stringRead(&subject);
  94. l = stringGetLength(&subject);
  95. }
  96. streamWriteChars(&out, subject.data);
  97. streamWriteChar(&out, '\n');
  98. stringRemove(&subject);
  99. // message
  100. printf("Type a message: ");
  101. String message;
  102. stringInit(&message);
  103. stringRead(&message);
  104. streamWriteChars(&out, message.data);
  105. streamWriteChars(&out, "\n.\n");
  106. stringRemove(&message);
  107. // send and clear stream
  108. clientSendData(&client, &out);
  109. streamRemove(&out);
  110. }
  111. else if(stringCompare(&s, "LIST"))
  112. {
  113. //outgoing stream
  114. Stream out;
  115. streamInit(&out, 16);
  116. streamWriteInt(&out, 1);
  117. // user
  118. printf("Type a user with at most 8 characters: ");
  119. String sender;
  120. stringInit(&sender);
  121. stringRead(&sender);
  122. int l = stringGetLength(&sender);
  123. while(l < 1 || l > 8)
  124. {
  125. printf("Please type a valid user: ");
  126. stringRemove(&sender);
  127. stringInit(&sender);
  128. stringRead(&sender);
  129. l = stringGetLength(&sender);
  130. }
  131. streamWriteChars(&out, sender.data);
  132. streamWriteChar(&out, '\n');
  133. stringRemove(&sender);
  134. // send and clear stream
  135. clientSendData(&client, &out);
  136. streamRemove(&out);
  137. }
  138. else if(stringCompare(&s, "READ"))
  139. {
  140. //outgoing stream
  141. Stream out;
  142. streamInit(&out, 16);
  143. streamWriteInt(&out, 2);
  144. // user
  145. printf("Type a user with at most 8 characters: ");
  146. String sender;
  147. stringInit(&sender);
  148. stringRead(&sender);
  149. int l = stringGetLength(&sender);
  150. while(l < 1 || l > 8)
  151. {
  152. printf("Please type a valid user: ");
  153. stringRemove(&sender);
  154. stringInit(&sender);
  155. stringRead(&sender);
  156. l = stringGetLength(&sender);
  157. }
  158. streamWriteChars(&out, sender.data);
  159. streamWriteChar(&out, '\n');
  160. stringRemove(&sender);
  161. // message number
  162. int number = readNumber("Type a message number: ");
  163. streamWriteInt(&out, number);
  164. streamWriteChar(&out, '\n');
  165. // send and clear stream
  166. clientSendData(&client, &out);
  167. streamRemove(&out);
  168. }
  169. else if(stringCompare(&s, "DEL"))
  170. {
  171. //outgoing stream
  172. Stream out;
  173. streamInit(&out, 16);
  174. streamWriteInt(&out, 3);
  175. // user
  176. printf("Type a user with at most 8 characters: ");
  177. String sender;
  178. stringInit(&sender);
  179. stringRead(&sender);
  180. int l = stringGetLength(&sender);
  181. while(l < 1 || l > 8)
  182. {
  183. printf("Please type a valid user: ");
  184. stringRemove(&sender);
  185. stringInit(&sender);
  186. stringRead(&sender);
  187. l = stringGetLength(&sender);
  188. }
  189. streamWriteChars(&out, sender.data);
  190. streamWriteChar(&out, '\n');
  191. stringRemove(&sender);
  192. // message number
  193. int number = readNumber("Type a message number: ");
  194. streamWriteInt(&out, number);
  195. streamWriteChar(&out, '\n');
  196. // send and clear stream
  197. clientSendData(&client, &out);
  198. streamRemove(&out);
  199. }
  200. else if(stringCompare(&s, "QUIT"))
  201. {
  202. Stream out;
  203. streamInit(&out, 16);
  204. streamWriteInt(&out, 4);
  205. clientSendData(&client, &out);
  206. streamRemove(&out);
  207. }
  208. stringRemove(&s);
  209. /*if(type == -1)
  210. {
  211. break;
  212. }
  213. else if(type >= 0 && type < 4)
  214. {
  215. Stream out;
  216. streamInit(&out, 16);
  217. streamWriteInt(&out, type);
  218. switch(type)
  219. {
  220. case 0:
  221. printf("first number: ");
  222. streamWriteInt(&out, readInt());
  223. printf("second number: ");
  224. streamWriteInt(&out, readInt());
  225. break;
  226. case 1:
  227. printf("text: ");
  228. char buffer[128];
  229. fgets(buffer, 128, stdin);
  230. if(strlen(buffer) == 127 && buffer[126] != '\n')
  231. {
  232. clearInput();
  233. }
  234. streamWriteChars(&out, buffer);
  235. break;
  236. case 2:
  237. for(int i = 0; i < 260; i++)
  238. {
  239. streamWriteInt(&out, i);
  240. }
  241. break;
  242. }
  243. if(clientSendData(&client, &out) == -1)
  244. {
  245. streamRemove(&out);
  246. break;
  247. }
  248. streamRemove(&out);
  249. }
  250. else
  251. {
  252. printf("%d is not a valid package number\n", type);
  253. }*/
  254. }
  255. }
  256. void packageAnswer(Stream* in)
  257. {
  258. char buffer[32];
  259. streamGetChars(in, buffer, 32);
  260. printf("%s\n", buffer);
  261. }
  262. void packageList(Stream* in)
  263. {
  264. int number;
  265. if(streamGetInt(in, &number) == -1)
  266. {
  267. return;
  268. }
  269. char c;
  270. while(streamGetChar(in, &c) != -1)
  271. {
  272. printf("%c", c);
  273. }
  274. printf("\n");
  275. }
  276. int main(int argc, char** argv)
  277. {
  278. if(argc < 2)
  279. {
  280. printf("Usage: %s server_address\n", argv[0]);
  281. return EXIT_FAILURE;
  282. }
  283. clientInitDefaults(&client);
  284. signal(SIGINT, interruptHandler);
  285. signal(SIGKILL, interruptHandler);
  286. if(clientInit(&client, argv[1], 6543) == -1)
  287. {
  288. return EXIT_FAILURE;
  289. }
  290. clientRegisterHandler(&client, packageAnswer);
  291. clientRegisterHandler(&client, packageList);
  292. waitForData();
  293. clientRemove(&client);
  294. return EXIT_SUCCESS;
  295. }