File indexing completed on 2024-05-05 05:34:26

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 
0043     setHostName(host);
0044     mPort = port;
0045 
0046     mSocket.connectToHost(hostName(), mPort);
0047 
0048     return true;
0049 }
0050 
0051 void SensorSocketAgent::hostInfo(QString &shell, QString &command, int &port) const
0052 {
0053     shell.clear();
0054     command.clear();
0055     port = mPort;
0056 }
0057 
0058 void SensorSocketAgent::msgSent()
0059 {
0060     if (mSocket.bytesToWrite() != 0) {
0061         return;
0062     }
0063 
0064     // Try to send next request if available.
0065     executeCommand();
0066 }
0067 
0068 void SensorSocketAgent::msgRcvd()
0069 {
0070     int buflen = mSocket.bytesAvailable();
0071     char *buffer = new char[buflen];
0072 
0073     mSocket.read(buffer, buflen);
0074 
0075     processAnswer(buffer, buflen);
0076     delete[] buffer;
0077 }
0078 
0079 void SensorSocketAgent::connectionClosed()
0080 {
0081     setDaemonOnLine(false);
0082     if (sensorManager()) {
0083         sensorManager()->disengage(this); // delete ourselves
0084     }
0085 }
0086 
0087 void SensorSocketAgent::error(QAbstractSocket::SocketError id)
0088 {
0089     switch (id) {
0090     case QAbstractSocket::ConnectionRefusedError:
0091         SensorMgr->notify(i18n("Connection to %1 refused", hostName()));
0092         break;
0093     case QAbstractSocket::HostNotFoundError:
0094         SensorMgr->notify(i18n("Host %1 not found", hostName()));
0095         break;
0096     case QAbstractSocket::NetworkError:
0097         SensorMgr->notify(i18n("An error occurred with the network (e.g. the network cable was accidentally unplugged) for host %1.", hostName()));
0098         break;
0099     default:
0100         SensorMgr->notify(i18n("Error for host %1: %2", hostName(), mSocket.errorString()));
0101     }
0102 
0103     setDaemonOnLine(false);
0104     if (sensorManager()) {
0105         sensorManager()->disengage(this);
0106     }
0107 }
0108 
0109 bool SensorSocketAgent::writeMsg(const char *msg, int len)
0110 {
0111     int writtenLength = mSocket.write(msg, len);
0112     mSocket.flush();
0113     return writtenLength == len;
0114 }