Ejemplo n.º 1
0
func writeLoop(client client.Client) {
	shouldStop := false
	for !shouldStop {
		func() {
			defer protocol.PanicLogger()
			reader := bufio.NewReader(os.Stdin)
			text, err := reader.ReadString('\n')
			if err != nil {
				return
			}
			if strings.TrimSpace(text) == "" {
				return
			}

			if strings.TrimSpace(text) == "?" || strings.TrimSpace(text) == "help" {
				printHelp()
				return
			}

			if strings.HasPrefix(text, ">") {
				fmt.Print("header: ")
				header, err := reader.ReadString('\n')
				if err != nil {
					return
				}
				text += header
				fmt.Print("body: ")
				body, err := reader.ReadString('\n')
				if err != nil {
					return
				}
				text += strings.TrimSpace(body)
			}

			if *verbose {
				log.Printf("Sending: %v\n", text)
			}
			if err := client.WriteRawMessage([]byte(text)); err != nil {
				shouldStop = true
				logger.WithError(err).Error("Error on Writing  message")
			}
		}()
	}
}
Ejemplo n.º 2
0
func (router *router) Start() error {
	router.panicIfInternalDependenciesAreNil()
	logger.Info("Starting router")
	resetRouterMetrics()

	router.wg.Add(1)
	router.setStopping(false)

	go func() {
		for {
			if router.stopping && router.channelsAreEmpty() {
				router.closeRoutes()
				router.wg.Done()
				return
			}

			func() {
				defer protocol.PanicLogger()

				select {
				case message := <-router.handleC:
					router.routeMessage(message)
					runtime.Gosched()
				case subscriber := <-router.subscribeC:
					router.subscribe(subscriber.route)
					subscriber.doneC <- true
				case unsubscriber := <-router.unsubscribeC:
					router.unsubscribe(unsubscriber.route)
					unsubscriber.doneC <- true
				case <-router.Done():
					router.setStopping(true)
				}
			}()
		}
	}()

	return nil
}