knotes

knotesnetrecv.cpp

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
00005                2004, 2006, Michael Brade <brade@kde.org>
00006 
00007  This program is free software; you can redistribute it and/or
00008  modify it under the terms of the GNU General Public License
00009  as published by the Free Software Foundation; either version 2
00010  of the License, or (at your option) any later version.
00011 
00012  This program is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with this program; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021  In addition, as a special exception, the copyright holders give
00022  permission to link the code of this program with any edition of
00023  the Qt library by Trolltech AS, Norway (or with modified versions
00024  of Qt that use the same license as Qt), and distribute linked
00025  combinations including the two.  You must obey the GNU General
00026  Public License in all respects for all of the code used other than
00027  Qt.  If you modify this file, you may extend this exception to
00028  your version of the file, but you are not obligated to do so.  If
00029  you do not wish to do so, delete this exception statement from
00030  your version.
00031 *******************************************************************/
00032 
00033 #include <qtimer.h>
00034 #include <qdatetime.h>
00035 #include <qregexp.h>
00036 #include <qcstring.h>
00037 
00038 #include <kdebug.h>
00039 #include <kbufferedsocket.h>
00040 #include <ksocketaddress.h>
00041 #include <klocale.h>
00042 #include <kglobal.h>
00043 
00044 #include "knotesnetrecv.h"
00045 
00046 // Maximum note size in chars we are going to accept,
00047 // to prevent "note floods".
00048 #define MAXBUFFER 4096
00049 
00050 // Maximum time we are going to wait between data receptions,
00051 // to prevent memory and connection floods. In milliseconds.
00052 #define MAXTIME 10000
00053 
00054 // Small buffer's size
00055 #define SBSIZE 512
00056 
00057 using namespace KNetwork;
00058 
00059 
00060 KNotesNetworkReceiver::KNotesNetworkReceiver( KBufferedSocket *s )
00061   : QObject(),
00062     m_buffer( new QByteArray() ), m_sock( s )
00063 {
00064     QString date = KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(), true, false );
00065 
00066     // Add the remote IP or hostname and the date to the title, to help the
00067     // user guess who wrote it.
00068     m_titleAddon = QString(" [%1, %2]")
00069                    .arg( m_sock->peerAddress().nodeName() )
00070                    .arg( date );
00071 
00072     // Setup the communications
00073     connect( m_sock, SIGNAL(readyRead()), SLOT(slotDataAvailable()) );
00074     connect( m_sock, SIGNAL(closed()), SLOT(slotConnectionClosed()) );
00075     connect( m_sock, SIGNAL(gotError( int )), SLOT(slotError( int )) );
00076 
00077     m_sock->enableRead( true );
00078 
00079     // Setup the timer
00080     m_timer = new QTimer( this );
00081     connect( m_timer, SIGNAL(timeout()), SLOT(slotReceptionTimeout()) );
00082     m_timer->start( MAXTIME, true );
00083 }
00084 
00085 KNotesNetworkReceiver::~KNotesNetworkReceiver()
00086 {
00087     delete m_buffer;
00088     delete m_sock;
00089 }
00090 
00091 void KNotesNetworkReceiver::slotDataAvailable()
00092 {
00093     char smallBuffer[SBSIZE];
00094     int smallBufferLen;
00095 
00096     do
00097     {
00098         // Append to "big buffer" only if we have some space left.
00099         int curLen = m_buffer->count();
00100 
00101         smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE );
00102 
00103         // Limit max transfer over buffer, to avoid overflow.
00104         smallBufferLen = kMin( smallBufferLen, MAXBUFFER - curLen );
00105         
00106         if ( smallBufferLen > 0 )
00107         {
00108             m_buffer->resize( curLen + smallBufferLen );
00109             memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
00110         }
00111     }
00112     while ( smallBufferLen == SBSIZE );
00113 
00114     // If we are overflowing, close connection.
00115     if ( m_buffer->count() == MAXBUFFER )
00116         m_sock->close();
00117     else
00118         m_timer->changeInterval( MAXTIME );
00119 }
00120 
00121 void KNotesNetworkReceiver::slotReceptionTimeout()
00122 {
00123     m_sock->close();
00124 }
00125 
00126 void KNotesNetworkReceiver::slotConnectionClosed()
00127 {
00128     if ( m_timer->isActive() )
00129     {
00130         QString noteText = QString( *m_buffer ).stripWhiteSpace();
00131 
00132         // First line is the note title or, in case of ATnotes, the id
00133         int pos = noteText.find( QRegExp("[\r\n]") );
00134         QString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon;
00135 
00136         noteText = noteText.mid( pos ).stripWhiteSpace();
00137 
00138         if ( !noteText.isEmpty() )
00139             emit sigNoteReceived( noteTitle, noteText );
00140     }
00141 
00142     deleteLater();
00143 }
00144 
00145 void KNotesNetworkReceiver::slotError( int err )
00146 {
00147     kdWarning() << k_funcinfo
00148                 << m_sock->errorString( static_cast<KSocketBase::SocketError>(err) ) 
00149                 << endl;
00150 }
00151 
00152 #include "knotesnetrecv.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys