score interface function seperate another go file
authorjaehoon.hyun <jaehoon.hyun@samsung.com>
Tue, 2 Apr 2019 10:08:16 +0000 (19:08 +0900)
committerjaehoon.hyun <jaehoon.hyun@samsung.com>
Tue, 2 Apr 2019 10:08:16 +0000 (19:08 +0900)
src/scoringmgr/score.go [new file with mode: 0644]

diff --git a/src/scoringmgr/score.go b/src/scoringmgr/score.go
new file mode 100644 (file)
index 0000000..d516a15
--- /dev/null
@@ -0,0 +1,98 @@
+package scoringmgr
+
+import (
+  "strings"
+  "errors"
+  "strconv"
+  "encoding/json"
+  "net"
+)
+
+
+// GetScore is getting score of device
+func GetScore(target string, name string) (scoreValue float64, err error) {
+       if strings.Compare(target, getOutboundIP()) == 0 || strings.Compare(target, ConstLocalTarget) == 0 {
+               scoreValue, err = getScoreLocalEnv(name)
+       } else {
+               scoreValue, err = getScoreRemoteEnv(target, name)
+       }
+
+       DLog.Println("scoreValue", scoreValue)
+
+       return
+}
+
+// func getScore(name string) (score float64, err error) {
+//     handlerObj := handlers.table[name]
+
+//     if handlerObj == nil {
+//             err = errors.New("Invalid Service Name")
+//             return
+//     }
+
+//     score = handlerObj.scoreValue
+
+//     return
+// }
+
+func getScoreLocalEnv(name string) (scoreValue float64, err error) {
+       // scoreChan, err := getScore(name)
+       // if err != nil {
+       //      return
+       // }
+
+       // select {
+       // case scoreValue = <-scoreChan:
+       //      DLog.Println(scoreValue)
+       // }
+       // return
+
+  handlerObj := handlers.table[name]
+
+       if handlerObj == nil {
+               err = errors.New("Invalid Library Name")
+               return
+       }
+
+       scoreValue = handlerObj.scoreValue
+  
+  return
+}
+
+func getScoreRemoteEnv(target string, name string) (scoreValue float64, err error) {
+       targetURL := ConstPrefixHTTP + target + ":" + strconv.Itoa(ConstWellknownPort) + "/api/v1/scoringmgr/score/" + name
+
+       respBytes, err := 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)
+       }
+
+       return
+}
+
+func getOutboundIP() string {
+       conn, err := net.Dial("udp", "8.8.8.8:80")
+       if err != nil {
+               ELog.Fatal(err)
+       }
+       defer conn.Close()
+
+       localAddr := conn.LocalAddr().(*net.UDPAddr)
+
+       return localAddr.IP.String()
+}
+
+func checkError(err error) bool {
+       if err != nil {
+               ELog.Println(err.Error())
+               return true
+       }
+       return false
+}