public bool pingServer(ServerInfo server, EventHandler <MessageEvent> messageLog) { if (!communicators.ContainsKey(server)) { if (messageLog != null) { messageLog(this, new MessageEvent("Attempted to ping unconnected server " + server.ToString())); } return(false); } ServerCommunicator comm = communicators[server]; if (comm == null) { if (messageLog != null) { messageLog(this, new MessageEvent("Server communication object is null, unable to ping server " + server.ToString())); } return(false); } try { Thread pingThread = new Thread(new ParameterizedThreadStart(ping_server_proc)); pingThread.Start(comm); pingThread.Join(1000); while (pingThread.ThreadState == ThreadState.Running && !StopPing) { pingThread.Join(1000); } if (pingThread.ThreadState != ThreadState.Stopped) { messageLog(this, new MessageEvent("Server ping failed.")); pingThread.Abort(); return(false); } //pingThread.Join(1000); //if (pingThread.ThreadState == ThreadState.Running) //{ // if (messageLog != null) // messageLog(this, new MessageEvent("Server ping took longer than 1000ms. Aborting.")); // // pingThread.Abort(); // // return false; //} } catch (Exception e) { messageLog?.Invoke(this, new MessageEvent("Caught exception when attempting to ping server " + server.ToString() + ": " + e.Message + e.StackTrace)); return(false); } return(true); }
private void thisFunctionNeverGetsCalled(ServerCommunicator temp) { // The purpose of this function is to be found by perplexed programmers // who have attempted to search for references to ServerCommunicator methods. // The reason this is necessary is that there are NO direct calls made to any ServerCommunicator // methods. There is an added layer of complexity which was added to allow for better performance. // The added complexity is that instead of just calling methods on ServerCommunicator, I am instead // calling them from the function InvokeMethodOnServer_Proc, run in its own thread. This allows me // to kill or cancel the call to ServerCommunicator if it times out, due for instance to a network // disconnection or a server shutting down. // As a result of this, and in order to make the InvokeMethodOnServer_Proc function more general purpose, // there are no direct calls to ServerCommunicator functions. Instead, they are referenced by name // through the .NET reflection MethodInfo class. To see how this works, look at the function // setSettingsOnConnectedServers and work your way down. // Should you need to add new methods to ServerCommunicator, and thus corresponding new // methods to ServerManager, it should be straightforward to copy the technique I used. // Alas, this is a bit confusing and not particularly transparent, which is why I have // created this note. // -- Aviv Keshet temp.armTasks(0); temp.generateBuffers(0); temp.generateTrigger(); temp.getHardwareChannels(); temp.getServerName(); temp.getServerSettings(); temp.nextRunTimeStamp(DateTime.Now); temp.outputGPIBGroup(null, null); temp.outputRS232Group(null, null); temp.outputSingleTimestep(null, null); temp.ping(); temp.runSuccess(); temp.setSequence(null); temp.setSettings(null); temp.stop(); }
private static void ping_server_proc(object obj) { bool success = false; ServerCommunicator comm = (ServerCommunicator)obj; while (!success) { try { comm.ping(); success = true; } catch (Exception e) { //If the user is trying to exit then we should let them. Otherwise, continue looping. if (e.ToString() == "Thread was being aborted.") { success = true; } } } }
public InvokeMethodOnServer_Parameters(MethodInfo method, object[] parameters, ServerCommunicator communicator) { this.method = method; this.parameters = parameters; this.communicator = communicator; }
/// <summary> /// Runs the method, specified by method, on the server specified by i, with parameters given by parameters. /// Correctly handles exceptions thrown in the running of that method, and bool and BufferGenerationStatus /// type return values from the server. /// </summary> /// <param name="method"></param> /// <param name="i"></param> /// <param name="parameters"></param> /// <returns></returns> public ServerActionStatus runMethodOnServer(MethodInfo method, ServerInfo i, object[] parameters, int msTimeout, EventHandler <MessageEvent> messageLog) { if (verifyConnection(i) == ServerActionStatus.Failed_No_Connection) { return(ServerActionStatus.Failed_No_Connection); } ServerCommunicator communicator = communicators[i]; Object returnValue; try { InvokeMethodOnServer_Parameters invoke_parameters = new InvokeMethodOnServer_Parameters(method, parameters, communicator); Thread invokeMethodThread = new Thread(new ParameterizedThreadStart(InvokeMethodOnServer_Proc)); invokeMethodThread.Start(invoke_parameters); // invokeMethodThread.Join(msTimeout); invokeMethodThread.Join(); if (invokeMethodThread.ThreadState == ThreadState.Running) { invokeMethodThread.Abort(); throw new Exception("Method on server " + i.ToString() + "." + method.Name + " took longer than " + msTimeout + " ms timeout. Aborting."); } returnValue = invoke_parameters.returnValue; if (invoke_parameters.runException != null) { throw new Exception("Caught an exception when attempting to run method " + i.ToString() + "." + method.Name + ": " + invoke_parameters.runException.Message); } } catch (Exception e) { if (messageLog != null) { messageLog(this, new MessageEvent("Caught exception when attempting to run method on server " + i.ToString() + ": " + e.Message + e.StackTrace)); } serverGotDisconnectedInError(i); return(ServerActionStatus.Failed_Loss_Of_Connection); } if (returnValue is bool) { bool attempt = (bool)returnValue; if (attempt) { return(ServerActionStatus.Success); } return(ServerActionStatus.Failed_On_Server); } else if (returnValue is ServerCommunicator.BufferGenerationStatus) { ServerCommunicator.BufferGenerationStatus bufferStatus = (ServerCommunicator.BufferGenerationStatus)returnValue; if (bufferStatus == ServerCommunicator.BufferGenerationStatus.Success) { return(ServerActionStatus.Success); } return(ServerActionStatus.Failed_On_Server); } return(ServerActionStatus.Success); }
private static void ping_server_proc(object obj) { ServerCommunicator comm = (ServerCommunicator)obj; comm.ping(); }