Fixed XDS Server connection lost and reconnection.
[src/xds/xds-agent.git] / lib / agent / projects.go
index 1d21931..a2d8fe1 100644 (file)
@@ -1,14 +1,34 @@
+/*
+ * Copyright (C) 2017 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package agent
 
 import (
        "fmt"
        "log"
+       "os"
+       "path/filepath"
        "strings"
        "time"
 
        "github.com/franciscocpg/reflectme"
        "github.com/iotbzh/xds-agent/lib/syncthing"
        "github.com/iotbzh/xds-agent/lib/xaapiv1"
+       common "github.com/iotbzh/xds-common/golib"
        "github.com/iotbzh/xds-server/lib/xsapiv1"
        "github.com/syncthing/syncthing/lib/sync"
 )
@@ -121,12 +141,30 @@ func (p *Projects) GetProjectArrUnsafe() []xaapiv1.ProjectConfig {
 }
 
 // Add adds a new folder
-func (p *Projects) Add(newF xaapiv1.ProjectConfig, fromSid string) (*xaapiv1.ProjectConfig, error) {
-       prj, err := p.createUpdate(newF, true, false)
+func (p *Projects) Add(newP xaapiv1.ProjectConfig, fromSid, requestURL string) (*xaapiv1.ProjectConfig, error) {
+       prj, err := p.createUpdate(newP, true, false)
        if err != nil {
                return prj, err
        }
 
+       // Create xds-project.conf file
+       prjConfFile := filepath.Join(prj.ClientPath, "xds-project.conf")
+       if !common.Exists(prjConfFile) {
+               fd, err := os.OpenFile(prjConfFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
+               if err != nil {
+                       return prj, fmt.Errorf("Cannot create xds-project.conf: %v", err.Error())
+               }
+               fd.WriteString("# XDS project settings\n")
+               fd.WriteString("export XDS_AGENT_URL=" + requestURL + "\n")
+               fd.WriteString("export XDS_PROJECT_ID=" + prj.ID + "\n")
+               if prj.DefaultSdk != "" {
+                       fd.WriteString("export XDS_SDK_ID=" + prj.DefaultSdk + "\n")
+               } else {
+                       fd.WriteString("#export XDS_SDK_ID=???\n")
+               }
+               fd.Close()
+       }
+
        // Notify client with event
        if err := p.events.Emit(xaapiv1.EVTProjectAdd, *prj, fromSid); err != nil {
                p.Log.Warningf("Cannot notify project deletion: %v", err)
@@ -210,10 +248,12 @@ func (p *Projects) createUpdate(newF xaapiv1.ProjectConfig, create bool, initial
 
        // Force sync after creation
        // (need to defer to be sure that WS events will arrive after HTTP creation reply)
-       go func() {
-               time.Sleep(time.Millisecond * 500)
-               fld.Sync()
-       }()
+       if create {
+               go func() {
+                       time.Sleep(time.Millisecond * 500)
+                       fld.Sync()
+               }()
+       }
 
        return newPrj, nil
 }