dvbd / source.cpp @ bfdb7446
History | View | Annotate | Download (2.55 KB)
1 |
/*
|
---|---|
2 |
Copyright 2003 John Knottenbelt
|
3 |
|
4 |
This program is free software; you can redistribute it and/or modify
|
5 |
it under the terms of the GNU General Public License as published by
|
6 |
the Free Software Foundation; either version 2 of the License, or
|
7 |
(at your option) any later version.
|
8 |
|
9 |
This program is distributed in the hope that it will be useful,
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
GNU General Public License for more details.
|
13 |
|
14 |
You should have received a copy of the GNU General Public License
|
15 |
along with this program; if not, write to the Free Software
|
16 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
|
17 |
*/
|
18 |
|
19 |
#include "config.h" |
20 |
#include "source.h" |
21 |
|
22 |
#include <climits> |
23 |
#include <iostream> |
24 |
#include <cstdlib> |
25 |
|
26 |
Priority::~Priority() |
27 |
{ |
28 |
} |
29 |
|
30 |
void Priority::setPriority( int newPriority ) |
31 |
{ |
32 |
std::cerr << "Fatal error: attempt to call unimplemented setPriority method\n";
|
33 |
exit(1);
|
34 |
} |
35 |
|
36 |
Sink::~Sink() |
37 |
{ |
38 |
} |
39 |
|
40 |
void Sink::notifyUnsubscribe(Source *s)
|
41 |
{ |
42 |
} |
43 |
|
44 |
Source::~Source() |
45 |
{ |
46 |
} |
47 |
|
48 |
bool Source::subscribe(Sink *s)
|
49 |
{ |
50 |
subscribers.push_back(s); |
51 |
return true; |
52 |
} |
53 |
|
54 |
void Source::unsubscribe(Sink *s)
|
55 |
{ |
56 |
subscribers.remove(s); |
57 |
s->notifyUnsubscribe(this);
|
58 |
} |
59 |
|
60 |
void Source::unsubscribeAll()
|
61 |
{ |
62 |
List copy = subscribers; |
63 |
|
64 |
for (List::const_iterator i = copy.begin(); i != copy.end(); i++)
|
65 |
unsubscribe(*i); |
66 |
} |
67 |
|
68 |
void Source::sendData(unsigned char *data, unsigned size) const |
69 |
{ |
70 |
for (List::const_iterator i = subscribers.begin(); i != subscribers.end(); i++)
|
71 |
(*i)->receiveData(data, size); |
72 |
} |
73 |
|
74 |
int Source::unsubscribe( int priority ) |
75 |
{ |
76 |
// Kick subscribers with priority < specified priority
|
77 |
// Return number of subscribers kicked.
|
78 |
int count = 0; |
79 |
List copy = subscribers; |
80 |
|
81 |
for (List::iterator i = copy.begin(); i != copy.end(); i++) {
|
82 |
Sink *d = *i; |
83 |
|
84 |
if (d->getPriority() < priority) {
|
85 |
unsubscribe(d); |
86 |
count++; |
87 |
} |
88 |
} |
89 |
return count;
|
90 |
} |
91 |
|
92 |
int Source::getNumSubscribers(int priority) const |
93 |
{ |
94 |
// Return the number of subscribers with priority >= specified priority
|
95 |
int count = 0; |
96 |
for (List::const_iterator i = subscribers.begin(); i != subscribers.end(); i++) {
|
97 |
const Sink *d = *i;
|
98 |
|
99 |
if (d->getPriority() >= priority)
|
100 |
count++; |
101 |
} |
102 |
return count;
|
103 |
} |
104 |
|
105 |
int Source::getPriority() const |
106 |
{ |
107 |
// Return the maximum subscribed priority
|
108 |
int result = INT_MIN;
|
109 |
for (List::const_iterator i = subscribers.begin(); i != subscribers.end(); i++) {
|
110 |
const Sink *c = *i;
|
111 |
|
112 |
if (c->getPriority() > result)
|
113 |
result = c->getPriority(); |
114 |
} |
115 |
return result;
|
116 |
} |