#include <iostream>
#include <cstdlib>
#include "../Ganef/client/clientframework.h"
#include "../Ganef/client/clientdata.h"
class MyClientData : public ClientData
{
public:
MyClientData(ClientFramework * f, ClientPacket * p) : ClientData(f,p)
{
std::cout << "Construct: object " << getFrameworkId() << std::endl;
}
~MyClientData()
{
std::cout << "Destruct: object " << getFrameworkId() << std::endl;
}
virtual void updatePacket(ClientFramework * f, unsigned char updatetype, ClientPacket * p)
{
}
static ClientData * constructMyClientData(ClientFramework * f, ClientPacket * p)
{
return new MyClientData(f,p);
}
};
class TestClientFramework : public ClientFramework
{
private:
bool is_running;
std::string server_hostname;
public:
TestClientFramework(const std::string & server_hostname) : ClientFramework(int(0)),server_hostname(server_hostname)
{
registerConstructor(0x424242, MyClientData::constructMyClientData);
}
~TestClientFramework()
{
};
protected:
virtual void writeHandshakeInitMessage( ClientPacket * fp ){}
virtual void onHandshakingDiscontinue( ClientPacket * fp ){}
virtual bool onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
{
return true;
}
virtual void onDisconnect( ) { std::cout << "Disconnected\n"; }
virtual void onConnect( ){ std::cout << "Connected\n"; }
public:
void mainLoop( )
{
std::cout << "Connecting to: " << server_hostname <<":"<< 8000<< std::endl;
initServerConnection(server_hostname, 8000);
is_running = true;
while(is_running)
{
keepAlive();
std::cout << "We have " << getClientDataObjects().size() << " objects on the client" << std::endl;
psleep(50);
}
}
};
int main(int argc, char *argv[])
{
std::string server_host("localhost");
if(argc > 1)
server_host = argv[1];
TestClientFramework app(server_host);
app.mainLoop();
}
#include <iostream>
#include <cstdlib>
#include <vector>
#include "../Ganef/server/serverframework.h"
class MyServerData : public ServerData
{
private:
std::string name;
public:
MyServerData(ServerFramework * fw, const std::string & name) : ServerData(fw, 10, false),name(name)
{
};
public:
virtual const unsigned int getClassId() const { return 0x424242; };
virtual void fillUpdatePacket(ServerPacket * packet, unsigned char type) const
{
}
virtual void fillCreateObjectPacket(ServerPacket * packet) const
{
}
virtual void clientPacket(Client * client, unsigned char type, ServerPacket * p)
{
}
};
class TestServerFramework : public ServerFramework
{
public:
TestServerFramework() : ServerFramework(8000)
{
}
~TestServerFramework()
{
}
protected:
virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply)
{
return true;
}
virtual void onClientConnected(Client * client)
{
std::cout << "Client: " << client->getId() << " connected\n";
}
virtual void onClientDisconnected(Client * client)
{
std::cout << "Client: " << client->getId() << " disconnected\n";
}
virtual Client * createClient(const ipaddress &addr, int port)
{
Client * client = new Client(this, addr, port);
std::map<unsigned int, ServerData *>::const_iterator it;
for(it = getServerDataObjects().begin(); it != getServerDataObjects().end(); ++it)
{
ServerData * s_data = it->second;
s_data->sendCreateObject(client);
}
return client;
}
public:
void mainLoop()
{
std::cout << "TestServerFramework: Running\n";
int counter = 0;
std::vector<MyServerData *> data;
data.clear();
while(true)
{
keepAlive();
if(counter % 5 == 0)
{
MyServerData * d = new MyServerData(this, "SomeData");
data.push_back(d);
d->sendCreateObjectToAll();
}
counter++;
if(counter % 27 == 0)
{
std::cout << "Destroy all objects" << std::endl;
for(std::vector<MyServerData *>::iterator it = data.begin(); it != data.end(); ++it)
{
(*it)->sendDestroyToAll();
delete *it;
}
data.clear();
}
psleep(100);
}
}
};
int main(int argc, char *argv[])
{
try
{
TestServerFramework framework;
framework.start();
framework.mainLoop();
}
catch(FrameworkError *err)
{
std::cout << "FrameworkError:" << err->msg << std::endl;
}
catch(PacketError *err)
{
std::cout << "PacketError:" << err->msg << std::endl;
}
return EXIT_SUCCESS;
}