stream.go
changeset 51 1af366d10d32
parent 46 4a4530b8f622
child 57 e6cb3f049137
equal deleted inserted replaced
50:08d2b9deb710 51:1af366d10d32
   284 				input = cliIn
   284 				input = cliIn
   285 			case -1:
   285 			case -1:
   286 				break
   286 				break
   287 			}
   287 			}
   288 		case x := <- input:
   288 		case x := <- input:
       
   289 			if x == nil {
       
   290 				log.Println("Refusing to send nil stanza")
       
   291 				continue
       
   292 			}
   289 			srvOut <- x
   293 			srvOut <- x
   290 		}
   294 		}
   291 	}
   295 	}
   292 }
   296 }
   293 
   297 
   294 // Stanzas from the remote go up through a stack of filters to the
   298 // Stanzas from the remote go up through a stack of filters to the
   295 // app. This function manages the filters.
   299 // app. This function manages the filters.
   296 func filter(srvIn2 <-chan Stanza, cliOut2 chan<- Stanza,
   300 func filterTop(filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza,
   297 	filterOut <-chan <-chan Stanza, filterIn chan<- <-chan Stanza) {
   301 	topFilter <-chan Stanza, app chan<- Stanza) {
   298 	defer close(cliOut2)
   302 	defer close(app)
   299 	var srvIn1, topFilterOut1, topFilterOut2 <-chan Stanza
       
   300 	var cliOut1, botFilterIn1, botFilterIn2 chan<- Stanza
       
   301 	ch := make(chan Stanza, 1)
       
   302 	topFilterOut2 = ch
       
   303 	botFilterIn2 = ch
       
   304 	topFilterOut1 = topFilterOut2
       
   305 	srvIn1 = srvIn2
       
   306 	var botItem Stanza
       
   307 	var topItem Stanza
       
   308 	var ok bool
       
   309 	for {
   303 	for {
   310 		select {
   304 		select {
   311 		case newFilterOut := <- filterOut:
   305 		case newFilterOut := <- filterOut:
   312 			filterIn <- topFilterOut2
   306 			if newFilterOut == nil {
   313 			topFilterOut2 = newFilterOut
   307 				log.Println("Received nil filter")
   314 			if topFilterOut1 != nil {
   308 				filterIn <- nil
   315 				topFilterOut1 = topFilterOut2
   309 				continue
   316 			}
   310 			}
   317 
   311 			filterIn <- topFilter
   318 		case topItem, ok = <-topFilterOut1:
   312 
       
   313 		case data, ok := <-topFilter:
   319 			if !ok {
   314 			if !ok {
   320 				break
   315 				break
   321 			}
   316 			}
   322 			topFilterOut1 = nil
   317 			app <- data
   323 			cliOut1 = cliOut2
   318 		}
   324 		case cliOut1 <- topItem:
   319 	}
   325 			topFilterOut1 = topFilterOut2
   320 }
   326 			cliOut1 = nil
   321 
   327 
   322 func filterBottom(from <-chan Stanza, to chan<- Stanza) {
   328 		case botItem, ok = <-srvIn1:
   323 	defer close(to)
   329 			if !ok {
   324 	for data := range(from) {
   330 				close(botFilterIn2)
   325 		to <- data
   331 				srvIn1 = nil
       
   332 				continue
       
   333 			}
       
   334 			srvIn1 = nil
       
   335 			botFilterIn1 = botFilterIn2
       
   336 		case botFilterIn1 <- botItem:
       
   337 			srvIn1 = srvIn2
       
   338 			botFilterIn1 = nil
       
   339 		}
       
   340 	}
   326 	}
   341 }
   327 }
   342 
   328 
   343 func handleStream(ss *stream) {
   329 func handleStream(ss *stream) {
   344 }
   330 }