/// <summary> /// Establish a connection to the server (ARE) /// </summary> /// <param name="ipAddress">IP-Address of the ARE</param> /// <param name="port">Port to connect</param> /// <returns>A ASAPI-client object</returns> public AsapiServer.Client Connect(string ipAddress, int port, int timeOutParam) { int timeOut = 10000; if (timeOutParam == -1) { // read socket timeout from the ini-file try { IniFile ini = null; if (File.Exists(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\AsTeRICS\\ACS\\asterics.ini")) { ini = new IniFile(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\AsTeRICS\\ACS\\asterics.ini"); } else if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "asterics.ini")) { ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + "asterics.ini"); } if (ini != null) { timeOut = int.Parse(ini.IniReadValue("ARE", "socket_timeout")); if (timeOut < 100) { timeOut = 10000; } } } catch (Exception) { } } else { timeOut = timeOutParam; } try { transport = new TSocket(ipAddress, port, timeOut); // set socket timeout to 10000ms protocol = new TBinaryProtocol(transport); client = new AsapiServer.Client(protocol); transport.Open(); return client; } catch (Exception) { //catch (TApplicationException x) { //throw e; return null; } }
/// <summary> /// Close the connection to the ARE /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Disconnect_Click(object sender, RoutedEventArgs e) { try { asapiNet.Disconnect(asapiClient); AREHostIP = ""; AREPort = 0; } catch (Exception ex) { MessageBox.Show(Properties.Resources.AREDisconnectErrorDialog, Properties.Resources.AREDisconnectErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message); } asapiClient = null; statusList.Clear(); areStatus.Status = AREStatus.ConnectionStatus.Disconnected; }
/// <summary> /// Stop the status polling thread /// </summary> private void StopStatusPolling() { try { if (statusTimer != null) { statusTimer.Dispose(); Thread.Sleep(100); // needed to stop the timer process before closing the connection //Console.WriteLine("timer stopped"); statusTimer = null; } if (asapiStatusClient != null) { asapiNet.Disconnect(asapiStatusClient); //Console.WriteLine("connection discinnected"); asapiStatusClient = null; } } catch (Exception ex) { MessageBox.Show(Properties.Resources.StatusPollingThreadErrorDialog, Properties.Resources.StatusPollingThreadErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message); } }
/// <summary> /// Establish a connection to the ARE /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Connect_Click(object sender, RoutedEventArgs e) { ConnectAREDialog connectDialog; bool doConnect = true; if (ini.IniReadValue("ARE", "enable_autodetection").Equals("true")) { // autodection of ARE int receivePort = int.Parse(ini.IniReadValue("ARE", "autodetect_receive_port")); int sendPort = int.Parse(ini.IniReadValue("ARE", "autodetect_send_port")); AutodetectARE autodet = new AutodetectARE(sendPort, receivePort); try { autodet.Detect(this); } catch (Exception ex) { //MessageBox.Show(Properties.Resources.ConnectAREErrorDialogText, Properties.Resources.ConnectAREErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show(Properties.Resources.AREAutodetectionException, Properties.Resources.ConnectAREErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message + "\n" + ex.StackTrace); doConnect = false; } if (autodet.IpAdd.Equals("")) { // no ARE found MessageBox.Show(Properties.Resources.AREAutodetectionNoARE, Properties.Resources.ConnectAREErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); doConnect = false; } else { AREHostIP = autodet.IpAdd; Int32.TryParse(ini.IniReadValue("ARE", "default_port"), out AREPort); AREHost = autodet.Hostname; } } else { // manual connection with host IP // show connection dialog if set in options if (showHostPortDialogOnConnect) { string nameErrorStr = ""; do { connectDialog = new ConnectAREDialog(); connectDialog.Owner = this; connectDialog.hostNameBox.Text = ini.IniReadValue("ARE", "default_host"); connectDialog.portNumberBox.Text = ini.IniReadValue("ARE", "default_port"); if (nameErrorStr != "") { connectDialog.errorField.Text = nameErrorStr; } connectDialog.ShowDialog(); if (connectDialog.hostNameBox.Text.Length == 0) { nameErrorStr = Properties.Resources.ConnectAREDialogHostEmpty; } else if (connectDialog.portNumberBox.Text.Length == 0) { nameErrorStr = Properties.Resources.ConnectAREDialogPortEmpty; } else if (!Int32.TryParse(connectDialog.portNumberBox.Text, out AREPort)) { nameErrorStr = Properties.Resources.ConnectAREDialogInvalidNumber; } else { nameErrorStr = ""; AREHostIP = connectDialog.hostNameBox.Text; AREHost = AREHostIP; } if (connectDialog.DialogResult == false) { nameErrorStr = ""; doConnect = false; } showHostPortDialogOnConnect = (bool)connectDialog.showCheckbox.IsChecked; } while (nameErrorStr != ""); if (showHostPortDialogOnConnect) { ini.IniWriteValue("Options", "showHostPortDialogOnConnect", "true"); } else { ini.IniWriteValue("Options", "showHostPortDialogOnConnect", "false"); } } else { AREHostIP = ini.IniReadValue("ARE", "default_host"); AREHost = AREHostIP; if ((AREHostIP.Length == 0) || !Int32.TryParse(ini.IniReadValue("ARE", "default_port"), out AREPort)) { doConnect = false; } } } if (doConnect) { asapiClient = asapiNet.Connect(AREHostIP, AREPort, -1); if (asapiClient != null) { if (showAREConnectedMessage) { CustomMessageBox messageBox = new CustomMessageBox(Properties.Resources.AREConnectedDialogFormat(AREHostIP), Properties.Resources.AREConnectedDialogHeader, CustomMessageBox.messageType.Info, CustomMessageBox.resultType.OK); messageBox.Owner = this; messageBox.showCheckbox.IsChecked = showAREConnectedMessage; messageBox.ShowDialog(); showAREConnectedMessage = (bool)messageBox.showCheckbox.IsChecked; if (showAREConnectedMessage) { ini.IniWriteValue("Options", "showAREConnectedMessage", "true"); } else { ini.IniWriteValue("Options", "showAREConnectedMessage", "false"); } } areStatus.Status = AREStatus.ConnectionStatus.Connected; // check the status of the ARE and change model of ACS, if requested List<StatusObject> newStatus = asapiClient.QueryStatus(true); if (newStatus.Count > 0) { String currentAREStatus = ""; foreach (StatusObject so in newStatus) { statusList.Add(so); // get the current status of the ARE if (so.InvolvedComponentID == "") { if ((so.Status == "ok") && (currentAREStatus == "running")) { currentAREStatus = "deployed"; } else if ((currentAREStatus == "running") && (so.Status == "paused")) { currentAREStatus = "paused"; } else if (so.Status == "deployed") { currentAREStatus = "deployed"; } else if (so.Status == "running") { currentAREStatus = "running"; } else if (so.Status == "error") { currentAREStatus = "error"; } } } // check, if a model is loaded and remove error marker if ((deploymentModel != null) && (deploymentModel.components != null) && (deploymentModel.components.Count() > 0)) { foreach (componentType mc in deploymentModel.components) { if ((mc != null) && ((mc.ComponentCanvas.Background == Brushes.Red) || (mc.ComponentCanvas.Background == Brushes.Orange))) { mc.ComponentCanvas.Background = null; } } } // check, if a deployed model is ready on ARE if (currentAREStatus == "deployed") { // also stopped? if (showOverrideAtConnectionQuestion) { //if (MessageBox.Show(Properties.Resources.AREStatusMessageDeployed, Properties.Resources.AREStatusMessageHeader, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { CustomMessageBox messageBox = new CustomMessageBox(Properties.Resources.AREStatusMessageDeployed, Properties.Resources.AREStatusMessageHeader, CustomMessageBox.messageType.Info, CustomMessageBox.resultType.YesNo); messageBox.Owner = this; messageBox.showCheckbox.IsChecked = showOverrideAtConnectionQuestion; messageBox.ShowDialog(); showOverrideAtConnectionQuestion = (bool)messageBox.showCheckbox.IsChecked; if (showOverrideAtConnectionQuestion) { ini.IniWriteValue("Options", "showOverrideLocalWhenConnected", "true"); } else { ini.IniWriteValue("Options", "showOverrideLocalWhenConnected", "false"); } if ((bool)messageBox.DialogResult) { DownloadAndCheckModel(); } } else { // If the message will not be shown, the model will not be downloaded (default = no). Uncomment to set the default to yes. // downloadAndCheckModel(); } } else if (currentAREStatus == "running" || currentAREStatus == "paused") { if (showOverrideAndRunAtConnectionQuestion) { CustomMessageBox messageBox = new CustomMessageBox(Properties.Resources.AREStatusMessageRunning, Properties.Resources.AREStatusMessageHeader, CustomMessageBox.messageType.Info, CustomMessageBox.resultType.YesNo); messageBox.Owner = this; messageBox.showCheckbox.IsChecked = showOverrideAndRunAtConnectionQuestion; messageBox.ShowDialog(); showOverrideAndRunAtConnectionQuestion = (bool)messageBox.showCheckbox.IsChecked; if (showOverrideAndRunAtConnectionQuestion) { ini.IniWriteValue("Options", "showOverrideAndRunLocalWhenConnected", "true"); } else { ini.IniWriteValue("Options", "showOverrideAndRunLocalWhenConnected", "false"); } if ((bool)messageBox.DialogResult) { DownloadAndCheckModel(); areStatus.Status = AREStatus.ConnectionStatus.Running; if (currentAREStatus == "paused") { areStatus.Status = AREStatus.ConnectionStatus.Pause; } } } else { // If the message will not be shown, the model will not be downloaded and started (default = no). Uncomment to set the default to yes. //downloadAndCheckModel(); //areStatus.Status = AREStatus.ConnectionStatus.Running; //if (currentAREStatus == "paused") { // areStatus.Status = AREStatus.ConnectionStatus.Pause; //} } } } } else { MessageBox.Show(Properties.Resources.ConnectAREErrorDialogText, Properties.Resources.ConnectAREErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); traceSource.TraceEvent(TraceEventType.Error, 3, Properties.Resources.ConnectAREErrorDialogText); } } }
/// <summary> /// Starts a new theard, requesting the status of the ARE in a defined period of time /// </summary> private void StartStatusPolling() { if (ini.IniReadValue("ARE", "enable_status_polling").Equals("true")) { if (statusTimer == null) { try { asapiStatusClient = asapiNet.Connect(AREHostIP, AREPort, 300); TimerCallback tcb = this.DoPollingTimer; //sc.CheckStatus; int pollingFrequency = 2000; int.TryParse(ini.IniReadValue("ARE", "status_polling_frequency"), out pollingFrequency); if (pollingFrequency < 500) { // 500ms is the minimum for the polling frequency pollingFrequency = 500; } //Console.WriteLine("starting timer"); statusTimer = new Timer(tcb, null, 100, pollingFrequency); } catch (Exception ex) { MessageBox.Show(Properties.Resources.StatusPollingThreadErrorDialog, Properties.Resources.StatusPollingThreadErrorDialogHeader, MessageBoxButton.OK, MessageBoxImage.Error); traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message); } } } }