summaryrefslogtreecommitdiffstats
path: root/examples/network/infoprotocol/infoserver/server.cpp
blob: 967115c462148c5b540ded8a688f55d9a723d92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <tqtextview.h>
#include <tqpushbutton.h>
#include <tqtextstream.h>
#include <tqapplication.h>
#include <tqmessagebox.h>
#include <stdlib.h>

#include "server.h"



ServerInfo::ServerInfo( TQ_UINT16 port, TQWidget *parent, const char *name ) :
    ServerInfoBase( parent, name )
{
    SimpleServer *server = new SimpleServer( port, this, "simple server" );
    connect( server, TQ_SIGNAL(newConnect()), TQ_SLOT(newConnect()) );
    connect( btnQuit, TQ_SIGNAL(clicked()), tqApp, TQ_SLOT(quit()) );
}

void ServerInfo::newConnect()
{
    infoText->append( tr( "New connection\n" ) );
}


SimpleServer::SimpleServer( TQ_UINT16 port, TQObject* parent, const char *name ) :
    TQServerSocket( port, 1, parent, name )
{
    if ( !ok() ) {
	TQMessageBox::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, TQObject *parent, const char *name ) :
    TQSocket( parent, name ), info( i )
{
    connect( this, TQ_SIGNAL(readyRead()), TQ_SLOT(readClient()) );
    connect( this, TQ_SIGNAL(connectionClosed()), TQ_SLOT(connectionClosed()) );
    setSocket( sock );
}

void ClientSocket::readClient()
{
    TQTextStream stream( this );
    TQStringList answer;
    while ( canReadLine() ) {
	stream << processCommand( stream.readLine() );
    }
}

TQString ClientSocket::processCommand( const TQString& command )
{
    TQString answer;
    TQString com = command.simplifyWhiteSpace ();
    if ( com.startsWith( "LIST" ) ) {
	bool ok;
	TQStringList nodes = info->list( com.mid( 5 ), &ok );
	if ( ok ) {
	    for ( TQStringList::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;
	TQStringList data = TQStringList::split( '\n', info->get( com.mid( 4 ), &ok ), TRUE );
	if ( ok ) {
	    for ( TQStringList::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;
}