//根节点远程执行命令 public static string RootRemoteCommand(string identify, string eprocess, string method, string arg) { Dictionary <string, string> argDic = JsonConvert.DeserializeObject <Dictionary <string, string> >(arg); string argstr = ""; foreach (var i in argDic) { if (argstr == "") { argstr = i.Key + "=" + i.Value; } else { argstr += "&" + i.Key + "=" + i.Value; } } if (WcfGlobal.Identify == identify) { return(WcfGlobal.normalIPC.CallCmd(eprocess, method, argstr)); } else { MNodePath NodePath = null; MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); NodePath = mtree.CalculateMNodePath(WcfGlobal.Identify, identify); return(ReplyRemoteCommand(eprocess, method, argstr, NodePath)); } }
//路径首次执行 private static MNodePath FirstGetNodePath(string plugin, MNodePlugin localPlugin) { try { List <MNodePath> pathlist = new List <MNodePath>(); string[] remoteNodeId = localPlugin.RemotePlugin.Find(x => x.PluginName == plugin).MNodeIdentify.ToArray(); MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); foreach (string Id in remoteNodeId) { pathlist.Add(mtree.CalculateMNodePath(WcfGlobal.Identify, Id)); } MNodePath nodePath = null; if (localPlugin.PathStrategy == 0)//随机策略 { Random ro = new Random(); int index = ro.Next(0, pathlist.Count); nodePath = pathlist[index]; } else//最短路径? { nodePath = pathlist[0]; } return(nodePath); } catch (Exception err) { throw err; } }
//根节点远程获取服务 public static string RootRemoteGetServices(string identify) { if (WcfGlobal.Identify == identify) { return(GetServiceConfig()); } else { MNodePath NodePath = null; MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); NodePath = mtree.CalculateMNodePath(WcfGlobal.Identify, identify); return(ReplyRemoteGetServices(NodePath)); } }
//处理请求 public static string ProcessRequest(string clientId, string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "服务正在执行:" + controller + "." + method + "(" + jsondata + ")"); } if (para.NodePath == null) { // 首次请求验证 FirstVerification(clientId, plugin, controller, method, jsondata, para); if (string.IsNullOrEmpty(para.endidentify))//计算远程节点路径 { //验证本地执行还是远程执行服务 MNodePlugin localPlugin = RemotePluginManage.GetLocalPlugin(); if (localPlugin.LocalPlugin.ToList().FindIndex(x => x == plugin) != -1)//本地插件 { //执行本地数据请求 retJson = LocalDataRequest(plugin, controller, method, jsondata, para); } else if (localPlugin.RemotePlugin.FindIndex(x => x.PluginName == plugin) != -1)//远程插件 { para.NodePath = FirstGetNodePath(plugin, localPlugin); retJson = PathNextRequest(plugin, controller, method, jsondata, para); } else { throw new Exception("本中间件节点中没有配置此插件:" + plugin); } } else//计算指定节点的路径 { MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); para.NodePath = mtree.CalculateMNodePath(WcfGlobal.Identify, para.endidentify); retJson = PathNextRequest(plugin, controller, method, jsondata, para); } } else//由远程执行服务发送请求 { retJson = PathNextRequest(plugin, controller, method, jsondata, para); } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "服务" + controller + "." + method + "执行完成:" + retJson); } //更新客户端信息 ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length); return(retJson); } catch (Exception err) { //记录错误日志 if (err.InnerException == null) { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "服务" + controller + "." + method + "执行失败:" + err.Message); return(retJson); } else { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "服务" + controller + "." + method + "执行失败:" + err.InnerException.Message); return(retJson); } } }