Skip to content

Commit

Permalink
eventsocket.go: add logprettyprint()
Browse files Browse the repository at this point in the history
  • Loading branch information
fredposner committed Jul 21, 2024
1 parent 3acb6e5 commit 2d572a2
Showing 1 changed file with 80 additions and 18 deletions.
98 changes: 80 additions & 18 deletions eventsocket/eventsocket.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
// Copyright 2013 Alexandre Fiori
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
Copyright 2013 Alexandre Fiori
Copyright 2024 The Palner Group, Inc. / Fred Posner
Use of this source code is governed by a MIT license that can be
found in the LICENSE file.
FreeSWITCH Event Socket library for the Go programming language.
eventsocket supports both inbound and outbound event socket connections,
acting either as a client connecting to FreeSWITCH or as a server accepting
connections from FreeSWITCH to control calls.
Reference:
http://wiki.freeswitch.org/wiki/Event_Socket
http://wiki.freeswitch.org/wiki/Event_Socket_Outbound
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// FreeSWITCH Event Socket library for the Go programming language.
//
// eventsocket supports both inbound and outbound event socket connections,
// acting either as a client connecting to FreeSWITCH or as a server accepting
// connections from FreeSWITCH to control calls.
//
// Reference:
// http://wiki.freeswitch.org/wiki/Event_Socket
// http://wiki.freeswitch.org/wiki/Event_Socket_Outbound
//
// WORK IN PROGRESS, USE AT YOUR OWN RISK.
package eventsocket

