From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- ksysguard/gui/ksgrd/SensorShellAgent.cc | 141 ++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 ksysguard/gui/ksgrd/SensorShellAgent.cc (limited to 'ksysguard/gui/ksgrd/SensorShellAgent.cc') diff --git a/ksysguard/gui/ksgrd/SensorShellAgent.cc b/ksysguard/gui/ksgrd/SensorShellAgent.cc new file mode 100644 index 000000000..0f06e88fe --- /dev/null +++ b/ksysguard/gui/ksgrd/SensorShellAgent.cc @@ -0,0 +1,141 @@ +/* + KSysGuard, the KDE System Guard + + Copyright (c) 1999 - 2001 Chris Schlaeger + + This program is free software; you can redistribute it and/or + modify it under the terms of version 2 of the GNU General Public + License as published by the Free Software Foundation. + + 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +*/ + +#include + +#include +#include +#include + +#include "SensorClient.h" +#include "SensorManager.h" + +#include "SensorShellAgent.h" + +using namespace KSGRD; + +SensorShellAgent::SensorShellAgent( SensorManager *sm ) + : SensorAgent( sm ), mDaemon( 0 ) +{ +} + +SensorShellAgent::~SensorShellAgent() +{ + if ( mDaemon ) { + mDaemon->writeStdin( "quit\n", strlen( "quit\n" ) ); + delete mDaemon; + mDaemon = 0; + } +} + +bool SensorShellAgent::start( const QString &host, const QString &shell, + const QString &command, int ) +{ + mRetryCount = 3; + mDaemon = new KProcess; + mDaemon->setUseShell(true); + setHostName( host ); + mShell = shell; + mCommand = command; + + connect( mDaemon, SIGNAL( processExited( KProcess* ) ), + SLOT( daemonExited( KProcess* ) ) ); + connect( mDaemon, SIGNAL( receivedStdout( KProcess*, char*, int ) ), + SLOT( msgRcvd( KProcess*, char*, int ) ) ); + connect( mDaemon, SIGNAL( receivedStderr( KProcess*, char*, int ) ), + SLOT( errMsgRcvd( KProcess*, char*, int ) ) ); + connect( mDaemon, SIGNAL( wroteStdin( KProcess* ) ), + SLOT( msgSent( KProcess* ) ) ); + + QString cmd; + if ( !command.isEmpty() ) + cmd = command; + else + cmd = mShell + " " + hostName() + " ksysguardd"; + *mDaemon << cmd; + + if ( !mDaemon->start( KProcess::NotifyOnExit, KProcess::All ) ) { + sensorManager()->hostLost( this ); + kdDebug (1215) << "Command '" << cmd << "' failed" << endl; + return false; + } + + return true; +} + +void SensorShellAgent::hostInfo( QString &shell, QString &command, + int &port) const +{ + shell = mShell; + command = mCommand; + port = -1; +} + +void SensorShellAgent::msgSent( KProcess* ) +{ + setTransmitting( false ); + + // Try to send next request if available. + executeCommand(); +} + +void SensorShellAgent::msgRcvd( KProcess*, char *buffer, int buflen ) +{ + if ( !buffer || buflen == 0 ) + return; + mRetryCount = 3; //we recieved an answer, so reset our retry count back to 3 + QString aux = QString::fromLocal8Bit( buffer, buflen ); + + processAnswer( aux ); +} + +void SensorShellAgent::errMsgRcvd( KProcess*, char *buffer, int buflen ) +{ + if ( !buffer || buflen == 0 ) + return; + + QString buf = QString::fromLocal8Bit( buffer, buflen ); + + kdDebug(1215) << "SensorShellAgent: Warning, received text over stderr!" + << endl << buf << endl; +} + +void SensorShellAgent::daemonExited( KProcess *process ) +{ + kdDebug() << "daemonExited" << endl; + if ( mRetryCount-- <= 0 || !mDaemon->start( KProcess::NotifyOnExit, KProcess::All ) ) { + kdDebug() << "daemon could not be restart" << endl; + setDaemonOnLine( false ); + sensorManager()->hostLost( this ); + sensorManager()->requestDisengage( this ); + } +} + +bool SensorShellAgent::writeMsg( const char *msg, int len ) +{ + return mDaemon->writeStdin( msg, len ); +} + +bool SensorShellAgent::txReady() +{ + return !transmitting(); +} + +#include "SensorShellAgent.moc" -- cgit v1.2.1