func getClient(ctx *cli.Context) *client.Client { opt := client.Option{ TransporterCallback: func() (net.Conn, error) { var conn net.Conn var err error if ctx.Bool("websocket") { origin := ctx.String("origin") conn, err = websocket.Dial(ctx.String("url"), "", origin) } else { conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", ctx.String("host"), ctx.Int("port"))) } return conn, err }, Keepalive: 0, Magic: []byte("MQTT"), Version: 4, } opt.UserName = ctx.String("u,user") opt.Password = ctx.String("P,password") return client.NewClient(opt) }
func NewApplication(configPath string) *Application { conf, err := configuration.LoadConfiguration(configPath) if err != nil { log.Error("Can't read config.toml. use default setting.: %s", err) } log.SetupLogging(conf.Server.LogLevel, conf.Server.LogFile) pid := strconv.Itoa(os.Getpid()) if conf.Server.PidFile != "" { if err := ioutil.WriteFile(conf.Server.PidFile, []byte(pid), 0644); err != nil { panic(err) } util.WritePid(conf.Server.PidFile) } // NOTE: INHERIT=TRUE means the process invoked from os.StartProess inherit := false if os.Getenv("INHERIT") == "TRUE" { inherit = true } log.Info("Momonga started pid: %s (inherit:%t)", pid, inherit) engine := NewMomonga(conf) app := &Application{ Engine: engine, Servers: []Server{}, configPath: configPath, config: conf, } // TODO: improve this block if conf.Server.Port > 0 { t := NewTcpServer(engine, conf, inherit) t.wg = &app.wg app.RegisterServer(t) } if conf.Server.Socket != "" { u := NewUnixServer(engine, conf, inherit) u.wg = &app.wg app.RegisterServer(u) } if conf.Server.HttpPort > 0 { h := NewHttpServer(engine, conf, inherit) h.wg = &app.wg app.RegisterServer(h) } if conf.Server.EnableTls && conf.Server.TlsPort > 0 { h := NewTlsServer(engine, conf, inherit) h.wg = &app.wg app.RegisterServer(h) } // memonized application path app.execPath, err = exec.LookPath(os.Args[0]) if err != nil { log.Error("Error: %s", err) return app } app.workingDir, err = os.Getwd() if err != nil { log.Error("Error: %s", err) return app } if conf.Bridge.Address != "" { //TODO: Bridgeは複数指定できる //TODO: Bridgeは途中で制御できる // /api/bridge/list // /api/bridge/connection/stop // /api/bridge/connection/status // /api/bridge/connection/start // /api/bridge/connection/delete // /api/bridge/connection/new?address=&port=&type=both&topic[]= // /api/bridge/config go func() { flag := 0 switch conf.Bridge.Type { case "both": flag = 3 case "out": flag = 1 case "in": flag = 2 default: panic(fmt.Sprintf("%s does not support.", conf.Bridge.Type)) } // in addr := fmt.Sprintf("%s:%d", conf.Bridge.Address, conf.Bridge.Port) c := client.NewClient(client.Option{ TransporterCallback: func() (net.Conn, error) { conn, err := net.Dial("tcp", addr) return conn, err }, Identifier: fmt.Sprintf(conf.Bridge.ClientId), Magic: []byte("MQTT"), Version: 4 | 0x80, Keepalive: 0, }) c.Connect() c.WaitConnection() if flag == 1 || flag == 3 { c.Subscribe("#", 2) c.SetRequestPerSecondLimit(-1) c.On("publish", func(msg *codec.PublishMessage) { engine.SendPublishMessage(msg, conf.Bridge.Connection, true) }) } //out if flag == 2 || flag == 3 { addr2 := fmt.Sprintf("%s:%d", conf.Server.BindAddress, conf.Server.Port) c2 := client.NewClient(client.Option{ TransporterCallback: func() (net.Conn, error) { conn, err := net.Dial("tcp", addr2) return conn, err }, Identifier: fmt.Sprintf(conf.Bridge.ClientId), Magic: []byte("MQTT"), Version: 4 | 0x80, Keepalive: 0, }) c2.Connect() c2.WaitConnection() c2.Subscribe("#", 2) c2.SetRequestPerSecondLimit(-1) c2.On("publish", func(msg *codec.PublishMessage) { c.Publish(msg.TopicName, msg.Payload, msg.QosLevel) }) } select {} }() } return app }