Java ESL program for connecting to the FreeSWITCH
Next simple Java program using Event Socket interface to control FreeSWITCH.
First you should read:
Next you should not forget to change
Now we can write simple java ESL program for connecting to the FreeSWITCH.
MyEslEventListener.java
event_socket.conf.xml
(to allow connections from any host on the network):
MyEslEventListener.java
package myeslevent; import java.util.Map; import java.util.Set; import org.freeswitch.esl.client.IEslEventListener; import org.freeswitch.esl.client.transport.event.EslEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyEslEventListener implements IEslEventListener { private final Logger log = LoggerFactory.getLogger(this.getClass()); @Override public void eventReceived(EslEvent event) { log.info("eventReceived [{}]\n[{}]\n", event, getEventToLog(event)); } @Override public void backgroundJobResultReceived(EslEvent event) { log.info("backgroundJobResultReceived [{}]\n[{}]\n", event, getEventToLog(event)); } private String getEventToLog(EslEvent event) { StringBuffer buf = new StringBuffer(); MapMyEslEventTest.javamap = event.getEventHeaders(); Set set = map.keySet(); for (String name : set) { buf.append(name + " " + map.get(name) + "\n"); } return buf.toString(); } }
package myeslevent; import java.util.Scanner; import org.freeswitch.esl.client.inbound.Client; import org.freeswitch.esl.client.inbound.InboundConnectionFailure; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyEslEventTest { private static final Logger log = LoggerFactory.getLogger(MyEslEventTest.class); public static void main(String[] args) throws InterruptedException, InboundConnectionFailure { System.out.println("--- hello freeswitch (terminate program to finish) ---\n"); Thread.sleep(2000); log.info("--- connect client ---"); Client client = new Client(); client.addEventListener(new MyEslEventListener()); client.connect("fshost", 8021, "ClueCon", 1); log.info("--- event subscription ---"); Thread.sleep(3000); client.setEventSubscriptions("plain", "all"); log.info("--- call 1000 1001 ---"); client.sendSyncApiCommand("originate", "user/1000 1001"); new Scanner(System.in).nextLine(); client.close(); System.out.println("\n--- Bye freeswitch ---"); } }When we run this program we can see on the console much more than below:
--- hello freeswitch (terminate program to finish) --- --- connect client --- Received message: [EslMessage: contentType=[auth/request] headers=1, body=0 lines.] Received message: [EslMessage: contentType=[command/reply] headers=2, body=0 lines.] --- event subscription --- Received message: [EslMessage: contentType=[command/reply] headers=2, body=0 lines.] --- call 1000 1001 --- Received message: [EslMessage: contentType=[api/response] headers=2, body=1 lines.] eventReceived [EslEvent: name=[API] headers=2, eventHeaders=16, eventBody=0 lines.] [ Event-Name API Event-Calling-Function switch_api_execute API-Command sofia_contact Event-Date-Timestamp 1385849755755588 API-Command-Argument */1000@[?].[?].[?].[?] ] eventReceived [EslEvent: name=[CHANNEL_OUTGOING] headers=2, eventHeaders=49, eventBody=0 lines.] [ Caller-Source src/switch_ivr_originate.c Event-Calling-Function switch_core_session_outgoing_channel Answer-State ringing Caller-Orig-Caller-ID-Number 0000000000 Channel-Name sofia/internal/sip:1000@[?].[?].[?].[?]:24878 ... ] 9302 [EslEventNotifier-1] INFO myeslevent.MyEslEventListener - eventReceived [EslEvent: name=[CHANNEL_STATE]] [ ... ] eventReceived [EslEvent: name=[CHANNEL_CREATE] headers=2, eventHeaders=76, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_ORIGINATE] headers=2, eventHeaders=76, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[PRESENCE_IN] headers=2, eventHeaders=63, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_STATE] headers=2, eventHeaders=50, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_CALLSTATE] headers=2, eventHeaders=53, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_STATE] headers=2, eventHeaders=50, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CODEC] headers=2, eventHeaders=60, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CODEC] headers=2, eventHeaders=58, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_PROGRESS] headers=2, eventHeaders=102, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_ANSWER] headers=2, eventHeaders=124, eventBody=0 lines.] [ ... ] 9320 [EslEventNotifier-1] INFO myeslevent.MyEslEventListener - eventReceived [EslEvent: name=[CHANNEL_OUTGOING]] [ ... ] eventReceived [EslEvent: name=[MESSAGE_QUERY] headers=2, eventHeaders=17, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[PRESENCE_IN] headers=2, eventHeaders=70, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_STATE] headers=2, eventHeaders=59, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_EXECUTE_COMPLETE] headers=2, eventHeaders=154, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[CHANNEL_EXECUTE] headers=2, eventHeaders=153, eventBody=0 lines.] [ ... ] ... eventReceived [EslEvent: name=[HEARTBEAT] headers=2, eventHeaders=27, eventBody=0 lines.] [ ... ] eventReceived [EslEvent: name=[RE_SCHEDULE] headers=2, eventHeaders=18, eventBody=0 lines.] [ ... ] ]]>Good start to do something cool :)
Komentarze