123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using System.Collections;
- using System.Web;
- using System.Reflection;
- using Nini.Config;
- using OpenSim.Server.Base;
- using OpenSim.Services.Interfaces;
- using OpenSim.Framework.Servers.HttpServer;
- using OpenSim.Server.Handlers.Base;
- using log4net;
- using OpenMetaverse;
- using OpenMetaverse.StructuredData;
- namespace OpenSim.Server.Handlers.Freeswitch
- {
- public class FreeswitchServerConnector : ServiceConnector
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private IFreeswitchService m_FreeswitchService;
- private string m_ConfigName = "FreeswitchService";
- protected readonly string m_freeSwitchAPIPrefix = "/fsapi";
- public FreeswitchServerConnector(IConfigSource config, IHttpServer server, string configName) :
- base(config, server, configName)
- {
- if (configName != String.Empty)
- m_ConfigName = configName;
- IConfig serverConfig = config.Configs[m_ConfigName];
- if (serverConfig == null)
- throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
- string freeswitchService = serverConfig.GetString("LocalServiceModule",
- String.Empty);
- if (freeswitchService == String.Empty)
- throw new Exception("No LocalServiceModule in config file");
- Object[] args = new Object[] { config };
- m_FreeswitchService =
- ServerUtils.LoadPlugin<IFreeswitchService>(freeswitchService, args);
- server.AddHTTPHandler(String.Format("{0}/freeswitch-config", m_freeSwitchAPIPrefix), FreeSwitchConfigHTTPHandler);
- server.AddHTTPHandler(String.Format("{0}/region-config", m_freeSwitchAPIPrefix), RegionConfigHTTPHandler);
- }
- public Hashtable FreeSwitchConfigHTTPHandler(Hashtable request)
- {
- Hashtable response = new Hashtable();
- response["str_response_string"] = string.Empty;
- response["content_type"] = "text/plain";
- response["keepalive"] = false;
- response["int_response_code"] = 500;
- Hashtable requestBody = ParseRequestBody((string) request["body"]);
- string section = (string) requestBody["section"];
- if (section == "directory")
- response = m_FreeswitchService.HandleDirectoryRequest(requestBody);
- else if (section == "dialplan")
- response = m_FreeswitchService.HandleDialplanRequest(requestBody);
- else
- m_log.WarnFormat("[FreeSwitchVoice]: section was {0}", section);
- return response;
- }
- private Hashtable ParseRequestBody(string body)
- {
- Hashtable bodyParams = new Hashtable();
-
- string [] nvps = body.Split(new Char [] {'&'});
- foreach (string s in nvps)
- {
- if (s.Trim() != "")
- {
- string [] nvp = s.Split(new Char [] {'='});
- bodyParams.Add(HttpUtility.UrlDecode(nvp[0]), HttpUtility.UrlDecode(nvp[1]));
- }
- }
- return bodyParams;
- }
- public Hashtable RegionConfigHTTPHandler(Hashtable request)
- {
- Hashtable response = new Hashtable();
- response["content_type"] = "text/json";
- response["keepalive"] = false;
- response["int_response_code"] = 200;
- response["str_response_string"] = m_FreeswitchService.GetJsonConfig();
- return response;
- }
- }
- }
|