1
0

FreeswitchServerConnector.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Web;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using OpenSim.Server.Base;
  33. using OpenSim.Services.Interfaces;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. using OpenSim.Server.Handlers.Base;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. namespace OpenSim.Server.Handlers.Freeswitch
  40. {
  41. public class FreeswitchServerConnector : ServiceConnector
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private IFreeswitchService m_FreeswitchService;
  45. private string m_ConfigName = "FreeswitchService";
  46. protected readonly string m_freeSwitchAPIPrefix = "/fsapi";
  47. public FreeswitchServerConnector(IConfigSource config, IHttpServer server, string configName) :
  48. base(config, server, configName)
  49. {
  50. if (configName != String.Empty)
  51. m_ConfigName = configName;
  52. IConfig serverConfig = config.Configs[m_ConfigName];
  53. if (serverConfig == null)
  54. throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
  55. string freeswitchService = serverConfig.GetString("LocalServiceModule",
  56. String.Empty);
  57. if (freeswitchService.Length == 0)
  58. throw new Exception("No LocalServiceModule in config file");
  59. Object[] args = new Object[] { config };
  60. m_FreeswitchService =
  61. ServerUtils.LoadPlugin<IFreeswitchService>(freeswitchService, args);
  62. server.AddHTTPHandler(String.Format("{0}/freeswitch-config", m_freeSwitchAPIPrefix), FreeSwitchConfigHTTPHandler);
  63. server.AddHTTPHandler(String.Format("{0}/region-config", m_freeSwitchAPIPrefix), RegionConfigHTTPHandler);
  64. }
  65. public Hashtable FreeSwitchConfigHTTPHandler(Hashtable request)
  66. {
  67. Hashtable response = new Hashtable();
  68. response["str_response_string"] = string.Empty;
  69. response["content_type"] = "text/plain";
  70. response["keepalive"] = false;
  71. response["int_response_code"] = 500;
  72. Hashtable requestBody = ParseRequestBody((string) request["body"]);
  73. string section = (string) requestBody["section"];
  74. if (section == "directory")
  75. response = m_FreeswitchService.HandleDirectoryRequest(requestBody);
  76. else if (section == "dialplan")
  77. response = m_FreeswitchService.HandleDialplanRequest(requestBody);
  78. else
  79. m_log.WarnFormat("[FreeSwitchVoice]: section was {0}", section);
  80. return response;
  81. }
  82. private Hashtable ParseRequestBody(string body)
  83. {
  84. Hashtable bodyParams = new Hashtable();
  85. // split string
  86. string [] nvps = body.Split(new Char [] {'&'});
  87. foreach (string s in nvps)
  88. {
  89. if (s.Trim() != "")
  90. {
  91. string [] nvp = s.Split(new Char [] {'='});
  92. bodyParams.Add(HttpUtility.UrlDecode(nvp[0]), HttpUtility.UrlDecode(nvp[1]));
  93. }
  94. }
  95. return bodyParams;
  96. }
  97. public Hashtable RegionConfigHTTPHandler(Hashtable request)
  98. {
  99. Hashtable response = new Hashtable();
  100. response["content_type"] = "text/json";
  101. response["keepalive"] = false;
  102. response["int_response_code"] = 200;
  103. response["str_response_string"] = m_FreeswitchService.GetJsonConfig();
  104. return response;
  105. }
  106. }
  107. }