2015-04-16 11:50:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010, 2014 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This 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, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rpcserver.h"
|
|
|
|
|
|
|
|
#include "rpcconn.h"
|
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
#include <QThread>
|
2015-04-16 11:50:07 -05:00
|
|
|
|
2019-07-02 07:45:26 -05:00
|
|
|
RpcServer::RpcServer(bool perConnLogs)
|
2015-04-16 11:50:07 -05:00
|
|
|
{
|
|
|
|
service = NULL;
|
|
|
|
|
2019-07-02 07:45:26 -05:00
|
|
|
if (perConnLogs)
|
|
|
|
qInstallMessageHandler(RpcConnection::connIdMsgHandler);
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
RpcServer::~RpcServer()
|
|
|
|
{
|
2020-12-27 01:22:47 -06:00
|
|
|
close();
|
|
|
|
emit closed();
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
2015-11-06 07:27:07 -06:00
|
|
|
bool RpcServer::registerService(::google::protobuf::Service *service,
|
|
|
|
QHostAddress address, quint16 tcpPortNum)
|
2015-04-16 11:50:07 -05:00
|
|
|
{
|
|
|
|
this->service = service;
|
|
|
|
|
2015-11-06 07:27:07 -06:00
|
|
|
if (!listen(address, tcpPortNum))
|
2015-04-16 11:50:07 -05:00
|
|
|
{
|
2015-11-06 07:27:07 -06:00
|
|
|
qDebug("Unable to start the server on <%s>: %s",
|
|
|
|
qPrintable(address.toString()),
|
2018-03-14 11:28:56 -05:00
|
|
|
qPrintable(errorString()));
|
2015-04-16 11:50:07 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("The server is running on %s: %d",
|
2018-03-14 11:28:56 -05:00
|
|
|
qPrintable(serverAddress().toString()),
|
2015-04-16 11:50:07 -05:00
|
|
|
serverPort());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:30:01 -05:00
|
|
|
void RpcServer::incomingConnection(qintptr socketDescriptor)
|
2015-04-16 11:50:07 -05:00
|
|
|
{
|
2019-07-28 00:08:32 -05:00
|
|
|
QThread *thread = new QThread;
|
2015-04-16 11:50:07 -05:00
|
|
|
RpcConnection *conn = new RpcConnection(socketDescriptor, service);
|
|
|
|
|
2020-05-15 08:25:03 -05:00
|
|
|
thread->setObjectName("RPC");
|
2015-04-16 11:50:07 -05:00
|
|
|
conn->moveToThread(thread);
|
|
|
|
|
|
|
|
connect(thread, SIGNAL(started()), conn, SLOT(start()));
|
|
|
|
|
|
|
|
// NOTE: conn "self-destructs" after emitting closed
|
|
|
|
// use 'closed' to stop execution of the thread
|
|
|
|
connect(conn, SIGNAL(closed()), thread, SLOT(quit()));
|
|
|
|
|
|
|
|
// setup thread to "self-destruct" when it is done
|
|
|
|
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
|
|
|
|
|
2015-04-28 08:15:35 -05:00
|
|
|
connect(this, SIGNAL(notifyClients(int, SharedProtobufMessage)),
|
|
|
|
conn, SLOT(sendNotification(int, SharedProtobufMessage)));
|
2020-12-27 01:22:47 -06:00
|
|
|
connect(this, SIGNAL(closed()), thread, SLOT(quit()));
|
2015-04-24 10:23:00 -05:00
|
|
|
|
2015-04-16 11:50:07 -05:00
|
|
|
thread->start();
|
|
|
|
}
|