add: 提交
This commit is contained in:
120
lib/file/file.go
Normal file
120
lib/file/file.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ems.agt/lib/dborm"
|
||||
"ems.agt/lib/global"
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/lib/services"
|
||||
"ems.agt/restagent/config"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
const (
|
||||
//经过测试,linux下,延时需要大于100ms
|
||||
TIME_DELAY_AFTER_WRITE = 200
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Data []string `json:"data"`
|
||||
}
|
||||
|
||||
type MMLRequest struct {
|
||||
MML []string `json:"mml"`
|
||||
}
|
||||
|
||||
func GetFile(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debug("PostMMLToNF processing... ")
|
||||
|
||||
vars := mux.Vars(r)
|
||||
neType := vars["elementTypeValue"]
|
||||
params := r.URL.Query()
|
||||
neId := params["ne_id"]
|
||||
log.Debug("neType:", neType, "neId", neId)
|
||||
|
||||
neInfo := new(dborm.NeInfo)
|
||||
var err error
|
||||
if len(neId) == 0 {
|
||||
log.Error("ne_id NOT FOUND")
|
||||
services.ResponseBadRequest400WrongParamValue(w)
|
||||
return
|
||||
}
|
||||
neInfo, err = dborm.XormGetNeInfo(neType, neId[0])
|
||||
if err != nil {
|
||||
log.Error("dborm.XormGetNeInfo is failed:", err)
|
||||
services.ResponseInternalServerError500DatabaseOperationFailed(w)
|
||||
return
|
||||
}
|
||||
|
||||
var buf [8192]byte
|
||||
var n int
|
||||
var mmlResult []string
|
||||
|
||||
if neInfo != nil {
|
||||
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
|
||||
conn, err := net.Dial("tcp", hostMML)
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("Failed to dial %s: %v", hostMML, err)
|
||||
log.Error(errMsg)
|
||||
mmlResult = append(mmlResult, errMsg)
|
||||
response := Response{mmlResult}
|
||||
services.ResponseWithJson(w, http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
|
||||
loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password)
|
||||
n, err = conn.Write([]byte(loginStr))
|
||||
if err != nil {
|
||||
log.Errorf("Error: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
||||
|
||||
n, err = conn.Read(buf[0:])
|
||||
if err != nil {
|
||||
log.Errorf("Error: %s", err.Error())
|
||||
return
|
||||
}
|
||||
log.Debug(string(buf[0:n]))
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||
if err != nil {
|
||||
log.Error("io.ReadAll is failed:", err)
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
log.Debug("Body:", string(body))
|
||||
|
||||
mmlRequest := new(MMLRequest)
|
||||
_ = json.Unmarshal(body, mmlRequest)
|
||||
|
||||
for _, mml := range mmlRequest.MML {
|
||||
mmlCommand := fmt.Sprintf("%s\n", mml)
|
||||
log.Debug("mml command:", mmlCommand)
|
||||
n, err = conn.Write([]byte(mmlCommand))
|
||||
if err != nil {
|
||||
log.Errorf("Error: %s", err.Error())
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
||||
|
||||
n, err = conn.Read(buf[0:])
|
||||
if err != nil {
|
||||
log.Errorf("Error: %s", err.Error())
|
||||
return
|
||||
}
|
||||
log.Debug(string(buf[0 : n-len(neType)-2]))
|
||||
mmlResult = append(mmlResult, string(buf[0:n-len(neType)-2]))
|
||||
}
|
||||
}
|
||||
|
||||
response := Response{mmlResult}
|
||||
services.ResponseWithJson(w, http.StatusOK, response)
|
||||
}
|
||||
Reference in New Issue
Block a user