00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "../Portage/Error.h"
00012 #include "../Portage/Tools.h"
00013 #include "../Ini/Ini.h"
00014 #include "NetServer.h"
00015 #include "NetParser.h"
00016
00017
00018 MUTEX MutexEcran;
00019 NetServer Server;
00020
00021 int ThreadGestionClient(NetConnect *pConnect);
00022
00023 int RequestServerData(NetRequestParameter *pParameter);
00024
00025 int main(void)
00026 {
00027 char Buffer[NET_BUFFER];
00028 char Key;
00029 int Size;
00030 int i;
00031 Ini Fichier("Net.conf");
00032 BEGIN
00033 LOC("main")
00034 LOG("serveur.log")
00035
00036 if(Fichier.GetSection("langage"))
00037 {
00038 if(Fichier.GetSection("langage")->GetItem("error"))
00039 {
00040 LANG(Fichier.GetSection("langage")->GetItem("error")->GetData())
00041 }
00042 else
00043 printf("Pas d'info de langage d'erreur\n");
00044 }
00045 else
00046 printf("Pas de section langage\n");
00047
00048 sprintf(Buffer, "%s ", NET_DATA);
00049 for(i = strlen(Buffer); i <= (NET_BUFFER - 1); i++)
00050 Buffer[i] = 'a';
00051
00052 MutexInit(&MutexEcran);
00053
00054 TRY if(Fichier.GetSection("client"))
00055 {
00056 if(Fichier.GetSection("serveur")->GetItem("port") && Fichier.GetSection("serveur")->GetItem("limit") && Fichier.GetSection("serveur")->GetItem("pass"))
00057 TRY CERROR = Server.Start(
00058 AtoD(Fichier.GetSection("serveur")->GetItem("port")->GetData()),
00059 AtoD(Fichier.GetSection("serveur")->GetItem("limit")->GetData()),
00060 Fichier.GetSection("serveur")->GetItem("pass")->GetData(),
00061 (ROUTINE)ThreadGestionClient
00062 );
00063 else
00064 {
00065 printf("Pas d'info de port, limit ou pass\n");
00066 CERROR = 2;
00067 }
00068 }
00069 else
00070 {
00071 printf("Pas de section serveur\n");
00072 CERROR = 2;
00073 }
00074
00075 TRY do
00076 {
00077 MutexLock(&MutexEcran);
00078 printf("Clients : %i/%i\n\n", Server.GetNbClient(), Server.GetLimit());
00079 printf("--- Menu d'administration serveur ---\n");
00080 printf("\tl : Liste les clients\n");
00081 printf("\te : Envoie un packet aux clients\n");
00082 printf("\tq : Quitte l'application\n\n");
00083 printf(" Choix : ");
00084 MutexUnLock(&MutexEcran);
00085
00086 Key = KeyGet();
00087
00088 switch(Key)
00089 {
00090 case 'l' :
00091 MutexLock(&MutexEcran);
00092 printf("Serveur en ecoute sur : %i\n\n", Server.Connect->Port);
00093 printf("Nombre de client : %i\n\n", Server.GetNbClient());
00094 for(i = 0; i < Server.GetNbClient(); i++)
00095 printf("IP : %s\tID : %i\n", Server.GetClientNo(i)->Ip, Server.GetClientNo(i)->GetId());
00096
00097 MutexUnLock(&MutexEcran);
00098 break;
00099 case 'e' :
00100 MutexLock(&MutexEcran);
00101 printf("\n\n Entrez la taille des packets : ");
00102 MutexUnLock(&MutexEcran);
00103
00104 scanf("%i", &Size);
00105
00106 for(i = 0; i < Server.GetNbClient(); i++)
00107 {
00108 TRYIF(Server.GetClientNo(i)->SetBufferSize(Size + strlen(NET_DATA) + 1), 14)
00109 TRYIF(Server.GetClientNo(i)->SetBufferManu(Buffer), 14)
00110
00111 TRY CERROR = Server.GetClientNo(i)->Send();
00112 }
00113 break;
00114 }
00115
00116 if(Key != 'q')
00117 {
00118 MutexLock(&MutexEcran);
00119 printf("\nPressez entree pour le menu\n");
00120 MutexUnLock(&MutexEcran);
00121
00122 Key = KeyGet();
00123 }
00124 }
00125 while((Key != 'q') && !CERROR);
00126
00127 Server.Stop();
00128 printf("\nServeur stoper\n");
00129
00130 MutexStop(&MutexEcran);
00131
00132 PERROR
00133
00134 ENDBEGIN
00135 }
00136
00137 int ThreadGestionClient(NetConnect *pConnect)
00138 {
00139 NetParser Parser(pConnect);
00140 BEGIN
00141 LOC("ThreadGestionClient")
00142
00143 TRY Parser.AddRequest(NET_DATA, 0, (ROUTINE)RequestServerData, "%s %i");
00144
00145 do
00146 {
00147 TRY CERROR = pConnect->Receive();
00148
00149 TRY CERROR = Parser.Parse(pConnect->GetBuffer());
00150 }
00151 while(!CERROR && pConnect);
00152
00153 if(pConnect)
00154 Server.KillClient(pConnect->GetId());
00155
00156 ThreadExit(CERROR);
00157
00158 return 0;
00159 }
00160
00161 int RequestServerData(NetRequestParameter *pParameter)
00162 {
00163 MutexLock(&MutexEcran);
00164 printf("Client %i : %i chars\n", pParameter->Connect->GetId(), pParameter->GetInt(1));
00165 MutexUnLock(&MutexEcran);
00166
00167 return 0;
00168 }
00169