Added socket to Package::Source
[dashstudio.git] / src / dash / network / package / dispatcher.cpp
blob53b1321056411bdf3044caced7cb8a1ea47ec3cc
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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"
23 #include <QSet>
24 #include <QQueue>
25 #include <QThread>
26 #include <QDomDocument>
28 #include "observer.h"
29 #include "source.h"
31 #include <dcore/debug.h>
33 namespace Dash {
34 namespace Network {
35 namespace Package {
37 class DispatcherThread// : public QThread
39 public:
40 DispatcherThread();
41 // protected:
42 void run();
44 public:
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() )
63 break;
66 delete package;
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);
84 delete d->thread;
85 delete d;
88 /**
89 * @~english
90 * Adds a package to be dispatched, the package is owned by the dispatcher.
91 * @param pkg
93 void Dispatcher::addSource(Source *pkg)
95 d->thread->packages.enqueue(pkg);
98 /**
99 * @~english
100 * Create and adds a package to be dispatched.
101 * @param id
102 * @param xml
104 void Dispatcher::addSource(const QString &id, const QString &xml, Dash::Network::Socket *socket)
106 addSource(new Source(id, xml, socket));
110 * @~english
111 * Create and adds a package to be dispatched.
112 * @param xml
114 void Dispatcher::addSource(const QString &xml, Dash::Network::Socket *socket)
116 QDomDocument doc;
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.
126 * @param observer
128 void Dispatcher::addObserver(Observer *observer)
130 d->thread->observers << observer;
134 * Removes an observer.
135 * @param 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();
150 d->thread->run();