protected override void OnStart(string[] args) { PBioEventLog.WriteEntry("Initializing..."); // Inicializamos las DB webDB = new webappDBEntities(); //Cargamos la configuración sConfig = new PBioServiceConfiguration(); PBioEventLog.WriteEntry("Configuration loaded."); // Inicializamos el Listener donde recibiremos los resultados de las simulaciones resultsListener = new ResultsListener(sConfig.PortSvc); // Inicializamos el serverSocket donde recibir los resultados de las simulaciones tListener = new Thread(new ThreadStart(resultsListener.StartListening)); tListener.Start(); PBioEventLog.WriteEntry("[OnStart] Started. Cluster at " + sConfig.IpDaemon+":"+sConfig.PortDaemon); // Comprobamos simulaciones Run y las establecemos en ToRun try { EstadoSimulacion RunState = webDB.EstadoSimulacion.Where(s => s.Nombre.Equals("Run")).Single(); EstadoSimulacion ToRunState = webDB.EstadoSimulacion.Where(s => s.Nombre.Equals("ToRun")).Single(); List<Simulacion> simRunning = webDB.Simulacion.Where(s => s.IdEstadoSimulacion.Equals(RunState.IdEstadoSimulacion)).ToList<Simulacion>(); //List<Simulacion> simRunning = webDB.Simulacion.Where(s => s.EstadoSimulacion.Equals(RunState)).ToList<Simulacion>(); foreach (Simulacion s in simRunning) { s.IdEstadoSimulacion = ToRunState.IdEstadoSimulacion; } webDB.SaveChanges(); PBioEventLog.WriteEntry("[CHECK SIM] " + simRunning.Count); // Configuramos el timer timerUpdateSimulations = new System.Threading.Timer( new System.Threading.TimerCallback(timerUpdateSimulations_Elapsed), null, 1000, 1000 ); } catch (Exception e) { PBioEventLog.WriteEntry("[CHECK SIM] Error: "+e); tListener.Abort(); this.Stop(); } }
public static void SaveFromListener(String content) { EventLog PBioEventLog = PBioEventLogger.initLogger(); webappDBEntities webDB = new webappDBEntities(); // SAve results // All the data has been read from the // client. Display it on the console. PBioEventLog.WriteEntry("[RESULTS LISTENER] Read " + content.Length + " bytes from socket. \n"); // Remove <PBIOEOF> content = content.Replace(EndFileTag, ""); // Chequeamos si es Resultados o Error if (content.StartsWith(ErrorFileTag)) { PBioEventLog.WriteEntry("[RESULTS LISTENER] Error received. Processing:\n"+content); try { // Eliminamos ErrorTag //content.Replace(ErrorFileTag, ""); // Parseamos el XML XDocument errorXML = XDocument.Parse(content); // Guardamos error log en BD Log l = Log.LoadFromXML(errorXML); Guid idSimulacion = Log.GetIdSimulationOfLogFromXML(errorXML); PBioEventLog.WriteEntry("[RESULTS LISTENER] Error file received: " + l.Texto); webDB.Log.Add(l); webDB.SaveChanges(); webDB.Simulacion.Single(s => s.IdSimulacion.Equals(idSimulacion)).Log = l; webDB.Simulacion.Single(s => s.IdSimulacion.Equals(idSimulacion)).EstadoSimulacion = webDB.EstadoSimulacion.Where(es => es.Nombre.Equals("Error")).Single(); webDB.SaveChanges(); } catch (Exception e) { PBioEventLog.WriteEntry("[RESULTS LISTENER] ERROR processing error response: " + e.ToString()); } } else if (content.StartsWith(ResultsFileTag)) { PBioEventLog.WriteEntry("[RESULTS LISTENER] Results received. Processing..."); try { // Eliminamos el ResultsTag content.Replace(ResultsFileTag, ""); // Parseamos el XML XDocument resultadosXML = XDocument.Parse(content); // Convertimos a clase Resultado PBioEventLog.WriteEntry("[RESULTS LISTENER] Results:" + resultadosXML.ToString()); Resultado resultado = Resultado.LoadFromXML(resultadosXML); // Guardamos en base de datos los resultados webDB.Resultado.Add(resultado); // Establecemos como finalizada la simulación PBioEventLog.WriteEntry("[RESULTS LISTENER] Set the simulation finished - " + resultado.IdSimulacion.GetType().ToString() + " - " + resultado.IdSimulacion.ToString()); Simulacion simulation = webDB.Simulacion.Single(s => s.IdSimulacion.Equals((Guid)resultado.IdSimulacion)); EstadoSimulacion simulationState = webDB.EstadoSimulacion.Where(es => es.Nombre.Equals("Terminate")).Single(); simulation.EstadoSimulacion = simulationState; webDB.SaveChanges(); } catch (Exception e) { PBioEventLog.WriteEntry("[RESULTS LISTENER] ERROR: " + e.ToString()); } } else { PBioEventLog.WriteEntry("[RESULTS LISTENER] Unknow tag:" + content.Substring(0,15)); } }