codice:
void* thread_mucclientgroup()
{
//5:usermittente:messaggio
char* msg;
int flag=0;
int exitCond=0;
while(!exitCond)
{
msg=(char*)calloc(256,sizeof(char));
printf("\n");
if(!flag){
printf("join con il gruppo! inserisci Messagio da inviare\n");
flag++;
}
fgets(msg,256,stdin);
//comando di uscita dalla MUC
if(strcmp(msg,":exit")==0)exitCond=1;
char* msg1=(char*)calloc(256,sizeof(char));
strcpy(msg1,"5:");
strcat(msg1,username);
strcat(msg1,":");
strcat(msg1,msg);
int DescrittoreSocketsend=CreaSocketsend("127.0.0.1",1024);
sleep(0.4);
SpedisciMessaggio(DescrittoreSocketsend,msg1);
ChiudiSocket(DescrittoreSocketsend);
//mi metto in attesa dell'ascolto
}
return;
}
definitionGroup()
{
/*DEFINIZIONE DEL GRUPPO DI CONVERSAZIONE*/
/*
- INVIARE STRINGA AL SERVER PER LA DEFINIZIONE DEL GRUPPO 4:USERMITTENTE:USER1:USER2:USER3....
* - INVIARE A TUTTI GLI UTENTI IL JOIN AL GRUPPO DI CONVERSAZIONE (ACCETTARE O NEGARE IL JOIN)
*
* - SE SI ACCETTA IL JOIN SI CHIUDONO LE CONVERSAZIONI PRECEDENTI
* E SI RICEVONO SOLO I MESSAGGI DEL GRUPPO
* - SE SI RIFIUTA L'UTENTE CONTINUA LA SUA CONVERSAZIONE
*
* - L'UTENTE CHE DEFINISCE IL GRUPPO LEGGE SOLO I MESSAGGI DEFINITI IN QUEL GRUPPO
* E SOLO QUEL GRUPPO RICEVE I MESSAGGI DELL'UTENTE
*
*/
//stampiamo lista utenti
listonlineuser();
sleep(1);
pthread_t thmucgroup;
//definiamo la stringa da inviare al server
char* listusergroup=(char*)calloc(256,sizeof(char));
printf("Inserisci gli utenti che vuoi nel gruppo: (user1:user2:user3...)\n");
fgets(listusergroup,256,stdin);
char* msg=(char*)calloc(256,sizeof(char));
strcpy(msg,"4:");
strcat(msg,username);
strcat(msg,":");
strcat(msg,listusergroup);
int DescrittoreSocketsend=CreaSocketsend("127.0.0.1",1024);
//printf("complete string=%s\n",msg);
flagmucgroup=1;
SpedisciMessaggio(DescrittoreSocketsend,msg);
ChiudiSocket(DescrittoreSocketsend);
int retcode=pthread_create(&thmucgroup,NULL,*thread_mucclientgroup,0);
pthread_join(thmucgroup,0);
}
void* my_thread_listen(int DescrittoreSocketrecv)
{
char buffer[512];
int NuovoSocket;
int exitCond=0;
int Quanti;
int n=0;
struct timeval tv;
fd_set readfds;
FD_ZERO(&readfds);
//printf("Server: Attendo connessioni...\n");
while (!exitCond)
{
FD_SET(DescrittoreSocketrecv,&readfds);
//Test sul socket: accept non blocca, ma il ciclo while continua
//l'esecuzione fino all'arrivo di una connessione.
n=select(99999,&readfds,NULL,NULL,NULL);
if(n>0)
{
if ((NuovoSocket=accept(DescrittoreSocketrecv,0,0))!=-1)
{
//Lettura dei dati dal socket (messaggio ricevuto)
if ((Quanti=read(NuovoSocket,buffer,sizeof(buffer)))<0)
{
printf("Impossibile leggere il messaggio.\n");
ChiudiSocket(NuovoSocket);
}
else
{
//Aggiusto la lunghezza...
buffer[Quanti]=0;
//Elaborazione dati ricevuti
if (strcmp(buffer,"exit")==0)
exitCond=1;
//printf("Server: %s \n",buffer);
char* cmd=stringcmd(buffer);
//printf("cmd=%s\n",cmd);
/******************************/
// LOGIN
if(strcmp(cmd,"1:")==0)
{
//verificare se login andato a buon termine
//printf("Server: %s \n",buffer);
if(strcmp(buffer,"1:logincorrect")==0)
{
flaglogin=1;
}
else{flaglogin=0;}
}
/******************************/
/******************************/
// AGGIORNAMENTO LISTA USER ONLINE
if(strcmp(buffer,"updatelistuser")==0)
{
//aggiorna la lista utenti online al collegamento di un nuovo utente
listonlineuser();
}
if(strcmp(cmd,"2:")==0)
{
//definire meglio la stampa della lista utenti
//printf("%s \n",buffer);
int i=0,j=0;
int inimsg=0;
int flag=0;
char* list=(char*)calloc(32,sizeof(char));
//definizione stampa lista utenti 2:user1:user2:user3:
for(i=2,j=0;i<strlen(buffer) && !flag;i++) {
if(buffer[i]!=':'){
list[j]=buffer[i];
j++;
}
else{
//salva il punto in cui inizia la sottostringa successiva
inimsg=i;
flag=1;
}
}
printf("->%s\n",list);
inimsg++;
list=(char*)calloc(32,sizeof(char));
for(i=inimsg,j=0;i<strlen(buffer);i++) {
if(buffer[i]!=':'){
list[j]=buffer[i];
j++;
}else{
printf("->%s\n",list);
j=0;
list=(char*)calloc(32,sizeof(char));
}
}
printf("\n");
}
/******************************/
//ricevo messaggi dagli altri utenti se solo se ho attivato la MUC
if(strcmp(cmd,"3:")==0 && flagmuc==1)
{
//muc client
//3:usermittente:messaggio
int i=0,j=0;
char* mess=(char*)calloc(256,sizeof(char));
for(i=2,j=0;i<strlen(buffer);i++) {
mess[j]=buffer[i];
j++;
}
mess[j]='\0';
printf("%s\n",mess);
}
/******************************/
//MUC CON DEFINIZIONE DI GRUPPO
if(strcmp(cmd,"4:")==0 && !flagmucgroup)
{
//sei stato aggiunto al gruppo di conversazione!
printf("Server: %s \n",buffer);
if(flagmucgroup!=1){
printf("PROVA!!!\n");
flagmucgroup=1;
pthread_cancel(thmenu);
printf("WOOOOOOOOOOOOW\n");
pthread_t thmucgroup;
int retcode=pthread_create(&thmucgroup,NULL,*thread_mucclientgroup,0);
}
}
if(strcmp(cmd,"5:")==0)
{
//MUC CON DEFINIZIONE DI GRUPPO
//Ricevo dal server la creazione del gruppo di conversazione
//printf("Server: %s \n",buffer);
//Ricezione dei messaggi nel gruppo
// se l'utente non è il definitore del gruppo devo avviare la conversazione
int i=0,j=0;
char* mess=(char*)calloc(256,sizeof(char));
for(i=2,j=0;i<strlen(buffer);i++) {
mess[j]=buffer[i];
j++;
}
mess[j]='\0';
printf("%s\n",mess);
}
/******************************/
}
//Chiusura del socket temporaneo
ChiudiSocket(NuovoSocket);
}
}
}
}
//Conversazione multi utente
void mucclient()
{
//3:usermittente:messaggio
char* msg;
int flag=0;
int exitCond=0;
while(!exitCond)
{
msg=(char*)calloc(256,sizeof(char));
printf("\n");
if(!flag){
printf("Sei collegato! inserisci Messagio da inviare\n");
flag++;
}
fgets(msg,256,stdin);
//comando di uscita dalla MUC
if(strcmp(msg,":exit")==0)exitCond=1;
char* msg1=(char*)calloc(256,sizeof(char));
strcpy(msg1,"3:");
strcat(msg1,username);
strcat(msg1,":");
strcat(msg1,msg);
int DescrittoreSocketsend=CreaSocketsend("127.0.0.1",1024);
sleep(0.4);
SpedisciMessaggio(DescrittoreSocketsend,msg1);
ChiudiSocket(DescrittoreSocketsend);
//mi metto in attesa dell'ascolto
}
return;
}
void* my_thread_menu()
{
pidmenu=pthread_self();
int condition=0;
while(!condition)
{
printf("\n");
printf("1)aggiorna la lista utenti online\n");
printf("2)avvia conversazione multi-user\n");
printf("3)avvia conversazione multi-user definendo il gruppo di conversazione\n");
printf("4)esci\n");
//menu
char* msg=(char*)calloc(256,sizeof(char));
fgets(msg,256,stdin);
if(strncmp(msg,"1",1)==0)
{
listonlineuser();
sleep(1);
}
if(strncmp(msg,"2",1)==0)
{
flagmuc=1;
mucclient();
flagmuc=0;
sleep(1);
}
if(strncmp(msg,"3",1)==0)
{
definitionGroup();
sleep(1);
}
}
}
int main()
{
//N.B. L'esempio non usa la funzione fork per far vedere l'utilizzo di
// socket non bloccanti
int DescrittoreSocketsend;
char* msg;
int recvport;
char* recvports;
printf("INSERISCI LA PORTA DI CONVERSAZIONE DA 1024 IN POI....\n");
recvports=(char*)calloc(256,sizeof(char));
fgets(recvports,256,stdin);
recvport=atoi(recvports);
int retcode;
int DescrittoreSocketrecv;
DescrittoreSocketrecv=CreaSocketrecv(recvport);
retcode=pthread_create(&thlisten,NULL,*my_thread_listen,DescrittoreSocketrecv);
int aux=0;
while(!flaglogin)
{
if(aux!=0){
printf("Tentativo di login fallito! Riprova!\n");}
username=login(recvports);
printf("In attesa di risposta dal server...\n");
sleep(1);
aux++;
}
printf("\n");
printf("\n");
printf("Login Avvenuto con successo!\n");
// Accesso al menu utente
printf("\n");
printf("\n");
int cond=0;
sleep(1);
printf("BENVENUTO %s\n",username);
retcode=pthread_create(&thmenu,NULL,*my_thread_menu,NULL);
pthread_join(thmenu,NULL);
pthread_join(thlisten,NULL);
//Chiusura del socket
ChiudiSocket(DescrittoreSocketrecv);
printf("Client: Terminato.\n");
return 0;
}