private InstrumentControlProxy() { // Create the objects that represent the control/event connections... // Make initial attempt to connect to the Instrument Control server. // Get the number of times to re-try if the connection attempt fails. NameValueCollection nvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("Separator/InstrumentControlConnection"); int retryCount, retryWait; try { retryCount = int.Parse(nvc.Get("RetryCount")); retryWait = int.Parse(nvc.Get("RetryWait_ms")); } catch { // Set default values in case the parse fails retryCount = 3; retryWait = 2000; // milliseconds } do { try { myXmlRpcProxy = new InstrumentControlXmlRpcProxy( RemotingInfo.GetInstance().IcsServerURL); retryCount = 0; // Clear the retry count as we're now connected. } catch (Exception ex) { // Connection attempt failed. Re-try if the number of attempts so far is // within the retry count specified in the application configuration file, // otherwise propagate an exception to tell the caller we failed to connect. myXmlRpcProxy = null; LogFile.AddMessage(TraceLevel.Warning, "retryCount = " + retryCount + " " + ex.Message); if (--retryCount <= 0) { throw new ApplicationException( SeparatorResourceManager.GetSeparatorString( StringId.ExFailedToConnectToInstrumentControl)); } // Pause before retrying connection attempt Thread.Sleep(retryWait); } } while (retryCount > 0); }
/// <summary> /// </summary> public void Unsubscribe() { try { if (myXmlRpcProxy != null && IsConnectionEnabled) { // Disconnect from the XML-RPC server myXmlRpcProxy.Unsubscribe(RemotingInfo.GetInstance().IcsEventSinkURL); } } catch (Exception ex) { LogFile.LogException(TraceLevel.Error, ex); //DisableConnection(); throw new ApplicationException( "Unsubscribe exception", ex); } }
/// <summary> /// Make the connection to the Instrument Control XML-RPC server. /// </summary> public void Subscribe() { try { if (myXmlRpcProxy != null) { // Register for Instrument Control Events myXmlRpcProxy.Subscribe(RemotingInfo.GetInstance().IcsEventSinkURL); } } catch (Exception ex) { LogFile.LogException(TraceLevel.Error, ex); //DisableConnection(); throw new ApplicationException( SeparatorResourceManager.GetSeparatorString( StringId.ExFailedToSubscribeToInstrumentControl), ex); } }
/// <summary> /// /// </summary> public void Halt() { bool haltResult = false, unsubscribeResult = false; try { if (IsConnectionEnabled) { // Tell the XML-RPC server to shut down its "RoboSep" command handling haltResult = myXmlRpcProxy.Halt(); // Disconnect from the XML-RPC server unsubscribeResult = myXmlRpcProxy.Unsubscribe( RemotingInfo.GetInstance().IcsEventSinkURL); } } catch (WebException wex) { if (wex.Status != WebExceptionStatus.ConnectionClosed) { // Log the details of the exception LogException(wex, "Halt1"); } } catch (Exception ex) { // Log the details of the exception LogException(ex, "Halt2"); } finally { LogFile.AddMessage(TraceLevel.Verbose, "Instrument Control API Halt() returned " + haltResult.ToString()); LogFile.AddMessage(TraceLevel.Verbose, "Instrument Control API Unsubscribe() returned " + unsubscribeResult.ToString()); DisableConnection(); } }
/// <summary> /// Retrieve Instrument Control server information including version number. /// </summary> /// <param name="gatewayURL"></param> /// <param name="gatewayInterfaceVersion"></param> /// <param name="serverUptime_seconds"></param> /// <param name="instrumentControlVersion"></param> /// <param name="instrumentSerialNumber"></param> /// <param name="serviceConnection"></param> public void GetServerInfo(out string gatewayURL, out string gatewayInterfaceVersion, out string serverUptime_seconds, out string instrumentControlVersion, out string instrumentSerialNumber, out string serviceConnection) { gatewayURL = gatewayInterfaceVersion = serverUptime_seconds = string.Empty; instrumentControlVersion = instrumentSerialNumber = serviceConnection = string.Empty; try { if (IsConnectionEnabled) { string[] serverInfo = myXmlRpcProxy.GetServerInfo(); if (serverInfo != null && serverInfo.GetLength(0) == 5) { gatewayURL = serverInfo[0]; gatewayInterfaceVersion = serverInfo[1]; serverUptime_seconds = serverInfo[2]; instrumentControlVersion = serverInfo[3]; instrumentSerialNumber = serverInfo[4]; } serviceConnection = RemotingInfo.GetInstance().HostDomainName; // If the service connection indicates the loopback address or local // machine, clear the service connection information (this will trigger // the UI to display a localised prompt for the user to connect a // cable and re-start. if (serviceConnection == RemotingInfo.GetInstance().LocalMachineName || serviceConnection == RemotingInfo.GetInstance().LoopbackAddress) { serviceConnection = string.Empty; } } } catch (Exception ex) { // Log the details of the exception LogException(ex, "GetServerInfo"); } }
private SeparatorImpl() { try { // ------------------- // Set the fully-qualified machine name for the server (optionally supplied in // the application configuration file). NameValueCollection nvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("Separator/InstrumentControlConnection"); string serverAddress = string.Empty; try { serverAddress = nvc.Get("ServerAddress"); } finally { // Ignore any exceptions -- if we can't read the IP address configuration // setting then we act as if it were not specified. } RemotingInfo.ConfigureInstance(serverAddress); string configuredAddress = (serverAddress == null || serverAddress == string.Empty) ? "<Not specified>" : serverAddress; LogFile.AddMessage(TraceLevel.Verbose, "Configured address = " + configuredAddress); // ------------------- // Register an event handler for IInstrumentControlEvents RemotingConfiguration.Configure("Separator.exe.config", false); // ------------------- // Setup TCP Server channel BinaryClientFormatterSinkProvider tcpClientSinkProvider = new BinaryClientFormatterSinkProvider(); BinaryServerFormatterSinkProvider tcpServerSinkProvider = new BinaryServerFormatterSinkProvider(); tcpServerSinkProvider.TypeFilterLevel = TypeFilterLevel.Full; ListDictionary tcpChannelProperties = new ListDictionary(); tcpChannelProperties.Add("name", "SeparatorAPI"); tcpChannelProperties.Add("port", 3149); TcpChannel tcpChannel = new TcpChannel(tcpChannelProperties, tcpClientSinkProvider, tcpServerSinkProvider); ChannelServices.RegisterChannel(tcpChannel, false); RemotingConfiguration.RegisterWellKnownServiceType( typeof(InstrumentControlEventSink), RemotingInfo.GetInstance().IcsEventSinkURI, WellKnownObjectMode.Singleton); myTCPSvr = OrcaTCPServer.GetInstance(); myTCPSvr.TCPPort = myTCP_Port; // ------------------- // Create the InstrumentControlEventSink try { theInstrumentControlEventSink = (IInstrumentControlEvents)Activator.GetObject( typeof(InstrumentControlEventSink), RemotingInfo.GetInstance().IcsEventSinkURL); theInstrumentControlEventSink.ping(); // Call a method to trigger the server } catch (Exception ex) { Console.WriteLine(ex.Message); } } catch (Exception ex) { myInstrument = null; Console.WriteLine(ex.GetType()); Console.WriteLine(ex.Message + "\n" + ex.InnerException); Console.WriteLine(ex.StackTrace); throw new ApplicationException(); } }