File indexing completed on 2024-05-05 17:39:48

0001 /*
0002     KSysGuard, the KDE System Guard
0003 
0004     SPDX-FileCopyrightText: 1999-2001 Chris Schlaeger <cs@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 
0008 */
0009 
0010 //#include <stdlib.h>
0011 
0012 #include "ksgrd_debug.h"
0013 #include <klocalizedstring.h>
0014 
0015 #include "SensorClient.h"
0016 #include "SensorManager.h"
0017 
0018 #include "SensorSocketAgent.h"
0019 
0020 using namespace KSGRD;
0021 
0022 SensorSocketAgent::SensorSocketAgent(SensorManager *sm)
0023     : SensorAgent(sm)
0024 {
0025     connect(&mSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(error(QAbstractSocket::SocketError)));
0026     connect(&mSocket, &QIODevice::bytesWritten, this, &SensorSocketAgent::msgSent);
0027     connect(&mSocket, &QIODevice::readyRead, this, &SensorSocketAgent::msgRcvd);
0028     connect(&mSocket, &QAbstractSocket::disconnected, this, &SensorSocketAgent::connectionClosed);
0029 }
0030 
0031 SensorSocketAgent::~SensorSocketAgent()
0032 {
0033     mSocket.write("quit\n", sizeof("quit\n"));
0034     mSocket.flush();
0035 }
0036 
0037 bool SensorSocketAgent::start(const QString &host, const QString &, const QString &, int port)
0038 {
0039     if (port <= 0)
0040         qCDebug(LIBKSYSGUARD_KSGRD) << "SensorSocketAgent::start: Invalid port " << port;
0041 
0042     setHostName(host);
0043     mPort = port;
0044 
0045     mSocket.connectToHost(hostName(), mPort);
0046 
0047     return true;
0048 }
0049 
0050 void SensorSocketAgent::hostInfo(QString &shell, QString &command, int &port) const
0051 {
0052     shell.clear();
0053     command.clear();
0054     port = mPort;
0055 }
0056 
0057 void SensorSocketAgent::msgSent()
0058 {
0059     if (mSocket.bytesToWrite() != 0)
0060         return;
0061 
0062     // Try to send next request if available.
0063     executeCommand();
0064 }
0065 
0066 void SensorSocketAgent::msgRcvd()
0067 {
0068     int buflen = mSocket.bytesAvailable();
0069     char *buffer = new char[buflen];
0070 
0071     mSocket.read(buffer, buflen);
0072 
0073     processAnswer(buffer, buflen);
0074     delete[] buffer;
0075 }
0076 
0077 void SensorSocketAgent::connectionClosed()
0078 {
0079     setDaemonOnLine(false);
0080     if (sensorManager()) {
0081         sensorManager()->disengage(this); // delete ourselves
0082     }
0083 }
0084 
0085 void SensorSocketAgent::error(QAbstractSocket::SocketError id)
0086 {
0087     switch (id) {
0088     case QAbstractSocket::ConnectionRefusedError:
0089         SensorMgr->notify(i18n("Connection to %1 refused", hostName()));
0090         break;
0091     case QAbstractSocket::HostNotFoundError:
0092         SensorMgr->notify(i18n("Host %1 not found", hostName()));
0093         break;
0094     case QAbstractSocket::NetworkError:
0095         SensorMgr->notify(i18n("An error occurred with the network (e.g. the network cable was accidentally unplugged) for host %1.", hostName()));
0096         break;
0097     default:
0098         SensorMgr->notify(i18n("Error for host %1: %2", hostName(), mSocket.errorString()));
0099     }
0100 
0101     setDaemonOnLine(false);
0102     if (sensorManager())
0103         sensorManager()->disengage(this);
0104 }
0105 
0106 bool SensorSocketAgent::writeMsg(const char *msg, int len)
0107 {
0108     int writtenLength = mSocket.write(msg, len);
0109     mSocket.flush();
0110     return writtenLength == len;
0111 }