vinoromano saoule | _darkalt3_ a écrit :
Ben t'as une idée de ton algo ... je vois pas ce qu'il te faut de plus, à part s'y mettre .
|
Voici mon algo et l'erreur à la compilation:
Code :
- #include "ServerSocket.h"
- #include "SocketException.h"
- #include <string>
- #include <mysql/mysql.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <pthread.h>
- #include <unistd.h>
- #define MYSQL_HOST "localhost"
- #define MYSQL_DB "cmm_produits"
- #define MYSQL_LOGIN "cmmuser"
- #define MYSQL_PASSWD "voltair"
- #define mysql_connect(m, h, u, p) mysql_real_connect((m), (h), (u), (p), NULL, 0, NULL, 0)
- class GestionServer
- {
- public:
- MYSQL mysql; /* Vers la connexion de la base*/
- int StartDataBaseConnection();
- void StopDataBaseConnection();
- //void Tsrv_memoireAdd(char *arg);
- //void Tsrv_iaxAdd(char *arg);
- };
- //struct tm {
- // int tm_sec; /* Secondes */
- // int tm_min; /* Minutes */
- // int tm_hour; /* Heures (0 - 23) */
- // int tm_mday; /* Quantième du mois (1 - 31) */
- // int tm_mon; /* Mois (0 - 11) */
- // int tm_year; /* An (année calendaire - 1900) */
- // int tm_wday; /* Jour de semaine (0 - 6 Dimanche = 0) */
- // int tm_yday; /* Jour dans l'année (0 - 365) */
- // int tm_isdst; /* 1 si "daylight saving time" */
- //};
- /* ****************************************************************
- * Nom :StartDataBaseConnection *
- * But :lancer une connection avec une base de donnée *
- **************************************************************** */
- int GestionServer::StartDataBaseConnection()
- {
- mysql_init(&mysql);
- /* Se connecter a la base*/
- if (!mysql_connect(&mysql,MYSQL_HOST,MYSQL_LOGIN,MYSQL_PASSWD))
- {
- printf ("Probleme de connection a la BDD\n" );
- return -1;
- }
- if (mysql_select_db(&mysql,MYSQL_DB))
- {
- printf ("Ne peut pas sectionner la BASE\n" );
- mysql_close(&mysql);
- return -1;
- }
- return 0;
- }
- /* ****************************************************************
- * Nom :StopDataBaseConnection *
- * But : Arreter la connection avec la base de donnée *
- **************************************************************** */
- void GestionServer::StopDataBaseConnection()
- {
- mysql_close(&mysql);
- }
- /* ****************************************************************
- * Nom :TableTsrv_memoireAdd *
- * But :Ajouter les donnees dans la Table Tsrv_memoire *
- **************************************************************** */
- void* Tsrv_memoireAdd(char *arg, MYSQL mysql)
- {
- char *prt;
- char valeur[600],valeur1[40],sqlrequest1[450];
- char val1[20],val2[20], val3[20], val6[20], val5[20],val7[20],val8[20],val9[20],val10[20],val11[20],val13[20],val14[20],mac[20];
- int val12, val4,val22,val32,val52,val62,val72;
- static pthread_mutex_t mutex;
-
-
- prt = strtok(arg, "#" );
- strcpy(mac,prt);
- printf("%s\n",mac);
- prt = strtok(NULL, "#" );
- strcpy(valeur, prt);
- printf("%s\n",valeur);
-
- if (strcmp(valeur,"memoire" )==0)
- {
- prt = strtok(NULL,"#" );
- while (prt!=NULL)
- {
- pthread_mutex_lock(&mutex);
- strcpy(valeur,prt);
- sscanf(valeur,"%s %s %s %s %s %s", val1,val2,val3,val5,val6,val7);
- val12 = atoi(val1);
- val22 = atoi(val2);
- val32 = atoi(val3);
- val52 = atoi(val5);
- val62 = atoi(val6);
- val72 = atoi(val7);
- sprintf(sqlrequest1,"INSERT INTO Tsrv_Memoire (Mac,MemTotal,MemLibr,MemActive,MemInactive,SwapLibr,SwapTotal,Heure) VALUES ('%s','%d','%d','%d','%d','%d','%d',now())",mac,val12,val22,val32,val52,val62,val72);
- mysql_query(&mysql,sqlrequest1);
- prt = strtok(NULL,"#" );
-
- pthread_mutex_unlock(&mutex);
- sleep(1);
-
- }
- pthread_exit(0);
- }
- }
- void* Tsrv_iaxAdd(char *arg, MYSQL mysql)
- {
-
- char *prt;
- char valeur[600],valeur1[40],sqlrequest1[450];
- char val1[20],val2[20], val3[20], val6[20], val5[20],val7[20],val8[20],val9[20],val10[20],val11[20],val13[20],val14[20],mac[20];
- int val12, val4,val22,val32,val52,val62,val72;
- static pthread_mutex_t mutex;
- pthread_mutex_lock(&mutex);
-
- prt = strtok(arg, "#" );
- strcpy(mac,prt);
- printf("%s\n",mac);
- prt = strtok(NULL, "#" );
- strcpy(valeur, prt);
- printf("%s\n",valeur);
-
-
- if (strcmp(valeur,"iax" )==0)
- {
- prt = strtok(NULL,"#" );
- while (prt!=NULL)
- {
- pthread_mutex_lock(&mutex);
- strcpy(valeur,prt);
- sscanf(valeur,"%s %s", val1,val2);
- sprintf(sqlrequest1,"INSERT INTO Tsrv_TelEtat(ident, Mac, Etat,Heure) VALUES ('%s','%s','%s',now())", val1, mac, val2);
- mysql_query(&mysql,sqlrequest1);
- prt = strtok(NULL,"#" );
- pthread_mutex_unlock(&mutex);
- sleep(2);
- }
- pthread_exit(0);
- }
- }
- int main (int argc, char *argv[])
- {
- pthread_t thread1, thread2;
- void *retour;
- static pthread_mutex_t mutex;
- pthread_mutex_init (&mutex, NULL);
- MYSQL mysql;
- GestionServer oGestionServer;
- oGestionServer.StartDataBaseConnection();
-
-
- std::cout << "running....\n";
- try
- {
- /* Create the socket*/
- //int port = 10245;
- int port = 22266;
- ServerSocket server ( port );
- while ( true )
- {
- ServerSocket new_sock;
- server.accept ( new_sock );
- try
- {
- while ( true )
- {
- std::string datast;
- new_sock >> datast;
- //std::cout << datast << "\n";
-
- size_t size = datast.size() + 1;
- char * data = new char[ size ];
- strncpy( data, datast.c_str(), size );
- //printf("%s\n", data);
-
- //oGestionServer.Tsrv_iaxAdd(data);
- if (pthread_create (&thread1, NULL, (void*()(void*)) Tsrv_memoireAdd(data, mysql), NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 1\\n" );
- exit (1);
- }
-
- if (pthread_create (&thread2, NULL, (void*()(void*)) Tsrv_iaxAdd(data, mysql), NULL) < 0)
- {
- fprintf (stderr, "Impossible de créer le thread 2\\n" );
- exit (1);
- }
-
- (void)pthread_join (thread1, &retour);
- (void)pthread_join (thread2, &retour);
-
-
- new_sock << "OK";
- }
- }
- catch ( SocketException& ) {}
-
- }
- }
- catch ( SocketException& e )
- {
- std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
- }
-
- oGestionServer.StopDataBaseConnection();
-
- return EXIT_SUCCESS;
- }
- g++ -c -o testthread.o testthread.cpp
- testthread.cpp: In function `int main(int, char **)':
- testthread.cpp:204: `type name' declared as function returning a function
- testthread.cpp:204: casting to function type `int ()()'
- testthread.cpp:210: `type name' declared as function returning a function
- testthread.cpp:210: casting to function type `int ()()'
- make: *** [testthread.o] Error 1
|
|