Exemple #1
0
// NewInputFromConfig creates a new Input instance for a given configuration.
func NewInputFromConfig(cfg *config.Config) (i *Input, err error) {
	m, err := manager.New(cfg)
	if err != nil {
		return
	}
	i = &Input{
		m:       m,
		cfg:     cfg,
		r:       eventual2go.NewReactor(),
		listen:  map[string]interface{}{},
		logger:  log.New(cfg.Logger(), fmt.Sprintf("%s INPUT ", cfg.UUID()), 0),
		results: &messages.ResultStream{m.MessagesOfType(messages.RESULT).Transform(connection.ToMessage)},
	}

	i.logger.Println("Launching with tagset", cfg.Tags())

	i.r.React(connectionEvent{}, i.onConnection)
	i.r.AddStream(connectionEvent{}, m.Connected().Stream)

	i.r.React(arriveEvent{}, i.sendListenFunctions)
	i.r.AddStream(arriveEvent{}, m.PeerArrive().Stream)

	i.r.React(startListenEvent{}, i.startListen)
	i.r.React(stopListenEvent{}, i.stopListen)

	return
}
// NewOutputFromConfig creates a new Output instance for a given configuration.
func NewOutputFromConfig(cfg *config.Config) (o *Output, err error) {
	m, err := manager.New(cfg)
	o = &Output{
		cfg:       cfg,
		m:         m,
		requests:  &messages.RequestStream{m.MessagesOfType(messages.REQUEST).Transform(connection.ToMessage)},
		listeners: map[string]map[config.UUID]interface{}{},
		logger:    log.New(cfg.Logger(), fmt.Sprintf("%s OUTPUT ", cfg.UUID()), 0),
		r:         eventual2go.NewReactor(),
	}

	o.logger.Println("Launching with tagset", cfg.Tags())

	o.r.React(startListenEvent{}, o.onListen)
	o.r.AddStream(startListenEvent{}, m.MessagesOfType(messages.LISTEN).Stream)

	o.r.React(stopListenEvent{}, o.onStopListen)
	o.r.AddStream(stopListenEvent{}, m.MessagesOfType(messages.STOPLISTEN).Stream)

	o.r.React(leaveEvent{}, o.onPeerGone)

	o.r.React(replyEvent{}, o.deliverResult)
	return
}