X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=main.go;h=fd1480e8237d6436deaae6ddb8ffccc9ddec09e2;hb=1efdb28f1bf9246004a7b145e8d91d89be785772;hp=ba445f5eb39e9e3b410849573abe394bd9c2498b;hpb=c07adb807c41a1545a9a0f5bbf40080d86946538;p=src%2Fxds%2Fxds-server.git diff --git a/main.go b/main.go index ba445f5..fd1480e 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" + "github.com/iotbzh/xds-server/lib/crosssdk" "github.com/iotbzh/xds-server/lib/model" "github.com/iotbzh/xds-server/lib/syncthing" "github.com/iotbzh/xds-server/lib/webserver" @@ -39,15 +40,17 @@ var AppSubVersion = "unknown-dev" // Context holds the XDS server context type Context struct { - ProgName string - Cli *cli.Context - Config *xdsconfig.Config - Log *logrus.Logger - SThg *st.SyncThing - SThgCmd *exec.Cmd - MFolder *model.Folder - WWWServer *webserver.ServerService - Exit chan os.Signal + ProgName string + Cli *cli.Context + Config *xdsconfig.Config + Log *logrus.Logger + SThg *st.SyncThing + SThgCmd *exec.Cmd + SThgInotCmd *exec.Cmd + MFolder *model.Folder + SDKs *crosssdk.SDKs + WWWServer *webserver.Server + Exit chan os.Signal } // NewContext Create a new instance of XDS server @@ -86,9 +89,10 @@ func NewContext(cliCtx *cli.Context) *Context { func handlerSigTerm(ctx *Context) { <-ctx.Exit if ctx.SThg != nil { - ctx.Log.Infof("Stopping Syncthing... (PID %d)", - ctx.SThgCmd.Process.Pid) + ctx.Log.Infof("Stoping Syncthing... (PID %d)", ctx.SThgCmd.Process.Pid) ctx.SThg.Stop() + ctx.Log.Infof("Stoping Syncthing-inotify... (PID %d)", ctx.SThgInotCmd.Process.Pid) + ctx.SThg.StopInotify() } if ctx.WWWServer != nil { ctx.Log.Infof("Stoping Web server...") @@ -97,7 +101,7 @@ func handlerSigTerm(ctx *Context) { os.Exit(1) } -// xdsServer main routine +// XDS Server application main routine func xdsApp(cliCtx *cli.Context) error { var err error @@ -128,16 +132,26 @@ func xdsApp(cliCtx *cli.Context) error { if err != nil { return cli.NewExitError(err, 2) } - ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid) + fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid) + + ctx.Log.Infof("Starting Syncthing-inotify...") + ctx.SThgInotCmd, err = ctx.SThg.StartInotify() + if err != nil { + return cli.NewExitError(err, 2) + } + fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid) // Establish connection with local Syncthing (retry if connection fail) - retry := 10 + fmt.Printf("Establishing connection with Syncthing...\n") + time.Sleep(2 * time.Second) + maxRetry := 30 + retry := maxRetry err = nil for retry > 0 { if err = ctx.SThg.Connect(); err == nil { break } - ctx.Log.Warningf("Establishing connection to Syncthing (retry %d/10)", retry) + ctx.Log.Warningf("Establishing connection to Syncthing (retry %d/%d)", retry, maxRetry) time.Sleep(time.Second) retry-- } @@ -156,16 +170,25 @@ func xdsApp(cliCtx *cli.Context) error { } // Retrieve initial Syncthing config + + // FIXME: cannot retrieve default SDK, need to save on disk or somewhere + // else all config to be able to restore it. + defaultSdk := "" stCfg, err := ctx.SThg.ConfigGet() if err != nil { return cli.NewExitError(err, 2) } for _, stFld := range stCfg.Folders { - relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.ShareRootDir) + relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.FileConf.ShareRootDir) if relativePath == "" { relativePath = stFld.RawPath } - newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.Trim(relativePath, "/")) + + newFld := xdsconfig.NewFolderConfig(stFld.ID, + stFld.Label, + ctx.Config.FileConf.ShareRootDir, + strings.TrimRight(relativePath, "/"), + defaultSdk) ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld}) } @@ -177,8 +200,14 @@ func xdsApp(cliCtx *cli.Context) error { return cli.NewExitError(err, 3) } + // Init cross SDKs + ctx.SDKs, err = crosssdk.Init(ctx.Config, ctx.Log) + if err != nil { + return cli.NewExitError(err, 2) + } + // Create and start Web Server - ctx.WWWServer = webserver.NewServer(ctx.Config, ctx.MFolder, ctx.Log) + ctx.WWWServer = webserver.New(ctx.Config, ctx.MFolder, ctx.SDKs, ctx.Log) if err = ctx.WWWServer.Serve(); err != nil { ctx.Log.Println(err) return cli.NewExitError(err, 3)