PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-servers/SIPSorcery.SIPAppServer/SIPAppServerProgram.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 115 lines | 84 code | 17 blank | 14 comment | 13 complexity | ee51531ddfcd9466bc5a1c29e28aff25 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using SIPSorcery.CRM;
  10. using SIPSorcery.Net;
  11. using SIPSorcery.Persistence;
  12. using SIPSorcery.SIP;
  13. using SIPSorcery.SIP.App.Entities;
  14. using SIPSorcery.Sys;
  15. using log4net;
  16. namespace SIPSorcery.SIPAppServer
  17. {
  18. class MainConsole
  19. {
  20. private static readonly string m_storageTypeKey = SIPSorceryConfiguration.PERSISTENCE_STORAGETYPE_KEY;
  21. private static readonly string m_connStrKey = SIPSorceryConfiguration.PERSISTENCE_STORAGECONNSTR_KEY;
  22. private static ILog logger = AppState.logger;
  23. private static ManualResetEvent m_proxyUp = new ManualResetEvent(false);
  24. private static StorageTypes m_serverStorageType;
  25. private static string m_serverStorageConnStr;
  26. [STAThread]
  27. static void Main(string[] args)
  28. {
  29. bool isConsole = false;
  30. try
  31. {
  32. // Get DateTime.ToString() to use a format ot ToString("o") instead of ToString("G").
  33. CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
  34. culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
  35. culture.DateTimeFormat.LongTimePattern = "THH:mm:ss.fffffffzzz";
  36. Thread.CurrentThread.CurrentCulture = culture;
  37. m_serverStorageType = (AppState.GetConfigSetting(m_storageTypeKey) != null) ? StorageTypesConverter.GetStorageType(AppState.GetConfigSetting(m_storageTypeKey)) : StorageTypes.Unknown;
  38. m_serverStorageConnStr = AppState.GetConfigSetting(m_connStrKey);
  39. bool monitorCalls = true;
  40. if (m_serverStorageType == StorageTypes.Unknown || m_serverStorageConnStr.IsNullOrBlank())
  41. {
  42. throw new ApplicationException("The SIP Application Service cannot start with no persistence settings specified.");
  43. }
  44. // Need to force the System.Data.Entity assembly to load before a dialplan instantiation. The assembly will fail to load if
  45. // requested from the Dynamic Language Runtime which is what happens if the first time it's requested is in an IronRuby dialplan.
  46. using (SIPSorceryAppEntities appEntities = new SIPSorceryAppEntities())
  47. {
  48. logger.Debug("Lookups count=" + (from lookup in appEntities.SIPDialplanLookups select lookup).Count() + ", forcing entity framework assemblies to load.");
  49. }
  50. //if (args != null && args.Length > 0)
  51. //{
  52. isConsole = true;
  53. Console.WriteLine("SIP App Server starting");
  54. logger.Debug("SIP App Server Console starting...");
  55. string sipSocket = null;
  56. string callManagerSvcAddress = null;
  57. foreach (string arg in args) {
  58. if (arg.StartsWith("-sip:")) {
  59. sipSocket = arg.Substring(5);
  60. }
  61. else if (arg.StartsWith("-cms:")) {
  62. callManagerSvcAddress = arg.Substring(5);
  63. }
  64. else if (arg.StartsWith("-hangupcalls:")) {
  65. monitorCalls = Convert.ToBoolean(arg.Substring(13));
  66. }
  67. }
  68. SIPAppServerDaemon daemon = null;
  69. if (sipSocket.IsNullOrBlank() || callManagerSvcAddress.IsNullOrBlank()) {
  70. daemon = new SIPAppServerDaemon(m_serverStorageType, m_serverStorageConnStr);
  71. }
  72. else {
  73. daemon = new SIPAppServerDaemon(m_serverStorageType, m_serverStorageConnStr, SIPEndPoint.ParseSIPEndPoint(sipSocket), callManagerSvcAddress, monitorCalls);
  74. }
  75. Thread daemonThread = new Thread(new ThreadStart(daemon.Start));
  76. daemonThread.Start();
  77. m_proxyUp.WaitOne();
  78. //}
  79. //else
  80. //{
  81. // logger.Debug("SIP App Server Windows Service Starting...");
  82. // System.ServiceProcess.ServiceBase[] ServicesToRun;
  83. // SIPAppServerDaemon daemon = new SIPAppServerDaemon(m_serverStorageType, m_serverStorageConnStr);
  84. // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service(daemon) };
  85. // System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  86. //}
  87. }
  88. catch (Exception excp)
  89. {
  90. Console.WriteLine("Exception SIP App Server Main. " + excp.Message);
  91. if (isConsole) {
  92. Console.WriteLine("press any key to exit...");
  93. Console.ReadLine();
  94. }
  95. }
  96. }
  97. }
  98. }