Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using smpp34 as a server #10

Closed
jaroszan opened this issue May 28, 2015 · 2 comments
Closed

Using smpp34 as a server #10

jaroszan opened this issue May 28, 2015 · 2 comments

Comments

@jaroszan
Copy link

As mentioned in subj.

It seems that currently smpp34 can connect to external entity, but cannot operate in server mode (creating RX/TX/TRX assumes that we connect and act as a client, not a server), eg:

From transceiver.go (receiver and transmitter example are identical):

func NewTransceiver(host string, port int, eli int, bindParams Params) (*Transceiver, error) {
    trx := &Transceiver{}
    if err := trx.Connect(host, port); err != nil {
        return nil, err
    }

Is there a workaroung/quick fix to be able to listen for incoming connections and use trx/tx/rx methods?

@CodeMonkeyKevin
Copy link
Owner

Server mode is not created as the this lib is just for parsing/creating SMPP3.4 PDUs. A lot of SMPP server have business logic tied to it so you can use this lib to create our own server.

You can use something like this to handle incoming connections.

func startServer {
    ln, err := net.Listen("tcp", ":8080")

    if err != nil {
        // handle error
    }

    for {
        conn, err := ln.Accept()
        if err != nil {
            // handle error
            continue
        }
        go handleConnection(conn)
    }
}

func handleConnection(conn net.Conn) {
        // setup struct
        trx := &Smpp{}
        trx.conn = conn

    // start reading PDUs
    for {
        pdu, err := trx.Read() // This is blocking
        if err != nil {
            fmt.Println("Read Err:", err)
            fmt.Println(trx.Err)
            break
        }

        // Transceiver auto handles EnquireLinks
        switch pdu.GetHeader().Id {
        case smpp.SUBMIT_SM_RESP:
            // message_id should match this with seq message
            fmt.Println("MSG ID:", pdu.GetField("message_id").Value())
        case smpp.DELIVER_SM:
            // received Deliver Sm
            for _, v := range pdu.MandatoryFieldsList() {
                f := pdu.GetField(v)
                fmt.Println(v, ":", f)
            }

            // Respond back to Deliver SM with Deliver SM Resp
            err := trx.DeliverSmResp(pdu.GetHeader().Sequence, smpp.ESME_ROK)

            if err != nil {
                fmt.Println("DeliverSmResp err:", err)
            }

            // trx.Unbind()
        case smpp.UNBIND:
            // Received Unbind connection is already closed. Stop the read loop
            fmt.Println("Unbound")
            return
        default:
            fmt.Println("PDU:", pdu.GetHeader())
        }
    }
}

@jaroszan
Copy link
Author

jaroszan commented Jun 2, 2015

Thanks a lot!

@jaroszan jaroszan closed this as completed Jun 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants