stream.go
changeset 51 1af366d10d32
parent 46 4a4530b8f622
child 57 e6cb3f049137
--- a/stream.go	Mon Jan 02 10:01:41 2012 -0700
+++ b/stream.go	Mon Jan 02 13:18:48 2012 -0700
@@ -286,6 +286,10 @@
 				break
 			}
 		case x := <- input:
+			if x == nil {
+				log.Println("Refusing to send nil stanza")
+				continue
+			}
 			srvOut <- x
 		}
 	}
@@ -293,50 +297,32 @@
 
 // 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
+func filterTop(filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza,
+	topFilter <-chan Stanza, app chan<- Stanza) {
+	defer close(app)
 	for {
 		select {
 		case newFilterOut := <- filterOut:
-			filterIn <- topFilterOut2
-			topFilterOut2 = newFilterOut
-			if topFilterOut1 != nil {
-				topFilterOut1 = topFilterOut2
+			if newFilterOut == nil {
+				log.Println("Received nil filter")
+				filterIn <- nil
+				continue
 			}
+			filterIn <- topFilter
 
-		case topItem, ok = <-topFilterOut1:
+		case data, ok := <-topFilter:
 			if !ok {
 				break
 			}
-			topFilterOut1 = nil
-			cliOut1 = cliOut2
-		case cliOut1 <- topItem:
-			topFilterOut1 = topFilterOut2
-			cliOut1 = nil
+			app <- data
+		}
+	}
+}
 
-		case botItem, ok = <-srvIn1:
-			if !ok {
-				close(botFilterIn2)
-				srvIn1 = nil
-				continue
-			}
-			srvIn1 = nil
-			botFilterIn1 = botFilterIn2
-		case botFilterIn1 <- botItem:
-			srvIn1 = srvIn2
-			botFilterIn1 = nil
-		}
+func filterBottom(from <-chan Stanza, to chan<- Stanza) {
+	defer close(to)
+	for data := range(from) {
+		to <- data
 	}
 }