fsock

package module
v0.0.0-...-b6cc1d9 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 16 Imported by: 10

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},
	}
	errChan := make(chan error)
	fs, err := fsock.NewFSock("127.0.0.1:8021", "ClueCon", 10, evHandlers, evFilters, l, 0, false, errChan)
	if err != nil {
		l.Crit(fmt.Sprintf("FreeSWITCH error:", err))
		return
	}
	<- errChan

}

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 (
	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

FSEventStrToMap transforms an FreeSWITCH event string into a map, optionally filtering headers.

func MapChanData

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

MapChanData parses channel information from a given string coming from fsock into a slice of maps, where each map contains individual channel data.

Types

type FSConn

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

func NewFSConn

func NewFSConn(addr, passwd string,
	connIdx int,
	replyTimeout time.Duration,
	connErr chan error,
	lgr logger,
	evFilters map[string][]string,
	eventHandlers map[string][]func(string, int),
	bgapi bool,
) (*FSConn, error)

NewFSConn constructs and connects a FSConn

func (*FSConn) Disconnect

func (fsConn *FSConn) Disconnect() error

Disconnect will disconnect the fsConn from FreeSWITCH

func (*FSConn) LocalAddr

func (fsConn *FSConn) LocalAddr() net.Addr

LocalAddr returns the local address of the connection

func (*FSConn) Send

func (fsConn *FSConn) Send(payload string) (string, error)

Send will send the content over the connection, exposing synchronous interface outside

func (*FSConn) SendBgapiCmd

func (fsConn *FSConn) SendBgapiCmd(cmdStr string) (out chan string, err error)

Send BGAPI command

type FSock

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

FSock reperesents the connection to FreeSWITCH Socket

func NewFSock

func NewFSock(addr, passwd string, reconnects int,
	maxReconnectInterval, replyTimeout time.Duration,
	delayFunc func(time.Duration, time.Duration) func() time.Duration,
	eventHandlers map[string][]func(string, int),
	eventFilters map[string][]string,
	logger logger, connIdx int, bgapi bool, stopError chan error,
) (fsock *FSock, err error)

NewFSock connects to FS and starts buffering input.

func (*FSock) Connect

func (fs *FSock) Connect() (err error)

Connect adds locking to connect method.

func (*FSock) Connected

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

Connected adds up locking on top of normal connected method.

func (*FSock) Disconnect

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

Disconnect adds up locking for disconnect

func (*FSock) LocalAddr

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

func (*FSock) ReconnectIfNeeded

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

ReconnectIfNeeded adds up locking to reconnectIfNeeded

func (*FSock) SendApiCmd

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

Send API command

func (*FSock) SendBgapiCmd

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

Send BGAPI command

func (*FSock) SendCmd

func (fs *FSock) SendCmd(cmdStr string) (rply string, err error)

Generic proxy for commands

func (*FSock) SendCmdWithArgs

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

func (*FSock) SendEvent

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

SendEvent command

func (*FSock) SendEventWithBody

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

SendEventWithBody command

func (*FSock) SendMsgCmd

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

SendMsgCmd command

func (*FSock) SendMsgCmdWithBody

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

SendMsgCmdWithBody command

type FSockPool

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

Connection handler for commands sent to FreeSWITCH

func NewFSockPool

func NewFSockPool(maxFSocks int,
	addr, passwd string,
	reconnects int,
	maxWaitConn, maxReconnectInterval, replyTimeout time.Duration,
	delayFuncConstructor func(time.Duration, time.Duration) func() time.Duration,
	eventHandlers map[string][]func(string, int),
	eventFilters map[string][]string,
	logger logger,
	connIdx int,
	bgapi bool,
	stopError chan error,
) *FSockPool

Instantiates a new FSockPool

func (*FSockPool) PopFSock

func (fs *FSockPool) PopFSock() (fsock *FSock, err error)

func (*FSockPool) PushFSock

func (fs *FSockPool) PushFSock(fsk *FSock)

Jump to

Keyboard shortcuts

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