1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include "StdAfx.h"
#include "Serveur.h"
#include <iostream>
#include <string>
#include <WinSock2.h>
#include <windows.h> // Win32
#include <Winbase.h> // Win32
#include <windowsx.h>
#include <tlhelp32.h>
#include <iomanip>
#include <sstream>
#pragma comment(lib, "ws2_32.lib")
Serveur::Serveur(int d, int port, string ip)
{
cout<<"constructeur "<<this<<endl;
this->ipSurv = ip;
this->portSurv = port;
this->delai = d;
this->alim = 0;
this->ip21 = 0;
this->acq61 = 0;
this->acq68 = 0;
this->acqTor = 0;
this->raid = 0;
// initialisation de WSAData
// initialisation du socket
}
bool Serveur::etatProcess(char * nom)
{
DWORD pid = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 process;
ZeroMemory(&process, sizeof(process)); // rempli la zone mémoire de 0
process.dwSize = sizeof(process);
if(Process32First(snapshot, &process))
{
do
{
// process.szExeFile renvoie une adresse, la comparaison ne peux donc se faire
// pourquoi ?
if (!strcmp((char *)process.szExeFile, nom))
{
pid = process.th32ProcessID;
break;
}
}while(Process32Next(snapshot, &process));
}
CloseHandle(snapshot);
if (pid != 0)
{
return true;
}
else
{
return false;
}
}
bool Serveur::etatAlims()
{
bool etat = false;
char* rec = 0;
char tmp;
unsigned long int nb, taille;
HANDLE log = OpenEventLog(0, (LPCWSTR) "System"); // lecture journal . 0 : machine local - System : journal à lire
if(log == NULL)
{
cout<<"Impossible d'ouvrir le journal"<<endl;
}
ReadEventLog(log, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &tmp, 1, &nb, &taille);
rec = new char[taille + 1];
if(ReadEventLog(log, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, rec, taille + 1, &nb, &taille))
{
int event_num = GET_ATTR(rec, EventID) & FLAG_CODE;
if(event_num == 1 || event_num == 5) // détecte la perte redondance et alim perdu pour plus de sécurité
{
etat = false;
}
else
{
etat = true;
}
}
else
{
cout<<"Erreur de lecture du journal"<<endl;
}
if(log) // ferme le journal
{
CloseEventLog(log);
}
return etat;
}
bool Serveur::etatRAID()
{
bool etat = false;
char* rec = 0;
char tmp;
unsigned long int nb, taille;
HANDLE log = OpenEventLog(0, (LPCWSTR) "Application"); // 0 : machine local - Application : journal à lire
if(log == NULL)
{
cout<<"Impossible d'ouvrir le journal"<<endl;
}
ReadEventLog(log, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &tmp, 1, &nb, &taille);
rec = new char[taille + 1];
if(ReadEventLog(log, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, rec, taille + 1, &nb, &taille))
{
int event_num = GET_ATTR(rec, EventID) & FLAG_CODE;
if(event_num == 338) // 338 => défaut RAID
{
etat = false; // défaut détecté => retourne faux
}
else
{
etat = true; // défaut non détecté => retourne vrai
}
}
else
{
cout<<"Erreur de lecture du journal"<<endl;
}
if(log) // ferme le journal
{
CloseEventLog(log);
}
return etat;
}
void Serveur::transmission()
{
ostringstream msg; // équivalent à sprintf en c
// stockage dans des variables propre à la fonction -> souci de lisibilité et de facilité de développement
int ip21 = this->ip21;
int alim = this->alim;
int acq61 = this->acq61;
int acq68 = this->acq68;
int acqTor = this->acqTor;
int raid = this->raid;
string ip = this->ipSurv;
int port = this->portSurv;
int check = ip21 + alim + acq61 + acq68 + acqTor + raid; // somme de contrôle
// vérifications socket
//préparation de la trame
msg<<alim<<"|"<<ip21<<"|"<<acq61<<"|"<<acq68<<"|"<<acqTor<<"|"<<raid<<"|"<<check;
//envoi de la trame
// vérification de l'envoi et affichage
cout<<"Transmission : MARCHE"<<endl;
}
void Serveur::setEtat(string nom, int etat)
{
// écrit de cette façon pour le moment, à écrire en switch plus tard
if(nom == "alim")
{
this->alim = etat;
}
if(nom == "ip21")
{
this->ip21 = etat;
}
if(nom == "acq61")
{
this->acq61 = etat;
}
if(nom == "acq68")
{
this->acq68 = etat;
}
if(nom == "acqTor")
{
this->acqTor = etat;
}
if(nom == "raid")
{
this->raid = etat;
}
}
int Serveur::getDelai()
{
int d = this->delai;
d *= 1000; // x 1000 -> conversion de ms à s
return d;
}
Serveur::~Serveur(void)
{
cout<<"destructeur "<<this<<endl;
} |
Partager