protected void ProcessCreditLimitUpdatesThread(object param) { DoLog(string.Format("Starting ProcessCreditLimitUpdatesThread thread"), MessageType.Information); object[] parameters = (object[])param; IWebSocketConnection socket = (IWebSocketConnection)parameters[0]; WebSocketSubscribeMessage subscrMsg = (WebSocketSubscribeMessage)parameters[1]; string firmId = subscrMsg.ServiceKey; if (subscrMsg.ServiceKey.Contains("@")) { firmId = subscrMsg.ServiceKey.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)[0]; } DoLog(string.Format("Getting account record for firmId {0}", firmId), MessageType.Information); AccountRecord accRecord = AccountRecords.Where(x => x.EPFirmId == firmId).FirstOrDefault(); DoLog(string.Format("Account record for firmId {0} {1} found", firmId, accRecord != null? "do": "not"), MessageType.Information); if (accRecord == null) { return; } double maxNotional = accRecord.MaxNotional; double creditLimit = accRecord.CreditLimit; while (true) { Thread.Sleep(10000); TimeSpan elapsed = DateTime.Now - new DateTime(1970, 1, 1); creditLimit += 1000d; maxNotional += 10000; CreditLimitUpdate climUpd = new CreditLimitUpdate(); climUpd.Active = true; climUpd.CreditLimit = creditLimit; climUpd.FirmId = accRecord.EPFirmId; climUpd.MaxNotional = maxNotional; climUpd.Msg = "CreditLimitUpdate"; climUpd.RouteId = accRecord.RouteId; climUpd.Sender = 0; climUpd.Time = Convert.ToInt64(elapsed.TotalMilliseconds); DoLog(string.Format("Sending Credit Limit Update New MaxLimit:{0} New MaxNotional:{1}", creditLimit, maxNotional), MessageType.Information); DoSend <CreditLimitUpdate>(socket, climUpd); } }
protected void ProcessAccountRecord(IWebSocketConnection socket, WebSocketSubscribeMessage subscrMsg) { string prevServiceKey = subscrMsg.ServiceKey; if (subscrMsg.ServiceKey != "*") { if (subscrMsg.ServiceKey.EndsWith("@*")) { subscrMsg.ServiceKey = subscrMsg.ServiceKey.Replace("@*", ""); } AccountRecordsFirmId = subscrMsg.ServiceKey; List <AccountRecord> accountRecords = AccountRecords.Where(x => x.EPFirmId == subscrMsg.ServiceKey).ToList(); accountRecords.ForEach(x => DoSend <AccountRecord>(socket, x)); } else { AccountRecords.ToList().ForEach(x => DoSend <AccountRecord>(socket, x)); } ProcessSubscriptionResponse(socket, "TD", prevServiceKey, subscrMsg.UUID); }
private static void ProcessEvent(WebSocketMessage msg) { if (msg is ClientLoginResponse) { ClientLoginResponse loginResp = (ClientLoginResponse)msg; if (loginResp.JsonWebToken != null) { ClientLoginResponse = loginResp; } DoLog(string.Format("Client successfully logged with token {0}", loginResp.JsonWebToken)); SubscribeUserRecord(loginResp); } else if (msg is UserRecord) { //3.1-We get the UserRecord for the logged user. The main field that we are interested here is the FirmId //we have to get all the accounts for that FirmId, and we will use those accounts to fill the Accounts Combo UserRecord userRecord = (UserRecord)msg; ShowUserRecord(userRecord); SubscribeAccountRecord(userRecord); Thread.Sleep(1000); //3.2-We also want to know how much of credit it's used (CREDIT USAGE). // The credit LIMIT is calculated at a firm level and account level // In the screen we will show the credit USAGE at the FIRM level. // So we will request a Credit Limit for the Firm and based on the selected account (Account Credit Limit) // we will calculate the CREDIT USAGE. // So every time we change the account, we will have to calculate a new CREDIT USAGE <USAGE = CreditUsed <FirmLevel> / CreditLimit <AccountLevel>> //For mor info about ow to calculate the Credit Usage bar, see specs Order entry panel specs v1.x SubscribeCreditRecordUpdate(userRecord); WaitForCreditUsageBar(); } else if (msg is AccountRecord) { //4.1- After subscribing for account record, I will start getting all the accounts //I will have to save those accounts in a Collection until the SubscriptionResponse message arrives (or the timout mechanism is activated) AccountRecord accRecord = (AccountRecord)msg; AccountRecords.Add(accRecord); } else if (msg is CreditRecordUpdate) { //5.1- After subscribing for credit record updates, I will start getting all the credit records //I will have to save those recordsuntil the SubscriptionResponse message arrives (or the timout mechanism is activated) CreditRecordUpdate = (CreditRecordUpdate)msg; } else if (msg is SubscriptionResponse) { SubscriptionResponse subscrResp = (SubscriptionResponse)msg; if (subscrResp.Service == "TB") { } else if (subscrResp.Service == "TD") { ShowAccountRecords(); AccountsReceived = true;//4.2 Once we receive all the accounts , we mark the accounts as received } else if (subscrResp.Service == "CU") { CreditUsageReceived = true;//5.2 Once we receive the credit usage for the Firm, we mark the credit usage as received } //4.2-5.2 --> Once all the accounts and the credit usage is received, we are ready to show the Credit Usage progress bar <see WaitForCreditUsageBar()> } }
public static void ShowCreditUsageBarThread(object param) { string accountIdSelected = ConfigurationManager.AppSettings["AccountIdSelected"]; while (!AccountsReceived && !CreditUsageReceived) { Thread.Sleep(1000); } //We will use the first account in the combo as the Credit Limit just for the example //Every time we change the combo selection, we will have to calculate this credit usage ratio again double creditLimit = 0; if (AccountRecords.Count > 0) { AccountRecord accSelected = AccountRecords.Where(x => x.AccountId == accountIdSelected).FirstOrDefault(); if (accSelected != null && accSelected.CreditLimit >= 0) { creditLimit = accSelected.CreditLimit; } else if (accSelected == null) { creditLimit = 0; } else if (accSelected != null && accSelected.CreditLimit < 0) { creditLimit = accSelected.CreditLimit; } } DoLog(""); DoLog("================ 2)Showing CreditUsageBar ================"); if (creditLimit > 0 && CreditRecordUpdate != null) { double ratio = (CreditRecordUpdate.CreditUsed / creditLimit) * 100; DoLog(string.Format("{0}% ({1}/{2})", ratio.ToString("0.##"), CreditRecordUpdate.CreditUsed, creditLimit)); } else if (creditLimit < 0) { DoLog(string.Format("No Limits")); } else if (creditLimit == 0) { if (CreditRecordUpdate != null && CreditRecordUpdate.CreditUsed > 0) { DoLog(string.Format("100%")); } else if (CreditRecordUpdate == null || CreditRecordUpdate.CreditUsed == 0) { DoLog("No Limits"); } } else if (CreditRecordUpdate == null) { //we use 0 as a reference DoLog(string.Format("0% (0/{0})", creditLimit)); } DoLog(""); }