Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Elenco Telefonico Remoto - funzioniClient.c

funzioniClient.c

Caricato da:
Scarica il programma completo

  1. /* funzioniClient.c */
  2.  
  3. #include "macro.h"
  4. #include "funzioniClient.h"
  5.  
  6. int trovaRecord(int sock){
  7.  
  8.         /* Invio al server il tipo di servizio desiderato */
  9.         while(write(sock, "TROVA", RISPOSTA) <= 0){
  10.                 if(errno != EINTR){
  11.                         if(errno == ECONNRESET || errno == EPIPE){
  12.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  13.                                 return -1;
  14.                         }
  15.                         else{
  16.                                 printf("La read è fallita\n");fflush(stdout);
  17.                                 perror("Errore tipo");fflush(stdout);
  18.                                 return -1;
  19.                         }
  20.                 }
  21.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  22.         }
  23.  
  24.         Record record, risultato;
  25.  
  26.         /* Resetto i valori della struct record */
  27.         memset(record.nome, 0, sizeof(record.nome));
  28.         memset(record.cognome, 0, sizeof(record.cognome));
  29.         memset(record.numeroTelefono, 0, sizeof(record.numeroTelefono));
  30.  
  31.         /* Resetto i valori della struct risultato */
  32.         memset(risultato.nome, 0, sizeof(risultato.nome));
  33.         memset(risultato.cognome, 0, sizeof(risultato.cognome));
  34.         memset(risultato.numeroTelefono, 0, sizeof(risultato.numeroTelefono));
  35.  
  36.         /* Digito il nome da cercare */
  37.         printf("\nInserisci nome e premi INVIO   ");fflush(stdout);
  38.         while(scanf("%s", record.nome) == -1){
  39.                 if(errno != EINTR){
  40.                         printf("\nErrore scanf\n");
  41.                         chiusuraClient();
  42.                 }
  43.         }
  44.  
  45.         /* Invio il nome al server */
  46.         while(write(sock, record.nome, strlen(record.nome)) <= 0){
  47.                 if(errno != EINTR){
  48.                         if(errno == ECONNRESET || errno == EPIPE){
  49.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  50.                                 return -1;
  51.                         }
  52.                         else{
  53.                                 printf("La read è fallita\n");fflush(stdout);
  54.                                 perror("Errore tipo");fflush(stdout);
  55.                                 return -1;
  56.                         }
  57.                 }
  58.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  59.         }
  60.  
  61.         /* Digito il cognome da cercare */
  62.         printf("\nInserisci cognome e premi INVIO   ");fflush(stdout);
  63.         while(scanf("%s", record.cognome) == -1){
  64.                 if(errno != EINTR){
  65.                         printf("\nErrore scanf\n");
  66.                         chiusuraClient();
  67.                 }
  68.         }
  69.  
  70.         /* Invio il cognome al server */
  71.         while(write(sock, record.cognome, strlen(record.cognome)) <= 0){
  72.                 if(errno != EINTR){
  73.                         if(errno == ECONNRESET || errno == EPIPE){
  74.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  75.                                 return -1;
  76.                         }
  77.                         else{
  78.                                 printf("La read è fallita\n");fflush(stdout);
  79.                                 perror("Errore tipo");fflush(stdout);
  80.                                 return -1;
  81.                         }
  82.                 }
  83.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  84.         }
  85.  
  86.         /* Ricevo il nome-risultato dal server */
  87.         while(read(sock, risultato.nome, NOME) <= 0){
  88.                 if(errno != EINTR){
  89.                         if(errno == ECONNRESET || errno == EPIPE){
  90.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  91.                                 return -1;
  92.                         }
  93.                         else{
  94.                                 printf("La read è fallita\n");fflush(stdout);
  95.                                 perror("Errore tipo");fflush(stdout);
  96.                                 return -1;
  97.                         }
  98.                 }
  99.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  100.         }
  101.  
  102.         /* Ricevo il cognome-risultato dal server */
  103.         while(read(sock, risultato.cognome, COGNOME) <= 0){
  104.                 if(errno != EINTR){
  105.                         if(errno == ECONNRESET || errno == EPIPE){
  106.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  107.                                 return -1;
  108.                         }
  109.                         else{
  110.                                 printf("La read è fallita\n");fflush(stdout);
  111.                                 perror("Errore tipo");fflush(stdout);
  112.                                 return -1;
  113.                         }
  114.                 }
  115.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  116.         }
  117.        
  118.         /* Ricevo il numero-risultato dal server */
  119.         while(read(sock, risultato.numeroTelefono, NUMERO) <= 0){
  120.                 if(errno != EINTR){
  121.                         if(errno == ECONNRESET || errno == EPIPE){
  122.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  123.                                 return -1;
  124.                         }
  125.                         else{
  126.                                 printf("La read è fallita\n");fflush(stdout);
  127.                                 perror("Errore tipo");fflush(stdout);
  128.                                 return -1;
  129.                         }
  130.                 }
  131.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  132.         }
  133.  
  134.         /* Il record cercato è inesistente */
  135.         if(strcmp(risultato.nome, "NULL") == 0){
  136.                 printf("\nIl numero cercato è inesistente!\n");
  137.                 premiInvio();
  138.                 return 1;
  139.         }
  140.         else{
  141.                 /* Stampo il record cercato */
  142.                 printf("\n\n%-13s %-13s %s\n", "NOME", "COGNOME", "N. TEL");fflush(stdout);
  143.                 printf("\n%-13s %-13s %s\n\n", risultato.nome, risultato.cognome, risultato.numeroTelefono);fflush(stdout);
  144.                 premiInvio();
  145.                 return 1;
  146.         }
  147. }
  148.  
  149. int aggiungiRecord(int sock){
  150.  
  151.         /* Invio al server il tipo di servizio desiderato */
  152.         while(write(sock, "AGGIUNGI", RISPOSTA) <= 0){
  153.                 if(errno != EINTR){
  154.                         if(errno == ECONNRESET || errno == EPIPE){
  155.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  156.                                 return -1;
  157.                         }
  158.                         else{
  159.                                 printf("La read è fallita\n");fflush(stdout);
  160.                                 perror("Errore tipo");fflush(stdout);
  161.                                 return -1;
  162.                         }
  163.                 }
  164.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  165.         }
  166.  
  167.         char permessi[RISPOSTA];
  168.  
  169.         /* Resetto il valore della stringa permessi */
  170.         memset(permessi, 0, strlen(permessi));
  171.  
  172.         /* Ricevo il tipo di permesso dal server */
  173.         while(read(sock, permessi, RISPOSTA) <= 0){
  174.                 if(errno != EINTR){
  175.                         if(errno == ECONNRESET || errno == EPIPE){
  176.                                 printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  177.                                 return -1;
  178.                         }
  179.                         else{
  180.                                 printf("La read è fallita\n");fflush(stdout);
  181.                                 perror("Errore tipo");fflush(stdout);
  182.                                 return -1;
  183.                         }
  184.                 }
  185.                 printf("Errore durante la system call, ripeto\n");fflush(stdout);
  186.         }
  187.  
  188.         /* Ho i permessi sufficienti per questa operazione */
  189.         if(strcmp("NO", permessi) != 0){
  190.  
  191.                 Record record;
  192.                 char risposta[RISPOSTA];
  193.                
  194.                 /* Resetto il valore della stringa risposta */
  195.                 memset(risposta, 0, RISPOSTA);
  196.  
  197.                 /* Digito il nome da aggiungere */
  198.                 printf("\nInserisci nome e premi INVIO   ");fflush(stdout);
  199.                 while(scanf("%s", record.nome) == -1){
  200.                         if(errno != EINTR){
  201.                                 printf("\nErrore scanf\n");
  202.                                 chiusuraClient();
  203.                         }
  204.                 }
  205.  
  206.                 /* Invio il nome al server */
  207.                 while(write(sock, record.nome, NOME) <= 0){
  208.                         if(errno != EINTR){
  209.                                 if(errno == ECONNRESET || errno == EPIPE){
  210.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  211.                                         return -1;
  212.                                 }
  213.                                 else{
  214.                                         printf("La read è fallita\n");fflush(stdout);
  215.                                         perror("Errore tipo");fflush(stdout);
  216.                                         return -1;
  217.                                 }
  218.                         }
  219.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  220.                 }
  221.  
  222.                 /* Digito il cognome da aggiungere */
  223.                 printf("\nInserisci cognome e premi INVIO   ");fflush(stdout);
  224.                 while(scanf("%s", record.cognome) == -1){
  225.                         if(errno != EINTR){
  226.                                 printf("\nErrore scanf\n");
  227.                                 chiusuraClient();
  228.                         }
  229.                 }
  230.  
  231.                 /* Invio il cognome al server */
  232.                 while(write(sock, record.cognome, COGNOME) <= 0){
  233.                         if(errno != EINTR){
  234.                                 if(errno == ECONNRESET || errno == EPIPE){
  235.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  236.                                         return -1;
  237.                                 }
  238.                                 else{
  239.                                         printf("La read è fallita\n");fflush(stdout);
  240.                                         perror("Errore tipo");fflush(stdout);
  241.                                         return -1;
  242.                                 }
  243.                         }
  244.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  245.                 }
  246.  
  247.  
  248.                 /* Digito il numero di telefono da aggiungere */
  249.                 printf("\nInserisci numero di telefono e premi INVIO   ");fflush(stdout);
  250.                 while(scanf("%s", record.numeroTelefono) == -1){
  251.                         if(errno != EINTR){
  252.                                 printf("\nErrore scanf\n");
  253.                                 chiusuraClient();
  254.                         }
  255.                 }
  256.  
  257.                 /* Invio il numero di telefono al server */
  258.                 while(write(sock, record.numeroTelefono, NUMERO) <= 0){
  259.                         if(errno != EINTR){
  260.                                 if(errno == ECONNRESET || errno == EPIPE){
  261.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  262.                                         return -1;
  263.                                 }
  264.                                 else{
  265.                                         printf("La read è fallita\n");fflush(stdout);
  266.                                         perror("Errore tipo");fflush(stdout);
  267.                                         return -1;
  268.                                 }
  269.                         }
  270.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  271.                 }
  272.  
  273.  
  274.                 /* Ricevo la risposta dal server */
  275.                 while(read(sock, risposta, RISPOSTA) <= 0){
  276.                         if(errno != EINTR){
  277.                                 if(errno == ECONNRESET || errno == EPIPE){
  278.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  279.                                         return -1;
  280.                                 }
  281.                                 else{
  282.                                         printf("La read è fallita\n");fflush(stdout);
  283.                                         perror("Errore tipo");fflush(stdout);
  284.                                         return -1;
  285.                                 }
  286.                         }
  287.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  288.                 }
  289.  
  290.                 /* Il record è già presente nell'archivio */
  291.                 if(strcmp(risposta, "NO") == 0){
  292.                         printf("\nTentativo fallito. Record già presente nell'archivio!\n");
  293.                         premiInvio();
  294.                         return 1;
  295.                 }
  296.  
  297.                 /* Operazione conclusa con successo */
  298.                 else{
  299.                         printf("\nOperazione conclusa con successo!\n");
  300.                         premiInvio();
  301.                         return 1;
  302.                 }
  303.         }
  304.        
  305.         /* Non ho i permessi sufficienti per questa operazione */
  306.         else{
  307.                 printf("\nPermessi non sufficienti per quest'operazione!\n");
  308.                 premiInvio();
  309.                 return 1;
  310.         }
  311. }
  312.  
  313. int login(int sock){
  314.  
  315.         int tentativi = 3;
  316.  
  317.         printf("\n--------------\033[00;34mLOGIN\033[01;34m--------------\n");fflush(stdout);
  318.  
  319.         Utente utente;
  320.  
  321.         /* Resetto il valore della struct utente */
  322.         memset(utente.nickname, 0, sizeof(utente.nickname));
  323.         memset(utente.password, 0, sizeof(utente.password));
  324.  
  325.         char risposta[RISPOSTA];
  326.  
  327.         /* Resetto il valore della stringa risposta */
  328.         memset(risposta, 0, sizeof(risposta));
  329.  
  330.         while(tentativi>0){
  331.  
  332.                 /* Digito il nickname e la password per il login */
  333.                 printf("\nInserisci nickname:   ");fflush(stdout);
  334.                 while(scanf("%s", utente.nickname) == -1){
  335.                         if(errno != EINTR){
  336.                                 printf("\nErrore scanf\n");
  337.                                 chiusuraClient();
  338.                         }
  339.                 }
  340.                 printf("\nInserisci password:   ");fflush(stdout);
  341.                 while(scanf("%s", utente.password) == -1){
  342.                         if(errno != EINTR){
  343.                                 printf("\nErrore scanf\n");
  344.                                 chiusuraClient();
  345.                         }
  346.                 }
  347.  
  348.                 printf("\n\nConnessione in corso...attendere...\n");
  349.  
  350.                 /* Ricevo il via libera dal server */
  351.                 /* QUESTA READ PERMETTE L'ATTESA BLOCCANTE NEL CASO IN CUI PIÙ CLIENT SIANO CONNESSI AL SERVER */
  352.                 while(read(sock, risposta, sizeof(risposta)) <= 0){
  353.                         if(errno != EINTR){
  354.                                 if(errno == ECONNRESET || errno == EPIPE){
  355.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  356.                                         return -1;
  357.                                 }
  358.                                 else{
  359.                                         printf("La read è fallita\n");fflush(stdout);
  360.                                         perror("Errore tipo");fflush(stdout);
  361.                                         return -1;
  362.                                 }
  363.                         }
  364.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  365.                 }
  366.  
  367.                 printf("Effettuata connessione al server\n");
  368.  
  369.                 /* Invio il nickname al server */
  370.                 while(write(sock, utente.nickname, strlen(utente.nickname)) <= 0){
  371.                         if(errno != EINTR){
  372.                                 if(errno == ECONNRESET || errno == EPIPE){
  373.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  374.                                         return -1;
  375.                                 }
  376.                                 else{
  377.                                         printf("La read è fallita\n");fflush(stdout);
  378.                                         perror("Errore tipo");fflush(stdout);
  379.                                         return -1;
  380.                                 }
  381.                         }
  382.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  383.                 }
  384.  
  385.                 /* Invio la password al server */
  386.                 while(write(sock, utente.password, strlen(utente.password)) <= 0){
  387.                         if(errno != EINTR){
  388.                                 if(errno == ECONNRESET || errno == EPIPE){
  389.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  390.                                         return -1;
  391.                                 }
  392.                                 else{
  393.                                         printf("La read è fallita\n");fflush(stdout);
  394.                                         perror("Errore tipo");fflush(stdout);
  395.                                         return -1;
  396.                                 }
  397.                         }
  398.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  399.                 }
  400.  
  401.                 /* Ricevo la risposta dal server */
  402.                 while(read(sock, risposta, sizeof(risposta)) <= 0){
  403.                         if(errno != EINTR){
  404.                                 if(errno == ECONNRESET || errno == EPIPE){
  405.                                         printf("\n\nCONNESSIONE INTERROTTA, il server si è disconnesso in maniera scorretta!");fflush(stdout);
  406.                                         return -1;
  407.                                 }
  408.                                 else{
  409.                                         printf("La read è fallita\n");fflush(stdout);
  410.                                         perror("Errore tipo");fflush(stdout);
  411.                                         return -1;
  412.                                 }
  413.                         }
  414.                         printf("Errore durante la system call, ripeto\n");fflush(stdout);
  415.                 }
  416.  
  417.                 /* Login effettuato con successo */
  418.                 if(strcmp(risposta, "OK") == 0){
  419.                         printf("\nLogin effettuato con successo\n\n");
  420.                         memset(risposta, 0, sizeof(risposta));
  421.                         return 1;
  422.                 }
  423.  
  424.                 /* Login errato, nuovo tentativo*/
  425.                 if(strcmp(risposta, "NO") == 0){
  426.                         printf("\nNickname o password ERRATI\n");
  427.                         memset(risposta, 0, sizeof(risposta));
  428.                         --tentativi;
  429.                         if(tentativi == 2){
  430.                                 printf("Hai ancora %d tentativi di LOGIN\n", tentativi);
  431.                         }
  432.                         if(tentativi == 1){
  433.                                 printf("Hai ancora %d tentativo di LOGIN\n", tentativi);
  434.                         }
  435.                         premiInvio();
  436.                 }
  437.         }
  438.  
  439.         /* Login definitivamente fallito dopo 3 tentativi errati */
  440.         if(tentativi == 0){
  441.                
  442.                 printf("\nTentativo di LOGIN fallito\n");
  443.                 chiusuraClient();
  444.         }
  445.  
  446. }
  447.  
  448. void premiInvio(){
  449.  
  450.         printf("\nPremere INVIO per continuare...\n");
  451.  
  452.         /* Svuoto l'input buffer */
  453.         while ( getchar() != '\n' );
  454.  
  455.         /* Leggo dallo stdin SOLO \n */
  456.         while(scanf("%*[^\n]") == -1){
  457.                 if(errno != EINTR){
  458.                         printf("\nErrore scanf\n");
  459.                         chiusuraClient();
  460.                 }
  461.         }
  462. }
  463.  
  464. void chiusuraClient(){
  465.        
  466.         printf("\033[00m\n\nCHIUSURA CLIENT IN CORSO...");fflush(stdout);
  467.         printf("\nCLIENT CHIUSO CORRETTAMENTE\n\n");fflush(stdout);
  468.         exit(0);
  469. }