Exemple #1
0
func TestMapSetTextCodec(t *testing.T) {
	m := make(Map)
	text := pdutext.Latin1("Olá mundo")
	err := m.Set(ShortMessage, text)
	if err != nil {
		t.Fatal(err)
	}
	dc, exists := m[DataCoding]
	if !exists {
		t.Fatal("missing data_coding pdu")
	}
	dv, ok := dc.(*Fixed)
	if !ok {
		t.Fatalf("unexpected type for data_coding: %#v", dc)
	}
	if dv.Data != uint8(text.Type()) {
		t.Fatalf("unexpected value for data_coding: want %d, have %d",
			text.Type(), dv.Data)
	}
	pt, exists := m[ShortMessage]
	if !exists {
		t.Fatal("missing short_message pdu")
	}
	nt := pdutext.Latin1(pt.Bytes()).Decode()
	if !bytes.Equal(text, nt) {
		t.Fatalf("unexpected text: want %q, have %q", text, nt)
	}
}
Exemple #2
0
func TestServer(t *testing.T) {
	s := NewServer()
	defer s.Close()
	c, err := net.Dial("tcp", s.Addr())
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()
	rw := newConn(c)
	// bind
	p := pdu.NewBindTransmitter()
	f := p.Fields()
	f.Set(pdufield.SystemID, "client")
	f.Set(pdufield.Password, "secret")
	f.Set(pdufield.InterfaceVersion, 0x34)
	if err = rw.Write(p); err != nil {
		t.Fatal(err)
	}
	// bind resp
	resp, err := rw.Read()
	if err != nil {
		t.Fatal(err)
	}
	id, ok := resp.Fields()[pdufield.SystemID]
	if !ok {
		t.Fatalf("missing system_id field: %#v", resp)
	}
	if id.String() != "smpptest" {
		t.Fatalf("unexpected system_id: want smpptest, have %q", id)
	}
	// submit_sm
	p = pdu.NewSubmitSM()
	f = p.Fields()
	f.Set(pdufield.SourceAddr, "foobar")
	f.Set(pdufield.DestinationAddr, "bozo")
	f.Set(pdufield.ShortMessage, pdutext.Latin1("Lorem ipsum"))
	if err = rw.Write(p); err != nil {
		t.Fatal(err)
	}
	// same submit_sm
	r, err := rw.Read()
	if err != nil {
		t.Fatal(err)
	}
	want, have := *p.Header(), *r.Header()
	if want != have {
		t.Fatalf("unexpected header: want %#v, have %#v", want, have)
	}
	for k, v := range p.Fields() {
		vv, exists := r.Fields()[k]
		if !exists {
			t.Fatalf("unexpected fields: want %#v, have %#v",
				p.Fields(), r.Fields())
		}
		if !bytes.Equal(v.Bytes(), vv.Bytes()) {
			t.Fatalf("unexpected field data: want %#v, have %#v",
				v, vv)
		}
	}
}
Exemple #3
0
func ExampleTransmitter() {
	tx := &smpp.Transmitter{
		Addr:   "localhost:2775",
		User:   "******",
		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())
	}
}
Exemple #4
0
func (rpc *SM) submit(req url.Values) (resp *ShortMessageResp, status int, err error) {
	sm := &smpp.ShortMessage{}
	var msg, enc, register string
	f := form{
		{"src", "number of sender", false, nil, &sm.Src},
		{"dst", "number of recipient", true, nil, &sm.Dst},
		{"text", "text message", true, nil, &msg},
		{"enc", "text encoding", false, []string{"latin1", "ucs2"}, &enc},
		{"register", "registered delivery", false, []string{"final", "failure"}, &register},
	}
	if err := f.Validate(req); err != nil {
		return nil, http.StatusBadRequest, err
	}
	switch enc {
	case "":
		sm.Text = pdutext.Raw(msg)
	case "latin1", "latin-1":
		sm.Text = pdutext.Latin1(msg)
	case "ucs2", "ucs-2":
		sm.Text = pdutext.UCS2(msg)
	}
	switch register {
	case "final":
		sm.Register = smpp.FinalDeliveryReceipt
	case "failure":
		sm.Register = smpp.FailureDeliveryReceipt
	}
	sm, err = rpc.tx.Submit(sm)
	if err == smpp.ErrNotConnected {
		return nil, http.StatusServiceUnavailable, err
	}
	if err != nil {
		return nil, http.StatusBadGateway, err
	}
	resp = &ShortMessageResp{MessageID: sm.RespID()}
	return resp, http.StatusOK, nil
}
Exemple #5
0
		defer tx.Close()
		log.Println("Connected to", tx.Addr)
		sender := c.Args()[0]
		recipient := c.Args()[1]
		text := strings.Join(c.Args()[2:], " ")
		log.Printf("Command: send %q %q %q", sender, recipient, text)
		var register smpp.DeliverySetting
		if c.Bool("register") {
			register = smpp.FinalDeliveryReceipt
		}
		var codec pdutext.Codec
		switch c.String("encoding") {
		case "ucs2", "ucs-2":
			codec = pdutext.UCS2(text)
		case "latin1", "latin-1":
			codec = pdutext.Latin1(text)
		default:
			codec = pdutext.Raw(text)
		}
		sm, err := tx.Submit(&smpp.ShortMessage{
			Src:                  sender,
			Dst:                  recipient,
			Text:                 codec,
			Register:             register,
			ServiceType:          c.String("service-type"),
			SourceAddrTON:        uint8(c.Int("source-addr-ton")),
			SourceAddrNPI:        uint8(c.Int("source-addr-npi")),
			DestAddrTON:          uint8(c.Int("dest-addr-ton")),
			DestAddrNPI:          uint8(c.Int("dest-addr-npi")),
			ESMClass:             uint8(c.Int("esm-class")),
			ProtocolID:           uint8(c.Int("protocol-id")),