/*******************************************************************************
- * Copyright 2019 Samsung Electronics All Rights Reserved.
- *
- * 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.
- *
- *******************************************************************************/
+* Copyright 2019 Samsung Electronics All Rights Reserved.
+*
+* 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 scoringmgr
import (
"common"
- "encoding/json"
"errors"
"log"
- "net/http"
"restapi/httpclient"
- "strconv"
"strings"
)
if strings.Compare(target, outboundIP) == 0 || strings.Compare(target, httpclient.ConstLocalTarget) == 0 {
scoreValue, err = getScoreLocalEnv(name)
} else {
- scoreValue, err = getScoreRemoteEnv(target, name)
+ scoreValue, err = httpclient.DoGetScoreRemoteDevice(name, target)
}
log.Println("[scoringmgr] scoreValue", scoreValue)
return
}
-
-func getScoreRemoteEnv(target string, name string) (scoreValue float64, err error) {
- targetURL := httpclient.ConstPrefixHTTP + target + ":" + strconv.Itoa(httpclient.ConstWellknownPort) + "/api/v1/scoringmgr/score/" + name
- respBytes, statusCode, err := httpclient.DoGet(targetURL)
- if checkError(err) == true {
- return scoreValue, err
- }
-
- var responseMsg map[string]interface{}
- err = json.Unmarshal(respBytes, &responseMsg)
- if err == nil {
- scoreValue = responseMsg["ScoreValue"].(float64)
- }
- if statusCode != http.StatusOK {
- log.Println(statusCode)
- }
-
- return
-}
-
-func checkError(err error) bool {
- if err != nil {
- log.Println(err.Error())
- return true
- }
- return false
-}
/*******************************************************************************
- * Copyright 2019 Samsung Electronics All Rights Reserved.
- *
- * 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 scoringmgr_test
-
- //$ touch resourceservice/resourceservice.c
-
- import (
- "context"
- "fmt"
- "log"
- "net/http"
- "testing"
- "time"
-
- "gopkg.in/sconf/ini.v0"
- "gopkg.in/sconf/sconf.v0"
-
- confdescription "configuremgr/description"
- restapi "restapi/v1"
- scoringmgr "scoringmgr"
- mockscoringmgr "scoringmgr/mock"
- )
-
- func TestScoringMgrMyScoringLibRemoveApp(t *testing.T) {
-
- //interface
- iscoringmgr := scoringmgr.Init()
- iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
- iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
- iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
- iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
- iscoringmgr.Ch = make(chan interface{}, 1024)
-
- //start
- iscoringmgr.Listening()
- iscoringmgr.IStartResourceService()
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- //setting
- libName := "myscoring"
- libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
- confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
- cfg := new(confdescription.Doc)
- sconf.Must(cfg).Read(ini.File(confPath))
- log.Println(cfg)
- scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
-
- //working on
- time.Sleep(time.Duration(3) * time.Second)
- scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
- log.Printf("scorevalue : %f\n", scoreValue)
-
- //release
- iscoringmgr.IStopResourceService()
- iscoringmgr.RemoveApp(cfg.ServiceInfo.ServiceName)
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- }
-
- func TestScoringMgrMyScoringLib(t *testing.T) {
-
- //interface
- iscoringmgr := scoringmgr.Init()
- iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
- iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
- iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
- iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
- iscoringmgr.Ch = make(chan interface{}, 1024)
-
- //start
- iscoringmgr.Listening()
- iscoringmgr.IStartResourceService()
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- //setting
- libName := "myscoring"
- libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
- confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
- cfg := new(confdescription.Doc)
- sconf.Must(cfg).Read(ini.File(confPath))
- log.Println("[scoringmgr]", cfg)
- scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
-
- //working on
- time.Sleep(time.Duration(3) * time.Second)
- scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
- log.Printf("[scoringmgr] scorevalue : %f\n", scoreValue)
-
- //release
- iscoringmgr.IStopResourceService()
- iscoringmgr.RemoveAll()
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- }
-
- func TestScoringMgrMyScoringLibGetScoreLocal(t *testing.T) {
-
- //interface
- iscoringmgr := scoringmgr.Init()
- iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
- iscoringmgr.IGetScore = scoringmgr.GetScore
- iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
- iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
- iscoringmgr.Ch = make(chan interface{}, 1024)
-
- //start
- iscoringmgr.Listening()
- iscoringmgr.IStartResourceService()
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- //setting
- libName := "myscoring"
- libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
- confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
- cfg := new(confdescription.Doc)
- sconf.Must(cfg).Read(ini.File(confPath))
- log.Println(cfg)
- scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
-
- //working on
- time.Sleep(time.Duration(3) * time.Second)
- scoreValue, _ := iscoringmgr.IGetScore("localhost", "myscoring")
- log.Printf("scorevalue : %f\n", scoreValue)
-
- //release
- iscoringmgr.IStopResourceService()
- iscoringmgr.RemoveAll()
-
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- }
-
- func TestScoringMgrMyScoringLibGetScoreRemote(t *testing.T) {
-
- //interface
- iscoringmgr := scoringmgr.Init()
- iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
- iscoringmgr.IGetScore = scoringmgr.GetScore
- iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
- iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
- iscoringmgr.Ch = make(chan interface{}, 1024)
-
- //start
- iscoringmgr.Listening()
- iscoringmgr.IStartResourceService()
- router := restapi.NewRouter()
- server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
- go server.ListenAndServe()
- time.Sleep(time.Duration(100) * time.Millisecond)
-
- //setting
- libName := "myscoring"
- libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
- confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
- cfg := new(confdescription.Doc)
- sconf.Must(cfg).Read(ini.File(confPath))
- log.Println(cfg)
- scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
-
- //working on
- time.Sleep(time.Duration(3) * time.Second)
- scoreValue, _ := iscoringmgr.IGetScore("127.0.0.1", "myscoring")
- log.Printf("scorevalue : %f\n", scoreValue)
-
- //release
- iscoringmgr.IStopResourceService()
- iscoringmgr.RemoveAll()
- if err := server.Shutdown(context.TODO()); err != nil {
- panic(err)
- }
-
- time.Sleep(time.Duration(100) * time.Millisecond)
-
-}
\ No newline at end of file
+* Copyright 2019 Samsung Electronics All Rights Reserved.
+*
+* 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 scoringmgr_test
+
+//$ touch resourceservice/resourceservice.c
+
+import (
+ "fmt"
+ "log"
+ "testing"
+ "time"
+
+ "gopkg.in/sconf/ini.v0"
+ "gopkg.in/sconf/sconf.v0"
+
+ confdescription "configuremgr/description"
+ scoringmgr "scoringmgr"
+ mockscoringmgr "scoringmgr/mock"
+)
+
+func TestScoringMgrMyScoringLibRemoveApp(t *testing.T) {
+
+ //interface
+ iscoringmgr := scoringmgr.Init()
+ iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+ iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
+ iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+ iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+ iscoringmgr.Ch = make(chan interface{}, 1024)
+
+ //start
+ iscoringmgr.Listening()
+ iscoringmgr.IStartResourceService()
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+ //setting
+ libName := "myscoring"
+ libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+ confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
+ cfg := new(confdescription.Doc)
+ sconf.Must(cfg).Read(ini.File(confPath))
+ log.Println(cfg)
+ scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
+
+ //working on
+ time.Sleep(time.Duration(3) * time.Second)
+ scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
+ log.Printf("scorevalue : %f\n", scoreValue)
+
+ //release
+ iscoringmgr.IStopResourceService()
+ iscoringmgr.RemoveApp(cfg.ServiceInfo.ServiceName)
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+}
+
+func TestScoringMgrMyScoringLib(t *testing.T) {
+
+ //interface
+ iscoringmgr := scoringmgr.Init()
+ iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+ iscoringmgr.IGetScore = mockscoringmgr.GetScoreRandom100Mock
+ iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+ iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+ iscoringmgr.Ch = make(chan interface{}, 1024)
+
+ //start
+ iscoringmgr.Listening()
+ iscoringmgr.IStartResourceService()
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+ //setting
+ libName := "myscoring"
+ libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+ confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
+ cfg := new(confdescription.Doc)
+ sconf.Must(cfg).Read(ini.File(confPath))
+ log.Println("[scoringmgr]", cfg)
+ scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
+
+ //working on
+ time.Sleep(time.Duration(3) * time.Second)
+ scoreValue, _ := iscoringmgr.IGetScore("mock", "mock")
+ log.Printf("[scoringmgr] scorevalue : %f\n", scoreValue)
+
+ //release
+ iscoringmgr.IStopResourceService()
+ iscoringmgr.RemoveAll()
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+}
+
+func TestScoringMgrMyScoringLibGetScoreLocal(t *testing.T) {
+
+ //interface
+ iscoringmgr := scoringmgr.Init()
+ iscoringmgr.IRunningScore = scoringmgr.LoadScoringGeneralInterface
+ iscoringmgr.IGetScore = scoringmgr.GetScore
+ iscoringmgr.IStartResourceService = scoringmgr.StartResourceService
+ iscoringmgr.IStopResourceService = scoringmgr.StopResourceService
+ iscoringmgr.Ch = make(chan interface{}, 1024)
+
+ //start
+ iscoringmgr.Listening()
+ iscoringmgr.IStartResourceService()
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+ //setting
+ libName := "myscoring"
+ libPath := fmt.Sprintf("mock/%s/lib%s.so", libName, libName)
+ confPath := fmt.Sprintf("mock/%s/%s.conf", libName, libName)
+ cfg := new(confdescription.Doc)
+ sconf.Must(cfg).Read(ini.File(confPath))
+ log.Println(cfg)
+ scoringmgr.PushLibPath(libPath, cfg, iscoringmgr.Ch)
+
+ //working on
+ time.Sleep(time.Duration(3) * time.Second)
+ scoreValue, _ := iscoringmgr.IGetScore("localhost", "myscoring")
+ log.Printf("scorevalue : %f\n", scoreValue)
+
+ //release
+ iscoringmgr.IStopResourceService()
+ iscoringmgr.RemoveAll()
+
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+}