public static Guid GetIdSimulationOfLogFromXML(XDocument xml) { Simulacion sim; webappDBEntities webDB = new webappDBEntities(); try { if (xml.Root.Element("IdSimulacion") != null && xml.Root.Element("IdSimulacion").Value != "") { String id = xml.Root.Element("IdSimulacion").Value; try { Guid idSimulacion = Guid.Parse(id); sim = webDB.Simulacion.Single(s => s.IdSimulacion.Equals(idSimulacion)); } catch (Exception e) { throw new Exception("Error parsing log of " + id + ". Exception \n:" + e.Message); } } else { throw new Exception("Error: No se encuentra el Id de la simulación en el XML recibido."); } } catch (Exception e) { throw e; } return sim.IdSimulacion; }
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 String ParseClasificationParameters(String s) { /* Formato esperado: * CLASIFICATION_METHOD=4ad04057-4bd4-4996-aa9a-2b039cc61c2e;NUM_NEIGHBOURS=1;DISTANCE_TYPE=1 * Hay que modificar el Guid por el nombre del método (Posiciones 16-52) */ if (s.Length > 0) { String old; Boolean exito = false; String parameters = s.Substring(57); Guid idMetodoClasificacion; String nombreMetodoClasificacion = ""; exito = Guid.TryParse(s.Substring(21, 36), out idMetodoClasificacion); if (exito) { using (webappDBEntities db = new webappDBEntities()) { MetodoClasificacion mc = db.MetodoClasificacion.Find(idMetodoClasificacion); nombreMetodoClasificacion = (mc == null) ? "" : mc.Nombre; } old = "CLASIFICATION_METHOD=" + nombreMetodoClasificacion + parameters; } else { old = null; } if (nombreMetodoClasificacion == "") s = ""; else s = old; } return s; }
public static Log LoadFromXML(XDocument xml) { Log log = new Log(); log.IdLog = Guid.NewGuid(); Simulacion sim; webappDBEntities webDB = new webappDBEntities(); try { if (xml.Root.Element("IdSimulacion") != null && xml.Root.Element("IdSimulacion").Value != "") { String id = xml.Root.Element("IdSimulacion").Value; try { Guid idSimulacion = Guid.Parse(id); sim = webDB.Simulacion.Single(s => s.IdSimulacion.Equals(idSimulacion)); log.FechaLog = DateTime.Parse(xml.Root.Element("FechaLog").Value); log.Texto = xml.Root.Element("Texto").Value; } catch (Exception e) { throw new Exception("Error parsing log of " + id + ". Exception \n:" + e.Message); } } else { throw new Exception("Error: No se encuentra el Id de la simulación en el XML recibido."); } } catch (Exception e) { throw e; } return log; }
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)); } }
public static String ParseSelectionParameters(String s) { /* Formato esperado: * SELECTION_METHOD=8979932d-e69f-4ac1-8fbe-0e15d8820668;NUM_EXECUTIONS=1;CHOOSE_CRITERIA=1 * Hay que modificar el Guid por el nombre del método (Posiciones 16-52) */ if (s.Length > 0) { String old; Boolean exito = false; String parameters = s.Substring(52); Guid idMetodoSeleccion; String nombreMetodoSeleccion = ""; exito = Guid.TryParse(s.Substring(16, 36), out idMetodoSeleccion); if (exito) { using (webappDBEntities db = new webappDBEntities()) { MetodoSeleccion ms = db.MetodoSeleccion.Find(idMetodoSeleccion); nombreMetodoSeleccion = (ms == null) ? "" : ms.Nombre; } old = "SELECTION_METHOD=" + nombreMetodoSeleccion + parameters; } else { old = null; } if (nombreMetodoSeleccion == "") s = ""; else s = old; s = old; } return s; }