1 /***************************************************************************
2 * Copyright (C) 2007 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 ***************************************************************************/
21 #include "dispatcher.h"
26 #include <QDomDocument>
31 #include <dcore/debug.h>
37 class DispatcherThread
// : public QThread
45 QSet
<Observer
*> observers
;
46 QQueue
<Source
*> packages
;
49 DispatcherThread::DispatcherThread()
53 void DispatcherThread::run()
55 while( ! packages
.isEmpty() )
57 Source
*package
= packages
.dequeue();
59 foreach(Observer
*observer
, observers
)
61 observer
->handlePackage(package
);
62 if( package
->isAccepted() )
70 struct Dispatcher::Private
72 DispatcherThread
*thread
;
75 Dispatcher::Dispatcher() : d(new Private
)
77 d
->thread
= new DispatcherThread
;
81 Dispatcher::~Dispatcher()
83 qDeleteAll(d
->thread
->packages
);
90 * Adds a package to be dispatched, the package is owned by the dispatcher.
93 void Dispatcher::addSource(Source
*pkg
)
95 d
->thread
->packages
.enqueue(pkg
);
100 * Create and adds a package to be dispatched.
104 void Dispatcher::addSource(const QString
&id
, const QString
&xml
, Dash::Network::Socket
*socket
)
106 addSource(new Source(id
, xml
, socket
));
111 * Create and adds a package to be dispatched.
114 void Dispatcher::addSource(const QString
&xml
, Dash::Network::Socket
*socket
)
117 if( doc
.setContent(xml
) )
119 addSource(doc
.documentElement().tagName(), xml
, socket
);
125 * Adds an observer to the dispatcher, The observer handle the package dispatched.
128 void Dispatcher::addObserver(Observer
*observer
)
130 d
->thread
->observers
<< observer
;
134 * Removes an observer.
137 void Dispatcher::removeObserver(Observer
*observer
)
139 d
->thread
->observers
.remove(observer
);
143 * Processes all packages
145 void Dispatcher::process()
147 // if( ! d->thread->isRunning() )
149 // d->thread->start();