public Form1() { InitializeComponent(); server = new SocketTcpServer(); client = new SocketTcpClient(); client.ConnectionTimeOut = 1000; updateServerConsoleEH = new EventHandler(updateServerConsole); updateClientConsoleEH = new EventHandler(updateClientConsole); server.ClientConnected += new TcpClientConnectedEventHandler(server_ClientConnected); server.ClientDisconnected += new TcpClientDisconnectedEventHandler(server_ClientDisconnected); server.DataReceived += new TcpDataReceivedEventHandler(server_DataReceived); client.Connected += new TcpClientConnectedEventHandler(client_Connected); client.DataReceived += new TcpDataReceivedEventHandler(client_DataReceived); client.Disconnected += new TcpClientDisconnectedEventHandler(client_Disconnected); }
/// <summary> /// Configures the TcpServer of the Blackboard /// </summary> private void SetupServer() { server = new SocketTcpServer(port); server.ClientConnected += new TcpClientConnectedEventHandler(server_ClientConnected); server.ClientDisconnected += new TcpClientDisconnectedEventHandler(server_ClientDisconnected); server.DataReceived += new TcpDataReceivedEventHandler(server_DataReceived); }
/// <summary> /// Main thread for redirecting messages /// </summary> private void MainThreadTask() { int i; int retrySendCount = 0; List<ModuleClient> modulesPendingToStop; IAsyncResult stopPluginsAsyncResult = null; //Response[] aResponses; //Command c; //Response r; running = true; RunningStatus = BlackboardRunningStatus.Starting; Log.WriteLine(4, "Main Thread: Running"); // Start Plugins StartPlugins(); // Start Client Modules StartModules(); // Start TCP Server and clear queues StartServer(); startupTime = DateTime.Now; testTimeOutExecuted = false; Log.WriteLine(1, "Blackboard Started"); if(running)RunningStatus = BlackboardRunningStatus.Running; this.mainThreadRunningEvent.Set(); this.parserThreadRunningEvent.WaitOne(); this.StartupSequence.StartModules(); while (running) { #region Redirector try { #region Send all pending messages // Send all pending messages SendPendingMessages(); #endregion #region Remove unnecesary responses // Remove unnecesary responses lock (commandsWaiting) { lock (responses) { if ((commandsWaiting.Count == 0) && (responses.Count > 0)) responses.Clear(); } } #endregion #region Response redirection if (commandsWaiting.Count > 0) { #region Send failed response on module disconnection SendDisconnectedResponses(); #endregion #region Redirect Responses RedirectResponses(); #endregion #region Send failed response on Timeout SendTimedOutResponses(); #endregion #region Retry Send if (retrySendCount++ > 10) { RetrySendResponses(); retrySendCount = 0; } #endregion } #endregion } catch (Exception ex) { Log.Write(1, ex.ToString()); } #endregion #region Request Handle if (restartRequested) { restartRequested = false; restartTestRequested = false; AsyncRestart(); } if (restartTestRequested) { restartTestRequested = false; AsyncRestartTest(); } #endregion #region Event firing if ((autoStopTime > TimeSpan.Zero) && (AutoStopTimeLeft <= TimeSpan.Zero)) { Log.WriteLine(1, "Auto-Stop timed out"); running = false; } if (!testTimeOutExecuted && (testTimeOut > TimeSpan.Zero) && (TestTimeLeft <= TimeSpan.Zero)) { testTimeOutExecuted = true; Log.WriteLine(4, "Test timed out: Executing Test-Timeout actions"); ExecuteTestTimeOutActions(); Log.WriteLine(5, "\tTest-Timeout actions executed"); } #endregion Thread.Sleep(1); } RunningStatus = BlackboardRunningStatus.Stopping; #region Stop secondary threads // Stop secondary threads Log.WriteLine(4, "Stopping threads..."); this.threadMessageParser.Join(100); if ((this.threadMessageParser != null) && this.threadMessageParser.IsAlive) this.threadMessageParser.Abort(); Log.WriteLine(4, "Done!"); #endregion #region Begin to Stop Modules // Begin to Stop modules Log.WriteLine(1, "Stopping modules..."); modulesPendingToStop = new List<ModuleClient>(); foreach (ModuleClient module in modules) { modulesPendingToStop.Add(module); module.BeginStop(); } this.ShutdownSequence.ShutdownModules(); #endregion #region Begin to stop Plugins stopPluginsAsyncResult = BeginStopPlugins(); #endregion #region Stop TCP server // Stop TCP server if (server.Started) server.Stop(); Log.WriteLine(1, "Blackboard server Stopped"); server = null; #endregion #region Wait for Modules to Stop Log.WriteLine(4, "Waiting for modules to stop: " + modulesPendingToStop.Count.ToString() + " pending"); do { // Waiting for modules to stop. // Log.WriteLine(9, "Waiting for modules to stop: " + modulesPendingToStop.Count.ToString() + " pending"); for (i = 0; i < modulesPendingToStop.Count; ++i ) { //if (!module.IsRunning || (module.Simulation != SimulationOptions.SimulationDisabled)) if (!modulesPendingToStop[i].IsRunning) { modulesPendingToStop.RemoveAt(i); --i; } } Thread.Sleep(10); } while (modulesPendingToStop.Count > 0); /* foreach (Module module in modules) { if (!module.IsRunning || (module.Simulation != SimulationOptions.SimulationDisabled)) module.Stop(); } */ #endregion #region Wait for Plugins to stop EndStopPlugins(stopPluginsAsyncResult); #endregion this.mainThreadRunningEvent.Reset(); Log.WriteLine(4, "Main Thread: Stopped"); RunningStatus = BlackboardRunningStatus.Stopped; Log.WriteLine(1, "Blackboard Stopped"); Log.Flush(); }
private void Initialize() { modules = new ModuleCollection(this, 6); server = new SocketTcpServer(port); server.ClientConnected += new TcpClientConnectedEventHandler(server_ClientConnected); server.ClientDisconnected += new TcpClientConnectedEventHandler(server_ClientDisconnected); server.DataReceived += new TcpDataReceivedEventHandler(server_DataReceived); }