source: trunk/examples/network/infoprotocol/infoserver/server.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1/****************************************************************************
2** $Id: server.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include <qtextview.h>
12#include <qpushbutton.h>
13#include <qtextstream.h>
14#include <qapplication.h>
15#include <qmessagebox.h>
16#include <stdlib.h>
17
18#include "server.h"
19
20
21
22ServerInfo::ServerInfo( Q_UINT16 port, QWidget *parent, const char *name ) :
23 ServerInfoBase( parent, name )
24{
25 SimpleServer *server = new SimpleServer( port, this, "simple server" );
26 connect( server, SIGNAL(newConnect()), SLOT(newConnect()) );
27 connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()) );
28}
29
30void ServerInfo::newConnect()
31{
32 infoText->append( tr( "New connection\n" ) );
33}
34
35
36SimpleServer::SimpleServer( Q_UINT16 port, QObject* parent, const char *name ) :
37 QServerSocket( port, 1, parent, name )
38{
39 if ( !ok() ) {
40 QMessageBox::critical( 0, tr( "Error" ), tr( "Failed to bind to port %1" ).arg( port ) );
41 exit(1);
42 }
43}
44
45void SimpleServer::newConnection( int socket )
46{
47 (void)new ClientSocket( socket, &info, this, "client socket" );
48 emit newConnect();
49}
50
51
52ClientSocket::ClientSocket( int sock, InfoData *i, QObject *parent, const char *name ) :
53 QSocket( parent, name ), info( i )
54{
55 connect( this, SIGNAL(readyRead()), SLOT(readClient()) );
56 connect( this, SIGNAL(connectionClosed()), SLOT(connectionClosed()) );
57 setSocket( sock );
58}
59
60void ClientSocket::readClient()
61{
62 QTextStream stream( this );
63 QStringList answer;
64 while ( canReadLine() ) {
65 stream << processCommand( stream.readLine() );
66 }
67}
68
69QString ClientSocket::processCommand( const QString& command )
70{
71 QString answer;
72 QString com = command.simplifyWhiteSpace ();
73 if ( com.startsWith( "LIST" ) ) {
74 bool ok;
75 QStringList nodes = info->list( com.mid( 5 ), &ok );
76 if ( ok ) {
77 for ( QStringList::Iterator it = nodes.begin(); it != nodes.end(); ++it )
78 answer += "212+" + *it + "\r\n";
79 answer += "212 \r\n";
80 } else
81 answer += "550 Invalid path\r\n";
82 } else if ( com.startsWith( "GET " ) ) {
83 bool ok;
84 QStringList data = QStringList::split( '\n', info->get( com.mid( 4 ), &ok ), TRUE );
85 if ( ok ) {
86 for ( QStringList::Iterator it = data.begin(); it != data.end(); ++it )
87 answer += "213+" + *it + "\r\n";
88 answer += "213 \r\n";
89 } else
90 answer += "550 Info not found\r\n";
91 } else
92 answer += "500 Syntax error\r\n";
93
94 return answer;
95}
96
97void ClientSocket::connectionClosed()
98{
99 delete this;
100}
Note: See TracBrowser for help on using the repository browser.