##// END OF EJS Templates
Sync
Sync

File last commit:

r20:d09469239955 default
r20:d09469239955 default
Show More
spwtcppacketserver.cpp
124 lines | 3.6 KiB | text/x-c | CppLexer
/*------------------------------------------------------------------------------
-- This file is a part of the SocExplorer Software
-- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------*/
/*-- Author : Alexis Jeandet
-- Mail : alexis.jeandet@member.fsf.org
----------------------------------------------------------------------------*/
#include "spwtcppacketserver.h"
#include "ui_spwtcppacketserver.h"
#include <QNetworkInterface>
#include <QByteArray>
SpwTcpPacketServer::SpwTcpPacketServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::SpwTcpPacketServer)
{
ui->setupUi(this);
this->p_bridge = NULL;
this->p_server = new QTcpServer();
connect(this->ui->startServeQpb,SIGNAL(clicked(bool)),SLOT(toggleServer()));
updateHostIP();
this->ui->PortLineEdit->setText("2200");
}
SpwTcpPacketServer::~SpwTcpPacketServer()
{
delete ui;
}
void SpwTcpPacketServer::setBridge(abstractSpwBridge *bridge)
{
if(this->p_bridge!=NULL)
{
disconnect(this,SLOT(pushPacket(char*,int)));
}
this->p_bridge = bridge;
connect(bridge,SIGNAL(pushPacketOverTCP(char*,int)),SLOT(pushPacket(char*,int)));
}
void SpwTcpPacketServer::pushPacket(char *packet, int size)
{
QByteArray data;
char sizech[3];
data.append((char)0);
sizech[0]=size & 0x0FF;
sizech[1]=(size>>8) & 0x0FF;
sizech[2]=(size>>16) & 0x0FF;
data.append(sizech,3);
data.append(packet,size);
for(int i=0;i<connectedClients.count();i++)
{
QTcpSocket* soc=connectedClients.at(i);
if(soc->state()!=QAbstractSocket::ConnectedState)
{
connectedClients.removeAt(i);
delete soc;
}
else
{
connectedClients.at(i)->write(data);
}
}
}
void SpwTcpPacketServer::toggleServer()
{
if(this->p_server->isListening())
{
disconnectServer();
}
else
{
connectServer();
}
}
void SpwTcpPacketServer::connectServer()
{
this->p_server->listen(QHostAddress::Any,this->ui->PortLineEdit->text().toInt());
this->ui->startServeQpb->setText("Stop Server");
}
void SpwTcpPacketServer::disconnectServer()
{
this->ui->startServeQpb->setText("Start Server");
this->p_server->close();
}
void SpwTcpPacketServer::setServerPort(qint32 port)
{
this->ui->PortLineEdit->setText(QString("%1").arg(port));
}
void SpwTcpPacketServer::newConnection()
{
this->connectedClients.append(this->p_server->nextPendingConnection());
}
void SpwTcpPacketServer::updateHostIP()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
this->ui->IPLineEdit->setText(list[nIter].toString());
}
}