Dan
Dan

Reputation: 1470

Use ColdFusion to read events over a TCP/IP stream

Our new phone system is making use of Asterisk manager API which allows to read events and issue commands over a TCP/IP stream. My question is.. Is there any way at all to use ColdFusion to read (and in-turn process) the stream of events? As of now I'm able to view the phone events (incoming calls, transfers, hang-ups etc) via telnet and I'm wondering if it's possible to use a ColdFusion event gateway to process these events as they come over?

Once the initial connection is made (via telnet), I have to submit the following key:values in order to authenticate the connection before the stream begins.

Action: login<CRLF>
Username: usr<CRLF>
Secret: abc123<CRLF>
<CRLF>

Just wanted to specify that as I'm not sure if that's a deal-breaker with possibly using a web service in this manner. Also note we are using ColdFusion 10 Enterprise.

Upvotes: 1

Views: 1045

Answers (3)

Leigh
Leigh

Reputation: 28873

I realize this is an old thread, but I am posting this in case it helps the next guy ....

AFAIK, it cannot be done with a standard CF Event Gateway. However, one possibility is using Asterisk-Java. It is a java library that allows communication with an Asterisk Server. More specifically it Manager interface:

... is capable of sending [actions] and receiving [responses] and [events]. It does not add any further functionality but rather provides a Java view to Asterisk's Manager API (freeing you from TCP/IP connection and parsing stuff).

So it can be used to issue commands to the server, and receive events, just like telnet.

Starter example:

  1. Download the Asterisk-Java jar and load it via this.javaSettings in your Application.cfc

  2. Create a ManagerConnection with the settings for your Asterisk server

    factory = createObject("java", "org.asteriskjava.manager.ManagerConnectionFactory");
    connection = factory.init( "hostNameOrIP"
                             , portNum
                             , "userName"
                             , "theSecret" ).createManagerConnection();
    
  3. Create a CFC to act as a listener. It will receive and handle events from Asterisk:

    component {
        public void function onManagerEvent(any managerEvent)
        {
            // For demo purposes, just output a summary of the event
            WriteLog( text=arguments.managerEvent.toString(), file="AsteriskLog" );
        }
    }
    
  4. Using a bit of dynamic proxy magic, register the CFC with the connection:

    proxyListener = createDynamicProxy("path.YourCFCListener"
                     , ["org.asteriskjava.manager.ManagerEventListener"]);
    connection.addEventListener( proxyListener );
    
  5. Login to the server to begin receiving events. Setting the appropriate event level: "off", "on" or csv list of available events - "system", "call" and/or "log".

    connection.login("on");
    
  6. Run a simple "Ping" test to verify everything is working. Then sleep for a few seconds to allow some events to flow. Then close the connection.

    action = createObject("java", "org.asteriskjava.manager.action.PingAction").init();
    response = application.connection.sendAction(action);
    writeDump(response.getResponse());      
    
    // disconnect and stop events
    sleep(4000);
    connection.logoff();
    
  7. Check the demo log file. It should contain one or more events.

    "Information","http-bio-8500-exec-4","10/14/16","15:17:19","XXXXX","org.asteriskjava.manager.event.ConnectEvent[dateReceived=Fri Oct 14 15:17:19 CDT 2016,....]"
    

NB: In a real application, the connection would probably be opened once, in OnApplicationStart, and stored in a persistent scope. Events would continue to stream as long as the connection remained open. The connection should only be closed when the application ends, to halt event streaming.

Upvotes: 1

Russ
Russ

Reputation: 1951

What you want is a server-side TCP client. I suggest easySocket, a simple UDF that allows you to send TCP messages via Coldfusion by utilizing Java sockets.

Upvotes: 0

Russ
Russ

Reputation: 1951

Yes-- you'd want to use a Socket Gateway. Ben Nadel has a great writeup about how to do this: Using Socket Gateways To Communicate Between ColdFusion And Node.js

Although he uses Node.js in his example, you should be able to use his guide to set up the Socket Gateway, then handle the data passed to it as you see fit.

Upvotes: 0

Related Questions