1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "connection.h"
27 #include <QHostAddress>
29 #include <dcore/debug.h>
31 #include "packagehandlerbase.h"
32 #include "defaultpackagehandler.h"
34 #include "bans/banmanager.h"
35 #include "backups/backupmanager.h"
36 #include "users/usermanager.h"
38 #include "registers/regmanager.h"
39 #include "project/projectmanager.h"
45 #include <dash/network/socket.h>
47 using namespace Dash::Network
;
49 namespace DashServer
{
51 class TcpServer::Private
54 QSet
<DashServer::Connection
*> connections
;
55 QSet
<DashServer::Connection
*> admins
;
57 Backups::Manager
*backupManager
;
58 Bans::Manager
*banManager
;
59 Users::Manager
*userManager
;
61 QList
<DashServer::Observer
*> observers
;
64 TcpServer::TcpServer(QObject
*parent
) : QTcpServer(parent
), d(new Private
)
67 d
->backupManager
= new Backups::Manager
;
68 d
->observers
<< d
->backupManager
;
70 d
->banManager
= new Bans::Manager
;
71 d
->observers
<< d
->banManager
;
73 d
->userManager
= new Users::Manager
;
74 d
->observers
<< d
->userManager
;
76 addObserver(new DashServer::Project::Manager
);
80 TcpServer::~TcpServer()
84 DashServer::Logger::self()->info("DashServer finished");
86 qDebug("=> Deleting observers");
87 qDeleteAll(d
->observers
);
89 qDebug("=> Deleting connections");
90 foreach(DashServer::Connection
*cnn
, d
->connections
)
92 removeConnection(cnn
);
98 bool TcpServer::openConnection(const QString
&host
, int port
)
100 DashServer::Logger::self()->info(QObject::tr("Initialized server on %1:%2").arg(host
).arg(port
));
102 QList
<QHostAddress
> addrs
= QHostInfo::fromName(host
).addresses();
103 if ( !addrs
.isEmpty() )
105 if(! listen(QHostAddress(addrs
[0]), port
) )
107 dError() << "Can't connect to " << host
<<":"<<port
<< " error was: " << errorString();
113 dError() << "Error while try to resolve " << host
;
119 void TcpServer::addAdmin(DashServer::Connection
*cnx
)
124 Backups::Manager
*TcpServer::backupManager() const
126 return d
->backupManager
;
129 Bans::Manager
*TcpServer::banManager() const
131 return d
->banManager
;
134 Users::Manager
*TcpServer::userManager() const
136 return d
->userManager
;
139 void TcpServer::addObserver(DashServer::Observer
*observer
)
141 d
->observers
<< observer
;
144 bool TcpServer::removeObserver(DashServer::Observer
*observer
)
146 return d
->observers
.removeAll(observer
) > 0;
149 void TcpServer::incomingConnection(int socketDescriptor
)
151 D_SHOW_VAR(d
->connections
.count());
152 D_SHOW_VAR(socketDescriptor
);
154 DashServer::Connection
*newConnection
= new DashServer::Connection(socketDescriptor
,this);
156 QString ip
= newConnection
->client()->peerAddress().toString();
157 d
->banManager
->initialize(ip
);
159 if ( !d
->banManager
->isBanned(ip
) )
161 handle(newConnection
);
162 d
->connections
<< newConnection
;
166 connect(newConnection
, SIGNAL(finished()), newConnection
, SLOT(deleteLater()));
167 newConnection
->sendError(tr("You're banned, Please contact to server administrator if you think is an error!"), Dash::Network::Package::Error::Err
);
171 void TcpServer::handle(DashServer::Connection
*cnx
)
173 connect(cnx
, SIGNAL(requestSendToAll( const QString
& )), this, SLOT(sendToAll( const QString
& )));
175 connect(cnx
, SIGNAL(packageReaded(DashServer::Connection
*, const QString
&, const QString
&)), this, SLOT(handlePackage(DashServer::Connection
*, const QString
&, const QString
&)));
177 connect(cnx
, SIGNAL(connectionClosed(DashServer::Connection
*)), this, SLOT(removeConnection(DashServer::Connection
*)));
181 void TcpServer::sendToAll(const QString
&msg
)
183 foreach(DashServer::Connection
*connection
, d
->connections
)
185 connection
->sendToClient(msg
, false);
189 void TcpServer::sendToAll(const QDomDocument
&pkg
)
192 sendToAll(pkg
.toString(0));
195 void TcpServer::sendToAdmins(const QString
&str
)
197 foreach(DashServer::Connection
*cnn
, d
->admins
)
199 cnn
->sendToClient(str
, false);
203 void TcpServer::removeConnection(DashServer::Connection
*cnx
)
209 if( !d
->connections
.contains(cnx
) ) return;
211 d
->connections
.remove(cnx
);
212 d
->admins
.remove(cnx
);
214 foreach(DashServer::Observer
*observer
, d
->observers
)
216 observer
->connectionClosed(cnx
);
219 cnx
->blockSignals(true);
221 cnx
->blockSignals(false);
223 if( !cnx
->isRunning() )
229 connect(cnx
, SIGNAL(finished()), cnx
, SLOT(deleteLater()));
235 void TcpServer::handlePackage(DashServer::Connection
* client
, const QString
&root
, const QString
&package
)
237 DashServer::Package
*pkg
= new DashServer::Package(root
, package
, client
);
239 foreach(DashServer::Observer
*observer
, d
->observers
)
241 observer
->handlePackage(pkg
);
243 if ( pkg
->isAccepted() )
247 if( ! pkg
->isAccepted() )
248 qWarning() << "NOT ACCEPTED: " << pkg
->root();