stream.go
changeset 45 abf958bcc201
parent 42 f6bb47ca12f2
child 46 4a4530b8f622
--- a/stream.go	Sun Jan 01 17:28:00 2012 -0700
+++ b/stream.go	Sun Jan 01 19:00:21 2012 -0700
@@ -293,6 +293,55 @@
 	}
 }
 
+// Stanzas from the remote go up through a stack of filters to the
+// app. This function manages the filters.
+func filter(srvIn2 <-chan Stanza, cliOut2 chan<- Stanza,
+	filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza) {
+	defer close(cliOut2)
+	var srvIn1, topFilterOut1, topFilterOut2 <-chan Stanza
+	var cliOut1, botFilterIn1, botFilterIn2 chan<- Stanza
+	ch := make(chan Stanza, 1)
+	topFilterOut2 = ch
+	botFilterIn2 = ch
+	topFilterOut1 = topFilterOut2
+	srvIn1 = srvIn2
+	var botItem Stanza
+	var topItem Stanza
+	var ok bool
+	for {
+		select {
+		case newFilterOut := <- filterOut:
+			filterIn <- topFilterOut2
+			topFilterOut2 = newFilterOut
+			if topFilterOut1 != nil {
+				topFilterOut1 = topFilterOut2
+			}
+
+		case topItem, ok = <-topFilterOut1:
+			if !ok {
+				break
+			}
+			topFilterOut1 = nil
+			cliOut1 = cliOut2
+		case cliOut1 <- topItem:
+			topFilterOut1 = topFilterOut2
+			cliOut1 = nil
+
+		case botItem, ok = <-srvIn1:
+			if !ok {
+				close(botFilterIn2)
+				srvIn1 = nil
+				continue
+			}
+			srvIn1 = nil
+			botFilterIn1 = botFilterIn2
+		case botFilterIn1 <- botItem:
+			srvIn1 = srvIn2
+			botFilterIn1 = nil
+		}
+	}
+}
+
 func handleStream(ss *stream) {
 }