Git Product home page Git Product logo

gami's Introduction

GAMI

GO - Asterisk AMI Interface

communicate with the Asterisk AMI, Actions and Events.

Example connecting to Asterisk and Send Action get Events.

package main
import (
	"log"
	"github.com/bit4bit/gami"
	"github.com/bit4bit/gami/event"
	"time"
)

func main() {
	ami, err := gami.Dial("127.0.0.1:5038")
	if err != nil {
		log.Fatal(err)
	}
	ami.Run()
	defer ami.Close()
	
	//install manager
	go func() {
		for {
			select {
			//handle network errors
			case err := <-ami.NetError:
				log.Println("Network Error:", err)
				//try new connection every second
				<-time.After(time.Second)
				if err := ami.Reconnect(); err == nil {
					//call start actions
					ami.Action("Events", gami.Params{"EventMask": "on"})
				}
				
			case err := <-ami.Error:
				log.Println("error:", err)
			//wait events and process
			case ev := <-ami.Events:
				log.Println("Event Detect: %v", *ev)
				//if want type of events
				log.Println("EventType:", event.New(ev))
			}
		}
	}()
	
	if err := ami.Login("admin", "root"); err != nil {
		log.Fatal(err)
	}
	
	
	if _, errPing := ami.Action("Ping", nil); errPing != nil {
		log.Fatal(errPing)
	}
	
	//async actions
	rsPing, rsErr := ami.AsyncAction("Ping", gami.Params{"ActionID": "pingo"})
	if rsErr != nil {
		log.Fatal(rsErr)
	}
						
	if _, err := ami.Action("Events", gami.Params{"EventMask":"on"}); err != nil {
		log.Fatal(err)
	}
	
	log.Println("ping:", <-rsPing)
	

}

###TLS SUPPORT In order to use TLS connection to manager interface you could Dial with additional parameters

//without TLS
ami, err := gami.Dial("127.0.0.1:5038")

//if certificate is trusted
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS)

//if self signed certificate
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS, gami.UnsecureTLS)

//if custom tls configuration
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLSConfig(&tls.Config{}))

WARNING: Only Asterisk >=1.6 supports TLS connection to AMI and it needs additional configuration(follow the Asterisk AMI configuration documentation)

CURRENT EVENT TYPES

The events use documentation and struct from PAMI.

use bit4bit/gami/event.New() for get this struct from raw event

EVENT ID TYPE TEST
Newchannel YES
Newexten YES
Newstate YES
Dial YES
ExtensionStatus YES
Hangup YES
PeerStatus YES
PeerEntry YES
VarSet YES
AgentLogin YES
Agents YES
AgentLogoff YES
AgentConnect YES
RTPReceiverStats YES
RTPSenderStats YES
Bridge YES

gami's People

Contributors

anatoliid avatar bit4bit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gami's Issues

Reconnect not working properly

Hello,

thank you for the AMI Adapter, it's great work.
Though it seems that there are two small issues with the reconnect machanism you provide. Please have a look at the following code snippets. I commented two lines with '!!!!!'

it seems that your reconnect does not work properly as there is a race condition in your example code:

func (client *AMIClient) Reconnect() error {
client.conn.Close()
reconnect, err := Dial(client.address)

if err != nil {
    //client.NetError <- err  <--- !!!!!! Do not pass the error to the channel, because it blocks
    return err
}

//new connection
client.conn = reconnect.conn
client.connRaw = reconnect.connRaw
client.waitNewConnection <- struct{}{}

if err := client.Login(client.amiUser, client.amiPass); err != nil {
    return err
}

return nil

}

//install manager
go func() {
for {
select {
//handle network errors
case err := <-ami.NetError:
log.Println("Network Error:", err)
//try new connection every second
for {
<-time.After(time.Second)
if err := ami.Reconnect(); err == nil {
//call start actions
if _, err := ami.Action("Events", gami.Params{"EventMask": "on"}); err != nil { // <--- !!!!!! This should break if 'err == nil' --> Seems to be a typo
break
}
}
}

        case err := <-ami.Error:
            log.Println("error:", err)
        //wait events and process
        case ev := <-ami.Events:
            log.Println("Event Detect: %v", *ev)
            //if want type of events
            log.Println("EventType:", event.New(ev))
        }
    }
}()

Regards,
Marko

Data race when concurrently accessing client.response map in notifyResponse func.

Hello,
data race conditions were detected when concurrently performing actions, presumably, due to read locks in notifyResponse method, which not only performs read ops but also a write operation on the map (delete). Would you consider fixing this (e.g. using a full lock instead or something)?
Thanks.

`==================
WARNING: DATA RACE
Read at 0x00c42001b740 by goroutine 62:
runtime.mapaccess1_faststr()
/usr/local/go/src/runtime/hashmap_fast.go:192 +0x0
github.com/bit4bit/gami.(*AMIClient).notifyResponse.func1()
.../src/github.com/bit4bit/gami/gami.go:273 +0xb9

Previous write at 0x00c42001b740 by goroutine 95:
runtime.mapdelete()
/usr/local/go/src/runtime/hashmap.go:558 +0x0
github.com/bit4bit/gami.(*AMIClient).notifyResponse.func1()
.../src/github.com/bit4bit/gami/gami.go:275 +0x1b3

Goroutine 62 (running) created at:
github.com/bit4bit/gami.(*AMIClient).notifyResponse()
.../src/github.com/bit4bit/gami/gami.go:277 +0x56
github.com/bit4bit/gami.(*AMIClient).Run.func1()
.../src/github.com/bit4bit/gami/gami.go:257 +0x3c4

Goroutine 95 (finished) created at:
github.com/bit4bit/gami.(*AMIClient).notifyResponse()
.../src/github.com/bit4bit/gami/gami.go:277 +0x56
github.com/bit4bit/gami.(*AMIClient).Run.func1()
.../src/github.com/bit4bit/gami/gami.go:257 +0x3c4
==================`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.