diff --git a/.gitignore b/.gitignore index 7bda526..09d2b5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ jstester/node_modules encoding/tmp pdu/tmp +tmp diff --git a/client/client.go b/client/client.go index cadf9e3..af8b62a 100644 --- a/client/client.go +++ b/client/client.go @@ -1 +1,62 @@ -package client \ No newline at end of file +package client + +import ( + "fmt" + "log" + "net" + "smpptester/pdu" +) + +type SMPPClient struct { + Id int + conn net.Conn + port string + log log.Logger +} + +func NewSMPPClient(port string, id int) *SMPPClient { + client := &SMPPClient{ + Id: id, + port: port, + log: log.Logger{}, + } + client.log = *log.New(log.Writer(), "", log.LstdFlags) + client.log.SetPrefix(fmt.Sprintf("SMPP client %d: ", client.Id)) + return client +} + +func (c *SMPPClient) Connect() error { + conn, err := net.Dial("tcp", c.port) + if err != nil { + return fmt.Errorf("failed to connect to SMPP server: %w", err) + } + c.log.Printf("SMPP client %d connected to %s", c.Id, c.port) + c.conn = conn + return nil +} + +func (c *SMPPClient) Close() { + c.conn.Close() + c.log.Printf("SMPP client %d closed connection", c.Id) +} + +func (c *SMPPClient) Send(pdata pdu.PDU) error { + if c.conn == nil { + return fmt.Errorf("connection is not established") + } + + pdata.UpdateSize() + buf := pdu.ByteBufferPool.Get(pdata.Size()) + err := pdata.Encode(buf) + if err != nil { + return fmt.Errorf("failed to encode PDU: %w", err) + } + + _, err = c.conn.Write(buf.Bytes()) + if err != nil { + return fmt.Errorf("failed to send PDU: %w", err) + } + + c.log.Printf("SMPP client %d sent PDU: %+v", c.Id, pdata) + return nil +} \ No newline at end of file diff --git a/main.go b/main.go index 6c9df02..820be1d 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "bytes" "encoding/binary" "log" - "net" "sync" + "smpptester/client" "smpptester/pdu" ) @@ -166,47 +166,19 @@ func main() { buf := pdu.ByteBufferPool.Get(submit.Size()) defer pdu.ByteBufferPool.Put(buf) submit.Encode(buf) - log.Println(buf.Bytes()) - log.Println(createSubmitSMPDU()) - // log.Println(pdu.Encode()) - // serviceType := "CMT" - // sourceAddrTON := byte(0x01) - // sourceAddrNPI := byte(0x01) - // sourceAddr := "12345" - // destAddrTON := byte(0x01) - // destAddrNPI := byte(0x01) - // destinationAddr := "67890" - // esmClass := byte(0x00) - // protocolID := byte(0x00) - // priorityFlag := byte(0x00) - // scheduleDeliveryTime := "" - // validityPeriod := "" - // registeredDelivery := byte(0x01) - // replaceIfPresentFlag := byte(0x00) - // dataCoding := byte(0x01) - // smDefaultMsgID := byte(0x00) - // shortMessage := "Hello, SMPP!" wg := &sync.WaitGroup{} wg.Add(1) - conn, err := net.Dial("tcp", "localhost:2775") + client := client.NewSMPPClient("localhost:2775", 1) + err := client.Connect() if err != nil { - log.Printf("Failed to connect to SMPP server: %+v", err) - return + log.Fatalf("Failed to connect to SMPP server: %v", err) + } + err = client.Send(submit) + if err != nil { + log.Fatalf("Failed to send PDU: %v", err) } - log.Printf("Connected to SMPP server") - defer conn.Close() - - go func() { - conn.Write(buf.Bytes()) - data, err := conn.Read(make([]byte, 1024)) - if err != nil { - log.Printf("Failed to read from connection: %+v", err) - return - } - log.Printf("Received data: %#v", data) - }() wg.Wait() }