封装发送MML
This commit is contained in:
88
lib/core/mml_client/mml_client.go
Normal file
88
lib/core/mml_client/mml_client.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package mmlclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"ems.agt/lib/core/conf"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 定义MMLClient结构体
|
||||||
|
type MMLClient struct {
|
||||||
|
awaitTime time.Duration // 等待时间
|
||||||
|
conn net.Conn
|
||||||
|
reader *bufio.Reader
|
||||||
|
size int // 包含字符
|
||||||
|
}
|
||||||
|
|
||||||
|
// 封装NewMMLClient函数,用于创建MMLClient实例
|
||||||
|
// 网元UDM的IP地址 "198.51.100.1"
|
||||||
|
func NewMMLClient(ip string) (*MMLClient, error) {
|
||||||
|
// 创建TCP连接
|
||||||
|
portMML := conf.Get("mml.port").(int)
|
||||||
|
hostMML := fmt.Sprintf("%s:%d", ip, portMML)
|
||||||
|
conn, err := net.Dial("tcp", hostMML)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 进行登录
|
||||||
|
usernameMML := conf.Get("mml.user").(string)
|
||||||
|
passwordMML := conf.Get("mml.password").(string)
|
||||||
|
fmt.Fprintln(conn, usernameMML)
|
||||||
|
fmt.Fprintln(conn, passwordMML)
|
||||||
|
|
||||||
|
// 发送后等待
|
||||||
|
sleepTime := conf.Get("mml.sleep").(int)
|
||||||
|
awaitTime := time.Duration(sleepTime) * time.Millisecond
|
||||||
|
time.Sleep(awaitTime)
|
||||||
|
|
||||||
|
// 读取内容
|
||||||
|
buf := make([]byte, 1024*8)
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建MMLClient实例
|
||||||
|
client := &MMLClient{
|
||||||
|
conn: conn,
|
||||||
|
reader: bufio.NewReader(conn),
|
||||||
|
awaitTime: awaitTime,
|
||||||
|
size: n,
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 封装Send函数,用于向TCP连接发送数据
|
||||||
|
func (c *MMLClient) Send(msg string) error {
|
||||||
|
_, err := fmt.Fprintln(c.conn, msg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
time.Sleep(c.awaitTime)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 封装Receive函数,用于从TCP连接中接收数据
|
||||||
|
func (c *MMLClient) Receive() (string, error) {
|
||||||
|
buf := make([]byte, 1024*8)
|
||||||
|
n, err := c.reader.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return "", fmt.Errorf("server closed the connection")
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(buf[0:n]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 封装Close函数,用于关闭TCP连接
|
||||||
|
func (c *MMLClient) Close() error {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
57
lib/core/mml_client/send.go
Normal file
57
lib/core/mml_client/send.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package mmlclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 发送MML
|
||||||
|
// ip 网元IP地址
|
||||||
|
// msg 指令
|
||||||
|
func MMLSendMsgToString(ip, msg string) (string, error) {
|
||||||
|
// 创建MMLClient实例
|
||||||
|
client, err := NewMMLClient(ip)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("创建MMLClient实例失败:%v", err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
// 发送数据
|
||||||
|
err = client.Send(msg)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("发送数据失败:%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接收数据
|
||||||
|
data, err := client.Receive()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("接收数据失败:%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送MML
|
||||||
|
// ip 网元IP地址
|
||||||
|
// msg 指令
|
||||||
|
func MMLSendMsgToMap(ip, msg string) (map[string]string, error) {
|
||||||
|
// 发送获取数据
|
||||||
|
str, err := MMLSendMsgToString(ip, msg)
|
||||||
|
|
||||||
|
// 初始化一个map用于存储拆分后的键值对
|
||||||
|
m := make(map[string]string)
|
||||||
|
|
||||||
|
// 按照分隔符"\r\n"进行拆分
|
||||||
|
items := strings.Split(str, "\r\n")
|
||||||
|
|
||||||
|
// 遍历拆分后的结果
|
||||||
|
for _, item := range items {
|
||||||
|
// 按照分隔符"="进行拆分键值对
|
||||||
|
pair := strings.Split(item, "=")
|
||||||
|
if len(pair) == 2 {
|
||||||
|
// 将键值对存入map中
|
||||||
|
m[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user