package service

import (
	"fmt"
	"net"
	"time"

	netElementService "ems.agt/src/modules/net_element/service"
)

// 实例化服务层 TcpdumpImpl 结构体
var NewTcpdumpImpl = &TcpdumpImpl{
	neInfoService: netElementService.NewNeInfoImpl,
}

// 通用请求 服务层处理
type TcpdumpImpl struct {
	// 网元信息服务
	neInfoService netElementService.INeInfo
}

// UPFTelnetStart UPF进行telnet抓包
func (s *TcpdumpImpl) UPFTelnet(neIp, cmdStr string) (string, error) {
	// 创建TCP连接
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", neIp, 5002))
	if err != nil {
		return "", err
	}
	defer conn.Close()

	fmt.Fprintln(conn, cmdStr)

	// 读取内容
	time.Sleep(time.Duration(300) * time.Millisecond)
	buf := make([]byte, 1024*8)
	n, err := conn.Read(buf)
	if err != nil {
		return "", err
	}
	return string(buf[0:n]), nil
}
