fsock

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT Imports: 18 Imported by: 0

README

FreeSWITCH socket client written in Go

Installation

go get github.com/cgrates/fsock

Support

Join CGRateS on Google Groups here.

License

fsock.go is released under the MIT License. Copyright (C) ITsysCOM GmbH. All Rights Reserved.

Sample usage code

package main

import (
	"fmt"
	"log/syslog"

	"github.com/cgrates/fsock"
)

// Formats the event as map and prints it out
func printHeartbeat(eventStr string, connIdx int) {
	// Format the event from string into Go's map type
	eventMap := fsock.FSEventStrToMap(eventStr, []string{})
	fmt.Printf("%v, connIdx: %d\n", eventMap, connIdx)
}

// Formats the event as map and prints it out
func printChannelAnswer(eventStr string, connIdx int) {
	// Format the event from string into Go's map type
	eventMap := fsock.FSEventStrToMap(eventStr, []string{})
	fmt.Printf("%v, connIdx: %d\n", eventMap, connIdx)
}

// Formats the event as map and prints it out
func printChannelHangup(eventStr string, connIdx int) {
	// Format the event from string into Go's map type
	eventMap := fsock.FSEventStrToMap(eventStr, []string{})
	fmt.Printf("%v, connIdx: %d\n", eventMap, connIdx)
}

func main() {
	// Init a syslog writter for our test
	l, errLog := syslog.New(syslog.LOG_INFO, "TestFSock")
	if errLog != nil {
		l.Crit(fmt.Sprintf("Cannot connect to syslog:", errLog))
		return
	}

	// Filters
	evFilters := make(map[string][]string)
	evFilters["Event-Name"] = append(evFilters["Event-Name"], "CHANNEL_ANSWER")
	evFilters["Event-Name"] = append(evFilters["Event-Name"], "CHANNEL_HANGUP_COMPLETE")

	// We are interested in heartbeats, channel_answer, channel_hangup define handler for them
	evHandlers := map[string][]func(string, int){
		"HEARTBEAT":               {printHeartbeat},
		"CHANNEL_ANSWER":          {printChannelAnswer},
		"CHANNEL_HANGUP_COMPLETE": {printChannelHangup},
	}

	fs, err := fsock.NewFSock("127.0.0.1:8021", "ClueCon", 10, evHandlers, evFilters, l, 0)
	if err != nil {
		l.Crit(fmt.Sprintf("FreeSWITCH error:", err))
		return
	}
	fs.ReadEvents()
}

Build Status

Documentation

Overview

utils.go is released under the MIT License <http://www.opensource.org/licenses/mit-license.php Copyright (C) ITsysCOM. All Rights Reserved.

Provides FreeSWITCH socket communication.

Index

Constants

View Source
const EventBodyTag = "EvBody"

Variables

View Source
var (
	FS        *FSock // Used to share FS connection via package globals
	DelayFunc func() func() int

	ErrConnectionPoolTimeout = errors.New("ConnectionPool timeout")
)

Functions

func EventToMap

func EventToMap(event string) (result map[string]string)

func FSEventStrToMap

func FSEventStrToMap(fsevstr string, headers []string) map[string]string

Convert fseventStr into fseventMap

func MapChanData

func MapChanData(chanInfoStr string) (chansInfoMap []map[string]string)

Converts string received from fsock into a list of channel info, each represented in a map

Types

type FSock

type FSock struct {
	// contains filtered or unexported fields
}

Connection to FreeSWITCH Socket

func NewFSock

func NewFSock(fsaddr, fspaswd string, reconnects int, eventHandlers map[string][]func(string, int), eventFilters map[string][]string,
	l *log.Logger, connIdx int) (fsock *FSock, err error)

Connects to FS and starts buffering input

func (*FSock) Connect

func (f *FSock) Connect() error

Connect or reconnect

func (*FSock) Connected

func (f *FSock) Connected() (ok bool)

Connected checks if socket connected. Can be extended with pings

func (*FSock) Disconnect

func (f *FSock) Disconnect() (err error)

Disconnects from socket

func (*FSock) LocalAddr

func (f *FSock) LocalAddr() net.Addr

func (*FSock) ReadEvents

func (f *FSock) ReadEvents() (err error)

ReadEvents reads events from socket, attempt reconnect if disconnected

func (*FSock) ReconnectIfNeeded

func (f *FSock) ReconnectIfNeeded() (err error)

If not connected, attempt reconnect if allowed

func (*FSock) SendApiCmd

func (f *FSock) SendApiCmd(cmdStr string) (string, error)

Send API command

func (*FSock) SendBgapiCmd

func (f *FSock) SendBgapiCmd(cmdStr string) (out chan string, err error)

Send BGAPI command

func (*FSock) SendCmd

func (f *FSock) SendCmd(cmdStr string) (string, error)

Generic proxy for commands

func (*FSock) SendCmdWithArgs

func (f *FSock) SendCmdWithArgs(cmd string, args map[string]string, body string) (string, error)

func (*FSock) SendEvent

func (f *FSock) SendEvent(eventSubclass string, eventParams map[string]string) (string, error)

SendEvent command

func (*FSock) SendEventWithBody

func (f *FSock) SendEventWithBody(eventSubclass string, eventParams map[string]string, body string) (string, error)

SendEventWithBody command

func (*FSock) SendMsgCmd

func (f *FSock) SendMsgCmd(uuid string, cmdargs map[string]string) error

SendMsgCmd command

func (*FSock) SendMsgCmdWithBody

func (f *FSock) SendMsgCmdWithBody(uuid string, cmdargs map[string]string, body string) error

SendMsgCmdWithBody command

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Connection handler for commands sent to FreeSWITCH

func NewFSockPool

func NewFSockPool(maxFSocks int, fsaddr, fspasswd string, reconnects int, maxWaitConn time.Duration,
	eventHandlers map[string][]func(string, int), eventFilters map[string][]string, l *log.Logger, connIdx int) (*Pool, error)

Instantiates a new FSockPool

func (*Pool) PopFSock

func (p *Pool) PopFSock() (fsock *FSock, err error)

func (*Pool) PushFSock

func (p *Pool) PushFSock(fsk *FSock)

Jump to

Keyboard shortcuts

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