#include <iostream>
#include <cstdlib>
#include "../Ganef/client/clientframework.h"
class TestClientFramework : public ClientFramework
{
private:
bool is_running;
std::string server_hostname;
std::string username;
std::string client_version;
public:
TestClientFramework(
const std::string & server_hostname,
const std::string & username,
const std::string & client_version
)
: ClientFramework(int(0)),
server_hostname(server_hostname),
username(username),
client_version(client_version)
{
std::cout << "TestClientFramework.Constructor(server:" << server_hostname
<< ",user: " << username
<< ",version: " << client_version << ")\n";
}
~TestClientFramework()
{
};
protected:
virtual void writeHandshakeInitMessage( ClientPacket * fp )
{
std::cout << "Writing our username: " << username << " to the initial Handshake\n";
fp->writeString(username);
}
virtual void onHandshakingDiscontinue( ClientPacket * fp )
{
std::cout << "The server did not want to let us continue..\n";
std::cout << "[Reason]" << fp->readString() << std::endl;
}
virtual bool onHandshakeDescription( ClientPacket * fp, ClientPacket * reply )
{
std::cout << "The server accepted our username, now lets look at it's version" << std::endl;
std::string server_version = fp->readString();
if(server_version == client_version)
{
return true;
}
std::cout << "Version mismatch: ";
std::cout << "(server: "<<server_version<<", client:"<<client_version<<")";
std::cout << ", abort connection" << std::endl;
return false;
}
virtual void onDisconnect( )
{
std::cout << "Disconnected (With state: "<<getState()<<")\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();
psleep(50);
}
}
};
int main(int argc, char *argv[])
{
std::string server_host("localhost");
std::string username("testuser");
std::string client_version("V.1");
if(argc > 1)
server_host = argv[1];
if(argc > 2)
username = argv[2];
if(argc > 3)
client_version = argv[3];
TestClientFramework app(server_host, username, client_version);
app.mainLoop();
}
#include <iostream>
#include <cstdlib>
#include <vector>
#include "../Ganef/server/serverframework.h"
class TestServerFramework : public ServerFramework
{
public:
TestServerFramework() : ServerFramework(8000)
{
}
~TestServerFramework()
{
}
protected:
virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply)
{
std::string client_username = handshake->readString();
if(client_username == "superclient")
{
reply->writeString("Version.42");
return true;
}
else
{
std::cout << "Invalid username: " << client_username << std::endl;
reply->writeString("Invalid username, sorry");
return false;
}
}
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)
{
return new Client(this, addr, port);
}
public:
void mainLoop()
{
std::cout << "TestServerFramework: Running\n";
while(true)
{
keepAlive();
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;
}