/// <summary> /// Initialize TabControl along with tab pages /// </summary> private void InitializeTabControl() { this.tabControl = new TabControl(); this.tabControl.Dock = DockStyle.Fill; // Status Tab events this.statusTab = new StatusTab(); this.statusTab.StartButtonClick += delegate(Object o, EventArgs e) { this.logic.Start(this); }; this.statusTab.StopButtonClick += delegate(Object o, EventArgs e) { this.logic.Stop(this); }; this.statusTab.NowButtonClick += delegate(Object o, EventArgs e) { this.logic.ConnectNow(this); }; // Settings Tab events this.settingsTab = new SettingsTab(); this.settingsTab.DataApplied += delegate(Object o, EventArgs e) { this.logic.SaveSettings(this, this.settingsTab.ConnectionData, this.settingsTab.StreamData); // If current state is not IDLE then we notify the user that // he must restart the job for the changes to take effect if (this.Status != JobStatus.Idle) { MessageBox.Show("The changes will have effect only when you start the schedule again.", "Schedule must be restarted", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; this.aboutTab = this.getAboutTab(); this.tabControl.TabPages.Add(this.statusTab); this.tabControl.TabPages.Add(this.settingsTab); this.tabControl.TabPages.Add(this.aboutTab); this.Controls.Add(this.tabControl); }
/// <summary> /// Connect to RVServer, send data, disconnect /// </summary> /// <param name="view">Main Window reference</param> /// <param name="connectionData"></param> /// <param name="streamData"></param> private void ConnectOnce(MainWindow view, SettingsTab.ConnectionSettingsStruct connectionData, RVClient.StreamDataV1[] streamData) { // Comments on object synchronization ThreadContext context = new ThreadContext(); context.connectionData = connectionData; // struct - needs no syncrhonization context.streamData = streamData; // array - this object is created every time when accessing MainWindow's property; // it can be also this.streamData reference but it is never modified in this class context.view = view; // passed only to call Invoke() // Conclusion: everything here is threadsafe. Thread thread = new Thread(delegate(Object _tContext) { ThreadContext tContext = (ThreadContext)_tContext; lock (this.isConnectingLock) { // If other thread is already connecting don't do the same if (this.isConnecting) { tContext.view.WriteLog("Warning: Can't start new connection because the previous one is still handled.", true); return; } else { this.isConnecting = true; } } tContext.view.EnableProgressBar(true); try { tContext.view.WriteLog("Opening connection to " + connectionData.host + ":" + connectionData.port + ".", true); RVClient client = new RVClient(tContext.connectionData.host, tContext.connectionData.port); bool result = client.sendStreamDataV1(tContext.connectionData.username, tContext.connectionData.password, tContext.streamData); if (result) { tContext.view.WriteLog("Connection successful. Data Sent.", true); } else { tContext.view.Invoke((System.Windows.Forms.MethodInvoker)delegate() { if (tContext.view.Status != MainWindow.JobStatus.Idle) { tContext.view.Status = MainWindow.JobStatus.Error; } }); tContext.view.WriteLog("Error: Data undelivered.", true); tContext.view.WriteLog("Additional error info: Server replied with failure code. Check username and/or password and try again.", true); } } catch (Exception e) { tContext.view.Invoke((System.Windows.Forms.MethodInvoker)delegate() { if (tContext.view.Status != MainWindow.JobStatus.Idle) { tContext.view.Status = MainWindow.JobStatus.Error; } }); tContext.view.WriteLog("Error: " + e.Message, true); if (e.InnerException != null) { tContext.view.WriteLog("Additional error info: " + e.InnerException.Message, true); } } tContext.view.EnableProgressBar(false); lock (isConnectingLock) { this.isConnecting = false; } }); thread.IsBackground = true; thread.Start(context); }
/// <summary> /// Load settings from a file /// </summary> /// <param name="view">Main Window reference</param> /// <param name="connectionData"></param> /// <param name="streamData"></param> /// <returns>Whether loading data succeeded and out parameters can be read.</returns> /// <remarks>If file is corrupted and loading fails this method tries to reinitialize the file.</remarks> public bool LoadSettings(MainWindow view, out SettingsTab.ConnectionSettingsStruct connectionData, out RVClient.StreamDataV1[] streamData) { FileStream stream = this.GetSettingsFileStream(view, false); bool defaultSettings = false; if (stream != null) { XmlSerializer serializer = new XmlSerializer(typeof(SerializedSettings)); SerializedSettings settings; try { settings = (SerializedSettings)serializer.Deserialize(stream); } catch { view.WriteLog("Error: Could not load previous settings"); view.WriteLog("Falling back to default settings"); defaultSettings = true; stream.SetLength(0); InitializeSettingsFile(stream); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); try { settings = (SerializedSettings)serializer.Deserialize(stream); } catch (Exception e2) { // Something must be really screwed up now... view.WriteLog("Error: Could not reset to default settings"); view.WriteLog("Additional error info: " + e2.Message); stream.Close(); connectionData = new SettingsTab.ConnectionSettingsStruct(); streamData = null; return false; } } connectionData = settings.connectionData; streamData = settings.streamData; stream.Close(); if (!defaultSettings) { view.WriteLog("Previous settings loaded successfully"); } return true; } else { connectionData = new SettingsTab.ConnectionSettingsStruct(); streamData = null; return false; } }
/// <summary> /// Save passed settings to settings file /// </summary> /// <param name="view">Main Window reference</param> /// <param name="connectionData"></param> /// <param name="streamData"></param> public void SaveSettings(MainWindow view, SettingsTab.ConnectionSettingsStruct connectionData, RVClient.StreamDataV1[] streamData) { FileStream stream = this.GetSettingsFileStream(view, true); if (stream != null) { SerializedSettings settings = new SerializedSettings(); settings.connectionData = connectionData; settings.streamData = streamData; XmlSerializer serializer = new XmlSerializer(typeof(SerializedSettings)); try { serializer.Serialize(stream, settings); } catch { view.WriteLog("Error: Could not save new settings"); stream.Close(); return; } stream.Close(); view.WriteLog("New settings saved successfully"); } }