CARS
|
#include "settings.h"
#include "commons.h"
#include "comsock.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include <pthread.h>
Defines | |
#define | INCORRECT_MESSAGE -1 |
#define | HELP_MESSAGE 0 |
#define | EXIT_MESSAGE 1 |
#define | REQ_MESSAGE 2 |
#define | OFF_MESSAGE 3 |
Functions | |
int | isAlphaNumDots (char *string) |
int | isNumeric (char *string) |
int | messageParser (char *input) |
int | setLoginMessage (char *login, int size, message_t *dest) |
void | createLoginString (char *user, char *pass, char *sck, char *dest) |
void | createSocketName (char *user, char *dest) |
void | setExitMessage (message_t *mess) |
void | setOfferMessage (char *input, message_t *mess) |
void | setRequestMessage (char *input, message_t *mess) |
void | printIncomingMessage (message_t *mess) |
void | print_help (void) |
void | print_invalid_command (void) |
static void | listen_cleanup () |
void | startReceiving () |
void | updateSharEndSem () |
void | updateMessageSem (message_t *in_message) |
int | goingToExit () |
void * | listen_thread () |
void | endPrompt () |
void | waitForReply () |
void | messageSent (int pars) |
int | tryStartThread (pthread_t *t, void *(*routine)(void *), void *arg, char *message) |
void | unlock () |
static void | err_handler (int sig) |
static void | sig_handler (int sig) |
void | cleanup (void) |
void | dummy () |
int | main (int argc, char *argv[]) |
Variables | |
int | sendfd = -1 |
int | sockfd = -1 |
int | receivefd = -1 |
int | message_sem = 1 |
char * | login = NULL |
char | socket_name [LUSERNAME+MAX_PID_CHAR] |
char | buffer [BUFF_SIZE] |
message_t | out_message |
message_t | in_message |
volatile sig_atomic_t | received_signal = 0 |
volatile sig_atomic_t | sharend_sem = 0 |
volatile sig_atomic_t | end_prompt = 0 |
pthread_mutex_t | mux = PTHREAD_MUTEX_INITIALIZER |
pthread_cond_t | cond = PTHREAD_COND_INITIALIZER |
Si dichiara che il contenuto di questo file e' in ogni sua parte opera originale dell' autore.
#define EXIT_MESSAGE 1 |
Il messaggio e' un messaggio di uscita
Referenced by main(), and messageParser().
#define HELP_MESSAGE 0 |
Il messaggio e' un messaggio di aiuto
Referenced by main(), and messageParser().
#define INCORRECT_MESSAGE -1 |
Il messaggio parsato e' incorretto
Referenced by main(), and messageParser().
#define OFF_MESSAGE 3 |
Il messaggio e' un messaggio di offerta
Referenced by main(), messageParser(), and messageSent().
#define REQ_MESSAGE 2 |
Il messaggio e' un messaggio di richiesta
Referenced by main(), messageParser(), and messageSent().
void cleanup | ( | void | ) |
Funzione per il cleanup del processo Chiude tutte le socket e rimuove il file relativo alla socket
References closeSocket(), receivefd, sendfd, socket_name, and sockfd.
Referenced by main().
void createLoginString | ( | char * | user, |
char * | pass, | ||
char * | sck, | ||
char * | dest | ||
) |
Crea la stringa per il login da inviare in un messaggio al server
[in] | user | la strina contenente il nome utente |
[in] | pass | la strina contenente la password |
[in] | sck | la strina contenente il nome della socket |
[out] | dest | la stringa contente il messaggio di login generato (deve essere gia' allocata) |
-1 | se size <= 0 oppure login vale NULL |
Referenced by main().
void createSocketName | ( | char * | user, |
char * | dest | ||
) |
Crea il nome della socket del client, contatenando il nome utente al pid del processo
[in] | user | la strina contenente il nome utente |
[out] | dest | la stringa contente il nome della socket generato (deve essere gia' allocata) |
References LUSERNAME, and MAX_PID_CHAR.
Referenced by main().
void dummy | ( | ) |
Gestore vuoto per inibire la gestione di default di SIGUSR1
Referenced by main().
void endPrompt | ( | ) |
Setta il flag end_prompt a 1 in modo da indicare che la sessione interattiva e' terminata Inoltre invia SIGUSR1 al thread listener
References end_prompt.
Referenced by main().
static void err_handler | ( | int | sig | ) | [static] |
Gestore dei segnali di sistema (SIGSEGV, etc...) Visualizza un messaggio di errore e termina immediatamente il processo
[in] | sig | numero del segnale |
Referenced by main().
int goingToExit | ( | ) |
Indica se il thread listener deve terminare
1 | se il thread listener deve terminare |
0 | se il thread listener non deve terminare |
References end_prompt, received_signal, and sharend_sem.
Referenced by listen_thread().
int isAlphaNumDots | ( | char * | string | ) |
Verifica se una stringa e' composta solamente da caratteri, numeri, spazi, ':', oppure '
'
[in] | string | Puntatore alla stringa da esaminare |
-1 | se la stringa e' NULL, oppure non e' soddisfa i parametri sopra indicati |
0 | se la stringa soddisfa i parametri sopra indicati |
Referenced by messageParser().
int isNumeric | ( | char * | string | ) |
Verifica se una stringa e' composta solamente da numeri, oppure '
'
[in] | string | Puntatore alla stringa da esaminare |
-1 | se la stringa e' NULL, oppure non e' soddisfa i parametri sopra indicati |
0 | se la stringa soddisfa i parametri sopra indicati |
Referenced by messageParser().
static void listen_cleanup | ( | ) | [static] |
Funzione di cleanup del listner Invocata tramite una ptread_cleanup_push/pop
References cond, message_sem, and mux.
Referenced by listen_thread().
void* listen_thread | ( | ) |
Thread che esegue l'ascolto dei messaggi in ingresso
Il thread sta in ascolto dei messaggi in arrivo dal server e li stampa a video. Inoltre opera in modo sincrono con il thread main mediante meccanismi di mutua esclusione
Per una descrizione dettagliata del suo comportamento, si faccia riferimento alla relazione
References message_t::buffer, ec_neg1_c, ec_zero_c, end_prompt, goingToExit(), message_t::length, listen_cleanup(), MSG_NO, MSG_OK, MSG_SHAREND, print, printIncomingMessage(), received_signal, receivefd, receiveMessage(), startReceiving(), sys_print, updateMessageSem(), and updateSharEndSem().
Referenced by main().
int main | ( | int | argc, |
char * | argv[] | ||
) |
Thread main Gestisce l'interazione con l'utente e l'invio dei messaggi in uscita.
Per una descrizione piu' dettagliata si faccia riferimento alla relazione
References acceptConnection(), BUFF_SIZE, message_t::buffer, buffer, cleanup(), createLoginString(), createServerChannel(), createSocketName(), dummy(), ec_neg1, ec_null, ec_zero, endPrompt(), err_handler(), err_print, err_print_1, EXIT_MESSAGE, HELP_MESSAGE, INCORRECT_MESSAGE, listen_thread(), listener, login, LUSERNAME, messageParser(), messageSent(), MSG_OK, OFF_MESSAGE, openConnection(), print, print_2, print_help(), print_invalid_command(), PROMPT, received_signal, receivefd, receiveMessage(), REQ_MESSAGE, sendfd, sendMessage(), setExitMessage(), setLoginMessage(), setOfferMessage(), setRequestMessage(), sharend_sem, sig_handler(), socket_name, SOCKET_PATH, sockfd, sys_print, tryStartThread(), unlock(), and waitForReply().
int messageParser | ( | char * | input | ) |
Controlla se la stringa input contiene un messaggio valido tra quelli stabiliti nel protocollo di comunicazione fra client e server.
[in] | input | la stringa in ingresso |
INCORRECT_MESSAGE | se il messaggio e' scorretto |
EXIT_MESSAGE | se il messaggio e' un messaggio di uscita |
HELP_MESSAGE | se il messaggio e' un messaggio di aiuto |
REQ_MESSAGE | se il messaggio e' un messaggio di richiesta |
OFF_MESSAGE | se il messaggio e' un messaggio di offerta |
References EXIT_MESSAGE, HELP_MESSAGE, INCORRECT_MESSAGE, isAlphaNumDots(), isNumeric(), OFF_MESSAGE, and REQ_MESSAGE.
Referenced by main().
void messageSent | ( | int | pars | ) |
Aggiora i valori dei flag per l'interazione fra i 2 thread e rilascia la mutua esclusione.
[in] | pars | Variabile che indica se il messaggio inviato era un'offerta o una richiesta |
References message_sem, mux, OFF_MESSAGE, REQ_MESSAGE, and sharend_sem.
Referenced by main().
void print_help | ( | void | ) |
Stampa alcuni messaggio di aiuto per l'utente
Referenced by main().
void print_invalid_command | ( | void | ) |
void printIncomingMessage | ( | message_t * | mess | ) |
Stampa un messaggio arrivato al client
[in] | mess | il messaggio da stampare |
References message_t::buffer, err_print_1, MSG_NO, MSG_OK, MSG_SHARE, MSG_SHAREND, print, and print_1.
Referenced by listen_thread().
void setExitMessage | ( | message_t * | mess | ) |
Imposta un messaggio di tipo message_t in modo che sia un messaggio di exit valido
[out] | mess | Il messaggio |
References message_t::length, and MSG_EXIT.
Referenced by main().
int setLoginMessage | ( | char * | login, |
int | size, | ||
message_t * | dest | ||
) |
Imposta un messaggio di tipo message_t in modo che sia un messaggio di login valido
[in] | login | Una stringa che contiene il messaggio di login |
[in] | size | La dimensione di tale stringa |
[out] | dest | Il messaggio di destinazione |
-1 | se size <= 0 oppure login vale NULL |
References message_t::buffer, message_t::length, and MSG_CONNECT.
Referenced by main().
void setOfferMessage | ( | char * | input, |
message_t * | mess | ||
) |
Imposta un messaggio di tipo message_t in modo che sia un messaggio di offerta valido
[in] | input | Una stringa che contiene l'offerta da inviare |
[in,out] | mess | Il messaggio generato, il cui campo buffer deve essere stato gia' allocato |
References BUFF_SIZE, message_t::buffer, message_t::length, and MSG_OFFER.
Referenced by main().
void setRequestMessage | ( | char * | input, |
message_t * | mess | ||
) |
Imposta un messaggio di tipo message_t in modo che sia un messaggio di richiesta valido
[in] | input | Una stringa che contiene la richiesta da inviare |
[in,out] | mess | Il messaggio generato, il cui campo buffer deve essere stato gia' allocato |
References BUFF_SIZE, message_t::buffer, message_t::length, and MSG_REQUEST.
Referenced by main().
static void sig_handler | ( | int | sig | ) | [static] |
Gestore dei segnali di terminazione (SIGINT, SIGTERM) Setta ad uno la variabile received_signal ed invia SIGUSR1 al thread
[in] | sig | numero del segnale |
References received_signal.
Referenced by main().
void startReceiving | ( | ) |
Aggiorna il flag message_sem per indicare che il listener e' pronto a ricevere messaggi in ingresso, ed il thread main puo' essere risvegliato
References cond, message_sem, and mux.
Referenced by listen_thread().
int tryStartThread | ( | pthread_t * | t, |
void *(*)(void *) | routine, | ||
void * | arg, | ||
char * | message | ||
) |
Prova ad avviare un thread. Nel caso in cui non vi siano sufficienti risorse attende per DELAY secondi e prova di nuovo ad avviarlo per MAX_TRY volte
[in,out] | t | il TID del thread avviato |
[in] | routine | la funzione che indica il thread da avviare |
[in] | arg | il parametro della funzione |
[in] | message | Nome del thread, in modo da poter scrivere a video l'errore |
0 | se il thread e' stato avviato correttamente |
-1 | se non si e' riusciti ad avviare il thread |
References DELAY, err_print_1, and MAX_TRY.
Referenced by main(), and startWorker().
void updateMessageSem | ( | message_t * | in_message | ) |
Cambia i valori dei flag per l'interazione dei 2 thread e risveglia il main
References cond, message_sem, MSG_NO, MSG_OK, mux, and sharend_sem.
Referenced by listen_thread().
void updateSharEndSem | ( | ) |
Resetta il flag sharend_sem se necessario
References sharend_sem.
Referenced by listen_thread().
void waitForReply | ( | ) |
Si mette in attesa su cond che il flag message_sem sia 0 Una volta usciti da questa funzione si ha acquisito la mutua esclusione con mux
References cond, message_sem, and mux.
Referenced by main().
Stringa utilizzata per la lettura da standard input
Referenced by findAssociation(), main(), and match().
cond = PTHREAD_COND_INITIALIZER |
Utilizzata per la mutua esclusione fra i 2 thread del client sulla variabile message_sem
Referenced by findActivateThread(), initializePool(), listen_cleanup(), signal_handler(), startReceiving(), updateMessageSem(), waitForActivation(), and waitForReply().
end_prompt = 0 |
Flag che viene utilizzato per la gesitone della chiusura della sessione
Di default vale 0 Viene settato ad 1 se e' stato ricevuto un EOF o e' stato digitato un messaggio di uscita Viene utilizzato per capire quando terminare l'esecuzione
Referenced by endPrompt(), goingToExit(), and listen_thread().
Struttura contentente in messaggio in entrata
Referenced by worker_thread().
login = NULL |
Puntantore alla stringa che conterra' il messaggio per il login Di default settata a NULL
Referenced by main().
message_sem = 1 |
Flag relativo ad un messaggio inviato Vale 0 se non si e' inviato alcun messaggio Vale 1 se si sta attendendo un esito del messaggio appena inviato
Referenced by listen_cleanup(), messageSent(), startReceiving(), updateMessageSem(), and waitForReply().
mux = PTHREAD_MUTEX_INITIALIZER |
Utilizzata per la mutua esclusione fra i 2 thread del client sulla variabile message_sem
Referenced by listen_cleanup(), messageSent(), startReceiving(), unlock(), updateMessageSem(), and waitForReply().
Struttura contentente in messaggio in uscita
Referenced by findAssociation(), match(), and worker_thread().
received_signal = 0 |
Flag che viene settato a 1 se e' stato ricevuto un messaggio di SIGINT, SIGTERM. Di default vale 0
Referenced by getFlagSig(), goingToExit(), increaseFlagSig(), listen_thread(), main(), and sig_handler().
receivefd = -1 |
File descriptor della socket in entrata al client Di default settata a -1
Referenced by cleanup(), listen_thread(), and main().
sendfd = -1 |
sharend_sem = 0 |
Flag che viene utilizzato per la gesitone degli SHARE_END
Di default vale 0 Viene settato ad 1 se e' stato inviato un messaggio di richiesta Viene settato a 2 se tale messaggio e' stato inviato Viene resettato appena si riceve uno SHARE_END
Referenced by goingToExit(), main(), messageSent(), updateMessageSem(), and updateSharEndSem().