// ***************************************************************** // **** Constructors **** // ***************************************************************** // // // public SpreadViewer() { InitializeComponent(); this.FormClosing += new FormClosingEventHandler(FormClosedByUser); // Create the needed services. typeof(MarketTTAPI).ToString(); AppServices appServices = AppServices.GetInstance("SpreadPriceGenerator"); appServices.Info.RequestShutdownAddHandler(new EventHandler(this.RequestShutdown)); appServices.ServiceStopped += new EventHandler(Service_ServiceStopped); // Write service names to config gile if it does not exist. string configPath = string.Format("{0}{1}", appServices.Info.UserConfigPath, m_ConfigFileName); if (!System.IO.File.Exists(configPath)) { DialogResult result = System.Windows.Forms.DialogResult.Abort; result = MessageBox.Show(string.Format("Config file does not exist! \r\nFile: {0}\r\nDir: {1}\r\nShould I create an empty one?", m_ConfigFileName, appServices.Info.UserConfigPath), "Config file not found", MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.Yes) { using (System.IO.StreamWriter writer = new System.IO.StreamWriter(configPath, false)) { writer.WriteLine("<Ambre.TTServices.Markets.MarketTTAPI/>"); writer.WriteLine("<Ambre.TTServices.TTApiService FollowXTrader=True/>"); writer.Close(); } } } // Create logs. bool isLogViewerVisible = true; Log = new LogHub("SpreadProjectLogs", AppInfo.GetInstance().LogPath, isLogViewerVisible, LogLevel.ShowAllMessages); // Load the services from config file and start services. appServices.LoadServicesFromFile(m_ConfigFileName); foreach (IService service in appServices.GetServices()) { this.AddService(service); } string filePath = AppServices.GetInstance().Info.UserPath; filePath = string.Format("{0}ProductSpreadInfo.csv", filePath); SpreadInfoReader.TryReadSpreadInfoTable(filePath, Log, out m_CSVSpreadInfoReader); // Get the market tt api service. IService iService = null; if (!appServices.TryGetService("MarketTTAPI", out iService)) { Log.NewEntry(LogLevel.Warning, "Failed to find the market tt api service."); return; } m_MarketTTAPI = (MarketTTAPI)iService; // Set market reading timer. m_MarketReadTimer = new System.Timers.Timer(); m_MarketReadTimer.Interval = 2000; m_MarketReadTimer.Elapsed += new System.Timers.ElapsedEventHandler(MarketReadingTimer_Elapsed); m_MarketReadTimer.Start(); // Start and connect all the services. appServices.Start(); appServices.Connect(); }//SpreadViewer()
// ***************************************************************** // **** Properties **** // ***************************************************************** // // #endregion//Properties #region Public Methods // ***************************************************************** // **** Public Methods **** // ***************************************************************** // // // // // // /// <summary> /// This function reads lines from full path provided. /// It will also have logging function. /// It creates a output of spread information reader type. /// </summary> /// <param name="fullPath"></param> /// <param name="log"></param> /// <param name="outputReader"></param> /// <returns></returns> public static bool TryReadSpreadInfoTable(string fullPath, LogHub log, out SpreadInfoReader outputReader) { outputReader = null; bool isSuccessful = true; if (System.IO.File.Exists(fullPath)) { log.NewEntry(LogLevel.Minor, "The file path of {0} is found successfully", fullPath); // Create a IO reader for the provided path. using (System.IO.StreamReader reader = new System.IO.StreamReader(fullPath)) { string lineRead; string[] words; char[] wordDelimiters = new char[] { ',' }; bool headerRead = false; string productName; while ((lineRead = reader.ReadLine()) != null) { words = lineRead.Split(wordDelimiters); // The first line read is header line. if (!headerRead) { if (words.Length != SpreadInfoField.ColumnNumber) { log.NewEntry(LogLevel.Warning, "The column number in excel is {0} different with the desired number of {1}", words.Length, SpreadInfoField.ColumnNumber); isSuccessful = false; break; } headerRead = true; outputReader = new SpreadInfoReader(); } // The lines after the header line are body, containing field information. else { productName = words[SpreadInfoField.ProductName].Trim(); if (!m_ProductNameList.Contains(productName)) { m_ProductNameList.Add(productName); } if (!m_FirstDateDelimiters.ContainsKey(productName)) { m_FirstDateDelimiters.Add(productName, words[SpreadInfoField.FirstDateDelimiter].Trim()); } if (!m_SecondDateDelimiters.ContainsKey(productName)) { m_SecondDateDelimiters.Add(productName, words[SpreadInfoField.SecondDateDelimiter].Trim()); } } } reader.Close(); } } else { log.NewEntry(LogLevel.Warning, "The file path of {0} does not exist.", fullPath); isSuccessful = false; } return(isSuccessful); }