smpp

package
v0.0.0-...-e021b08 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2018 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package smpp is an implementation of the SMPP 3.4 protocol.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotConnected is returned on attempts to use a dead connection.
	ErrNotConnected = errors.New("not connected")

	// ErrNotBound is returned on attempts to use a Transmitter,
	// Receiver or Transceiver before calling Bind.
	ErrNotBound = errors.New("not bound")
)
View Source
var ErrMaxWindowSize = errors.New("reached max window size")

ErrMaxWindowSize is returned when an operation (such as Submit) violates the maximum window size configured for the Transmitter or Transceiver.

Functions

This section is empty.

Types

type ClientConn

type ClientConn interface {
	// Bind starts the client connection and returns a
	// channel that is triggered every time the connection
	// status changes.
	Bind() <-chan ConnStatus

	// Closer embeds the Closer interface. When Close is
	// called, client sends the Unbind command first and
	// terminates the connection upon response, or 1s timeout.
	Closer
}

ClientConn provides a persistent client connection that handles reconnection with a back-off algorithm.

type Closer

type Closer interface {
	// Close terminates the connection.
	Close() error
}

Closer is the interface that wraps the basic Close method.

type Conn

type Conn interface {
	Reader
	Writer
	Closer
}

Conn is an SMPP connection.

func Dial

func Dial(addr string, TLS *tls.Config) (Conn, error)

Dial dials to the SMPP server and returns a Conn, or error. TLS is only used if provided.

type ConnStatus

type ConnStatus interface {
	Status() ConnStatusID
	Error() error
}

ConnStatus is an abstract interface for a connection status change.

type ConnStatusID

type ConnStatusID uint8

ConnStatusID represents a connection status change.

const (
	Connected ConnStatusID = iota + 1
	Disconnected
	ConnectionFailed
	BindFailed
)

Supported connection statuses.

func (ConnStatusID) String

func (cs ConnStatusID) String() string

String implements the Stringer interface.

type DeliverySetting

type DeliverySetting uint8

DeliverySetting is used to configure registered delivery for short messages.

const (
	NoDeliveryReceipt      DeliverySetting = 0x00
	FinalDeliveryReceipt   DeliverySetting = 0x01
	FailureDeliveryReceipt DeliverySetting = 0x02
)

Supported delivery settings.

type HandlerFunc

type HandlerFunc func(p pdu.Body)

HandlerFunc is the handler function that a Receiver calls when a new PDU arrives.

type QueryResp

type QueryResp struct {
	MsgID     string
	MsgState  string
	FinalDate string
	ErrCode   uint8
}

QueryResp contains the parsed the response of a QuerySM request.

type Reader

type Reader interface {
	// Read reads PDU binary data off the wire and returns it.
	Read() (pdu.Body, error)
}

Reader is the interface that wraps the basic Read method.

type Receiver

type Receiver struct {
	Addr        string
	User        string
	Passwd      string
	SystemType  string
	EnquireLink time.Duration
	TLS         *tls.Config
	Handler     HandlerFunc
	// contains filtered or unexported fields
}

Receiver implements an SMPP client receiver.

Example
package main

import (
	"log"
	"time"

	"github.com/zpas-lab/go-smpp/smpp"
	"github.com/zpas-lab/go-smpp/smpp/pdu"
	"github.com/zpas-lab/go-smpp/smpp/pdu/pdufield"
)

func main() {
	f := func(p pdu.Body) {
		switch p.Header().ID {
		case pdu.DeliverSMID:
			f := p.Fields()
			src := f[pdufield.SourceAddr]
			dst := f[pdufield.DestinationAddr]
			txt := f[pdufield.ShortMessage]
			log.Printf("Short message from=%q to=%q: %q",
				src, dst, txt)
		}
	}
	r := &smpp.Receiver{
		Addr:    "localhost:2775",
		User:    "foobar",
		Passwd:  "secret",
		Handler: f,
	}
	conn := r.Bind() // make persistent connection.
	time.AfterFunc(10*time.Second, func() { r.Close() })
	for c := range conn {
		log.Println("SMPP connection status:", c.Status())
	}
}
Output:

func (*Receiver) Bind

func (r *Receiver) Bind() <-chan ConnStatus

Bind starts the Receiver. It creates a persistent connection to the server, update its status via the returned channel, and calls the registered Handler when new PDU arrives.

Bind implements the ClientConn interface.

func (*Receiver) Close

func (r *Receiver) Close() error

Close implements the ClientConn interface.

func (*Receiver) DeliverSmResp

func (r *Receiver) DeliverSmResp(status pdu.Status, seq uint32) error

DeliverSmResp can be invoked by installed Handler in response to DeliverSm

type ShortMessage

type ShortMessage struct {
	Src      string
	Dst      string
	Text     pdutext.Codec
	Validity time.Duration
	Register DeliverySetting

	// Other fields, normally optional.
	ServiceType          string
	SourceAddrTON        uint8
	SourceAddrNPI        uint8
	DestAddrTON          uint8
	DestAddrNPI          uint8
	ESMClass             uint8
	ProtocolID           uint8
	PriorityFlag         uint8
	ScheduleDeliveryTime string
	ReplaceIfPresentFlag uint8
	SMDefaultMsgID       uint8
	// contains filtered or unexported fields
}

ShortMessage configures a short message that can be submitted via the Transmitter. When returned from Submit, the ShortMessage provides Resp and RespID.

func (*ShortMessage) Resp

func (sm *ShortMessage) Resp() pdu.Body

Resp returns the response PDU, or nil if not set.

