knotes Library API Documentation

knotesnetrecv.cpp

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
00005                2004, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 <kextsock.h>
00039 #include <ksockaddr.h>
00040 #include <klocale.h>
00041 #include <kglobal.h>
00042 
00043 #include "knotesnetrecv.h"
00044 
00045 // Maximum note size in chars we are going to accept,
00046 // to prevent "note floods".
00047 #define MAXBUFFER 4096
00048 
00049 // Maximum time we are going to wait between data receptions,
00050 // to prevent memory and connection floods. In milliseconds.
00051 #define MAXTIME 10000
00052 
00053 // Small buffer's size
00054 #define SBSIZE 512
00055 
00056 
00057 KNotesNetworkReceiver::KNotesNetworkReceiver( KExtendedSocket *s )
00058   : QObject(),
00059     m_buffer( new QByteArray() ), m_sock( s )
00060 {
00061     QString date = KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(), true, false );
00062 
00063     // Add the remote IP or hostname and the date to the title, to help the
00064     // user guess who wrote it.
00065     m_titleAddon = QString(" [%1, %2]")
00066                    .arg( m_sock->peerAddress()->nodeName() )
00067                    .arg( date );
00068 
00069     // Setup the timer
00070     m_timer = new QTimer( this );
00071     connect( m_timer, SIGNAL(timeout()), SLOT(slotReceptionTimeout()) );
00072 
00073     // Setup the communications
00074     connect( m_sock, SIGNAL(readyRead()), SLOT(slotDataAvailable()) );
00075     connect( m_sock, SIGNAL(closed( int )), SLOT(slotConnectionClosed( int )) );
00076     m_sock->enableRead( true );
00077 
00078     // Go!
00079     m_timer->start( MAXTIME, true );
00080 }
00081 
00082 KNotesNetworkReceiver::~KNotesNetworkReceiver()
00083 {
00084     delete m_buffer;
00085     delete m_sock;
00086 }
00087 
00088 void KNotesNetworkReceiver::slotDataAvailable()
00089 {
00090     char smallBuffer[SBSIZE];
00091     int smallBufferLen;
00092 
00093     do
00094     {
00095         // Append to "big buffer", only if we have some space left.
00096         int curLen = m_buffer->count();
00097 
00098         smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE );
00099         if ( smallBufferLen > MAXBUFFER - curLen )
00100             smallBufferLen = ( MAXBUFFER - curLen );   // Limit max transfer over buffer, to avoid overflow.
00101         if ( smallBufferLen > 0 )
00102         {
00103             m_buffer->resize( curLen + smallBufferLen );
00104             memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
00105         }
00106     }
00107     while ( smallBufferLen == SBSIZE );
00108 
00109     // If we are overflowing, close connection.
00110     if ( m_buffer->count() == MAXBUFFER )
00111         m_sock->closeNow();
00112     else
00113         m_timer->changeInterval( MAXTIME );
00114 }
00115 
00116 void KNotesNetworkReceiver::slotConnectionClosed( int /*state*/ )
00117 {
00118     if ( m_timer->isActive() )
00119     {
00120         QString noteText = QString( *m_buffer ).stripWhiteSpace();
00121 
00122         // First line is the note title or, in case of ATnotes, the id
00123         int pos = noteText.find( QRegExp("[\r\n]") );
00124         QString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon;
00125 
00126         noteText = noteText.mid( pos ).stripWhiteSpace();
00127 
00128         if ( !noteText.isEmpty() )
00129             emit sigNoteReceived( noteTitle, noteText );
00130     }
00131 
00132     delete this;
00133 }
00134 
00135 void KNotesNetworkReceiver::slotReceptionTimeout()
00136 {
00137     m_sock->closeNow();
00138 }
00139 
00140 #include "knotesnetrecv.moc"
KDE Logo
This file is part of the documentation for knotes Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:40:48 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003