#include "ChatServer.h"
#include "MainWindow.h"
#include "ChatClient.h"

ChatServer::ChatServer(QObject *parent) :
	QTcpServer(parent)
{
}

void ChatServer::incomingConnection(int socketId)
{
	MainWindow* mainWindow = dynamic_cast<MainWindow*>(parent()->parent());
	Q_ASSERT(mainWindow);

	ChatClient* socket = new ChatClient(mainWindow, this);
	if ( ! socket->setSocketDescriptor(socketId) )
		return qDebug("Server: incomming connection REJECTED");

	qDebug("Server: incomming connection accepted");
	mainWindow->connectionEstablished();

	// This chat is only for 2 peers, after connection is established, stop listening
	close();
}

void ChatServer::incomingConnection(qintptr socketId)
{
	return incomingConnection((int)socketId);
}