func (*ShortMessage) RespID

func (sm *ShortMessage) RespID() string

RespID is a shortcut to Resp().Fields()pdufield.MessageID. Returns empty if the response PDU is not available, or does not contain the MessageID field.

type Transceiver

type Transceiver struct {
	Addr        string
	User        string
	Passwd      string
	SystemType  string
	EnquireLink time.Duration
	RespTimeout time.Duration
	TLS         *tls.Config
	WindowSize  uint
	Handler     HandlerFunc

	Transmitter
}

Transceiver implements an SMPP transceiver.

The API is a combination of the Transmitter and Receiver.

Example
package main

import (
	"io"
	"log"
	"net/http"

	"github.com/zpas-lab/go-smpp/smpp"
	"github.com/zpas-lab/go-smpp/smpp/pdu"
	"github.com/zpas-lab/go-smpp/smpp/pdu/pdufield"
	"github.com/zpas-lab/go-smpp/smpp/pdu/pdutext"
)

func main() {
	f := func(p pdu.Body) {
		switch p.Header().ID {
		case pdu.DeliverSMID:
			f := p.Fields()
			src := f[pdufield.SourceAddr]
			dst := f[pdufield.DestinationAddr]
			txt := f[pdufield.ShortMessage]
			log.Printf("Short message from=%q to=%q: %q",
				src, dst, txt)
		}
	}
	tx := &smpp.Transceiver{
		Addr:    "localhost:2775",
		User:    "foobar",
		Passwd:  "secret",
		Handler: f,
	}
	conn := tx.Bind() // make persistent connection.
	go func() {
		for c := range conn {
			log.Println("SMPP connection status:", c.Status())
		}
	}()
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		sm, err := tx.Submit(&smpp.ShortMessage{
			Src:      r.FormValue("src"),
			Dst:      r.FormValue("dst"),
			Text:     pdutext.Raw(r.FormValue("text")),
			Register: smpp.FinalDeliveryReceipt,
		})
		if err == smpp.ErrNotConnected {
			http.Error(w, "Oops.", http.StatusServiceUnavailable)
			return
		}
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		io.WriteString(w, sm.RespID())
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}
Output:

func (*Transceiver) Bind

func (t *Transceiver) Bind() <-chan ConnStatus

Bind implements the ClientConn interface.

func (*Transceiver) DeliverSmResp

func (t *Transceiver) DeliverSmResp(status pdu.Status, seq uint32) error

DeliverSmResp can be invoked by installed Handler in response to DeliverSm

type Transmitter

type Transmitter struct {
	Addr        string
	User        string
	Passwd      string
	SystemType  string
	EnquireLink time.Duration
	RespTimeout time.Duration
	TLS         *tls.Config
	WindowSize  uint
	// contains filtered or unexported fields
}

Transmitter implements an SMPP client transmitter.

Example
package main

import (
	"log"

	"github.com/zpas-lab/go-smpp/smpp"
	"github.com/zpas-lab/go-smpp/smpp/pdu/pdutext"
)

func main() {
	tx := &smpp.Transmitter{
		Addr:   "localhost:2775",
		User:   "foobar",
		Passwd: "secret",
	}
	conn := <-tx.Bind() // make persistent connection.
	switch conn.Status() {
	case smpp.Connected:
		sm, err := tx.Submit(&smpp.ShortMessage{
			Src:      "sender",
			Dst:      "recipient",
			Text:     pdutext.Latin1("Olá mundo"),
			Register: smpp.NoDeliveryReceipt,
		})
		if err != nil {
			log.Fatal(err)
		}
		log.Println("Message ID:", sm.RespID())
	default:
		log.Fatal(conn.Error())
	}
}
Output:

func (*Transmitter) Bind

func (t *Transmitter) Bind() <-chan ConnStatus

Bind implements the ClientConn interface.

Any commands (e.g. Submit) attempted on a dead connection will return ErrNotConnected.

func (*Transmitter) Close

func (t *Transmitter) Close() error

Close implements the ClientConn interface.

func (*Transmitter) QuerySM

func (t *Transmitter) QuerySM(src, msgid string) (*QueryResp, error)

QuerySM queries the delivery status of a message. It requires the source address (sender) and message ID.

func (*Transmitter) Submit

func (t *Transmitter) Submit(sm *ShortMessage) (*ShortMessage, error)

Submit sends a short message and returns and updates the given sm with the response status. It returns the same sm object.

func (*Transmitter) SubmitLongAsOptional

func (t *Transmitter) SubmitLongAsOptional(sm *ShortMessage) (*ShortMessage, error)

Submit sends a short message and returns and updates the given sm with the response status. It returns the same sm object.

func (*Transmitter) SubmitLongMsg

func (t *Transmitter) SubmitLongMsg(sm *ShortMessage) (*ShortMessage, error)

SubmitLongMsg sends a long message (more than 140 bytes) and returns and updates the given sm with the response status. It returns the same sm object.

type Writer

type Writer interface {
	// Write serializes the given PDU and writes to the connection.
	Write(w pdu.Body) error
}

Writer is the interface that wraps the basic Write method.

Directories

Path Synopsis
pdu
Package pdu provide codecs for binary PDU data.
Package pdu provide codecs for binary PDU data.
pdufield
Package pdufield provides PDU field codecs and utilities.
Package pdufield provides PDU field codecs and utilities.
pdutext
Package pdutext provides text conversion for PDU fields.
Package pdutext provides text conversion for PDU fields.
Package smpptest provides an SMPP test server.
Package smpptest provides an SMPP test server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL