summaryrefslogtreecommitdiffstats
path: root/ksysguard/gui/ksgrd/SensorShellAgent.cc
blob: 8081aa49dcadfcb3e5db24296aa413ab98e64e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
    KSysGuard, the KDE System Guard
   
    Copyright (c) 1999 - 2001 Chris Schlaeger <[email protected]>
    
    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 <stdlib.h>

#include <kdebug.h>
#include <kpassdlg.h> 
#include <kprocess.h>

#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 TQString &host, const TQString &shell,
                              const TQString &command, int )
{
  mRetryCount = 3;
  mDaemon = new KProcess;
  mDaemon->setUseShell(true);
  setHostName( host );
  mShell = shell;
  mCommand = command;

  connect( mDaemon, TQT_SIGNAL( processExited( KProcess* ) ),
           TQT_SLOT( daemonExited( KProcess* ) ) );
  connect( mDaemon, TQT_SIGNAL( receivedStdout( KProcess*, char*, int ) ),
           TQT_SLOT( msgRcvd( KProcess*, char*, int ) ) );
  connect( mDaemon, TQT_SIGNAL( receivedStderr( KProcess*, char*, int ) ),
           TQT_SLOT( errMsgRcvd( KProcess*, char*, int ) ) );
  connect( mDaemon, TQT_SIGNAL( wroteStdin( KProcess* ) ),
           TQT_SLOT( msgSent( KProcess* ) ) );

  TQString 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( TQString &shell, TQString &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
  TQString aux = TQString::fromLocal8Bit( buffer, buflen );

  processAnswer( aux );
}

void SensorShellAgent::errMsgRcvd( KProcess*, char *buffer, int buflen )
{
  if ( !buffer || buflen == 0 )
    return;

  TQString buf = TQString::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"