#include "tennisserver.h"
#include "tennis.h"
#include <ql2capserver.h>
#include <qbluetoothsocket.h>
#include <QDebug>
TennisServer::TennisServer(QObject *parent)
: QObject(parent), l2capServer(0), clientSocket(0), stream(0), lagReplyTimeout(0)
{
elapsed.start();
ballElapsed.start();
lagTimer.setInterval(1000);
connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendEcho()));
}
TennisServer::~TennisServer()
{
if (stream){
QByteArray b;
QDataStream s(&b, QIODevice::WriteOnly);
s << QString("D");
clientSocket->write(b);
}
stopServer();
}
void TennisServer::startServer()
{
if (l2capServer)
return;
l2capServer = new QL2capServer(this);
connect(l2capServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
l2capServer->listen();
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);
QBluetoothServiceInfo::Sequence classId;
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Example Tennis Server"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
tr("Example bluetooth tennis server"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF"));
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap))
<< QVariant::fromValue(quint16(l2capServer->serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
protocolDescriptorList);
serviceInfo.registerService();
}
void TennisServer::stopServer()
{
qDebug() <<Q_FUNC_INFO;
serviceInfo.unregisterService();
delete stream;
stream = 0;
delete clientSocket;
clientSocket = 0;
delete l2capServer;
l2capServer = 0;
}
quint16 TennisServer::serverPort() const
{
return l2capServer->serverPort();
}
void TennisServer::moveBall(int x, int y)
{
int msec = ballElapsed.elapsed();
if (stream && msec > 30){
QByteArray b;
QDataStream s(&b, QIODevice::WriteOnly);
s << QString("m %1 %2").arg(x).arg(y);
clientSocket->write(b);
ballElapsed.restart();
}
}
void TennisServer::score(int left, int right)
{
if (stream){
QByteArray b;
QDataStream s(&b, QIODevice::WriteOnly);
s << QString("s %1 %2").arg(left).arg(right);
clientSocket->write(b);
}
}
void TennisServer::moveLeftPaddle(int y)
{
int msec = elapsed.elapsed();
if (stream && msec > 50) {
QByteArray b;
QDataStream s(&b, QIODevice::WriteOnly);
s << QString("l %1").arg(y);
clientSocket->write(b);
elapsed.restart();
}
}
void TennisServer::readSocket()
{
if (!clientSocket)
return;
while (clientSocket->bytesAvailable()) {
QString str;
*stream >> str;
QStringList args = str.split(QChar(' '));
QString s = args.takeFirst();
if (s == "r" && args.count() == 1){
emit moveRightPaddle(args.at(0).toInt());
}
else if (s == "e" && args.count() == 1){
lagReplyTimeout = 0;
QTime then = QTime::fromString(args.at(0), "hh:mm:ss.zzz");
if (then.isValid()) {
emit lag(then.msecsTo(QTime::currentTime()));
}
}
else if (s == "E"){
QByteArray b;
QDataStream st(&b, QIODevice::WriteOnly);
st << str;
clientSocket->write(b);
}
else if (s == "D"){
qDebug() << Q_FUNC_INFO << "closing!";
clientSocket->deleteLater();
clientSocket = 0;
}
else {
qDebug() << Q_FUNC_INFO << "Unknown command" << str;
}
}
}
void TennisServer::clientConnected()
{
qDebug() << Q_FUNC_INFO << "connect";
QBluetoothSocket *socket = l2capServer->nextPendingConnection();
if (!socket)
return;
if (clientSocket){
qDebug() << Q_FUNC_INFO << "Closing socket!";
delete socket;
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socketError(QBluetoothSocket::SocketError)));
stream = new QDataStream(socket);
clientSocket = socket;
qDebug() << Q_FUNC_INFO << "started";
emit clientConnected(clientSocket->peerName());
lagTimer.start();
}
void TennisServer::socketError(QBluetoothSocket::SocketError err)
{
qDebug() << Q_FUNC_INFO << err;
}
void TennisServer::sendEcho()
{
if (lagReplyTimeout) {
lagReplyTimeout--;
return;
}
if (stream) {
QByteArray b;
QDataStream s(&b, QIODevice::WriteOnly);
s << QString("e %1").arg(QTime::currentTime().toString("hh:mm:ss.zzz"));
clientSocket->write(b);
lagReplyTimeout = 10;
}
}
void TennisServer::clientDisconnected()
{
qDebug() << Q_FUNC_INFO << "client closing!";
lagTimer.stop();
lagReplyTimeout = 0;
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
if (!socket)
return;
emit clientDisconnected(socket->peerName());
clientSocket->deleteLater();
clientSocket = 0;
delete stream;
stream = 0;
}