/// <summary> /// Configure communication object. Set the base directory. /// </summary> /// <param name="configuration"></param> protected void ConfigureCommObject(CommConfig configuration) { try { if (configuration == null) { configuration = defaultConfigs[this.identity]; } this.commObject = new CommunicationObject(configuration); if (this.commObject != null && configuration.BaseDirectory != null) { if (!Directory.Exists(configuration.BaseDirectory)) { configuration.BaseDirectory = Directory.CreateDirectory(configuration.BaseDirectory).FullName; } else { configuration.BaseDirectory = Path.GetFullPath(configuration.BaseDirectory); } this.commObject.SetBaseDirectory(configuration.BaseDirectory); } } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } }
/*----< constructor >------------------------------------------*/ /* * - starts listener listening on specified endpoint */ public CommunicationObject(CommConfig config) { this.Configuration = config; receiver = new MessageReceiver(); receiver.Start(this.Configuration.ServiceURL); sender = new MessageSender(this.Configuration.ServiceURL, config.Identity); }
/// <summary> /// Start the receiver and the listener thread. /// </summary> /// <param name="config"></param> protected void StartListener(CommConfig config = null) { ConfigureCommObject(config); this.MessageHandlerThread = new Thread(HandleCommMessage); this.MessageHandlerThread.IsBackground = true; if (this.MessageHandlerThread.ThreadState != ThreadState.Running) { this.MessageHandlerThread.Start(); } }
/// <summary> /// Load configuration file. /// </summary> /// <param name="filename"></param> private void LoadConfigurationFile(string filename = "") { filename = (filename == "") ? SystemConfiguration.ServerConfigurationFileName : filename; if (File.Exists(filename)) { int times = 0; while (times < 10) { try { XmlDocument document = new XmlDocument(); document.Load(filename); var elem = (document.ChildNodes[1] as XmlNode).ChildNodes; if (elem.Count > 0) { for (int i = 0; i < 3; i++) { var config = new CommConfig( (Identity)Enum.Parse(typeof(Identity), elem[i].Name), elem[i].ChildNodes[0].InnerText, elem[i].ChildNodes[1].InnerText, elem[i].ChildNodes[2].InnerText); this.defaultConfigs.Add(config.Identity, config); } break; } } catch (IOException) { Thread.Sleep(1000); this.defaultConfigs.Clear(); times++; } } } else { throw new FileNotFoundException("Application needs " + filename + " to execute."); } }