PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-servers/SIPSorcery.SIPRegistrar/SIPRegistrarState.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 133 lines | 82 code | 12 blank | 39 comment | 8 complexity | bfae4ffc4483a51d36f33bc89f2687d8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: SIPRegistrarState.cs
  3. //
  4. // Description:
  5. // Application configuration for a SIP Registrar Server.
  6. //
  7. // Author(s):
  8. // Aaron Clauson
  9. //
  10. // History:
  11. // 29 Mar 2009 Aaron Clauson Created.
  12. //
  13. // License:
  14. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  15. //
  16. // Copyright (c) 2009 Aaron Clauson (aaronc@blueface.ie), Blue Face Ltd, Dublin, Ireland (www.blueface.ie)
  17. // All rights reserved.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  20. // the following conditions are met:
  21. //
  22. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  23. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  24. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd.
  25. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  26. // prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  29. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  31. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. // ============================================================================
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Net;
  40. using System.Text;
  41. using System.Xml;
  42. using log4net;
  43. using SIPSorcery.Sys;
  44. namespace SIPSorcery.SIPRegistrar
  45. {
  46. /// <summary>
  47. /// Retrieves application conifguration settings from App.Config.
  48. /// </summary>
  49. public class SIPRegistrarState
  50. {
  51. private const string LOGGER_NAME = "siprergistrar";
  52. public const string SIPREGISTRAR_CONFIGNODE_NAME = "sipregistrar";
  53. private const string SIPSOCKETS_CONFIGNODE_NAME = "sipsockets";
  54. private const string USERAGENTS_CONFIGNODE_NAME = "useragentconfigs";
  55. private const string MONITOR_LOOPBACK_PORT_KEY = "MonitorLoopbackPort";
  56. private const string MAXIMUM_ACCOUNT_BINDINGS_KEY = "MaximumAccountBindings";
  57. private const string NATKEEPALIVE_RELAY_SOCKET = "NATKeepAliveRelaySocket";
  58. private const string SWITCHBOARD_CERTIFICATE_NAME_KEY = "SwitchboardCertificateName";
  59. private const string THREAD_COUNT_KEY = "ThreadCount";
  60. public static ILog logger;
  61. private static readonly XmlNode m_sipRegistrarNode;
  62. public static readonly XmlNode SIPRegistrarSocketsNode;
  63. public static readonly XmlNode UserAgentsConfigNode;
  64. public static readonly int MonitorLoopbackPort;
  65. public static readonly int MaximumAccountBindings = 1;
  66. public static IPEndPoint NATKeepAliveRelaySocket;
  67. public static readonly string SwitchboardCertificateName;
  68. public static readonly int ThreadCount = 1;
  69. static SIPRegistrarState()
  70. {
  71. try
  72. {
  73. #region Configure logging.
  74. try
  75. {
  76. log4net.Config.XmlConfigurator.Configure();
  77. logger = log4net.LogManager.GetLogger(LOGGER_NAME);
  78. }
  79. catch (Exception logExcp)
  80. {
  81. Console.WriteLine("Exception SIPRegistrarState Configure Logging. " + logExcp.Message);
  82. }
  83. #endregion
  84. if (AppState.GetSection(SIPREGISTRAR_CONFIGNODE_NAME) != null)
  85. {
  86. m_sipRegistrarNode = (XmlNode)AppState.GetSection(SIPREGISTRAR_CONFIGNODE_NAME);
  87. }
  88. if (m_sipRegistrarNode == null)
  89. {
  90. //throw new ApplicationException("The SIP Registrar could not be started, no " + SIPREGISTRAR_CONFIGNODE_NAME + " config node available.");
  91. logger.Warn("The SIP Registrar " + SIPREGISTRAR_CONFIGNODE_NAME + " config node was not available, the agent will not be able to start.");
  92. }
  93. else
  94. {
  95. SIPRegistrarSocketsNode = m_sipRegistrarNode.SelectSingleNode(SIPSOCKETS_CONFIGNODE_NAME);
  96. if (SIPRegistrarSocketsNode == null)
  97. {
  98. throw new ApplicationException("The SIP Registrar could not be started, no " + SIPSOCKETS_CONFIGNODE_NAME + " node could be found.");
  99. }
  100. UserAgentsConfigNode = m_sipRegistrarNode.SelectSingleNode(USERAGENTS_CONFIGNODE_NAME);
  101. Int32.TryParse(AppState.GetConfigNodeValue(m_sipRegistrarNode, MONITOR_LOOPBACK_PORT_KEY), out MonitorLoopbackPort);
  102. Int32.TryParse(AppState.GetConfigNodeValue(m_sipRegistrarNode, MAXIMUM_ACCOUNT_BINDINGS_KEY), out MaximumAccountBindings);
  103. if (!AppState.GetConfigNodeValue(m_sipRegistrarNode, NATKEEPALIVE_RELAY_SOCKET).IsNullOrBlank())
  104. {
  105. NATKeepAliveRelaySocket = IPSocket.ParseSocketString(AppState.GetConfigNodeValue(m_sipRegistrarNode, NATKEEPALIVE_RELAY_SOCKET));
  106. }
  107. SwitchboardCertificateName = AppState.GetConfigNodeValue(m_sipRegistrarNode, SWITCHBOARD_CERTIFICATE_NAME_KEY);
  108. if (!AppState.GetConfigNodeValue(m_sipRegistrarNode, THREAD_COUNT_KEY).IsNullOrBlank())
  109. {
  110. Int32.TryParse(AppState.GetConfigNodeValue(m_sipRegistrarNode, THREAD_COUNT_KEY), out ThreadCount);
  111. }
  112. }
  113. }
  114. catch (Exception excp)
  115. {
  116. logger.Error("Exception SIPRegistrarState. " + excp.Message);
  117. throw;
  118. }
  119. }
  120. }
  121. }