~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/ksgrd/SensorSocketAgent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
   
 
4
    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
 
5
    
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU General Public
 
8
    License version 2 or at your option version 3 as published by
 
9
    the Free Software Foundation.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 
 
20
*/
 
21
 
 
22
//#include <stdlib.h>
 
23
 
 
24
#include <kdebug.h>
 
25
#include <klocale.h>
 
26
 
 
27
#include "SensorClient.h"
 
28
#include "SensorManager.h"
 
29
 
 
30
#include "SensorSocketAgent.h"
 
31
 
 
32
using namespace KSGRD;
 
33
 
 
34
SensorSocketAgent::SensorSocketAgent( SensorManager *sm )
 
35
  : SensorAgent( sm )
 
36
{
 
37
 
 
38
  connect( &mSocket, SIGNAL( error( QAbstractSocket::SocketError ) ), SLOT( error( QAbstractSocket::SocketError ) ) );
 
39
  connect( &mSocket, SIGNAL( bytesWritten( qint64 ) ), SLOT( msgSent( ) ) );
 
40
  connect( &mSocket, SIGNAL( readyRead() ), SLOT( msgRcvd() ) );
 
41
  connect( &mSocket, SIGNAL( disconnected() ), SLOT( connectionClosed() ) );
 
42
}
 
43
 
 
44
SensorSocketAgent::~SensorSocketAgent()
 
45
{
 
46
  mSocket.write( "quit\n", sizeof( "quit\n" ) );
 
47
  mSocket.flush();
 
48
}
 
49
        
 
50
bool SensorSocketAgent::start( const QString &host, const QString&,
 
51
                               const QString&, int port )
 
52
{
 
53
  if ( port <= 0 )
 
54
    kDebug(1215) << "SensorSocketAgent::start: Invalid port " << port;
 
55
 
 
56
  setHostName( host );
 
57
  mPort = port;
 
58
 
 
59
  mSocket.connectToHost( hostName(), mPort );
 
60
 
 
61
  return true;
 
62
}
 
63
 
 
64
void SensorSocketAgent::hostInfo( QString &shell, QString &command, int &port ) const
 
65
{
 
66
  shell.clear();
 
67
  command.clear();
 
68
  port = mPort;
 
69
}
 
70
 
 
71
void SensorSocketAgent::msgSent( )
 
72
{
 
73
  if ( mSocket.bytesToWrite() != 0 )
 
74
    return;
 
75
 
 
76
  // Try to send next request if available.
 
77
  executeCommand();
 
78
}
 
79
 
 
80
void SensorSocketAgent::msgRcvd()
 
81
{
 
82
  int buflen = mSocket.bytesAvailable();
 
83
  char* buffer = new char[ buflen ];
 
84
 
 
85
  mSocket.read( buffer, buflen );
 
86
 
 
87
  processAnswer( buffer, buflen ); 
 
88
  delete [] buffer;
 
89
}
 
90
 
 
91
void SensorSocketAgent::connectionClosed()
 
92
{
 
93
  setDaemonOnLine( false );
 
94
  if(sensorManager()) {
 
95
    sensorManager()->disengage( this ); //delete ourselves
 
96
  }
 
97
}
 
98
 
 
99
void SensorSocketAgent::error( QAbstractSocket::SocketError id )
 
100
{
 
101
  switch ( id ) {
 
102
    case QAbstractSocket::ConnectionRefusedError:
 
103
      SensorMgr->notify( i18n( "Connection to %1 refused" ,
 
104
                           hostName() ) );
 
105
      break;
 
106
    case QAbstractSocket::HostNotFoundError:
 
107
      SensorMgr->notify( i18n( "Host %1 not found" ,
 
108
                           hostName() ) );
 
109
      break;
 
110
    case QAbstractSocket::NetworkError:
 
111
      SensorMgr->notify( i18n( "An error occurred with the network (e.g. the network cable was accidentally unplugged) for host %1.",
 
112
                           hostName() ) );
 
113
      break;
 
114
    default:
 
115
      SensorMgr->notify( i18n( "Error for host %1: %2",
 
116
                           hostName(), mSocket.errorString() ) );
 
117
  }
 
118
 
 
119
  setDaemonOnLine( false );
 
120
  if(sensorManager())
 
121
    sensorManager()->disengage( this );
 
122
}
 
123
 
 
124
bool SensorSocketAgent::writeMsg( const char *msg, int len )
 
125
{
 
126
  int writtenLength = mSocket.write( msg, len );
 
127
  mSocket.flush();
 
128
  return writtenLength == len;
 
129
}
 
130
 
 
131
#include "SensorSocketAgent.moc"