Характеристика программы.
Список файлов.
trclient.h,trclient.cppкласс Клиент
Sdate.h,Sdate.cppкласс элемент списка Сервера
Server.h,Server.cppкласс Сервер
Msg.h,Msg.cppкласс сообщение
kuTest.cppтестирующая программа.
Исследование.
Все проведенные тесты показывают корректность работы программы. Представленная тестирующая программа демонстрирует работу интерфейсных функций.
Приложения.
Листинг исходного кода программы.
1. Сlient.h
#ifndef H009
#define H009
#include <string.h>
#include <conio.h>
class Client
{
private:
char Name[30];
int RequestNum;
public:
Client();
Client(char W[30]);
void GetName(char N[30]);
void SetName(char N[30]);
void Set(Client C);
void SetReq(int R);
void getmsg(char M[100]);
int getreq();
int sendrequest();
~Client();
};
#endif
2. Client.cpp
#ifndef CPP009
#define CPP009
#include "Client.h"
#include <string.h>
int Client::getreq()
{
return RequestNum;
}
Client::Client()
{
RequestNum=0;
}
Client::Client(char W[30])
{
strcpy(Name,W);
RequestNum = 0;
};
void Client::GetName(char N[30])
{
strcpy(N,Name);
}
void Client::Set(Client C)
{
char N[30];
C.GetName(N);
SetName(N);
RequestNum = C.getreq();
}
void Client::SetReq(int R)
{
RequestNum = R;
}
void Client::SetName(char N[30])
{
strcpy(Name,N);
}
void Client::getmsg(char M[100])
{
cout<<"Client "<<Name<<" incomming message: "<<endl
<<M<<endl;
SetReq(getreq() + 1);
}
int Client::sendrequest()
{
if (RequestNum > 0)
{
RequestNum -= 1;
return 1;
};
return 0;
}
Client::~Client(){};
#endif
3. Sdate.H
#ifndef H010
#define H010
#include "trClient.cpp"
class Sdate
{
private:
trclient Cl;
int NotReq;
public:
Sdate();
void SetCl(trclient NCl);
trclient GetCl();
void SetNReq(int NR);
void Set(Sdate S);
int GetNReq();
~Sdate();
};
#endif
4. Sdate.Cpp
#ifndef CPP010
#define CPP010
#include "Sdate.h"
Sdate::Sdate()
{
NotReq = 0;
};
void Sdate::SetCl(trclient NCl)
{
Cl.Set(NCl);
}
trclient Sdate::GetCl()
{
return Cl;
}
void Sdate::SetNReq(int R)
{
NotReq = R;
}
int Sdate::GetNReq()
{
return NotReq;
}
void Sdate::Set(Sdate S)
{
SetCl(S.GetCl());
SetNReq(S.GetNReq());
}
Sdate::~Sdate()
{}
#endif
5. Server.H
#ifndef H011
#define H011
#include "ctlist.cpp"
#include "Client.cpp"
#include "Sdate.cpp"
#include "msg.cpp"
#include "cmapboard.cpp"
class Server : public cMapboard
{
linlist<Sdate> ClientList;
int DupClient(char Name[30]);
void correctlist();
void add(Sdate C);
public:
Server();
void SetBoard(double X0, double Y0, double R0);
void addClient(char Name[30],cColTriangle T);
void delClient(char Name[30]);
void personalmsg(char Name[30],msg M);
void broadcastmsg(msg M);
void requestcheck();
void workaccount();
void SystemInfo();
~Server();
};
#endif
6. Server.Cpp
#ifndef CPP011
#define CPP011
#include "Server.h"
Server::Server()
{
cPoint P;
Set(P,10);
};
int Server::DupClient(char Name[30])
{
ClientList.tobegin();
if (!ClientList.Empty())
{
char S[30];
while (!ClientList.TheEnd())
{
((ClientList.GetDate()).GetCl()).GetName(S);
if (strcmp(Name,S) == 0)
return 1;
ClientList.move();
};
((ClientList.GetDate()).GetCl()).GetName(S);
if (strcmp(Name,S) == 0)
return 1;
};
return 0;
}
void Server::addClient(char Name[30],cColTriangle T)
{
if (DupClient(Name))
cout<<"Server : Client "<<Name<<" is already in list."<<endl;
else
{
trclient Cl(Name);
Cl.SetColor(T.GetColor());
int i;
for (i=0;i<3;i++)
{
Cl.SetTopXY(i,T.GetX(i),T.GetY(i));
};
if (inboard(Cl.GetX(0),Cl.GetY(0))&&
inboard(Cl.GetX(1),Cl.GetY(1))&&
inboard(Cl.GetX(2),Cl.GetY(2)))
{
Sdate C;
C.SetCl(Cl);
C.SetNReq(0);
ClientList.add(C);
cout<<"Server : Client "<<Name<<" added."<<endl;
}
else
{
cout<<"Server : Impossible to add. Not in-board coordinates";
}
};
};
void Server::add(Sdate C)
{
ClientList.add(C);
}
void Server::delClient(char Name[30])
{
if (DupClient(Name))
{
ClientList.del();
cout<<"Server : Client "<<Name<<" deleted."<<endl;
}
};
void Server::personalmsg(char Name[30],msg M)
{
if (DupClient(Name))
{
Sdate C;
C = (ClientList.GetCur());
trclient Cl;
Cl = C.GetCl();
Cl.getmsg(M);
C.SetNReq(C.GetNReq() + 1);
C.SetCl(Cl);
cout<<"Server: message to client "<<Name<<" sent"<<endl;
if (inboard(Cl.GetX(0),Cl.GetY(0))&&
inboard(Cl.GetX(1),Cl.GetY(1))&&
inboard(Cl.GetX(2),Cl.GetY(2)))
{
add(C);
}
}
};
void Server::broadcastmsg(msg M)
{
Sdate ND;
trclient Cl;
linlist<Sdate> NS;
while (!ClientList.Empty())
{
ClientList.tobegin();
ND = ClientList.GetCur();
Cl = ND.GetCl();
Cl.getmsg(M);
ND.SetNReq(ND.GetNReq() + 1);
ND.SetCl(Cl);
NS.add(ND);
};
ClientList.Set(NS);
};
void Server::correctlist()
{
char S[30];
Sdate ND;
trclient Cl;
linlist<Sdate> NS;
while (!ClientList.Empty())
{
ClientList.tobegin();
ND = ClientList.GetCur();
Cl = ND.GetCl();
if ( inboard(Cl.GetX(0),Cl.GetY(0))&&
inboard(Cl.GetX(1),Cl.GetY(1))&&
inboard(Cl.GetX(2),Cl.GetY(2)))
{
ND.SetCl(Cl);
NS.add(ND);
}
};
ClientList.Set(NS);
};
void Server::SetBoard(double X0, double Y0, double R0)
{
cPoint P0(X0,Y0);
Set(P0,R0);
correctlist();
}
void Server::requestcheck()
{
char S[30];
Sdate ND;
trclient Cl;
linlist<Sdate> NS;
while (!ClientList.Empty())
{
ClientList.tobegin();
ND = ClientList.GetCur();
Cl = ND.GetCl();
while (Cl.sendrequest())
{
Cl.GetName(S);
cout<<"Client "<<S<<" message delivered"<<endl;
ND.SetNReq(ND.GetNReq()-1);
};
ND.SetCl(Cl);
NS.add(ND);
};
ClientList.Set(NS);
};
void Server::workaccount()
{
cout<<endl<<"Server working account:"<<endl;
if (!ClientList.Empty())
{
ClientList.tobegin();
char S[30];
int UnDel,UnRep;
while (!ClientList.TheEnd())
{
((ClientList.GetDate()).GetCl()).GetName(S);
UnRep = ((ClientList.GetDate()).GetCl()).getreq();
UnDel = (ClientList.GetDate()).GetNReq();
if (UnRep != 0)
cout <<"Client "<<S<<" : "<<UnRep<<" messages are without request"<<endl;
if (UnDel != 0)
cout <<"Client "<<S<<" : "<<UnDel<<" messages are not yet delivered"<<endl;
if (UnDel > UnRep)
cout <<"Client "<<S<<" : "<<(UnDel-UnRep)<<" messages are lost!"<<endl;
if ((UnDel == 0)&&(UnRep == 0))
cout <<"Client "<<S<<" : "<<"all is OK"<<endl;
ClientList.move();
}
((ClientList.GetDate()).GetCl()).GetName(S);
UnRep = ((ClientList.GetDate()).GetCl()).getreq();
UnDel = (ClientList.GetDate()).GetNReq();
if (UnRep != 0)
cout <<"Client "<<S<<" : "<<UnRep<<" messages are without request"<<endl;
if (UnDel != 0)
cout <<"Client "<<S<<" : "<<UnDel<<" messages are not yet delivered"<<endl;
if (UnDel > UnRep)
cout <<"Client "<<S<<" : "<<(UnDel-UnRep)<<" messages are lost!"<<endl;
if ((UnDel == 0)&&(UnRep == 0))
cout <<"Client "<<S<<" : "<<"all is OK"<<endl;
};
getch();
cout<<endl;
}
void Server::SystemInfo()
{
cout<<"System information:"<<endl;
cout<<"Server: (("<<GetX()<<";"<<GetY()<<");"<<GetRad()<<")"<<endl;
cout<<"Clients:"<<endl;
if (ClientList.Empty())
{
cout<<"No registered clients"<<endl;
}
else
{
ClientList.tobegin();
while (!ClientList.TheEnd())
{
ClientList.GetDate().GetCl().out();
ClientList.move();
};
ClientList.GetDate().GetCl().out();
};
getch();
cout<<endl;
}
Server::~Server()
{}
#endif
