fsock

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

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

Go to latest
Published: Mar 13, 2019 License: MIT Imports: 17 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 (
    "github.com/cgrates/fsock"
    "log/syslog"
    "fmt"
)

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

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

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

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, string){
	    "HEARTBEAT":               {printHeartbeat},
	    "CHANNEL_ANSWER":          {printChannelAnswer},
	    "CHANNEL_HANGUP_COMPLETE": {printChannelHangup},
    }

    fs, err := fsock.NewFSock("127.0.0.1:8021", "ClueCon", 10, evHandlers, evFilters, l, "wetsfnmretiewrtpj")
    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, string), eventFilters map[string][]string, l log.Logger, connId string) (fsock *FSock, err error)

Connects to FS and starts buffering input

func (*FSock) Connect

func (self *FSock) Connect() error

Connect or reconnect

func (*FSock) Connected

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

Checks if socket connected. Can be extended with pings

func (*FSock) Disconnect

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

Disconnects from socket

func (*FSock) LocalAddr

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

func (*FSock) ReadEvents

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

Reads events from socket, attempt reconnect if disconnected

func (*FSock) ReconnectIfNeeded

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

If not connected, attempt reconnect if allowed

func (*FSock) SendApiCmd

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

Send API command

func (*FSock) SendBgapiCmd

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

Send BGAPI command

func (*FSock) SendCmd

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

Generic proxy for commands

func (*FSock) SendCmdWithArgs

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

func (*FSock) SendEvent

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

SendEvent command

func (*FSock) SendEventWithBody

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

SendEvent command

func (*FSock) SendMsgCmd

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

SendMessage command

func (*FSock) SendMsgCmdWithBody

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

SendMessage command

type FSockPool

type FSockPool 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, string), eventFilters map[string][]string, l log.Logger, connId string) (*FSockPool, error)

Instantiates a new FSockPool

func (*FSockPool) PopFSock

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

func (*FSockPool) PushFSock

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

Jump to

Keyboard shortcuts

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