// 停止一个 Instance public ServiceControlResult StopInstance(string strInstanceName) { ServiceControlResult result = new ServiceControlResult(); try { bool bRet = ServerInfo.Host.CloseHost(strInstanceName); if (bRet == true) { result.Value = 1; // 本次停止了它 } else { result.Value = 0; // 本来就是停止状态 //result.ErrorInfo = "实例名 '" + strInstanceName + "' 没有找到"; //result.Value = -1; } return(result); } catch (Exception ex) { result.Value = -1; result.ErrorInfo = "StopInstance() 出现异常: " + ex.Message; return(result); } }
// 启动一个 Instance public ServiceControlResult StartInstance(string strInstanceName) { ServiceControlResult result = new ServiceControlResult(); try { List <string> errors = null; int nCount = ServerInfo.Host.OpenHosts(new List <string>() { strInstanceName }, out errors); if (errors != null && errors.Count > 0) { result.ErrorInfo = StringUtil.MakePathList(errors); result.Value = -1; } return(result); } catch (Exception ex) { result.Value = -1; result.ErrorInfo = "StartInstance() 出现异常: " + ex.Message; return(result); } }
// 检测 dp2capo.exe 是否在运行状态 static bool IsDp2CapoRunning() { try { IpcInfo ipc = BeginIpc(); try { ServiceControlResult result = null; InstanceInfo info = null; // 获得一个实例的信息 result = ipc.Server.GetInstanceInfo(".", out info); if (result.Value == -1) { return(false); } if (info != null) { return(info.State == "running"); } else { return(true); } } finally { EndIpc(ipc); } } catch (Exception ex) { return(false); } }
// 获得一个实例的信息 // 当 result.Value 返回值为 -1 或 0 时,info 可能返回 null public ServiceControlResult GetInstanceInfo(string strInstanceName, out InstanceInfo info) { info = null; ServiceControlResult result = new ServiceControlResult(); try { if (strInstanceName == ".") { info = new InstanceInfo { InstanceName = strInstanceName, State = "running" }; result.Value = 1; // 表示 dp2kernel 正在运行状态 return(result); } ServiceHost host = ServerInfo.Host.FindHost(strInstanceName); if (host == null) { result.Value = 0; return(result); } HostInfo host_info = host.Extensions.Find <HostInfo>(); if (host_info == null) { result.Value = -1; result.ErrorInfo = "host 中没有找到 HostInfo "; return(result); } info = new InstanceInfo { InstanceName = host_info.InstanceName, State = "running" }; result.Value = 1; return(result); } catch (Exception ex) { result.Value = -1; result.ErrorInfo = "GetInstanceInfo() 出现异常: " + ex.Message; return(result); } }
// 获得一个实例的信息 public ServiceControlResult GetInstanceInfo(string strInstanceName, out InstanceInfo info) { info = null; ServiceControlResult result = new ServiceControlResult(); try { // 查询 dp2Capo 运行状态 if (strInstanceName == ".") { info = new InstanceInfo(); info.InstanceName = strInstanceName; info.State = "running"; result.Value = 1; // 表示 dp2capo 正在运行状态 return(result); } // 查询全局服务运行状态。全局服务包括 SIP 和 Z39.50 Service if (strInstanceName == ".global") { info = new InstanceInfo(); info.InstanceName = strInstanceName; info.State = ServerInfo.GlobalServiceRunning ? "running" : "stopped"; result.Value = 1; return(result); } Instance instance = ServerInfo.FindInstance(strInstanceName); if (instance == null) { result.Value = 0; return(result); } info = new InstanceInfo(); info.InstanceName = instance.Name; info.State = instance.Running ? "running" : "stopped"; result.Value = 1; return(result); } catch (Exception ex) { result.Value = -1; result.ErrorInfo = "GetInstanceInfo() 出现异常: " + ex.Message; return(result); } }
// parameters: // strCommand start/stop/getState // return: // -1 出错 // 0/1 strCommand 为 "getState" 时分别表示实例 不在运行/在运行 状态 public static int dp2capo_serviceControl( string strCommand, string strInstanceName, out string strError) { strError = ""; try { IpcInfo ipc = BeginIpc(); try { ServiceControlResult result = null; if (strCommand == "start") { result = ipc.Server.StartInstance(strInstanceName); } else if (strCommand == "stop") { result = ipc.Server.StopInstance(strInstanceName); } else if (strCommand == "getState") { InstanceInfo info = null; // 获得一个实例的信息 result = ipc.Server.GetInstanceInfo(strInstanceName, out info); if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } else { strError = info.State; } return(result.Value); } else { strError = "未知的命令 '" + strCommand + "'"; return(-1); } if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } strError = result.ErrorInfo; return(0); } finally { EndIpc(ipc); } } catch (Exception ex) { strError = ex.Message; return(-1); } }