PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/thecc4re/sipsorcery-mono
C# | 65 lines | 52 code | 13 blank | 0 comment | 13 complexity | c751b7e7aeb6c4f695298b7a64bfe985 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using SIPSorcery.Persistence;
  8. using SIPSorcery.SIP.App;
  9. using SIPSorcery.Sys;
  10. using log4net;
  11. namespace SIPSorcery.SIPRegistrar {
  12. public class SIPRegistrarProgram {
  13. private static readonly string m_storageTypeKey = SIPSorceryConfiguration.PERSISTENCE_STORAGETYPE_KEY;
  14. private static readonly string m_connStrKey = SIPSorceryConfiguration.PERSISTENCE_STORAGECONNSTR_KEY;
  15. private static readonly string m_sipAccountsXMLFilename = SIPSorcery.SIP.App.AssemblyState.XML_SIPACCOUNTS_FILENAME;
  16. private static readonly string m_sipRegistrarBindingsXMLFilename = SIPSorcery.SIP.App.AssemblyState.XML_REGISTRAR_BINDINGS_FILENAME;
  17. private static ILog logger = AppState.logger;
  18. private static ManualResetEvent m_registrarUp = new ManualResetEvent(false);
  19. private static StorageTypes m_sipRegistrarStorageType;
  20. private static string m_sipRegistrarStorageConnStr;
  21. static void Main(string[] args) {
  22. try {
  23. m_sipRegistrarStorageType = (AppState.GetConfigSetting(m_storageTypeKey) != null) ? StorageTypesConverter.GetStorageType(AppState.GetConfigSetting(m_storageTypeKey)) : StorageTypes.Unknown; ;
  24. m_sipRegistrarStorageConnStr = AppState.GetConfigSetting(m_connStrKey);
  25. if (m_sipRegistrarStorageType == StorageTypes.Unknown || m_sipRegistrarStorageConnStr.IsNullOrBlank()) {
  26. throw new ApplicationException("The SIP Registrar cannot start with no persistence settings.");
  27. }
  28. if (m_sipRegistrarStorageType == StorageTypes.XML && !Directory.Exists(m_sipRegistrarStorageConnStr)) {
  29. throw new ApplicationException("Directory " + m_sipRegistrarStorageConnStr + " does not exist for XML persistor.");
  30. }
  31. SIPAssetPersistor<SIPAccount> sipAccountsPersistor = SIPAssetPersistorFactory<SIPAccount>.CreateSIPAssetPersistor(m_sipRegistrarStorageType, m_sipRegistrarStorageConnStr, m_sipAccountsXMLFilename);
  32. SIPDomainManager sipDomainManager = new SIPDomainManager(m_sipRegistrarStorageType, m_sipRegistrarStorageConnStr);
  33. SIPAssetPersistor<SIPRegistrarBinding> sipRegistrarBindingPersistor = SIPAssetPersistorFactory<SIPRegistrarBinding>.CreateSIPAssetPersistor(m_sipRegistrarStorageType, m_sipRegistrarStorageConnStr, m_sipRegistrarBindingsXMLFilename);
  34. SIPRegistrarDaemon daemon = new SIPRegistrarDaemon(sipDomainManager.GetDomain, sipAccountsPersistor.Get, sipRegistrarBindingPersistor, SIPRequestAuthenticator.AuthenticateSIPRequest);
  35. if (args != null && args.Length == 1 && args[0].StartsWith("-c")) {
  36. Console.WriteLine("SIP Registrar starting");
  37. Thread daemonThread = new Thread(daemon.Start);
  38. daemonThread.Start();
  39. m_registrarUp.WaitOne();
  40. }
  41. else {
  42. System.ServiceProcess.ServiceBase[] ServicesToRun;
  43. ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service(daemon) };
  44. System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  45. }
  46. }
  47. catch (Exception excp) {
  48. Console.WriteLine("Exception Main. " + excp.Message);
  49. }
  50. }
  51. }
  52. }