From bd0f3345a938b35ce6a12f6150373b0955b8dd12 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 10 Jul 2011 15:24:15 -0500 Subject: Add Qt3 development HEAD version --- .../network/infoprotocol/infoserver/infodata.cpp | 88 ++++++++++++++++ .../network/infoprotocol/infoserver/infodata.h | 31 ++++++ .../network/infoprotocol/infoserver/infoserver.pro | 13 +++ examples/network/infoprotocol/infoserver/main.cpp | 22 ++++ .../network/infoprotocol/infoserver/server.cpp | 99 +++++++++++++++++ examples/network/infoprotocol/infoserver/server.h | 69 ++++++++++++ .../network/infoprotocol/infoserver/serverbase.ui | 117 +++++++++++++++++++++ 7 files changed, 439 insertions(+) create mode 100644 examples/network/infoprotocol/infoserver/infodata.cpp create mode 100644 examples/network/infoprotocol/infoserver/infodata.h create mode 100644 examples/network/infoprotocol/infoserver/infoserver.pro create mode 100644 examples/network/infoprotocol/infoserver/main.cpp create mode 100644 examples/network/infoprotocol/infoserver/server.cpp create mode 100644 examples/network/infoprotocol/infoserver/server.h create mode 100644 examples/network/infoprotocol/infoserver/serverbase.ui (limited to 'examples/network/infoprotocol/infoserver') diff --git a/examples/network/infoprotocol/infoserver/infodata.cpp b/examples/network/infoprotocol/infoserver/infodata.cpp new file mode 100644 index 0000000..597f8a3 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infodata.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "infodata.h" + + +// we hard code all nodes and data in InfoData class +InfoData::InfoData() : + nodes( 17, TRUE ), data( 17, TRUE ) +{ + nodes.setAutoDelete(TRUE); + data.setAutoDelete(TRUE); + QStringList *item; + + nodes.insert( "/", item = new QStringList( ) ); + (*item) << "D network"; + nodes.insert( "/network/", item = new QStringList() ); + (*item) << "D workstations" << "D printers" << "D fax"; + nodes.insert( "/network/workstations/", item = new QStringList() ); + (*item) << "D nibble" << "D douglas"; + nodes.insert( "/network/workstations/nibble/", item = new QStringList() ); + (*item) << "F os" << "F cpu" << "F memory"; + nodes.insert( "/network/workstations/douglas/", item = new QStringList() ); + (*item) << "F os" << "F cpu" << "F memory"; + nodes.insert( "/network/printers/", item = new QStringList() ); + (*item) << "D overbitt" << "D kroksleiven"; + nodes.insert( "/network/printers/overbitt/", item = new QStringList() ); + (*item) << "D jobs" << "F type"; + nodes.insert( "/network/printers/overbitt/jobs/", item = new QStringList() ); + (*item) << "F job1" << "F job2"; + nodes.insert( "/network/printers/kroksleiven/", item = new QStringList() ); + (*item) << "D jobs" << "F type"; + nodes.insert( "/network/printers/kroksleiven/jobs/", item = new QStringList() ); + nodes.insert( "/network/fax/", item = new QStringList() ); + (*item) << "F last_number"; + + data.insert( "/network/workstations/nibble/os", new QString( "Linux" ) ); + data.insert( "/network/workstations/nibble/cpu", new QString( "AMD Athlon 1000" ) ); + data.insert( "/network/workstations/nibble/memory", new QString( "256 MB" ) ); + data.insert( "/network/workstations/douglas/os", new QString( "Windows 2000" ) ); + data.insert( "/network/workstations/douglas/cpu", new QString( "2 x Intel Pentium III 800" ) ); + data.insert( "/network/workstations/douglas/memory", new QString( "256 MB" ) ); + data.insert( "/network/printers/overbitt/type", new QString( "Lexmark Optra S 1255 PS" ) ); + data.insert( "/network/printers/overbitt/jobs/job1", + new QString( "Qt manual\n" "A4 size\n" "3000 pages" ) ); + data.insert( "/network/printers/overbitt/jobs/job2", + new QString( "Monthly report\n" "Letter size\n" "24 pages\n" "8 copies" ) ); + data.insert( "/network/printers/kroksleiven/type", new QString( "HP C LaserJet 4500-PS" ) ); + data.insert( "/network/fax/last_number", new QString( "22 22 22 22" ) ); +} + +QStringList InfoData::list( QString path, bool *found ) const +{ + if ( !path.endsWith( "/" ) ) + path += "/"; + if ( !path.startsWith( "/" ) ) + path = "/" + path; + QStringList *list = nodes[ path ]; + if ( list ) { + *found = TRUE; + return *list; + } else { + *found = FALSE; + QStringList empty; + return empty; + } +} + +QString InfoData::get( QString path, bool *found ) const +{ + if ( !path.startsWith( "/" ) ) + path = "/" + path; + QString *file = data[ path ]; + if ( file ) { + *found = TRUE; + return *file; + } else { + *found = FALSE; + QString empty; + return empty; + } +} diff --git a/examples/network/infoprotocol/infoserver/infodata.h b/examples/network/infoprotocol/infoserver/infodata.h new file mode 100644 index 0000000..0189332 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infodata.h @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef INFODATA_H +#define INFODATA_H + +#include +#include + + +// The InfoData class manages data, organized in tree structure. +class InfoData +{ +public: + InfoData(); + QStringList list( QString path, bool *found ) const; + QString get( QString path, bool *found ) const; + +private: + QDict< QStringList > nodes; + QDict< QString > data; +}; + +#endif // INFODATA_H + diff --git a/examples/network/infoprotocol/infoserver/infoserver.pro b/examples/network/infoprotocol/infoserver/infoserver.pro new file mode 100644 index 0000000..3d79aef --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infoserver.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = infoserver + +CONFIG += qt warn_on release + +REQUIRES = network full-config nocrosscompiler + +HEADERS = server.h \ + infodata.h +SOURCES = main.cpp \ + server.cpp \ + infodata.cpp +INTERFACES = serverbase.ui diff --git a/examples/network/infoprotocol/infoserver/main.cpp b/examples/network/infoprotocol/infoserver/main.cpp new file mode 100644 index 0000000..fd0e8e3 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include + +#include "server.h" + +int main( int argc, char** argv ) +{ + QApplication app( argc, argv ); + Q_UINT16 port = ( argc > 1 ) ? QString( argv[ 1 ] ).toInt() : infoPort; + ServerInfo info( port, 0, "server info" ); + app.setMainWidget( &info ); + info.show(); + return app.exec(); +} diff --git a/examples/network/infoprotocol/infoserver/server.cpp b/examples/network/infoprotocol/infoserver/server.cpp new file mode 100644 index 0000000..f3815ae --- /dev/null +++ b/examples/network/infoprotocol/infoserver/server.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "server.h" + + + +ServerInfo::ServerInfo( Q_UINT16 port, QWidget *parent, const char *name ) : + ServerInfoBase( parent, name ) +{ + SimpleServer *server = new SimpleServer( port, this, "simple server" ); + connect( server, SIGNAL(newConnect()), SLOT(newConnect()) ); + connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()) ); +} + +void ServerInfo::newConnect() +{ + infoText->append( tr( "New connection\n" ) ); +} + + +SimpleServer::SimpleServer( Q_UINT16 port, QObject* parent, const char *name ) : + QServerSocket( port, 1, parent, name ) +{ + if ( !ok() ) { + QMessageBox::critical( 0, tr( "Error" ), tr( "Failed to bind to port %1" ).arg( port ) ); + exit(1); + } +} + +void SimpleServer::newConnection( int socket ) +{ + (void)new ClientSocket( socket, &info, this, "client socket" ); + emit newConnect(); +} + + +ClientSocket::ClientSocket( int sock, InfoData *i, QObject *parent, const char *name ) : + QSocket( parent, name ), info( i ) +{ + connect( this, SIGNAL(readyRead()), SLOT(readClient()) ); + connect( this, SIGNAL(connectionClosed()), SLOT(connectionClosed()) ); + setSocket( sock ); +} + +void ClientSocket::readClient() +{ + QTextStream stream( this ); + QStringList answer; + while ( canReadLine() ) { + stream << processCommand( stream.readLine() ); + } +} + +QString ClientSocket::processCommand( const QString& command ) +{ + QString answer; + QString com = command.simplifyWhiteSpace (); + if ( com.startsWith( "LIST" ) ) { + bool ok; + QStringList nodes = info->list( com.mid( 5 ), &ok ); + if ( ok ) { + for ( QStringList::Iterator it = nodes.begin(); it != nodes.end(); ++it ) + answer += "212+" + *it + "\r\n"; + answer += "212 \r\n"; + } else + answer += "550 Invalid path\r\n"; + } else if ( com.startsWith( "GET " ) ) { + bool ok; + QStringList data = QStringList::split( '\n', info->get( com.mid( 4 ), &ok ), TRUE ); + if ( ok ) { + for ( QStringList::Iterator it = data.begin(); it != data.end(); ++it ) + answer += "213+" + *it + "\r\n"; + answer += "213 \r\n"; + } else + answer += "550 Info not found\r\n"; + } else + answer += "500 Syntax error\r\n"; + + return answer; +} + +void ClientSocket::connectionClosed() +{ + delete this; +} diff --git a/examples/network/infoprotocol/infoserver/server.h b/examples/network/infoprotocol/infoserver/server.h new file mode 100644 index 0000000..fa07b93 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/server.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef SERVER_H +#define SERVER_H + +#include +#include + +#include "infodata.h" +#include "serverbase.h" + +static const Q_UINT16 infoPort = 42417; + + +/* + The ServerInfo class provides a small GUI for the server. It also creates the + SimpleServer and as a result the server. +*/ +class ServerInfo : public ServerInfoBase +{ + Q_OBJECT +public: + ServerInfo( Q_UINT16 port = infoPort, QWidget *parent = 0, const char *name = 0 ); + +private slots: + void newConnect(); +}; + + +class SimpleServer : public QServerSocket +{ + Q_OBJECT +public: + SimpleServer( Q_UINT16 port = infoPort, QObject* parent = 0, const char *name = 0 ); + void newConnection( int socket ); + +signals: + void newConnect(); + +private: + InfoData info; +}; + + +class ClientSocket : public QSocket +{ + Q_OBJECT +public: + ClientSocket( int sock, InfoData *i, QObject *parent = 0, const char *name = 0 ); + +private slots: + void readClient(); + void connectionClosed(); + +private: + QString processCommand( const QString& command ); + InfoData *info; +}; + + +#endif //SERVER_H + diff --git a/examples/network/infoprotocol/infoserver/serverbase.ui b/examples/network/infoprotocol/infoserver/serverbase.ui new file mode 100644 index 0000000..12998da --- /dev/null +++ b/examples/network/infoprotocol/infoserver/serverbase.ui @@ -0,0 +1,117 @@ + +ServerInfoBase + + + ServerInfoBase + + + + 0 + 0 + 272 + 282 + + + + Info Server + + + + unnamed + + + 11 + + + 6 + + + + TextLabel1 + + + This is a small Information Server. +Accepting client requests... + + + AlignCenter + + + + + infoText + + + true + + + + + Layout1 + + + + unnamed + + + 0 + + + 6 + + + + Spacer2 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + btnQuit + + + + 3 + 0 + 0 + 0 + + + + &Quit + + + + + Spacer1 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + + + + -- cgit v1.2.1