Skip to content

Commit 93e3ad2

Browse files
authored
Merge pull request #1 from weave-lab/errors
Errors
2 parents 6a93875 + 9587606 commit 93e3ad2

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

client.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ package goesl
99
import (
1010
"bufio"
1111
"fmt"
12+
"io"
13+
"net"
14+
"strconv"
1215
"time"
1316
)
1417

@@ -26,45 +29,49 @@ type Client struct {
2629
// EstablishConnection - Will attempt to establish connection against freeswitch and create new SocketConnection
2730
func (c *Client) EstablishConnection() error {
2831
conn, err := c.Dial(c.Proto, c.Addr, time.Duration(c.Timeout*int(time.Second)))
32+
if err != nil {
33+
return err
34+
}
2935

3036
c.SocketConnection = SocketConnection{
3137
Conn: conn,
3238
err: make(chan error),
3339
m: make(chan *Message),
3440
}
3541

36-
return err
42+
return nil
3743
}
3844

3945
// Authenticate - Method used to authenticate client against freeswitch. In case of any errors durring so
4046
// we will return error.
4147
func (c *Client) Authenticate() error {
4248

4349
m, err := newMessage(bufio.NewReaderSize(c, ReadBufferSize), false)
44-
4550
if err != nil {
4651
Error(ECouldNotCreateMessage, err)
4752
return err
4853
}
4954

5055
cmr, err := m.tr.ReadMIMEHeader()
51-
52-
Debug("A: %v %v ", cmr, err)
53-
5456
if err != nil && err.Error() != "EOF" {
5557
Error(ECouldNotReadMIMEHeaders, err)
5658
return err
5759
}
5860

61+
Debug("A: %v\n", cmr)
62+
5963
if cmr.Get("Content-Type") != "auth/request" {
6064
Error(EUnexpectedAuthHeader, cmr.Get("Content-Type"))
6165
return fmt.Errorf(EUnexpectedAuthHeader, cmr.Get("Content-Type"))
6266
}
6367

64-
fmt.Fprintf(c, "auth %s\r\n\r\n", c.Passwd)
68+
s := "auth " + c.Passwd + "\r\n\r\n"
69+
_, err = io.WriteString(c, s)
70+
if err != nil {
71+
return err
72+
}
6573

6674
am, err := m.tr.ReadMIMEHeader()
67-
6875
if err != nil && err.Error() != "EOF" {
6976
Error(ECouldNotReadMIMEHeaders, err)
7077
return err
@@ -80,22 +87,24 @@ func (c *Client) Authenticate() error {
8087

8188
// NewClient - Will initiate new client that will establish connection and attempt to authenticate
8289
// against connected freeswitch server
83-
func NewClient(host string, port uint, passwd string, timeout int) (Client, error) {
90+
func NewClient(host string, port uint, passwd string, timeout int) (*Client, error) {
8491
client := Client{
8592
Proto: "tcp", // Let me know if you ever need this open up lol
86-
Addr: fmt.Sprintf("%s:%d", host, port),
93+
Addr: net.JoinHostPort(host, strconv.Itoa(int(port))),
8794
Passwd: passwd,
8895
Timeout: timeout,
8996
}
9097

91-
if err := client.EstablishConnection(); err != nil {
92-
return client, err
98+
err := client.EstablishConnection()
99+
if err != nil {
100+
return nil, err
93101
}
94102

95-
if err := client.Authenticate(); err != nil {
103+
err = client.Authenticate()
104+
if err != nil {
96105
client.Close()
97-
return client, err
106+
return nil, err
98107
}
99108

100-
return client, nil
109+
return &client, nil
101110
}

connection.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bufio"
1111
"bytes"
1212
"fmt"
13+
"io"
1314
"net"
1415
"strconv"
1516
"strings"
@@ -34,13 +35,22 @@ func (c *SocketConnection) Dial(network string, addr string, timeout time.Durati
3435
func (c *SocketConnection) Send(cmd string) error {
3536

3637
if strings.Contains(cmd, "\r\n") {
37-
fmt.Errorf(EInvalidCommandProvided, cmd)
38+
return fmt.Errorf(EInvalidCommandProvided, cmd)
3839
}
40+
3941
// lock mutex
4042
c.mtx.Lock()
4143
defer c.mtx.Unlock()
4244

43-
fmt.Fprintf(c, "%s\r\n\r\n", cmd)
45+
_, err := io.WriteString(c, cmd)
46+
if err != nil {
47+
return err
48+
}
49+
50+
_, err = io.WriteString(c, "\r\n\r\n")
51+
if err != nil {
52+
return err
53+
}
4454

4555
return nil
4656
}
@@ -60,18 +70,35 @@ func (c *SocketConnection) SendMany(cmds []string) error {
6070
// SendEvent - Will loop against passed event headers
6171
func (c *SocketConnection) SendEvent(eventHeaders []string) error {
6272
if len(eventHeaders) <= 0 {
63-
fmt.Errorf(ECouldNotSendEvent, len(eventHeaders))
64-
return nil
73+
return fmt.Errorf(ECouldNotSendEvent, len(eventHeaders))
6574
}
75+
6676
// lock mutex to prevent event headers from conflicting
6777
c.mtx.Lock()
6878
defer c.mtx.Unlock()
6979

70-
fmt.Fprint(c, "sendevent ")
80+
_, err := io.WriteString(c, "sendevent ")
81+
if err != nil {
82+
return err
83+
}
84+
7185
for _, eventHeader := range eventHeaders {
72-
fmt.Fprintf(c, "%s\r\n", eventHeader)
86+
_, err := io.WriteString(c, eventHeader)
87+
if err != nil {
88+
return err
89+
}
90+
91+
_, err = io.WriteString(c, "\r\n")
92+
if err != nil {
93+
return err
94+
}
95+
96+
}
97+
98+
_, err = io.WriteString(c, "\r\n")
99+
if err != nil {
100+
return err
73101
}
74-
fmt.Fprint(c, "\r\n")
75102

76103
return nil
77104
}
@@ -131,9 +158,14 @@ func (c *SocketConnection) SendMsg(msg map[string]string, uuid, data string) (m
131158
b.WriteString(data)
132159
}
133160

134-
if _, err := b.WriteTo(c); err != nil {
161+
// lock mutex
162+
c.mtx.Lock()
163+
_, err = b.WriteTo(c)
164+
if err != nil {
165+
c.mtx.Unlock()
135166
return nil, err
136167
}
168+
c.mtx.Unlock()
137169

138170
select {
139171
case err := <-c.err:

helpers.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
package goesl
88

9-
import "fmt"
10-
119
// Set - Helper that you can use to execute SET application against active ESL session
1210
func (sc *SocketConnection) ExecuteSet(key string, value string, sync bool) (m *Message, err error) {
13-
return sc.Execute("set", fmt.Sprintf("%s=%s", key, value), sync)
11+
return sc.Execute("set", key+"="+value, sync)
1412
}
1513

1614
// ExecuteHangup - Helper desgned to help with executing Answer against active ESL session
@@ -29,12 +27,12 @@ func (sc *SocketConnection) ExecuteHangup(uuid string, args string, sync bool) (
2927

3028
// BgApi - Helper designed to attach api in front of the command so that you do not need to write it
3129
func (sc *SocketConnection) Api(command string) error {
32-
return sc.Send(fmt.Sprintf("api %s", command))
30+
return sc.Send("api " + command)
3331
}
3432

3533
// BgApi - Helper designed to attach bgapi in front of the command so that you do not need to write it
3634
func (sc *SocketConnection) BgApi(command string) error {
37-
return sc.Send(fmt.Sprintf("bgapi %s", command))
35+
return sc.Send("bgapi " + command)
3836
}
3937

4038
// Connect - Helper designed to help you handle connection. Each outbound server when handling needs to connect e.g. accept

0 commit comments

Comments
 (0)