monotone

monotone Mtn Source Tree

Root/botan/filter.cpp

1/*************************************************
2* Filter Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/filter.h>
7#include <botan/secqueue.h>
8#include <botan/ui.h>
9
10namespace Botan {
11
12/*************************************************
13* Filter Constructor *
14*************************************************/
15Filter::Filter()
16 {
17 next.resize(1);
18 port_num = 0;
19 filter_owns = 0;
20 }
21
22/*************************************************
23* Send data to all ports *
24*************************************************/
25void Filter::send(const byte input[], u32bit length)
26 {
27 UI::pulse(UI::PIPE_WRITE);
28
29 bool nothing_attached = true;
30 for(u32bit j = 0; j != total_ports(); j++)
31 if(next[j])
32 {
33 if(write_queue.has_items())
34 next[j]->write(write_queue, write_queue.size());
35 next[j]->write(input, length);
36 nothing_attached = false;
37 }
38 if(nothing_attached)
39 write_queue.append(input, length);
40 else if(write_queue.has_items())
41 write_queue.destroy();
42 }
43
44/*************************************************
45* Start a new message *
46*************************************************/
47void Filter::new_msg()
48 {
49 start_msg();
50 for(u32bit j = 0; j != total_ports(); j++)
51 if(next[j])
52 next[j]->new_msg();
53 }
54
55/*************************************************
56* End the current message *
57*************************************************/
58void Filter::finish_msg()
59 {
60 end_msg();
61 for(u32bit j = 0; j != total_ports(); j++)
62 if(next[j])
63 next[j]->finish_msg();
64 }
65
66/*************************************************
67* Attach a filter to the current port *
68*************************************************/
69void Filter::attach(Filter* new_filter)
70 {
71 if(new_filter)
72 {
73 Filter* last = this;
74 while(last->get_next())
75 last = last->get_next();
76 last->next[last->current_port()] = new_filter;
77 }
78 }
79
80/*************************************************
81* Set the active port on a filter *
82*************************************************/
83void Filter::set_port(u32bit new_port)
84 {
85 if(new_port >= total_ports())
86 throw Invalid_Argument("Filter: Invalid port number");
87 port_num = new_port;
88 }
89
90/*************************************************
91* Return the next Filter in the logical chain *
92*************************************************/
93Filter* Filter::get_next() const
94 {
95 if(port_num < next.size())
96 return next[port_num];
97 return 0;
98 }
99
100/*************************************************
101* Set the next Filters *
102*************************************************/
103void Filter::set_next(Filter* filters[], u32bit size)
104 {
105 while(size && filters && filters[size-1] == 0)
106 size--;
107
108 next.clear();
109 next.resize(size);
110
111 port_num = 0;
112 filter_owns = 0;
113
114 for(u32bit j = 0; j != size; j++)
115 next[j] = filters[j];
116 }
117
118/*************************************************
119* Return the total number of ports *
120*************************************************/
121u32bit Filter::total_ports() const
122 {
123 return next.size();
124 }
125
126}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status