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 "databaseobjectfactory.h"
46 #include "xdbms/manager.h"
48 #include <dash/network/socket.h>
50 using namespace Dash::Network
;
52 namespace DashServer
{
54 class TcpServer::Private
57 QSet
<DashServer::Connection
*> connections
;
58 QSet
<DashServer::Connection
*> admins
;
60 Backups::Manager
*backupManager
;
61 Bans::Manager
*banManager
;
62 Users::Manager
*userManager
;
63 Project::Manager
*projectManager
;
65 QList
<DashServer::Observer
*> observers
;
68 TcpServer::TcpServer(QObject
*parent
) : QTcpServer(parent
), d(new Private
)
72 XDBMS::Manager::self()->setObjectFactory(new DatabaseObjectFactory
);
74 d
->backupManager
= new Backups::Manager
;
75 d
->observers
<< d
->backupManager
;
77 d
->banManager
= new Bans::Manager
;
78 d
->observers
<< d
->banManager
;
80 d
->userManager
= new Users::Manager
;
81 d
->observers
<< d
->userManager
;
83 d
->projectManager
= new DashServer::Project::Manager
;
84 d
->observers
<< d
->projectManager
;
88 TcpServer::~TcpServer()
92 DashServer::Logger::self()->info("DashServer finished");
94 qDebug("=> Deleting observers");
95 qDeleteAll(d
->observers
);
97 qDebug("=> Deleting connections");
98 foreach(DashServer::Connection
*cnn
, d
->connections
)
100 removeConnection(cnn
);
106 bool TcpServer::openConnection(const QString
&host
, int port
)
108 DashServer::Logger::self()->info(QObject::tr("Initialized server on %1:%2").arg(host
).arg(port
));
110 QList
<QHostAddress
> addrs
= QHostInfo::fromName(host
).addresses();
111 if ( !addrs
.isEmpty() )
113 if(! listen(QHostAddress(addrs
[0]), port
) )
115 dError() << "Can't connect to " << host
<<":"<<port
<< " error was: " << errorString();
121 dError() << "Error while try to resolve " << host
;
127 void TcpServer::addAdmin(DashServer::Connection
*cnx
)
132 Backups::Manager
*TcpServer::backupManager() const
134 return d
->backupManager
;
137 Bans::Manager
*TcpServer::banManager() const
139 return d
->banManager
;
142 Users::Manager
*TcpServer::userManager() const
144 return d
->userManager
;
147 Project::Manager
*TcpServer::projectManager() const
149 return d
->projectManager
;
152 void TcpServer::addObserver(DashServer::Observer
*observer
)
154 d
->observers
<< observer
;
157 bool TcpServer::removeObserver(DashServer::Observer
*observer
)
159 return d
->observers
.removeAll(observer
) > 0;
162 void TcpServer::incomingConnection(int socketDescriptor
)
164 D_SHOW_VAR(d
->connections
.count());
165 D_SHOW_VAR(socketDescriptor
);
167 DashServer::Connection
*newConnection
= new DashServer::Connection(socketDescriptor
,this);
169 QString ip
= newConnection
->client()->peerAddress().toString();
170 d
->banManager
->initialize(ip
);
172 if ( !d
->banManager
->isBanned(ip
) )
174 handle(newConnection
);
175 d
->connections
<< newConnection
;
179 connect(newConnection
, SIGNAL(finished()), newConnection
, SLOT(deleteLater()));
180 newConnection
->sendError(tr("You're banned, Please contact to server administrator if you think is an error!"), Dash::Network::Package::Error::Err
);
184 void TcpServer::handle(DashServer::Connection
*cnx
)
186 connect(cnx
, SIGNAL(requestSendToAll( const QString
& )), this, SLOT(sendToAll( const QString
& )));
188 connect(cnx
, SIGNAL(packageReaded(DashServer::Connection
*, const QString
&, const QString
&)), this, SLOT(handlePackage(DashServer::Connection
*, const QString
&, const QString
&)));
190 connect(cnx
, SIGNAL(connectionClosed(DashServer::Connection
*)), this, SLOT(removeConnection(DashServer::Connection
*)));
194 void TcpServer::sendToAll(const QString
&msg
)
196 foreach(DashServer::Connection
*connection
, d
->connections
)
198 connection
->sendToClient(msg
, false);
202 void TcpServer::sendToAll(const QDomDocument
&pkg
)
205 sendToAll(pkg
.toString(0));
208 void TcpServer::sendToAdmins(const QString
&str
)
210 foreach(DashServer::Connection
*cnn
, d
->admins
)
212 cnn
->sendToClient(str
, false);
216 void TcpServer::removeConnection(DashServer::Connection
*cnx
)
222 if( !d
->connections
.contains(cnx
) ) return;
224 d
->connections
.remove(cnx
);
225 d
->admins
.remove(cnx
);
227 foreach(DashServer::Observer
*observer
, d
->observers
)
229 observer
->connectionClosed(cnx
);
232 cnx
->blockSignals(true);
234 cnx
->blockSignals(false);
236 if( !cnx
->isRunning() )
242 connect(cnx
, SIGNAL(finished()), cnx
, SLOT(deleteLater()));
248 void TcpServer::handlePackage(DashServer::Connection
* client
, const QString
&root
, const QString
&package
)
250 DashServer::Package
*pkg
= new DashServer::Package(root
, package
, client
);
252 foreach(DashServer::Observer
*observer
, d
->observers
)
254 observer
->handlePackage(pkg
);
256 if ( pkg
->isAccepted() )
260 if( ! pkg
->isAccepted() )
261 qWarning() << "NOT ACCEPTED: " << pkg
->root();