public String sendMessage(String cmd, MemoryStream msg) { int tryCount = 0; int tryLimit = 3; Exception lastException = new Exception("Unknown exception"); String respMessage = ""; Logger.GetInstance().Debug("SeapClient send message: <" + cmd + ">"); lock (this) { while (tryCount < tryLimit) { try { //clean and discard data on sockect if any if (stream.DataAvailable == true) { reader.ReadToEnd(); } writer.WriteLine(cmd); writer.Flush(); if (msg != null) { binaryWriter.Write(msg.GetBuffer(), 0, (int)msg.Length); binaryWriter.Flush(); } respMessage = reader.ReadLine().Trim(); tryCount = 0; break; } catch (Exception e) { Logger.GetInstance().Debug("IO Exception tryCount:" + tryCount); lastException = e; tryCount = handleStreamError(tryCount); } } if (tryCount >= tryLimit) { Logger.GetInstance().Error("SeapClient connection error, engine unreachable:" + lastException.Message + " " + lastException.StackTrace); SvcController.StopMyDLP(); return(""); } } Logger.GetInstance().Debug("SeapClient read response: <" + respMessage + ">"); return(respMessage); }
public static void Start() { javaStartCmd = @"cd " + Configuration.JavaBackendPath + " && Run.bat"; erlStartCmd = @"cd " + Configuration.ErlangPath + " && Run.bat"; erlInstallCmd = @"cd " + Configuration.ErlangPath + " && RegisterService.bat"; erlStartInteractiveCmd = @"cd " + Configuration.ErlangPath + " && InteractiveRun.bat"; DelPids(); KillProcByName("erl"); KillProcByName("werl"); KillProcByName("java"); EnvVar[] erlEnv = new EnvVar[] { new EnvVar("MYDLP_CONF", GetShortPath(Configuration.MydlpConfPath).Replace(@"\", @"/")), new EnvVar("MYDLPBEAMDIR", GetShortPath(Configuration.ErlangPath)), new EnvVar("MYDLP_APPDIR", GetShortPath(Configuration.AppPath)), new EnvVar("ERLANG_HOME", GetShortPath(Configuration.ErlangHome)) }; int phyMemory = GetPhysicalMemory(); int javaMemory = 256; if (phyMemory < 300) { Logger.GetInstance().Error("Not enough memory, MyDLP Engine can not function under 300 MB memory"); Engine.Stop(); return; } else if (phyMemory < 600) { javaMemory = 256; } else { javaMemory = 256 + ((phyMemory - 600) / 4); if (javaMemory > xmx32BitLimit) { javaMemory = xmx32BitLimit; } } Logger.GetInstance().Info("Setting Java memory: " + javaMemory); EnvVar[] javaEnv = new EnvVar[] { new EnvVar("JRE_BIN_DIR", GetShortPath(Configuration.JavaBinPaths)), new EnvVar("BACKEND_DIR", GetShortPath(Configuration.JavaPath)), new EnvVar("MYDLP_APPDIR", GetShortPath(Configuration.AppPath)), new EnvVar("JAVAXMX", javaMemory.ToString()) }; if (!System.Environment.UserInteractive && SvcController.IsServiceInstalled("mydlpengine")) { if (SvcController.IsServiceRunning("mydlpengine")) { SvcController.StopService("mydlpengine", 5000); } ProcessControl.ExecuteCommandSync(new ExecuteParameters("sc delete mydlpengine", "SC", erlEnv)); } try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Ericsson\\Erlang", true)) { key.DeleteSubKeyTree("ErlSrv"); } } catch { //Logger.GetInstance().Info("Unable to delete uneccessary keys or keys do not exists"); } Logger.GetInstance().Info("Starting Java Backend"); ProcessControl.ExecuteCommandAsync(javaStartCmd, "JAVA:", javaEnv); Configuration.SetErlConf(); Logger.GetInstance().Info("Starting Erlang Backend"); if (System.Environment.UserInteractive) { ProcessControl.ExecuteCommandAsync(erlStartInteractiveCmd, "WERL", erlEnv); } else { ProcessControl.ExecuteCommandAsync(erlStartCmd, "ERL", erlEnv); } }
public static void Stop() { Logger.GetInstance().Info("Stopping Erlang Backend"); /* * Logger.GetInstance().Debug("Kill erlang by pid:" + Configuration.ErlPid); * if (Configuration.ErlPid != 0) * { * try * { * Process p = Process.GetProcessById(Configuration.ErlPid); * p.Kill(); * } * catch * { * Logger.GetInstance().Debug("Kill erlang by pid failed:" + Configuration.ErlPid); * } * }*/ if (System.Environment.UserInteractive) { KillProcByName("werl"); } else { KillProcByName("erl"); } KillProcByName("epmd"); Logger.GetInstance().Info("Stopping Java Backend"); /* * Logger.GetInstance().Debug("Kill java by pid:" + Configuration.JavaPid); * if (Configuration.JavaPid != 0) * { * try * { * Process p = Process.GetProcessById(Configuration.JavaPid); * p.Kill(); * } * catch * { * Logger.GetInstance().Debug("Kill java by pid failed:" + Configuration.JavaPid); * } * }*/ KillProcByName("java"); if (!System.Environment.UserInteractive && SvcController.IsServiceInstalled("mydlpengine")) { if (SvcController.IsServiceRunning("mydlpengine")) { SvcController.StopService("mydlpengine", 5000); } EnvVar[] erlEnv = new EnvVar[] { new EnvVar("MYDLP_CONF", GetShortPath(Configuration.MydlpConfPath).Replace(@"\", @"/")), new EnvVar("MYDLPBEAMDIR", GetShortPath(Configuration.ErlangPath)), new EnvVar("MYDLP_APPDIR", GetShortPath(Configuration.AppPath)), new EnvVar("ERLANG_HOME", GetShortPath(Configuration.ErlangHome)) }; ProcessControl.ExecuteCommandSync(new ExecuteParameters("sc delete mydlpengine", "SC", erlEnv)); } DelPids(); try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Ericsson\\Erlang", true)) { key.DeleteSubKeyTree("ErlSrv"); } } catch { //Logger.GetInstance().Info("Unable to delete uneccessary keys or keys do not exists"); } }