Asterisk-java Tutorial (Chinese version)--fastagi protocol

Source: Internet
Author: User

Asterisk-java Tutorial (Chinese version)--fastagi protocol

Fastagi protocol

The Fastagi protocol enables our Java applications to interact with asterisk in the simplest way possible. The AGI script can handle any incoming or outgoing outbound calls through the manager API.

This disadvantage has been resolved by Fastagi, Fastagi based on AGI through TCP/IP socket connection instead of standard input and standard output as the medium of communication.

You can run a Java application using Fastagi (you can run asterisk on a different machine), which launches only one AGI script until it is closed. Using this protocol in conjunction with multithreaded support for Java can build very fast scripts.

Asterisk-java provides a container to help you run your Java scripts and receive Asterisk server connections, parse requests, or call your scripts through URLs.

To write AGI scripts you must implement the Agiscript interface, and you can further simplify this by inheriting the baseagiscript of the Agiscript interface and overriding its approach.

View Baseagiscript document, you will find more methods that can be used in your script. If the method you want to use is not in baseagiscript , or if you want to extend Fastagi with your command , you can use Channel.sendcommand (Agicommand) method to send arbitrary commands.

Fastagi protocol

The Fastagi protocol enables our Java applications to interact with asterisk in the simplest way possible. The AGI script can handle any incoming or outgoing outbound calls through the manager API.

AGI (Asterisk Gateway Interface) allows you to send scripts to the Asterisk dial plan, where communication between traditional scripts and Asterisk is through standard input and standard output, and scripts must and Asterisk run on the same server.

This disadvantage has been resolved by Fastagi, Fastagi based on AGI through TCP/IP socket connection instead of standard input and standard output as the medium of communication.

You can run a Java application using Fastagi (you can run asterisk on a different machine), which launches only one AGI script until it is closed. Using this protocol in conjunction with multithreaded support for Java can build very fast scripts.

Asterisk-java provides a container to help you run your Java scripts and receive Asterisk server connections, parse requests, or call your scripts through URLs.

Hello agi!

To write AGI scripts you must implement the Agiscript interface, and you can further simplify this by inheriting the baseagiscript of the Agiscript interface and overriding its approach.

An example of a simple AGI script:

Import Org.asteriskjava.fastagi.AgiChannel;

Importorg.asteriskjava.fastagi.AgiException;

Import Org.asteriskjava.fastagi.AgiRequest;

Import Org.asteriskjava.fastagi.BaseAgiScript;

public class Helloagiscript Extendsbaseagiscript

{

public void Service (Agirequest Request,agichannel channel)

Throws Agiexception

{

Answer the Channel ...

Answer ();

... say hello ...

Streamfile ("Welcome");

... and Hangup.

Hangup ();

}

}

Compile:

$ JAVAC-CP Asterisk-java.jarhelloagiscript.java

$

Next, add a call to your script in the asterisk dialing rules.

You may need to add an extension of 1300 to the extensions.conf default section:

[Default]

...

Exten =>1300,1,agi (Agi://localhost/hello.agi)

Replace the "localhost" in the code with the IP address of the running Asterisk-java machine.

Reload asterisk to make sure the changes take effect.

Now you have to map the name of the script Hello.agi to the helloagiscript we just created. By default, the Fastagi-mapping.properties property file under the Classpath path is called to complete by starting agiserver. Such as:

Hello.agi = Helloagiscript

Your directory should now contain the following files:

$ ls-l

-rw-r--r--1 srt srt 163689 2005-03-11 22:07asterisk-java.jar

-rw-r--r--1 srt SRT 2005-03-11 20:50fastagi-mapping.properties

-rw-r--r--1 SRT SRT 624 2005-03-11 22:07helloagiscript.class

-rw-r--r--1 SRT SRT 438 2005-03-11 20:50helloagiscript.java

Finally we run Agiserver:

$ Java-jar Asterisk-java.jar

If it is a Asterisk-java 0.3.1 or earlier version, you need to use

$ JAVA-CP Asterisk-java.jar:.org.asteriskjava.fastagi.defaultagiserver

On the Windows platform:

$ JAVA-CP asterisk-java.jar;. Org.asteriskjava.fastagi.DefaultAgiServer

If you see some log output that resembles the following, the Agiserver has successfully started running:

Mar, 2005 10:20:12 PMorg.asteriskjava.fastagi.DefaultAgiServer Run

Info:thread Pool started.

Mar, 2005 10:20:12 PMorg.asteriskjava.fastagi.DefaultAgiServer Run

Info:listening on *:4573.

When you call extension 1300, you'll seethe AGI script being launched:

Mar, 2005 10:22:47 PMorg.asteriskjava.fastagi.DefaultAgiServer Run

info:received connection.

Mar, 2005 10:22:47 PMorg.asteriskjava.fastagi.AgiConnectionHandler Run

Info:begin Agiscript Helloagiscript onagiserver-taskthread-0

Mar, 2005 10:22:48 PMorg.asteriskjava.fastagi.AGIConnectionHandler Run

Info:end Agiscript Helloagiscript onagiserver-taskthread-0

Extension examples

Looking at Baseagiscript's documentation, you'll find more ways to use it in your scripts. If the method you want to use is not in baseagiscript, or if you want to extend Fastagi with your command, you can use the Channel.sendcommand (Agicommand) method to send arbitrary commands.

With the AGI command you can pass parameters to your script via URL, in agirequest you can read these parameters by calling GetParameter (string) or Getparametervalues (string).

If you want to pass the name to user, the value is John's argument to your script Hello.agi, you can write:

Exten =>1300,1,agi (Agi://localhost/hello.agi?user=john)

You can pass multiple arguments, and the special characters in the arguments must be URL encoded.

If you are writing a complex script, please note that your agiscript must be thread safe. Only one instance is used to process all requests, which is an engine similar to the servlet container.

Configuration

You can adjust Defaultagiserver:bindport, poolsize by setting two attributes;

Bindport to set the TCP port server listening port. Fastagi The default port is 4573, if you have modified it, use the new port you set in extensions.conf. If you change the Bindport property to 1234, set the following in extensions.conf:

Exten =>1300,1,agi (Agi://localhost:1234/hello.agi)

Defaultagiserver uses a fixed-size thread pool for agiscripts. Poolsize determines how many threads can be used at the same time, so you should set it to the number of concurrent agiserver that can handle it. The default value for Poolsize is 10.

These property configurations can be configured by fastagi.properties files under Classpath.

As follows:

Bindport = 1234

Poolsize = 20

Asterisk-java Tutorial (Chinese version)--fastagi protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.