Clang: Make ConnectionServer adaptable

The server and client are adaptable by template parameter.

Change-Id: Ia28e2863ca73b633689964ea1df6df08f8358ac1
Reviewed-by: Tim Jenssen <tim.jenssen@theqtcompany.com>
This commit is contained in:
Marco Bubke
2016-07-04 16:07:45 +02:00
committed by Tim Jenssen
parent ae9962c60c
commit 2c698dff86
5 changed files with 126 additions and 157 deletions

View File

@@ -25,124 +25,9 @@
#include "connectionserver.h" #include "connectionserver.h"
#include <clangcodemodelserverinterface.h>
#include <QCoreApplication>
#include <QLocalSocket>
#include <QTimer>
#include <cstdlib>
namespace ClangBackEnd { namespace ClangBackEnd {
QString ConnectionServer::connectionName; QString ConnectionName::connectionName;
ConnectionServer::ConnectionServer(const QString &connectionName)
: aliveTimerId(startTimer(5000))
{
this->connectionName = connectionName;
localServer.setMaxPendingConnections(1);
connect(&localServer, &QLocalServer::newConnection, this, &ConnectionServer::handleNewConnection);
std::atexit(&ConnectionServer::removeServer);
#if defined(_GLIBCXX_HAVE_AT_QUICK_EXIT)
std::at_quick_exit(&ConnectionServer::removeServer);
#endif
std::set_terminate(&ConnectionServer::removeServer);
}
ConnectionServer::~ConnectionServer()
{
killTimer(aliveTimerId);
removeServer();
}
void ConnectionServer::start()
{
QLocalServer::removeServer(connectionName);
localServer.listen(connectionName);
}
void ConnectionServer::setClangCodeModelServer(ClangCodeModelServerInterface *ipcServer)
{
this->ipcServer = ipcServer;
}
int ConnectionServer::clientProxyCount() const
{
return static_cast<int>(ipcServerProxies.size());
}
void ConnectionServer::timerEvent(QTimerEvent *timerEvent)
{
if (aliveTimerId == timerEvent->timerId())
sendAliveMessage();
}
void ConnectionServer::handleNewConnection()
{
QLocalSocket *localSocket(nextPendingConnection());
ipcServerProxies.emplace_back(ipcServer, localSocket);
ipcServer->setClient(&ipcServerProxies.back());
localSockets.push_back(localSocket);
emit newConnection();
}
void ConnectionServer::sendAliveMessage()
{
ipcServer->client()->alive();
}
void ConnectionServer::handleSocketDisconnect()
{
QLocalSocket *senderLocalSocket = static_cast<QLocalSocket*>(sender());
removeClientProxyWithLocalSocket(senderLocalSocket);
localSockets.erase(std::remove_if(localSockets.begin(),
localSockets.end(),
[senderLocalSocket](QLocalSocket *localSocketInList) { return localSocketInList == senderLocalSocket;}));
delayedExitApplicationIfNoSockedIsConnected();
}
void ConnectionServer::removeClientProxyWithLocalSocket(QLocalSocket *localSocket)
{
ipcServerProxies.erase(std::remove_if(ipcServerProxies.begin(),
ipcServerProxies.end(),
[localSocket](const ClangCodeModelClientProxy &client) { return client.isUsingThatIoDevice(localSocket);}));
}
QLocalSocket *ConnectionServer::nextPendingConnection()
{
QLocalSocket *localSocket = localServer.nextPendingConnection();
connect(localSocket, &QLocalSocket::disconnected, this, &ConnectionServer::handleSocketDisconnect);
return localSocket;
}
void ConnectionServer::removeServer()
{
QLocalServer::removeServer(connectionName);
}
void ConnectionServer::delayedExitApplicationIfNoSockedIsConnected()
{
if (localSockets.size() == 0)
QTimer::singleShot(60000, this, &ConnectionServer::exitApplicationIfNoSockedIsConnected);
}
void ConnectionServer::exitApplicationIfNoSockedIsConnected()
{
if (localSockets.size() == 0)
QCoreApplication::exit();
}
} // namespace ClangBackEnd } // namespace ClangBackEnd

View File

@@ -25,10 +25,14 @@
#pragma once #pragma once
#include <clangcodemodelclientproxy.h> #include "clangbackendipc_global.h"
#include <QCoreApplication>
#include <QLocalServer> #include <QLocalServer>
#include <QLocalSocket>
#include <QTimer>
#include <memory>
#include <vector> #include <vector>
namespace ClangBackEnd { namespace ClangBackEnd {
@@ -36,42 +40,112 @@ namespace ClangBackEnd {
class ClangCodeModelServerInterface; class ClangCodeModelServerInterface;
class ClangCodeModelClientProxy; class ClangCodeModelClientProxy;
class CMBIPC_EXPORT ConnectionServer : public QObject struct CMBIPC_EXPORT ConnectionName {
{
Q_OBJECT
public:
ConnectionServer(const QString &connectionName);
~ConnectionServer();
void start();
void setClangCodeModelServer(ClangCodeModelServerInterface *ipcServer);
int clientProxyCount() const;
static void removeServer();
signals:
void newConnection();
protected:
void timerEvent(QTimerEvent *timerEvent);
private:
void handleNewConnection();
void sendAliveMessage();
void handleSocketDisconnect();
void removeClientProxyWithLocalSocket(QLocalSocket *localSocket);
QLocalSocket *nextPendingConnection();
void delayedExitApplicationIfNoSockedIsConnected();
void exitApplicationIfNoSockedIsConnected();
private:
std::vector<ClangCodeModelClientProxy> ipcServerProxies;
std::vector<QLocalSocket*> localSockets;
ClangCodeModelServerInterface *ipcServer;
QLocalServer localServer;
static QString connectionName; static QString connectionName;
int aliveTimerId; };
template <typename ServerInterface,
typename ClientProxy>
class ConnectionServer
{
public:
ConnectionServer(const QString &connectionName)
{
ConnectionName::connectionName = connectionName;
aliveTimer.start(5000);
localServer.setMaxPendingConnections(1);
QObject::connect(&localServer,
&QLocalServer::newConnection,
[&] { handleNewConnection(); });
QObject::connect(&aliveTimer,
&QTimer::timeout,
[&] { sendAliveMessage(); });
std::atexit(&ConnectionServer::removeServer);
#if defined(_GLIBCXX_HAVE_AT_QUICK_EXIT)
std::at_quick_exit(&ConnectionServer::removeServer);
#endif
std::set_terminate(&ConnectionServer::removeServer);
}
~ConnectionServer()
{
removeServer();
}
void start()
{
QLocalServer::removeServer(ConnectionName::connectionName);
localServer.listen(ConnectionName::connectionName);
}
void setClangCodeModelServer(ServerInterface *ipcServer)
{
this->ipcServer = ipcServer;
}
static void removeServer()
{
QLocalServer::removeServer(ConnectionName::connectionName);
}
private:
void handleNewConnection()
{
localSocket = nextPendingConnection();
ipcClientProxy.reset(new ClientProxy(ipcServer, localSocket));
ipcServer->setClient(ipcClientProxy.get());
}
void sendAliveMessage()
{
ipcClientProxy->alive();
}
void handleSocketDisconnect()
{
ipcClientProxy.reset();
localSocket = nullptr;
delayedExitApplicationIfNoSockedIsConnected();
}
QLocalSocket *nextPendingConnection()
{
QLocalSocket *localSocket = localServer.nextPendingConnection();
QObject::connect(localSocket,
&QLocalSocket::disconnected,
[&] { handleSocketDisconnect(); });
return localSocket;
}
void delayedExitApplicationIfNoSockedIsConnected()
{
if (localSocket == nullptr)
QTimer::singleShot(60000, [&] { exitApplicationIfNoSockedIsConnected(); });
}
void exitApplicationIfNoSockedIsConnected()
{
if (localSocket == nullptr)
QCoreApplication::exit();
}
private:
std::unique_ptr<ClientProxy> ipcClientProxy;
QLocalSocket* localSocket;
ServerInterface *ipcServer;
QLocalServer localServer;
QTimer aliveTimer;
}; };
} // namespace ClangBackEnd } // namespace ClangBackEnd

View File

@@ -29,6 +29,11 @@
#include <connectionserver.h> #include <connectionserver.h>
#include <clangcodemodelserver.h> #include <clangcodemodelserver.h>
#include <clangcodemodelclientproxy.h>
using ClangBackEnd::ClangCodeModelClientProxy;
using ClangBackEnd::ClangCodeModelServer;
using ClangBackEnd::ConnectionServer;
QString processArguments(QCoreApplication &application) QString processArguments(QCoreApplication &application)
{ {
@@ -62,8 +67,8 @@ int main(int argc, char *argv[])
clang_toggleCrashRecovery(true); clang_toggleCrashRecovery(true);
clang_enableStackTraces(); clang_enableStackTraces();
ClangBackEnd::ClangCodeModelServer clangCodeModelServer; ClangCodeModelServer clangCodeModelServer;
ClangBackEnd::ConnectionServer connectionServer(connection); ConnectionServer<ClangCodeModelServer, ClangCodeModelClientProxy> connectionServer(connection);
connectionServer.start(); connectionServer.start();
connectionServer.setClangCodeModelServer(&clangCodeModelServer); connectionServer.setClangCodeModelServer(&clangCodeModelServer);

View File

@@ -54,7 +54,7 @@ void EchoClangCodeModelServer::dispatch(const MessageEnvelop &message)
void EchoClangCodeModelServer::end() void EchoClangCodeModelServer::end()
{ {
ConnectionServer::removeServer(); ConnectionServer<EchoClangCodeModelServer, ClangCodeModelClientProxy>::removeServer();
QCoreApplication::quit(); QCoreApplication::quit();
} }

View File

@@ -25,10 +25,15 @@
#include "echoclangcodemodelserver.h" #include "echoclangcodemodelserver.h"
#include <clangcodemodelclientproxy.h>
#include <connectionserver.h> #include <connectionserver.h>
#include <QCoreApplication> #include <QCoreApplication>
using ClangBackEnd::ClangCodeModelClientProxy;
using ClangBackEnd::ConnectionServer;
using ClangBackEnd::EchoClangCodeModelServer;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication::setOrganizationName("QtProject"); QCoreApplication::setOrganizationName("QtProject");
@@ -43,8 +48,8 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
ClangBackEnd::EchoClangCodeModelServer echoClangCodeModelServer; EchoClangCodeModelServer echoClangCodeModelServer;
ClangBackEnd::ConnectionServer connectionServer(application.arguments()[1]); ConnectionServer<EchoClangCodeModelServer, ClangCodeModelClientProxy> connectionServer(application.arguments()[1]);
connectionServer.start(); connectionServer.start();
connectionServer.setClangCodeModelServer(&echoClangCodeModelServer); connectionServer.setClangCodeModelServer(&echoClangCodeModelServer);