func (_tailer *Tailer) loop() { transcript.TraceDebugging("running tailer...") var _error error var _offset int64 transcript.TraceDebugging("opening tailer file...") _tailer.file, _error = os.Open(_tailer.path) if _error != nil { transcript.TraceError("error encountered while opening tailer file; aborting...") panic(_error) } defer _tailer.file.Close() _offset, _error = _tailer.file.Seek(0, os.SEEK_END) if _error != nil { transcript.TraceWarning("error encountered while positioning tailer file; ignoring...") } _tailer.pushIdentifier, _tailer.pushChannel = _tailer.router.CreateInbound() defer _tailer.router.UnregisterInbound(_tailer.pushIdentifier) _reader := bufio.NewReader(_tailer.file) for _counter := 0; ; _counter += 1 { _line, _error := _reader.ReadBytes('\n') if _error == io.EOF { time.Sleep(100 * time.Millisecond) continue } if _error != nil { transcript.TraceError("error encountered while reading tailer file; aborting...") panic(_error) } if (_counter == 0) && (_offset != 0) { continue } _tailer.pushChannel <- Event{Payload: _line} } }
func (_publisher *Publisher) loop() { transcript.TraceDebugging("running publisher...") defer C.pxmq_outbound_channel_destroy(_publisher.backend) _publisher.pullChannel = make(chan Event, 1024) _publisher.pullIdentifier = _publisher.router.RegisterOutbound(_publisher.pullChannel) defer _publisher.router.UnregisterOutbound(_publisher.pullIdentifier) for { transcript.TraceDebugging("waiting publisher action...") _event := <-_publisher.pullChannel _outcome := C.pxmq_outbound_channel_push(_publisher.backend, unsafe.Pointer(&_event.Payload[0]), C.size_t(len(_event.Payload)), 0) _error := syscall.Errno(_outcome) if (_error != 0) && (_error != syscall.EAGAIN) { panic(errors.New("backend push failed: " + syscall.Errno(_outcome).Error())) } } }
func (_streamer *Streamer) loop() { transcript.TraceDebugging("starting http streamer...") _error := _streamer.server.ListenAndServe() if _error != nil { transcript.TraceError("error encountered while starting http streamer: `%s`; aborting...", _error.Error()) panic(_error) } }
func (_router *router) handleUnregisterInbound(_inputs interface{}) (interface{}, error) { _identifier, _ok := _inputs.(channelIdentifier) if !_ok || (_identifier == 0) { return nil, errors.New("invalid inputs") } transcript.TraceDebugging("unregistering router inbound channel...") delete(_router.inboundChannels, _identifier) return nil, nil }
func (_counter *Counter) loop() { transcript.TraceDebugging("running counter...") _counter.pushIdentifier, _counter.pushChannel = _counter.router.CreateInbound() defer _counter.router.UnregisterInbound(_counter.pushIdentifier) for { time.Sleep(_counter.interval) _counter.counter += 1 _counter.pushChannel <- Event{Payload: []byte(fmt.Sprintf("%d\n", _counter.counter))} } }
func (_router *router) handleEvent(_event Event) { transcript.TraceDebugging("routing event...") for _, _channel := range _router.outboundChannels { select { case _channel <- _event: default: transcript.TraceWarning("channel buffer overflow...") } } }
func (_router *router) handleRegisterOutbound(_inputs interface{}) (interface{}, error) { _channel, _ok := _inputs.(OutboundChannel) if !_ok || (_channel == nil) { return nil, errors.New("invalid inputs") } transcript.TraceDebugging("registering router outbound channel...") _router.channelCounter += 1 _identifier := _router.channelCounter _router.outboundChannels[_identifier] = _channel return _identifier, nil }
func (_router *router) handleCreateInbound(_inputs interface{}) (interface{}, error) { if _inputs != nil { return nil, errors.New("invalid inputs") } transcript.TraceDebugging("creating router inbound channel...") _router.channelCounter += 1 _identifier := _router.channelCounter _channel := make(chan Event, 1024) _router.inboundChannels[_identifier] = _channel go _router.pipeEvents(_identifier, _channel) return channelOutboundPair{_identifier, _channel}, nil }
func New() *router { transcript.TraceDebugging("creating router...") _router := &router{ operations: make(chan call), events: make(chan Event), inboundChannels: make(map[channelIdentifier]chan Event, 4), outboundChannels: make(map[channelIdentifier]OutboundChannel, 4), channelCounter: 0, } go _router.loop() return _router }
func (_looper *Looper) loop() { transcript.TraceDebugging("running looper...") _looper.pullChannel = make(chan Event, 1024) _looper.pullIdentifier = _looper.router.RegisterOutbound(_looper.pullChannel) defer _looper.router.UnregisterOutbound(_looper.pullIdentifier) _looper.pushIdentifier, _looper.pushChannel = _looper.router.CreateInbound() defer _looper.router.UnregisterInbound(_looper.pushIdentifier) for { _event := <-_looper.pullChannel _looper.pushChannel <- _event } }
func (_consumer *Consumer) loop() { transcript.TraceDebugging("running consumer...") defer C.pxmq_inbound_channel_destroy(_consumer.backend) _consumer.pushIdentifier, _consumer.pushChannel = _consumer.router.CreateInbound() defer _consumer.router.UnregisterInbound(_consumer.pushIdentifier) _buffer := make([]byte, 32*1024) _bufferCapacity := C.size_t(cap(_buffer)) for { transcript.TraceDebugging("waiting publisher action...") var _bufferSize C.size_t _outcome := C.pxmq_inbound_channel_pull(_consumer.backend, unsafe.Pointer(&_buffer[0]), _bufferCapacity, &_bufferSize, 1000) _error := syscall.Errno(_outcome) if (_error != 0) && (_error != syscall.EAGAIN) { panic(errors.New("backend pull failed: " + syscall.Errno(_outcome).Error())) } if _error == 0 { _payload := make([]byte, _bufferSize) os.Stdout.Write(_payload) copy(_payload, _buffer) _consumer.pushChannel <- Event{Payload: _payload} } } }
func (_router *router) loop() { transcript.TraceDebugging("running router...") for { transcript.TraceDebugging("waiting router action...") select { case _call := <-_router.operations: var _outputs interface{} var _error error switch _call.operation { case createInbound: _outputs, _error = _router.handleCreateInbound(_call.inputs) case unregisterInbound: _outputs, _error = _router.handleUnregisterInbound(_call.inputs) case registerOutbound: _outputs, _error = _router.handleRegisterOutbound(_call.inputs) case unregisterOutbound: _outputs, _error = _router.handleUnregisterOutbound(_call.inputs) } _call.callback <- callOutcome{_outputs, _error} case _event := <-_router.events: _router.handleEvent(_event) } } }
func (_server *Streamer) ServeHTTP(_writer http.ResponseWriter, _request *http.Request) { transcript.TraceDebugging("serving stream...") _pullChannel := make(chan Event, 1024) _pullIdentifier := _server.router.RegisterOutbound(_pullChannel) defer _server.router.UnregisterOutbound(_pullIdentifier) _headers := _writer.Header() _headers.Set("Content-Type", "text/plain") _writer.WriteHeader(http.StatusOK) for { _event := <-_pullChannel _, _error := _writer.Write(_event.Payload) if _error != nil { transcript.TraceError("error encountered while serving stream: `%s`; closing...", _error.Error()) return } _writer.(http.Flusher).Flush() } }