Esempio n. 1
0
func (app *App) Dial() sarif.Conn {
	cfg := sarif.NetConfig{
		Address: "tcp://localhost:" + sarif.DefaultPort,
	}
	app.Config.Get("dial", &cfg)
	app.WriteConfig()
	conn, err := sarif.Dial(&cfg)
	if err != nil {
		app.Log.Fatal(err)
	}
	return conn
}
Esempio n. 2
0
func (s *Server) InitBroker() error {
	if s.Broker != nil {
		return nil
	}

	sarif.SetDefaultLogger(s.Log)
	s.Broker = sarif.NewBroker()
	if s.Log.GetLevel() <= core.LogLevelTrace {
		s.Broker.TraceMessages(true)
	}

	s.Client = sarif.NewClient(s.ServerConfig.Name + "/sarifd")
	s.Client.Connect(s.Broker.NewLocalConn())
	s.Client.SetLogger(s.Log)

	cfg := &s.ServerConfig
	if _, ok := s.Config.Get("server", cfg); !ok {
		if len(cfg.Listen) == 0 {
			cfg.Listen = append(cfg.Listen, &sarif.NetConfig{
				Address: "tcp://localhost:23100",
				Auth:    sarif.AuthNone,
			})
			s.Config.Set("server", cfg)
		}
	}

	// Listen on connections
	for _, cfg := range cfg.Listen {
		go func(cfg *sarif.NetConfig) {
			s.Log.Infoln("[server] listening on", cfg.Address)
			s.Must(s.Broker.Listen(cfg))
		}(cfg)
	}

	// Setup bridges
	for _, cfg := range cfg.Bridges {
		go func(cfg *sarif.NetConfig) {
			for {
				s.Log.Infoln("[server] bridging to ", cfg.Address)
				conn, err := sarif.Dial(cfg)
				if err == nil {
					err = s.Broker.ListenOnBridge(conn)
				}
				s.Log.Errorln("[server] bridge error:", err)
				time.Sleep(5 * time.Second)
			}
		}(cfg)
	}

	// Setup gateways
	for _, cfg := range cfg.Gateways {
		go func(cfg *sarif.NetConfig) {
			for {
				s.Log.Infoln("[server] gateway to ", cfg.Address)
				conn, err := sarif.Dial(cfg)
				if err == nil {
					err = s.Broker.ListenOnGateway(conn)
				}
				s.Log.Errorln("[server] gateway error:", err)
				time.Sleep(5 * time.Second)
			}
		}(cfg)
	}

	return nil
}