Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. #include "peer.h"
  2.  
  3. char name_server_ip[IP_LEN]; // hostname and port of name server - these
  4. char name_server_port[PORT_LEN]; // are passed as command line arguments.
  5. int name_server_socket = -1; // socket to the name server. initialized to -1.
  6.  
  7. char my_ip[IP_LEN]; // my_ip and my_port are set on /login, and are used for listening.
  8. char my_port[PORT_LEN];
  9.  
  10. char my_username[USERNAME_LEN];
  11.  
  12. int logged_in = 0;
  13.  
  14. int main(int argc, char **argv) {
  15.  
  16. if (argc != MAIN_ARGNUM + 1) {
  17. fprintf(stderr, "Usage: %s <name server IP> <name server port>.\n", argv[0]);
  18. exit(EXIT_FAILURE);
  19. } else if (!is_valid_ip(argv[1])) {
  20. fprintf(stderr, ">> Invalid name server IP: %s\n", argv[1]);
  21. exit(EXIT_FAILURE);
  22. } else if (!is_valid_port(argv[2])) {
  23. fprintf(stderr, ">> Invalid name server port: %s\n", argv[2]);
  24. exit(EXIT_FAILURE);
  25. }
  26.  
  27. snprintf(name_server_ip, IP_LEN, "%s", argv[1]);
  28. snprintf(name_server_port, PORT_LEN, "%s", argv[2]);
  29.  
  30. printf(">> Connecting to name server at %s:%s ...\n",
  31. name_server_ip, name_server_port);
  32.  
  33. /*
  34. * TODO #1
  35. * TODO: SETUP NAME SERVER CONNECTION HERE
  36. * HINT: use the specified ip and port to setup a socket to the name server.
  37. * HINT: remember that you are free to use everything from csapp.c.
  38. */
  39. name_server_socket = Open_clientfd(name_server_ip, name_server_port);
  40.  
  41.  
  42. /*
  43. * we use the RIO library from csapp.c to read user input line by line.
  44. */
  45. rio_t rio;
  46. rio_t rio_server;
  47. char rio_buf[MAX_LINE];
  48. ssize_t num_read;
  49. Rio_readinitb(&rio, STDIN_FILENO);
  50. Rio_readinitb(&rio_server, name_server_socket);
  51.  
  52. char login_buf[MAX_LINE];
  53.  
  54. command_t command; // current command, and array of size MAX_USER_ARGNUM
  55. args_t args; // holding arguments to current command (see peer.h).
  56.  
  57. char *username, *password; // these pointers will serve different
  58. char *ip, *port; // purposes based on the current command.
  59. char *message;
  60.  
  61. int running = 1;
  62. while (running) {
  63. //Rio_readinitb(&rio, STDIN_FILENO);
  64.  
  65. /*
  66. * read line of user input and parse
  67. * the command, storing arguments in args.
  68. */
  69. if ((num_read = Rio_readlineb(&rio, rio_buf, MAX_LINE)) < 0) {
  70. fprintf(stderr, "rio_read() error: %s\n", strerror(errno));
  71. exit(EXIT_FAILURE);
  72. } else if (num_read <= 1) continue; // if input is an empty line or EOF.
  73.  
  74.  
  75. sprintf(login_buf, "%s", rio_buf);
  76.  
  77.  
  78. command = parse_command(rio_buf, args); // see common.h for a description of parse_command()
  79.  
  80. switch (command) {
  81.  
  82. case LOGIN:
  83. if (logged_in) {
  84. printf(">> /login error: already logged in as %s\n", my_username);
  85. break;
  86. }
  87.  
  88. username = args[0]; // username and password to login with.
  89. password = args[1];
  90. ip = args[2]; // ip and port that the name server should respond to
  91. port = args[3]; // (eg. with messages from other users).
  92.  
  93. snprintf(my_ip, IP_LEN, "%s", ip); // write ip and port to my_ip and my_port
  94. snprintf(my_port, PORT_LEN, "%s", port);
  95. snprintf(my_username, USERNAME_LEN, "%s", username);
  96.  
  97. /*
  98. * TODO #2
  99. * TODO: LOG INTO NAME SERVER HERE.
  100. * HINT: use the established connection to the
  101. * HINT: name server to send a login request
  102. *
  103. * HINT: write to the name_server_socket set up earlier; recall that a
  104. * HINT: socket is functionally similar to a file descriptor, and
  105. * HINT: is written to similarly. you can for example use the RIO library,
  106. * HINT: but otherwise google is your friend.
  107. *
  108. * HINT: eventually, you want to set logged_in to 1, but depending
  109. * HINT: on your protocol, you may want to somehow confirm the login first :)
  110. */
  111.  
  112. Rio_writen(name_server_socket, login_buf, strlen(login_buf));
  113.  
  114. Rio_readinitb(&rio_server, name_server_socket);
  115. if (Rio_readlineb(&rio_server, login_buf, MAX_LINE) > 0){
  116. printf("%s \n", login_buf);
  117. }
  118.  
  119. logged_in = 1;
  120.  
  121. break;
  122.  
  123.  
  124. case LOOKUP:
  125. if (!logged_in) {
  126. printf(">> /lookup error: not logged onto name server.\n");
  127. break;
  128. }
  129. Rio_readinitb(&rio_server, name_server_socket);
  130. username = args[0]; // username to lookup (may be null)
  131.  
  132. /*
  133. * TODO #3
  134. * TODO: LOOKUP USERS HERE.
  135. *
  136. * HINT: recall that the API specifies that the user can
  137. * HINT: optionally omit a username to the lookup command.
  138. * HINT: you can choose to either handle it here or server-
  139. * HINT: side, depending on your chosen protocol.
  140. */
  141. //strcat(username, "\n");
  142.  
  143. Rio_writen(name_server_socket, login_buf, strlen(login_buf));
  144. if (Rio_readlineb(&rio_server, login_buf, MAX_LINE) > 0){
  145. printf("User information, %s \n", login_buf);
  146. }
  147. break;
  148.  
  149.  
  150. case LOGOUT:
  151. if (!logged_in) {
  152. printf(">> /logout error: not logged onto name server.\n");
  153. break;
  154. }
  155. /*
  156. * TODO #4
  157. * TODO: LOGOUT OF NAME SERVER HERE.
  158. *
  159. * HINT: as with /login, you eventually want to set logged_in to 0.
  160. */
  161. // logged_in = 0;
  162.  
  163. break;
  164.  
  165.  
  166. case EXIT:
  167. running = 0;
  168. /*
  169. * TODO #5
  170. * TODO: EXIT CLIENT HERE.
  171. *
  172. * HINT: as is, the client simply exits. depending on your protocol,
  173. * HINT: consider what should happen if the user is logged in at exit.
  174. */
  175. break;
  176.  
  177.  
  178. case MSG:
  179. /*
  180. * NOT REQUIRED FOR A6. we save the actual messaging for A7.
  181. */
  182.  
  183. if (!logged_in) {
  184. printf(">> /msg error: not logged onto name server.\n");
  185. break;
  186. }
  187. username = args[1]; // username of recipient.
  188. message = args[2]; // actual message to send.
  189. break;
  190.  
  191.  
  192. case SHOW:
  193. /*
  194. * NOT REQUIRED FOR A6. we leave the actual messaging for A7
  195. */
  196.  
  197. if (!logged_in) {
  198. printf(">> /show error: not logged onto name server.\n");
  199. break;
  200. }
  201. username = args[1]; // name of user to show messages from (may be NULL)
  202. break;
  203.  
  204.  
  205. case ERROR:
  206. printf(">> Error: unknown command or wrong number of arguments.\n");
  207. break;
  208. }
  209. }
  210.  
  211. printf(">> Closing client ...\n");
  212. /*
  213. * TODO #6
  214. * TODO: CLOSE CONNECTION (ie. socket to name server) HERE.
  215. * HINT: at this point, the client is (should be) properly logged out of
  216. * HINT: the name server, so this step should be easy :)
  217. */
  218.  
  219. exit(EXIT_SUCCESS);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement