2e9da1e884ff968afe2029660323b6c9c49eedb1.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Threading;
  3. using System.Collections;
  4. using System.Reflection;
  5. using Core.Mes.IBaseInterface;
  6. namespace Core.Mes.ServerFrameWork
  7. {
  8. /// <summary>
  9. /// 容器型服务的统一接口
  10. /// </summary>
  11. public class IServerPool : IServerBase
  12. {
  13. #region IServerBase 成员
  14. public string Description
  15. {
  16. get
  17. {
  18. // TODO: 添加 IServerPool.Description getter 实现
  19. return null;
  20. }
  21. }
  22. private Hashtable _dbManager = null;
  23. public Hashtable DBManagerList
  24. {
  25. get{return _dbManager;}
  26. set{_dbManager = value;}
  27. }
  28. private Hashtable _htComponent = new Hashtable();
  29. public Hashtable HtComponent
  30. {
  31. get { return _htComponent;}
  32. set { _htComponent = value;}
  33. }
  34. #endregion
  35. #region IDisposable 成员
  36. public void Dispose()
  37. {
  38. System.Collections.IEnumerator ie = this.HtComponent.GetEnumerator() ;
  39. while(ie.MoveNext())
  40. {
  41. try
  42. {
  43. System.Collections.DictionaryEntry de = (DictionaryEntry)ie.Current;
  44. DisposeClass((Hashtable)de.Value,0);
  45. DisposeClass((Hashtable)de.Value,1);
  46. }
  47. catch{}
  48. }
  49. }
  50. private void DisposeClass(Hashtable ht, int index)
  51. {
  52. try
  53. {
  54. ArrayList al = ht[index] as ArrayList;
  55. int count = al.Count ;
  56. for(int i = 0; i < al.Count ; i++)
  57. {
  58. try
  59. {
  60. ((IComponent)al[0]).Dispose();
  61. }
  62. catch{}
  63. }
  64. }
  65. catch{}
  66. }
  67. //=======================================================
  68. // 加载单个类,并按最小副本数生成多个类的实例
  69. // classPoolHash 0--闲列表 1-忙列表 2--最小副本数 3--最大副本数
  70. //=======================================================
  71. public void LoadClass(Type moduleType, string dataBaseName)
  72. {
  73. try
  74. {
  75. string className = moduleType.FullName;
  76. //== 反射类
  77. IComponent module = (IComponent)Activator.CreateInstance(moduleType);
  78. module.DBName = dataBaseName;
  79. module.DBManagerList = this.DBManagerList;
  80. int maxCopyValue = module.maxValue;
  81. int minCopyValue = module.minValue;
  82. Hashtable classPoolHash = (Hashtable)HtComponent[className];
  83. if(classPoolHash == null)
  84. {
  85. classPoolHash = new Hashtable();
  86. HtComponent.Add(className, classPoolHash);
  87. }
  88. //存储每个类的空闲列表
  89. ArrayList freeList = classPoolHash[0] as ArrayList;
  90. if(freeList == null)
  91. {
  92. freeList = new ArrayList();
  93. classPoolHash.Add(0,freeList);
  94. }
  95. //存储一个空忙列表
  96. ArrayList busyList = classPoolHash[1] as ArrayList;
  97. if(busyList==null)
  98. {
  99. busyList=new ArrayList();
  100. classPoolHash.Add(1,busyList);
  101. }
  102. //加入最小副本数个副本到空闲列表
  103. freeList.Add(module);
  104. if (minCopyValue < 1) minCopyValue = 1;
  105. for(int i=0; i<minCopyValue-1; i++)
  106. {
  107. IComponent classObj = (IComponent)Activator.CreateInstance(moduleType);
  108. classObj.DBName = dataBaseName;
  109. classObj.DBManagerList = this.DBManagerList;
  110. freeList.Add(classObj);
  111. }
  112. //加入最大,最小副本值
  113. classPoolHash.Add(2,maxCopyValue);
  114. classPoolHash.Add(3,minCopyValue);
  115. }
  116. catch (Exception ex)
  117. {
  118. Console.WriteLine(ex.Message);
  119. }
  120. }
  121. #endregion
  122. private string _serverName = "";
  123. public string ServerName
  124. {
  125. get { return _serverName; }
  126. set { _serverName = value; }
  127. }
  128. private string _assemblyName = "";
  129. public string AssemblyName
  130. {
  131. get { return _assemblyName; }
  132. set { _assemblyName = value; }
  133. }
  134. public ReturnObject HandleMethod(string className, string methodName, object[] args)
  135. {
  136. //查找对应的业务组件子服务
  137. Result structObj = this.searchObject(className);
  138. if (structObj.busy.Equals("Error"))
  139. {
  140. //无此所访问的类
  141. return new ReturnObject(null,10003,"未找到请求的类!");
  142. }
  143. else if (structObj.busy.Equals("Wait"))
  144. {
  145. //达到最大访问限制,等待,超过规定时间放弃
  146. int i=1;
  147. while(structObj.busy.Equals("Wait"))
  148. {
  149. if ((i++)>30)
  150. {
  151. //3秒后放弃,超时返回
  152. return new ReturnObject(null,10001,"服务正忙,请稍后再试!");
  153. }
  154. Thread.Sleep(100);
  155. structObj = this.searchObject(className);
  156. }
  157. }
  158. //得到正确的副本后...
  159. //执行方法
  160. Type type = structObj.obj.GetType();
  161. MethodInfo mthod=type.GetMethod(methodName);
  162. object myReturnObj;
  163. if(mthod==null)
  164. {
  165. myReturnObj = new ReturnObject(null,10002,"未找到方法,请检查方法名是否正确!");
  166. }
  167. else
  168. {
  169. try
  170. {
  171. myReturnObj = mthod.Invoke(structObj.obj,args);
  172. }
  173. catch (Exception ex)
  174. {
  175. myReturnObj = new ReturnObject(null, ex.Message);
  176. }
  177. }
  178. //释放副本
  179. this.realeasObject(className,structObj.obj as IComponent);
  180. if (myReturnObj.GetType().Equals(typeof(ReturnObject)))
  181. return (ReturnObject)myReturnObj;
  182. else
  183. return new ReturnObject(myReturnObj,10005,"未按指定的类型定义返回值!");
  184. }
  185. private Result searchObject(string className)
  186. {
  187. string busy="Success";
  188. string err="";
  189. Hashtable poolHash=(Hashtable)this.HtComponent[className];
  190. if(poolHash==null)
  191. {
  192. busy="Error";
  193. err="no the class";
  194. Result result=new Result(busy,err,null);
  195. return result;
  196. }
  197. lock(poolHash)
  198. {
  199. //取出空闲和忙列表
  200. ArrayList frList = poolHash[0] as ArrayList;
  201. ArrayList bsyList = poolHash[1] as ArrayList;
  202. if(bsyList==null)
  203. {
  204. bsyList=new ArrayList();
  205. poolHash.Add(1,bsyList);
  206. }
  207. //如果无空闲副本,且忙列表超过最大副本数目,等待后重新请求
  208. //否则返回空闲副本
  209. if(frList.Count>0)
  210. {
  211. IComponent temp=(IComponent)frList[0];
  212. bsyList.Add(temp);
  213. frList.RemoveAt(0);
  214. Result result=new Result(busy,err,temp);
  215. return result;
  216. }
  217. //最大,等待
  218. if(bsyList.Count==Convert.ToInt16(poolHash[2]))
  219. {
  220. busy="Wait";
  221. Result result=new Result(busy,err,null);
  222. return result;
  223. }
  224. //创建并返回新副本
  225. if(bsyList.Count<Convert.ToInt16(poolHash[2]))
  226. {
  227. Assembly ab=Assembly.Load(this.AssemblyName);
  228. Type tp=ab.GetType(className);
  229. IComponent newObj=(IComponent)Activator.CreateInstance(tp);
  230. bsyList.Add(newObj);
  231. Result result=new Result(busy,err,newObj);
  232. return result;
  233. }
  234. return new Result("","",null);
  235. }
  236. }
  237. public string realeasObject(string className, IComponent busyObj)
  238. {
  239. string err = "success";
  240. Hashtable poolHash = (Hashtable)this.HtComponent[className];
  241. if (poolHash == null)
  242. {
  243. err = "no the class";
  244. return err;
  245. }
  246. lock (poolHash)
  247. {
  248. //移入空闲列表
  249. ArrayList frList = poolHash[0] as ArrayList;
  250. ArrayList bsyList = poolHash[1] as ArrayList;
  251. //如果空闲副本数目大于最小副本数目,销毁生存期较长副本
  252. if (frList.Count > Convert.ToInt16(poolHash[3]))
  253. {
  254. bsyList.Remove(busyObj);
  255. busyObj.Dispose();
  256. busyObj = null;
  257. return err;
  258. }
  259. frList.Add(busyObj);
  260. bsyList.Remove(busyObj);
  261. return err;
  262. }
  263. }
  264. public struct Result
  265. {
  266. public string busy;
  267. public string err;
  268. public object obj;
  269. public Result(string busy,string err,object obj)
  270. {
  271. this.busy=busy;
  272. this.err=err;
  273. this.obj=obj;
  274. }
  275. }
  276. }
  277. }