コピペ: OmegaChart DreamVisor終了対応 DataSourcs.cs 追加修正ポイントには//☆DreamVisor終了対応

形式
Plain text
投稿日時
2018-09-09 13:32
公開期間
無期限
  1. /*
  2. * Copyright (c) Daisuke OKAJIMA All rights reserved.
  3. *
  4. * $Id$
  5. */
  6. using System;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using System.Collections;
  11. using Zanetti.Data;
  12. namespace Zanetti.DataSource {
  13. //データの入手元の基底クラス
  14. internal abstract class DataSourceBase {
  15. public const int DATE_MASK = Int32.MinValue; //==0x80000000
  16. //エラーが起きたときのメッセージ取得
  17. public abstract string ErrorMessage { get; }
  18. //実行
  19. public abstract void Process();
  20. //外部への通知
  21. public abstract void SendMessage(int msg, int wparam, int lparam);
  22. //データの処理にかかるステップ数
  23. public abstract int TotalStep { get; }
  24. //中止
  25. public abstract void Abort();
  26. }
  27. //複数の入手元を連続的にアクセスする
  28. internal class CombinedDataSource : DataSourceBase {
  29. private IntPtr _notifyTarget;
  30. private SingleDataSource[] _elements;
  31. private int _index;
  32. protected Thread _downloader1;
  33. public CombinedDataSource(SingleDataSource[] e) {
  34. _elements = e;
  35. }
  36. public void AsyncProcess(IntPtr hwnd) {
  37. Debug.Assert(hwnd!=IntPtr.Zero);
  38. _notifyTarget = hwnd;
  39. _downloader1 = new Thread(new ThreadStart(AsyncRoot));
  40. _downloader1.Start();
  41. }
  42. public int Count {
  43. get {
  44. return _elements.Length;
  45. }
  46. }
  47. [STAThread]
  48. private void AsyncRoot() {
  49. Process();
  50. }
  51. public override void Process() {
  52. try {
  53. _index = 0;
  54. while(_index < _elements.Length) {
  55. SingleDataSource ds = _elements[_index];
  56. ds.Parent = this;
  57. ds.Process();
  58. _index++;
  59. }
  60. }
  61. catch(Exception ex) {
  62. Util.SilentReportCriticalError(ex);
  63. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  64. }
  65. }
  66. public override string ErrorMessage {
  67. get {
  68. return _elements[_index].ErrorMessage;
  69. }
  70. }
  71. public override void SendMessage(int msg, int wparam, int lparam) {
  72. //最終要素でなければ完了にはならない
  73. if(lparam==AsyncConst.LPARAM_FINISHED && _index<_elements.Length-1)
  74. lparam = AsyncConst.LPARAM_PROGRESS_SUCCESSFUL;
  75. Win32.SendMessage(_notifyTarget, msg, new IntPtr(wparam), new IntPtr(lparam));
  76. }
  77. public override void Abort() {
  78. if(_downloader1==null) throw new Exception("illegal operation: _downloader==null");
  79. if(_index<_elements.Length)
  80. _elements[_index].Abort();
  81. _downloader1.Abort();
  82. }
  83. public override int TotalStep {
  84. get {
  85. int r = 0;
  86. foreach(DataSourceBase ds in _elements)
  87. r += ds.TotalStep;
  88. return r;
  89. }
  90. }
  91. }
  92. internal abstract class SingleDataSource : DataSourceBase {
  93. protected string _errorMessage;
  94. private DataSourceBase _parent;
  95. public override string ErrorMessage {
  96. get {
  97. return _errorMessage;
  98. }
  99. }
  100. public DataSourceBase Parent {
  101. get {
  102. return _parent;
  103. }
  104. set {
  105. _parent = value;
  106. }
  107. }
  108. public override void SendMessage(int msg, int wparam, int lparam) {
  109. Debug.Assert(_parent!=null); //現状はSingleDataSourceは単独では行動できない。
  110. _parent.SendMessage(msg, wparam, lparam);
  111. }
  112. }
  113. //各銘柄について、全日付データを取得するタイプのDataSource
  114. internal abstract class FullDataSource : SingleDataSource {
  115. //ダウンロードとインポートを別個にできるようにメソッドを分ける
  116. public abstract byte[] DownloadFullData(BasicBrand br);
  117. public abstract void ImportFullData(BasicBrand br, byte[] data);
  118. protected CodeEnumerator _codeEnumerator;
  119. protected AbstractBrand _targetBrand;
  120. protected AsyncImporter _importer;
  121. protected FullDataSource(CodeEnumerator ce) {
  122. _codeEnumerator = ce;
  123. }
  124. public override void Process() {
  125. _importer = new AsyncImporter(this);
  126. try {
  127. _targetBrand = _codeEnumerator.Next;
  128. while(_targetBrand!=null) {
  129. if(_targetBrand is BasicBrand) {
  130. try {
  131. byte[] data = DownloadFullData((BasicBrand)_targetBrand);
  132. ImportFullData((BasicBrand)_targetBrand, data); //single thread
  133. _importer.ImportBrand((BasicBrand)_targetBrand, data);
  134. SendMessage(AsyncConst.WM_ASYNCPROCESS, _targetBrand.Code, AsyncConst.LPARAM_PROGRESS_SUCCESSFUL);
  135. }
  136. catch(Exception ex) {
  137. Debug.WriteLine("Download failed code " + _targetBrand.Code);
  138. Util.SilentReportCriticalError(ex);
  139. _errorMessage = ex.Message;
  140. SendMessage(AsyncConst.WM_ASYNCPROCESS, _targetBrand.Code, AsyncConst.LPARAM_PROGRESS_FAILURE);
  141. }
  142. }
  143. _targetBrand = _codeEnumerator.Next;
  144. }
  145. _importer.WaitFinish();
  146. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_FINISHED);
  147. }
  148. catch(ThreadAbortException) { //Abortのときは何もしない
  149. Debug.WriteLine("Download thread aborted");
  150. }
  151. catch(Exception ex) {
  152. Util.SilentReportCriticalError(ex);
  153. _errorMessage = ex.Message;
  154. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  155. }
  156. _importer.Finish();
  157. }
  158. public override int TotalStep {
  159. get {
  160. return _codeEnumerator.Count;
  161. }
  162. }
  163. public override void Abort() {
  164. if(_importer!=null)
  165. _importer.Abort();
  166. }
  167. public class AsyncImporter {
  168. private FullDataSource _parent;
  169. private BasicBrand _brand;
  170. private byte[] _data;
  171. private AutoResetEvent _importStartEvent;
  172. private AutoResetEvent _importFinishEvent;
  173. private bool _finished;
  174. private Thread _slave;
  175. public AsyncImporter(FullDataSource ds) {
  176. _parent = ds;
  177. _importStartEvent = new AutoResetEvent(false);
  178. _importFinishEvent = new AutoResetEvent(true);
  179. _slave = new Thread(new ThreadStart(Run));
  180. _slave.Start();
  181. }
  182. public void Finish() {
  183. _finished = true;
  184. _importStartEvent.Set();
  185. }
  186. public void WaitFinish() {
  187. _importFinishEvent.WaitOne();
  188. }
  189. //現状FinishとAbortに区別はない
  190. public void Abort() {
  191. _finished = true;
  192. _importStartEvent.Set();
  193. }
  194. public void ImportBrand(BasicBrand br, byte[] data) {
  195. _importFinishEvent.WaitOne();
  196. _brand = br;
  197. _data = data;
  198. _importStartEvent.Set(); //slave threadを起動
  199. }
  200. public void Run() {
  201. while(true) {
  202. try {
  203. _importStartEvent.WaitOne();
  204. if(_finished) break;
  205. _parent.ImportFullData(_brand, _data);
  206. _importFinishEvent.Set();
  207. }
  208. catch(ThreadAbortException) { //Abortのときは何もしない
  209. Debug.WriteLine("Download thread aborted");
  210. }
  211. catch(Exception ex) {
  212. Util.SilentReportCriticalError(ex);
  213. _parent._errorMessage = ex.Message;
  214. _parent.SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  215. }
  216. }
  217. CleanUp();
  218. }
  219. private void CleanUp() {
  220. _importStartEvent.Close();
  221. _importFinishEvent.Close();
  222. }
  223. }
  224. }
  225. //ダウンロードしたい銘柄をリストアップするクラス
  226. internal abstract class CodeEnumerator {
  227. public abstract AbstractBrand Next { get; }
  228. public abstract int Count { get; }
  229. internal class AllCodeEnumerator : CodeEnumerator {
  230. private IDictionaryEnumerator _e;
  231. public AllCodeEnumerator() {
  232. _e = Env.BrandCollection.GetEnumerator();
  233. }
  234. public override AbstractBrand Next {
  235. get {
  236. return _e.MoveNext()? (AbstractBrand)_e.Value : null;
  237. }
  238. }
  239. public override int Count {
  240. get {
  241. return Env.BrandCollection.StockBrandCount;
  242. }
  243. }
  244. }
  245. //個別銘柄のみのEnumerator
  246. internal class AllStockEnumerator : CodeEnumerator {
  247. private IDictionaryEnumerator _e;
  248. public AllStockEnumerator() {
  249. _e = Env.BrandCollection.GetEnumerator();
  250. }
  251. public override AbstractBrand Next {
  252. get {
  253. AbstractBrand br = null;
  254. do {
  255. br = _e.MoveNext()? (AbstractBrand)_e.Value : null;
  256. } while(br!=null && br.IsBuiltIn);
  257. return br;
  258. }
  259. }
  260. public override int Count {
  261. get {
  262. return Env.BrandCollection.StockBrandCount;
  263. }
  264. }
  265. }
  266. internal class BookmarkCodeEnumerator : CodeEnumerator {
  267. private ArrayList _codes;
  268. private int _index;
  269. public BookmarkCodeEnumerator() {
  270. _codes = Env.Bookmark.AllStockBrands;
  271. _index = 0;
  272. }
  273. public override AbstractBrand Next {
  274. get {
  275. return _index==_codes.Count? null : (AbstractBrand)_codes[_index++];
  276. }
  277. }
  278. public override int Count {
  279. get {
  280. return _codes.Count;
  281. }
  282. }
  283. }
  284. internal class Single : CodeEnumerator {
  285. private AbstractBrand _target;
  286. public Single(AbstractBrand br) {
  287. _target =br;
  288. }
  289. public override AbstractBrand Next {
  290. get {
  291. //これで1回だけ返すことができる
  292. AbstractBrand b = _target;
  293. _target = null;
  294. return b;
  295. }
  296. }
  297. public override int Count {
  298. get {
  299. return 1;
  300. }
  301. }
  302. }
  303. //ビルトインの指数を列挙する基底
  304. internal abstract class BuiltInEnumerator : CodeEnumerator {
  305. private int[] _codes;
  306. private int _index;
  307. public BuiltInEnumerator(int[] codes) {
  308. _codes = codes;
  309. _index = 0;
  310. }
  311. public override AbstractBrand Next {
  312. get {
  313. return _index==_codes.Length? null : Env.BrandCollection.FindBrand(_codes[_index++]);
  314. }
  315. }
  316. public override int Count {
  317. get {
  318. return _codes.Length;
  319. }
  320. }
  321. }
  322. //指数
  323. internal class DomesticIndicesEnumerator : BuiltInEnumerator {
  324. protected static int[] _codes;
  325. public DomesticIndicesEnumerator() : base(Init()) {
  326. }
  327. private static int[] Init() {
  328. if(_codes!=null) return _codes;
  329. ArrayList ar = new ArrayList();
  330. ar.Add((int)BuiltInIndex.Nikkei225);
  331. ar.Add((int)BuiltInIndex.TOPIX);
  332. ar.Add((int)BuiltInIndex.JASDAQ);
  333. //ar.Add((int)BuiltInIndex.LONGTERM_INTEREST);
  334. ar.Add((int)BuiltInIndex.Nikkei225_F);
  335. ar.Add((int)BuiltInIndex.TOPIX_F);
  336. _codes = (int[])ar.ToArray(typeof(int));
  337. return _codes;
  338. }
  339. }
  340. internal class ForeignIndicesEnumerator : BuiltInEnumerator {
  341. protected static int[] _codes;
  342. public ForeignIndicesEnumerator() : base(Init()) {
  343. }
  344. private static int[] Init() {
  345. if(_codes!=null) return _codes;
  346. ArrayList ar = new ArrayList();
  347. ar.Add((int)BuiltInIndex.Dow);
  348. ar.Add((int)BuiltInIndex.Nasdaq);
  349. ar.Add((int)BuiltInIndex.SP500);
  350. _codes = (int[])ar.ToArray(typeof(int));
  351. return _codes;
  352. }
  353. }
  354. //為替
  355. internal class CurrenciesEnumerator : BuiltInEnumerator {
  356. protected static int[] _codes;
  357. public CurrenciesEnumerator() : base(Init()) {
  358. }
  359. private static int[] Init() {
  360. if(_codes!=null) return _codes;
  361. ArrayList ar = new ArrayList();
  362. ar.Add((int)BuiltInIndex.JPYUSD);
  363. ar.Add((int)BuiltInIndex.JPYEUR);
  364. _codes = (int[])ar.ToArray(typeof(int));
  365. return _codes;
  366. }
  367. }
  368. //業種別
  369. internal class SectorsEnumerator : BuiltInEnumerator {
  370. protected static int[] _codes;
  371. public SectorsEnumerator() : base(Init()) {
  372. }
  373. private static int[] Init() {
  374. if(_codes!=null) return _codes;
  375. _codes = new int[(int)BuiltInIndexGroup.SectorIndexEnd - (int)BuiltInIndexGroup.SectorIndexStart + 1];
  376. for(int i = 0; i < _codes.Length; i++)
  377. _codes[i] = (int)BuiltInIndexGroup.SectorIndexStart + i;
  378. return _codes;
  379. }
  380. }
  381. //汎用
  382. internal class VariousEnumerator : BuiltInEnumerator
  383. {
  384. protected static int[] _codes;
  385. public VariousEnumerator(ArrayList ar) : base(Init(ar))
  386. {
  387. }
  388. private static int[] Init(ArrayList ar)
  389. {
  390. if (_codes != null) _codes = null;
  391. _codes = (int[])ar.ToArray(typeof(int));
  392. return _codes;
  393. }
  394. }
  395. #if DOJIMA
  396. //商品
  397. internal class CommodityEnumerator : BuiltInEnumerator {
  398. protected static int[] _codes;
  399. public CommodityEnumerator() : base(Init()) {
  400. }
  401. private static int[] Init() {
  402. if(_codes != null) return _codes;
  403. _codes = new int[CommodityUtil.INDEX_END - CommodityUtil.INDEX_START + 1];
  404. for(int i = 0; i < _codes.Length; i++)
  405. _codes[i] = CommodityUtil.INDEX_START + i;
  406. return _codes;
  407. }
  408. }
  409. #endif
  410. }
  411. //個別銘柄の日次データをダウンロードしてまとめて追加する
  412. internal abstract class DailyDataSource : SingleDataSource {
  413. protected int[] _dates;
  414. public DailyDataSource(int[] dates) {
  415. _dates = dates;
  416. }
  417. public override void Process() {
  418. try {
  419. Run();
  420. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_FINISHED);
  421. }
  422. catch(ThreadAbortException) { //Abortのときは何もしない
  423. Debug.WriteLine("Download thread aborted");
  424. }
  425. catch(Exception ex) {
  426. Debug.WriteLine(ex.Message);
  427. Debug.WriteLine(ex.StackTrace);
  428. _errorMessage = ex.Message;
  429. Util.SilentReportCriticalError(ex);
  430. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  431. }
  432. }
  433. public override int TotalStep {
  434. get {
  435. return Env.BrandCollection.StockBrandCount;
  436. }
  437. }
  438. public abstract void Run();
  439. //DailyDataSourceではマルチスレッドにはならないので何もしない
  440. public override void Abort() {
  441. }
  442. }
  443. //☆DreamVisor終了対応 新規追加
  444. //日付指定なし、日次データをダウンロードするためのClass
  445. internal abstract class DailyDataNoDatesSource : SingleDataSource
  446. {
  447. protected CodeEnumerator _ce;
  448. public DailyDataNoDatesSource(CodeEnumerator ce)
  449. {
  450. _ce = ce;
  451. }
  452. public override void Process()
  453. {
  454. try
  455. {
  456. Run();
  457. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_FINISHED);
  458. }
  459. catch (ThreadAbortException)
  460. { //Abortのときは何もしない
  461. Debug.WriteLine("Download thread aborted");
  462. }
  463. catch (Exception ex)
  464. {
  465. Debug.WriteLine(ex.Message);
  466. Debug.WriteLine(ex.StackTrace);
  467. _errorMessage = ex.Message;
  468. Util.SilentReportCriticalError(ex);
  469. SendMessage(AsyncConst.WM_ASYNCPROCESS, 0, AsyncConst.LPARAM_ERROR);
  470. }
  471. }
  472. public override int TotalStep
  473. {
  474. get
  475. {
  476. return Env.BrandCollection.StockBrandCount;
  477. }
  478. }
  479. public abstract void Run();
  480. //DailyDataSourceではマルチスレッドにはならないので何もしない
  481. public override void Abort()
  482. {
  483. }
  484. }
  485. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト