monotone

monotone Mtn Source Tree

Root/botan/filter.cpp

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

Archive Download this file

Branches

Tags

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