Skip to content

Commit 37899c1

Browse files
committed
First beta version of outbound server is here :)
1 parent ba0b030 commit 37899c1

File tree

6 files changed

+44
-38
lines changed

6 files changed

+44
-38
lines changed

connection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type SocketConnection struct {
2626
func (c *SocketConnection) Send(cmd string) error {
2727

2828
if strings.Contains(cmd, "\r\n") {
29-
fmt.Errorf("Invalid command provided. Command cannot contain \\r and/or \\n within. Command you provided is: %s", cmd)
29+
fmt.Errorf(EInvalidCommandProvided, cmd)
3030
}
3131

3232
fmt.Fprintf(c, "%s\r\n\r\n", cmd)
@@ -73,7 +73,7 @@ func (c *SocketConnection) SendMsg(msg map[string]string, uuid, data string) (m
7373

7474
if uuid != "" {
7575
if strings.Contains(uuid, "\r\n") {
76-
return nil, fmt.Errorf("Invalid command provided. Command cannot contain \\r and/or \\n within. Command you provided is: %s", msg)
76+
return nil, fmt.Errorf(EInvalidCommandProvided, msg)
7777
}
7878

7979
b.WriteString(" " + uuid)
@@ -83,12 +83,12 @@ func (c *SocketConnection) SendMsg(msg map[string]string, uuid, data string) (m
8383

8484
for k, v := range msg {
8585
if strings.Contains(k, "\r\n") {
86-
return nil, fmt.Errorf("Invalid command provided. Command cannot contain \\r and/or \\n within. Command you provided is: %s", msg)
86+
return nil, fmt.Errorf(EInvalidCommandProvided, msg)
8787
}
8888

8989
if v != "" {
9090
if strings.Contains(v, "\r\n") {
91-
return nil, fmt.Errorf("Invalid command provided. Command cannot contain \\r and/or \\n within. Command you provided is: %s", msg)
91+
return nil, fmt.Errorf(EInvalidCommandProvided, msg)
9292
}
9393

9494
b.WriteString(fmt.Sprintf("%s: %s\n", k, v))
@@ -139,7 +139,7 @@ func (c *SocketConnection) Handle() {
139139

140140
go func() {
141141
for {
142-
msg, err := newMessage(bufio.NewReaderSize(c, READER_BUFFER_SIZE))
142+
msg, err := newMessage(bufio.NewReaderSize(c, ReadBufferSize))
143143

144144
if err != nil {
145145
c.err <- err

constants.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,4 @@
66

77
package goesl
88

9-
const (
10-
//
11-
INBOUND_SERVER_CONN_PROTO = "tcp"
12-
13-
//
14-
READER_BUFFER_SIZE = 1024 << 6
15-
16-
//
17-
EVENTS_BUFFER = 32
18-
)
9+
const ()

errors.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,19 @@
22
// Please check out LICENSE file for more information about what you CAN and what you CANNOT do!
33
// Basically in short this is a free software for you to do whatever you want to do BUT copyright must be included!
44
// I didn't write all of this code so you could say it's yours.
5-
// MIT License
5+
// MIT License
6+
7+
package goesl
8+
9+
var (
10+
EInvalidCommandProvided = "Invalid command provided. Command cannot contain \\r and/or \\n. Provided command is: %s"
11+
ECouldNotReadMIMEHeaders = "Error while reading MIME headers: %s"
12+
EInvalidContentLength = "Unable to get size of content-length: %s"
13+
EUnsuccessfulReply = "Got error while reading from reply command: %s"
14+
ECouldNotReadyBody = "Got error while reading reader body: %s"
15+
EUnsupportedMessageType = "Unsupported message type! We got '%s'. Supported types are: %v "
16+
ECouldNotDecode = "Could not decode/unescape message: %s"
17+
ECouldNotStartListener = "Got error while attempting to start listener: %s"
18+
EListenerConnection = "Listener connection error: %s"
19+
EInvalidServerAddr = "Please make sure to pass along valid address. You've passed: \"%s\""
20+
)

message.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (m *Message) Parse() error {
5050
cmr, err := m.tr.ReadMIMEHeader()
5151

5252
if err != nil && err.Error() != "EOF" {
53-
Error("Error while reading MIME headers: %s", err)
53+
Error(ECouldNotReadMIMEHeaders, err)
5454
return err
5555
}
5656

@@ -65,14 +65,14 @@ func (m *Message) Parse() error {
6565
l, err := strconv.Atoi(lv)
6666

6767
if err != nil {
68-
Error("Unable to get size of content-length: %s", err)
68+
Error(EInvalidContentLength, err)
6969
return err
7070
}
7171

7272
m.Body = make([]byte, l)
7373

7474
if _, err := io.ReadFull(m.r, m.Body); err != nil {
75-
Error("Got error while reading reader body: %s", err)
75+
Error(ECouldNotReadyBody, err)
7676
return err
7777
}
7878
}
@@ -82,7 +82,7 @@ func (m *Message) Parse() error {
8282
Debug("Got message content (type: %s). Searching if we can handle it ...", msgType)
8383

8484
if !StringInSlice(msgType, AvailableMessageTypes) {
85-
return fmt.Errorf("Unsupported message type! We got '%s'. Supported types are: %v ", msgType, AvailableMessageTypes)
85+
return fmt.Errorf(EUnsupportedMessageType, msgType, AvailableMessageTypes)
8686
}
8787

8888
// Assing message headers IF message is not type of event-json
@@ -96,7 +96,7 @@ func (m *Message) Parse() error {
9696
m.Headers[k], err = url.QueryUnescape(v[0])
9797

9898
if err != nil {
99-
Debug("Could not decode/unescape message: %s", err)
99+
Error(ECouldNotDecode, err)
100100
continue
101101
}
102102
}
@@ -111,12 +111,12 @@ func (m *Message) Parse() error {
111111
case "command/reply":
112112
reply := cmr.Get("Reply-Text")
113113

114-
if reply[:2] == "-E" {
115-
return fmt.Errorf("Got error while reading from reply command: %s", reply[5:])
114+
if strings.Contains(reply, "-ERR") {
115+
return fmt.Errorf(EUnsuccessfulReply, reply[5:])
116116
}
117117
case "api/response":
118-
if string(m.Body[:2]) == "-E" {
119-
return fmt.Errorf("Got error while reading from reply command: %s", string(m.Body)[5:])
118+
if strings.Contains(string(m.Body), "-ERR") {
119+
return fmt.Errorf(EUnsuccessfulReply, string(m.Body)[5:])
120120
}
121121

122122
case "text/event-plain":
@@ -127,21 +127,21 @@ func (m *Message) Parse() error {
127127
emh, err := tr.ReadMIMEHeader()
128128

129129
if err != nil {
130-
return fmt.Errorf("Error while reading MIME headers (text/event-plain): %s", err)
130+
return fmt.Errorf(ECouldNotReadMIMEHeaders, err)
131131
}
132132

133133
if vl := emh.Get("Content-Length"); vl != "" {
134134
length, err := strconv.Atoi(vl)
135135

136136
if err != nil {
137-
Error("Unable to get size of content-length (text/event-plain): %s", err)
137+
Error(EInvalidContentLength, err)
138138
return err
139139
}
140140

141141
m.Body = make([]byte, length)
142142

143143
if _, err = io.ReadFull(r, m.Body); err != nil {
144-
Error("Got error while reading body (text/event-plain): %s", err)
144+
Error(ECouldNotReadyBody, err)
145145
return err
146146
}
147147
}

server.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net"
1212
"os"
1313
"os/signal"
14-
"runtime"
1514
"syscall"
1615
)
1716

@@ -32,13 +31,10 @@ func (s *OutboundServer) Start() error {
3231
s.Listener, err = net.Listen(s.Proto, s.Addr)
3332

3433
if err != nil {
35-
Error("Got error while attempting to create listener: %s", err)
34+
Error(ECouldNotStartListener, err)
3635
return err
3736
}
3837

39-
// @TODO -> Fix this so that concurrency is actually configurable ...
40-
_ = NewSemaphore(uint(runtime.NumCPU()))
41-
4238
quit := make(chan bool)
4339

4440
go func() {
@@ -48,11 +44,13 @@ func (s *OutboundServer) Start() error {
4844
c, err := s.Accept()
4945

5046
if err != nil {
51-
Error("Got connection error: %s", err)
47+
Error(EListenerConnection, err)
5248
quit <- true
5349
break
5450
}
5551

52+
Debug("Aha")
53+
5654
conn := SocketConnection{
5755
Conn: c,
5856
err: make(chan error),
@@ -64,7 +62,6 @@ func (s *OutboundServer) Start() error {
6462
go conn.Handle()
6563

6664
s.Conns <- conn
67-
6865
}
6966
}()
7067

@@ -90,14 +87,14 @@ func NewOutboundServer(addr string) (*OutboundServer, error) {
9087
addr = os.Getenv("GOES_OUTBOUND_SERVER_ADDR")
9188

9289
if addr == "" {
93-
return nil, fmt.Errorf("Please make sure to pass along valid address. You've passed: \"%s\"", addr)
90+
return nil, fmt.Errorf(EInvalidServerAddr, addr)
9491
}
9592
}
9693

9794
server := OutboundServer{
9895
Addr: addr,
99-
Proto: INBOUND_SERVER_CONN_PROTO,
100-
Conns: make(chan SocketConnection, EVENTS_BUFFER),
96+
Proto: "tcp", // Not seeing reason why this could become configurable at this moment
97+
Conns: make(chan SocketConnection),
10198
}
10299

103100
sig := make(chan os.Signal, 1)

vars.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ package goesl
88

99
var (
1010

11+
// Size of buffer when we read from connection
12+
ReadBufferSize = 1024 << 6
13+
1114
// Supported freeswitch events that we can/wish to parse out
1215
AvailableMessageTypes = []string{"text/disconnect-notice", "text/event-json", "text/event-plain", "api/response", "command/reply"}
1316
)

0 commit comments

Comments
 (0)