0% found this document useful (0 votes)
28 views17 pages

Asterisk Server Management Overview

This document discusses code for connecting to an Asterisk server and managing agents. It defines a BackgroundAsteriskServer class that implements an AsteriskServer interface to connect to and control an Asterisk instance in the background. It includes methods for starting and stopping the connection, retrieving information like channels and queues, and originating calls.

Uploaded by

Marcus Andreas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views17 pages

Asterisk Server Management Overview

This document discusses code for connecting to an Asterisk server and managing agents. It defines a BackgroundAsteriskServer class that implements an AsteriskServer interface to connect to and control an Asterisk instance in the background. It includes methods for starting and stopping the connection, retrieving information like channels and queues, and originating calls.

Uploaded by

Marcus Andreas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ManagerResponse loginResponse; QueueAddAction loginAction = new QueueAddAction(); [Link]("Bob"); [Link]("8889"); [Link]("Agent/205"); [Link](false); loginResponse = [Link](loginAction, 0); [Link].

println("Login Response:" + [Link]()); [Link]("Login Message:" + [Link]()); but then when I run the command: freepbx*CLI> agent show 205 (Bob) not logged in (musiconhold is 'default') 1 agents configured [0 online , 1 offline]

package [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; /** * A service-oriented implementation of the AsteriskServer interface. * * @see [Link] * @author gmi */ public class BackgroundAsteriskServer implements AsteriskServer { private final AsteriskServerImpl impl;

private boolean started = false; private String hostname = "localhost"; private int port = 5038; private String username = "asterisk"; private String password = "asterisk"; private boolean ssl = false; private int socketTimeout = 5000; private int socketReadTimeout = 60000;

private FirstInitializationThread initThread = null; public BackgroundAsteriskServer() { impl = new AsteriskServerImpl(); /* * Must be called in constructor to guarantee that an addListener() * call won't start the server automatically (in Spring there is no * guaranteed setters call order I think). */ [Link](false); } /* * Setters. Should be called before start() if needed. */ public void setHostname(String hostname) { [Link] = hostname; }

public void setPort(int port) { [Link] = port; }

public void setUsername(String username) { [Link] = username; } public void setPassword(String password) { [Link] = password; }

public void setSsl(boolean ssl) { [Link] = ssl; } public void setSocketTimeout(int socketTimeout) { [Link] = socketTimeout; } public void setSocketReadTimeout(int socketReadTimeout) { [Link] = socketReadTimeout; } public void setSkipQueues(boolean skipQueues) { [Link](skipQueues); } public void setManagerConnection(ManagerConnection eventConnection) { throw new UnsupportedOperationException("You are not supposed to set the eventConnection yourself on this implementation"); } public void initialize() throws ManagerCommunicationException { throw new UnsupportedOperationException("You are not supposed to call initialize yourself on this implementation"); } /* Implementation of the AsteriskServer interface */ public ManagerConnection getManagerConnection() { return [Link](); } public AsteriskChannel originateToExtension(String channel, String context, String exten, int priority, long timeout) throws ManagerCommunicationException, NoSuchChannelException { return [Link](channel, context, exten, priority, timeout); } public AsteriskChannel originateToExtension(String channel, String context,

String exten, int priority, long timeout, CallerId callerId, Map<String, String> variables) throws ManagerCommunicationException, NoSuchChannelException { return [Link](channel, context, exten, priority, timeout, callerId, variables); } public AsteriskChannel originateToApplication(String channel, String application, String data, long timeout) throws ManagerCommunicationException, NoSuchChannelException { return [Link](channel, application, data, timeout); } public AsteriskChannel originateToApplication(String channel, String application, String data, long timeout, CallerId callerId, Map<String, String> variables) throws ManagerCommunicationException, NoSuchChannelException { return [Link](channel, application, data, timeout, + callerId, variables); } public void originateToApplicationAsync(String channel, String application, String data, long timeout, CallerId callerId, Map<String, String> variables, OriginateCallback cb) throws ManagerCommunicationException { [Link](channel, application, data, timeout, callerId, variables, cb); } public void originateToApplicationAsync(String channel, String application, String data, long timeout, OriginateCallback cb) throws ManagerCommunicationException { [Link](channel, application, data, timeout, cb); } public void originateToExtensionAsync(String channel, String context, String exten, int priority, long timeout, CallerId callerId, Map<String, String> variables, OriginateCallback cb) throws ManagerCommunicationException

{ [Link](channel, context, exten, priority, timeout, callerId, variables, cb); } public void originateToExtensionAsync(String channel, String context, String exten, int priority, long timeout, OriginateCallback cb) throws ManagerCommunicationException { [Link](channel, context, exten, priority, timeout, cb); } public Collection<AsteriskChannel> getChannels() throws ManagerCommunicationException { return [Link](); } public AsteriskChannel getChannelByName(String name) throws ManagerCommunicationException { return [Link](name); } public AsteriskChannel getChannelById(String id) throws ManagerCommunicationException { return [Link](id); } public Collection<MeetMeRoom> getMeetMeRooms() throws ManagerCommunicationException { return [Link](); } public MeetMeRoom getMeetMeRoom(String name) throws ManagerCommunicationException { return [Link](name); } public Collection<AsteriskQueue> getQueues() throws ManagerCommunicationException

{ return [Link](); } public String getVersion() throws ManagerCommunicationException { return [Link](); } public ConfigFile getConfig(String filename) throws ManagerCommunicationException { return [Link](filename); } public int[] getVersion(String file) throws ManagerCommunicationException { return [Link](file); } public String getGlobalVariable(String variable) throws ManagerCommunicationException { return [Link](variable); } public void setGlobalVariable(String variable, String value) throws ManagerCommunicationException { [Link](variable, value); } public Collection<Voicemailbox> getVoicemailboxes() throws ManagerCommunicationException { return [Link](); } public List<String> executeCliCommand(String command) throws ManagerCommunicationException { return [Link](command); } public void addAsteriskServerListener(AsteriskServerListener listener) throws ManagerCommunicationException

{ [Link](listener); } public void removeAsteriskServerListener(AsteriskServerListener listener) { [Link](listener); } public void shutdown() { throw new UnsupportedOperationException("You are not supposed to call shutdown yourself on this implementation. Use stop() instead"); } public List<PeerEntryEvent> getPeerEntries() throws ManagerCommunicationException { return [Link](); } public DbGetResponseEvent dbGet(String family, String key) throws ManagerCommunicationException { return [Link](family,key); } public void dbDel(String family, String key) throws ManagerCommunicationException { [Link](family, key); } public void dbPut(String family, String key, String value) throws ManagerCommunicationException { [Link](family, key, value); } public AsteriskChannel getChannelByNameAndActive(String name) throws ManagerCommunicationException { return [Link](name); } /* GMI PRIVATE */ public ManagerResponse getSipShowPeerResponse(String sippeer) throws ManagerCommunicationException {

return [Link](sippeer); } public Collection<AsteriskAgent> getAgents() throws ManagerCommunicationException { return [Link](); } private ManagerConnection createNewConnection() { ManagerConnectionImpl myConnection = new ManagerConnectionImpl(); [Link](hostname); [Link](username); [Link](password); [Link](port); [Link](ssl); [Link](socketTimeout); [Link](socketReadTimeout); return myConnection; } public synchronized void start() { if (started) { return; } [Link](createNewConnection()); // This thread will loop until the first successful connection // Reconnection will be handled by the connection itself (ConnectEvent) [Link] = new FirstInitializationThread(); started = true; } public synchronized void stop() { if (! started) { return; } if ([Link] != null && ! [Link]()) { [Link](); }

initThread = null; [Link](); started = false; } class FirstInitializationThread implements Runnable { Thread myThread; private boolean abort = false; private boolean done = false; FirstInitializationThread() { myThread = new Thread(this); [Link]("First initialization thread"); [Link](); }

public void run() { while (! [Link]() && ! abort ) try { [Link](); } catch (ManagerCommunicationException e) { try { [Link](1000); } catch (InterruptedException e1) { // swallow } } done = true; } void abort() { abort = true; // Interrupt sleep if (myThread != null) [Link](); // Free resources myThread = null; }

boolean isDone() { return done; } } }

private SipEventListenerTest managerEventListener; private ManagerConnection managerConnection; private Logger log = [Link]([Link]); private OriginateAction asteriskAction = null; public AsteriskManagerTest(String asteriskHost, int asteriskManagerPort, String asteriskManagerUser, String asteriskManagerPsw) { ManagerConnectionFactory factory = new ManagerConnectionFactory(asteriskHost, asteriskManagerPort, asteriskManagerUser, asteriskManagerPsw); [Link] = [Link](); [Link]("Creata connessione al server Asterisk " + [Link]()); } @Override public void run() { try { [Link]([Link]); [Link](); //CONTROLLO LA VALIDITA' DELL'AZIONE [Link]("getAsteriskAction().getChannel() " + getAsteriskAction().getChannel()); [Link]("getAsteriskAction().getContext() " + getAsteriskAction().getContext()); [Link]("getAsteriskAction().getExten() " + getAsteriskAction().getExten()); ResponseEvents risposte = [Link](getAsteriskAction(), 3000000); [Link](); [Link] = null;

try { [Link](10000); } catch (InterruptedException ex) { } } catch (IllegalStateException ex) { [Link]("Illegal State Exception", ex); } catch (IOException ex) { [Link]("IO Exception", ex); } catch (AuthenticationFailedException ex) { [Link]("AuthenticationFailedException", ex); } catch (TimeoutException ex) { [Link]("TimeoutException" + [Link]()); } [Link]("**************************** FINE DEL RUN() ASTERISK_MANAGER ***************************************************"); } public static void main(String[] args) throws Exception { AsteriskManagerTest manager = null; for (int i = 0; i < 2; i++) { manager = new AsteriskManagerTest("host", 5038, "manager", "psw"); OriginateAction action = new OriginateAction(); if (i == 0) { [Link]("SIP/trunk/number1"); } else { [Link]("SIP/ehiweb/number2"); } [Link]("outgoing"); [Link]("s"); [Link](new Integer(1)); [Link](new Long(30000)); [Link]("data", "12/01/18/30"); [Link]("PROVA"); [Link](action); //LISTENER [Link](new SipEventListenerTest()); [Link]("**************************** START THREAD N." + i + " ASTERISK_MANAGER ***************************************************"); [Link](); } }

/** * @return the managerEventListener */ public ManagerEventListener getManagerEventListener() { return managerEventListener; } /** * @param managerEventListener the managerEventListener to set */ public void setManagerEventListener(SipEventListenerTest managerEventListener) { [Link] = managerEventListener; } /** * @return the asteriskAction */ public OriginateAction getAsteriskAction() { return asteriskAction; } /** * @param asteriskAction the asteriskAction to set */ public void setAsteriskAction(OriginateAction asteriskAction) { [Link] = asteriskAction; } }

public class SipEventListenerTest extends CallManagerEvent implements ManagerEventListener { @Override public void onManagerEvent(ManagerEvent event) { [Link]("Event " + ((AbstractChannelEvent) event).getUniqueId() + " - " + event); } }

import import import import import

[Link]; [Link]; [Link]; [Link]; [Link];

import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link] renceJoinEvent; import [Link] renceLeaveEvent; import [Link] renceMemberMuteEvent; import [Link] renceMemberUnmuteEvent; import [Link] renceStateEvent;

public class HelloEvents implements ManagerEventListener { private static final String KONFERENCE_LIST_COMMAND = "konference list"; private static final Pattern KONFERENCE_LIST_PATTERN = [Link]("^MemberId:(.+)CIDName:(.+)CID:(.+)Muted:(.+)UniqueID: (.+)ConfName:(.+)Speaking:(.+)Channel:(.+)$"); private ManagerConnection managerConnection; public HelloEvents() throws IOException { ManagerConnectionFactory factory = new ManagerConnectionFactory( "[Link]", "bbb", "secret"); [Link] = [Link](); [Link]([Link]); [Link]([Link]); [Link]([Link]); [Link]([Link] ss); [Link](ConferenceMemberUnmuteEvent.c lass); } public void run() throws IOException, AuthenticationFailedException, TimeoutException, InterruptedException { // register for events [Link](this); // connect to Asterisk and log in [Link](); // request channel state [Link](new StatusAction()); // wait 10 seconds for events to come in [Link](10000); // // } private void populateRoom(String room) { final CommandAction meetMeListAction; ManagerResponse response = null; final List<String> lines; final Collection<Integer> userNumbers = new ArrayList<Integer>(); // list of user numbers in the room meetMeListAction = new CommandAction(KONFERENCE_LIST_COMMAND + " " + room); populateRoom("85115"); // and finally log off and disconnect [Link]();

try { response = [Link](meetMeListAction); } catch (TimeoutException e) { [Link]("Unable to send \"" + KONFERENCE_LIST_COMMAND + "\" command"); return; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block [Link](); } catch (IllegalStateException e) { // TODO Auto-generated catch block [Link](); } catch (IOException e) { // TODO Auto-generated catch block [Link](); } if (response instanceof ManagerError) { [Link]("Unable to send \"" + KONFERENCE_LIST_COMMAND + "\" command: " + [Link]()); return; } if (!(response instanceof CommandResponse)) { [Link]("Response to \"" + KONFERENCE_LIST_COMMAND + "\" command is not a CommandResponse but " + [Link]()); return; } lines = ((CommandResponse) response).getResult(); for (String line : lines) { [Link](line); /* final Matcher matcher; final Integer userNumber; boolean muted = false; boolean talking = false; ConferenceMemberImpl channelUser; ConferenceMemberImpl roomUser; matcher = KONFERENCE_LIST_PATTERN.matcher(line); if (![Link]()) { continue; } userNumber = [Link]([Link](1)); channel = [Link]([Link](2)); [Link](userNumber); if ([Link]("(Admin Muted)") || [Link]("(Muted)")) { muted = true; } if ([Link]("(talking)")) { talking = true; }

channelUser = [Link](); if (channelUser != null && [Link]() != room) { [Link]([Link]()); channelUser = null; } roomUser = [Link](userNumber); if (roomUser != null && [Link]() != channel) { [Link](roomUser); roomUser = null; } if (channelUser == null && roomUser == null) { final ConferenceMemberImpl user; // using the current date as dateJoined is not correct but we // don't have anything that is better user = new ConferenceMemberImpl(server, room, userNumber, channel, [Link]()); [Link](muted); [Link](talking); [Link](user); [Link](user); [Link](user); } else if (channelUser != null && roomUser == null) { [Link](muted); [Link](channelUser); } else if (channelUser == null) { [Link](muted); [Link](roomUser); } else { if (channelUser != roomUser) { [Link]("Inconsistent state: channelUser != roomUser, channelUser=" + channelUser + ", roomUser=" + roomUser); } } */ } /* Collection<ConferenceMemberImpl> users = [Link](); Collection<ConferenceMemberImpl> usersToRemove = new ArrayList<ConferenceMemberImpl>(); for (ConferenceMemberImpl user : users) { if (![Link]([Link]())) { // remove user as he is no longer in the room [Link](user); } } for (ConferenceMemberImpl user : usersToRemove) { [Link]([Link]()); [Link](user); [Link]().setMeetMeUserImpl(null); } */ }

public void onManagerEvent(ManagerEvent event) { // just print received events [Link](event); } public static void main(String[] args) throws Exception { HelloEvents helloEvents; helloEvents = new HelloEvents(); [Link](); while (true) { } } }

You might also like