import (
Expand Down Expand Up @@ -86,7 +98,6 @@ type HandleFunc func(*Connection)
// ...
// }
// }
//
func ListenAndServe(addr string, fn HandleFunc) error {
srv, err := net.Listen("tcp", addr)
if err != nil {
Expand Down Expand Up @@ -114,7 +125,6 @@ func ListenAndServe(addr string, fn HandleFunc) error {
// ev.PrettyPrint()
// ...
// }
//
func Dial(addr, passwd string) (*Connection, error) {
c, err := net.Dial("tcp", addr)
if err != nil {
Expand Down Expand Up @@ -159,6 +169,7 @@ func (h *Connection) readOne() bool {
h.err <- err
return false
}

resp := new(Event)
resp.Header = make(EventHeader)
if v := hdr.Get("Content-Length"); v != "" {
Expand All @@ -167,31 +178,37 @@ func (h *Connection) readOne() bool {
h.err <- err
return false
}

b := make([]byte, length)
if _, err := io.ReadFull(h.reader, b); err != nil {
h.err <- err
return false
}

resp.Body = string(b)
}

switch hdr.Get("Content-Type") {
case "command/reply":
reply := hdr.Get("Reply-Text")
if len(reply)>1 && reply[:2] == "-E" {
if len(reply) > 1 && reply[:2] == "-E" {
h.err <- errors.New(reply[5:])
return true
}

if reply[0] == '%' {
copyHeaders(&hdr, resp, true)
} else {
copyHeaders(&hdr, resp, false)
}

h.cmd <- resp
case "api/response":
if len(resp.Body)>1 && string(resp.Body[:2]) == "-E" {
if len(resp.Body) > 1 && string(resp.Body[:2]) == "-E" {
h.err <- errors.New(string(resp.Body)[5:])
return true
}

copyHeaders(&hdr, resp, false)
h.api <- resp
case "text/event-plain":
Expand All @@ -203,19 +220,23 @@ func (h *Connection) readOne() bool {
h.err <- err
return false
}

if v := hdr.Get("Content-Length"); v != "" {
length, err := strconv.Atoi(v)
if err != nil {
h.err <- err
return false
}

b := make([]byte, length)
if _, err = io.ReadFull(reader, b); err != nil {
h.err <- err
return false
}

resp.Body = string(b)
}

copyHeaders(&hdr, resp, true)
h.evt <- resp
case "text/event-json":
Expand All @@ -225,23 +246,27 @@ func (h *Connection) readOne() bool {
h.err <- err
return false
}

// capitalize header keys for consistency.
for k, v := range tmp {
resp.Header[capitalize(k)] = v
}

if v, _ := resp.Header["_body"]; v != nil {
resp.Body = v.(string)
delete(resp.Header, "_body")
} else {
resp.Body = ""
}

h.evt <- resp
case "text/disconnect-notice":
copyHeaders(&hdr, resp, false)
h.evt <- resp
default:
log.Fatal("Unsupported event:", hdr)
}

return true
}

Expand All @@ -266,6 +291,7 @@ func (h *Connection) ReadEvent() (*Event, error) {
ev *Event
err error
)

select {
case err = <-h.err:
return nil, err
Expand Down Expand Up @@ -301,23 +327,27 @@ func capitalize(s string) string {
if s[0] == '_' {
return s
}

ns := bytes.ToLower([]byte(s))
if len(s) > 9 && s[1:9] == "ariable_" {
ns[0] = 'V'
return string(ns)
}

toUpper := true
for n, c := range ns {
if toUpper {
if 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}

ns[n] = c
toUpper = false
} else if c == '-' || c == '_' {
toUpper = true
}
}

return string(ns)
}

Expand All @@ -335,6 +365,7 @@ func (h *Connection) Send(command string) (*Event, error) {
ev *Event
err error
)

select {
case err = <-h.err:
return nil, err
Expand Down Expand Up @@ -380,32 +411,40 @@ func (h *Connection) SendMsg(m MSG, uuid, appData string) (*Event, error) {
if strings.IndexAny(uuid, "\r\n") > 0 {
return nil, errInvalidCommand
}

b.WriteString(" " + uuid)
}

b.WriteString("\n")
for k, v := range m {
// Make sure there's no \r or \n in the key, and value.
if strings.IndexAny(k, "\r\n") > 0 {
return nil, errInvalidCommand
}

if v != "" {
if strings.IndexAny(v, "\r\n") > 0 {
return nil, errInvalidCommand
}

b.WriteString(fmt.Sprintf("%s: %s\n", k, v))
}
}

b.WriteString("\n")
if m["content-length"] != "" && appData != "" {
b.WriteString(appData)
}

if _, err := b.WriteTo(h.conn); err != nil {
return nil, err
}

var (
ev *Event
err error
)

select {
case err = <-h.err:
return nil, err
Expand All @@ -431,6 +470,7 @@ func (h *Connection) Execute(appName, appArg string, lock bool) (*Event, error)
// send event-lock when it's set to false.
evlock = "true"
}

return h.SendMsg(MSG{
"call-command": "execute",
"execute-app-name": appName,
Expand Down Expand Up @@ -472,9 +512,11 @@ func (r *Event) Get(key string) string {
if !ok || val == nil {
return ""
}

if s, ok := val.([]string); ok {
return strings.Join(s, ", ")
}

return val.(string)
}

Expand All @@ -485,6 +527,7 @@ func (r *Event) GetInt(key string) (int, error) {
if err != nil {
return 0, err
}

return n, nil
}

Expand All @@ -494,11 +537,30 @@ func (r *Event) PrettyPrint() {
for k := range r.Header {
keys = append(keys, k)
}

sort.Strings(keys)
for _, k := range keys {
fmt.Printf("%s: %#v\n", k, r.Header[k])
}

if r.Body != "" {
fmt.Printf("BODY: %#v\n", r.Body)
}
}

// PrettyPrint prints Event headers and body to the logger.
func (r *Event) LogPrettyPrint() {
var keys []string
for k := range r.Header {
keys = append(keys, k)
}

sort.Strings(keys)
for _, k := range keys {
log.Printf("%s: %#v\n", k, r.Header[k])
}

if r.Body != "" {
log.Printf("BODY: %#v\n", r.Body)
}
}

0 comments on commit 2d572a2

Please sign in to comment.