public byte[] ToBytesForQuotation(Token token,TraderState state,QuotationCommand command) { byte[] result = null; try { if (token.AppType == AppType.Mobile) { result = GetQuotationWhenMobile(token, state, command); } else if (token.AppType == AppType.TradingConsole) { var quotation = Quotation4Bit.TryAddQuotation(command.OverridedQs, state, command.Sequence); if (quotation == null) { return null; } result = quotation.GetData(); } } catch (Exception ex) { _Logger.Error(ex); } return result; }
protected bool IsQuotationExceedTimeDiff(QuotationCommand quotationCommand) { DateTime baseTime = DateTime.Now; if (quotationCommand.OverridedQs != null) { foreach (var eachOverrideQs in quotationCommand.OverridedQs) { if ((baseTime - eachOverrideQs.Timestamp).TotalMilliseconds >= SettingManager.Default.QuotationTimeDiffInMS) { return(true); } } } if (quotationCommand.OriginQs != null) { foreach (var eachOriginQuotation in quotationCommand.OriginQs) { if ((baseTime - eachOriginQuotation.Timestamp).TotalMilliseconds >= SettingManager.Default.QuotationTimeDiffInMS) { return(true); } } } return(false); }
public void GetQuotation_WholesalerNotExist_ThrowException() { QuotationCommand command = new QuotationCommand() { WholesalerId = 70, TotalPrice = 100, Items = new List <ItemCommand> { new ItemCommand() { BeerId = 1, Quantity = 10 }, new ItemCommand() { BeerId = 2, Quantity = 5 } } }; using (var context = new BrasserieContext(ContextOptions)) { var wholesalerService = new WholesalerService(context); Action action = () => wholesalerService.GetQuotation(command); action.Should().ThrowExactly <NotFindObjectException>().WithMessage(ExceptionMessage.WHOLESALER_NOT_EXIST); } }
public void AddQuotation(QuotationCommand quotation) { this._Commands.Add(quotation); var target = CommandWithQuotationPool.Default.Pop(); target.Initialize(quotation, null, true); AgentController.Default.AddQuotation(target); }
private static byte[] GetDataIn4BitFormat(TraderState state, QuotationCommand command, int?beginSequence = null, int?endSequence = null) { var stringBuilder = new StringBuilder(CAPACITY); string seqenceStr = string.Format("{0}{1}{2}", (beginSequence ?? command.Sequence), _SequenceSeparator, (endSequence ?? command.Sequence)); stringBuilder.Append(seqenceStr); stringBuilder.Append(_StartSeparator); OverridedQuotation[] overridedQuotations = command.OverridedQs; if (overridedQuotations != null && overridedQuotations.Length > 0) { for (int i = 0; i < overridedQuotations.Length; i++) { OverridedQuotation overridedQuotation = overridedQuotations[i]; if (!state.InstrumentsView.ContainsKey(overridedQuotation.InstrumentID)) { continue; } if (overridedQuotation.QuotePolicyID != state.InstrumentsView[overridedQuotation.InstrumentID]) { continue; } if (i != 0) { stringBuilder.Append(_OutterSeparator); } AppendQuotationParts(stringBuilder, overridedQuotation); } } stringBuilder.Append(_StartSeparator); return(Quotation4BitEncoder.Encode(stringBuilder.ToString())); }
public void GetQuotation_CheckDuplicateOrder_ThrowException() { QuotationCommand command = new QuotationCommand() { WholesalerId = 1, TotalPrice = 100, Items = new List <ItemCommand> { new ItemCommand() { BeerId = 7, Quantity = 10 }, new ItemCommand() { BeerId = 7, Quantity = 3 } } }; using (var context = new BrasserieContext(ContextOptions)) { var wholesalerService = new WholesalerService(context); Action action = () => wholesalerService.GetQuotation(command); action.Should().ThrowExactly <DuplicateItemException>().WithMessage(ExceptionMessage.DUPLICATE_ITEM); } }
public void Add(QuotationCommand quotation) { lock (this._Lock) { this._QuotationQueue.Add(quotation); } }
public void Push(QuotationCommand quotation) { lock (this._Lock) { this._Pool.Push(quotation); } }
public ActionResult <QuotationCommand> GetQuotations(QuotationCommand command) { var price = _wholesalerService.GetQuotation(command); command.TotalPrice = price; return(Ok(command)); }
public static byte[] GetPriceInBytes(Token token, TraderState state, QuotationCommand command, int beginSequence, int endSequence) { if (token.AppType == AppType.TradingConsole) { return(GetDataIn4BitFormat(state, command, beginSequence, endSequence)); } return(GetDataInGeneralFormat(token, state, command, beginSequence, endSequence)); }
public static byte[] GetPriceInBytes(Token token,TraderState state,QuotationCommand command,int beginSequence,int endSequence) { if (token.AppType == AppType.TradingConsole) { return GetDataIn4BitFormat(state, command,beginSequence,endSequence); } return GetDataInGeneralFormat(token, state, command,beginSequence,endSequence); }
public void AddCommand(iExchange.Common.Token token, iExchange.Common.Command command) { QuotationCommand quotationCommand = command as QuotationCommand; if (quotationCommand != null) { var targetCommand = SystemController.Factory.CommandFactory.CreateQuotationCommand(quotationCommand.OriginQs, quotationCommand.OverridedQs); Broadcaster.Default.AddCommand(targetCommand); } }
public void SendCommand(QuotationCommand quotationCommand = null, Command command = null) { if (_ClientsProcessor.IsEmpty()) { return; } var target = new CommandForClient(quotationCommand: quotationCommand, command: command); _CommandTransfer.Send(target); }
public XmlNode GetCommands(Token token, State state, int firstSequence, int lastSequence) { this._ReaderWriterLock.EnterReadLock(); try { XmlDocument xmlDoc = new XmlDocument(); XmlElement commands = xmlDoc.CreateElement("Commands"); xmlDoc.AppendChild(commands); List <Command> commandList = this.InnerGet(firstSequence, lastSequence); if (commandList == null) { this._Logger.ErrorFormat("get lost command {0} -------{1} return null", firstSequence, lastSequence); return(null); } TradingConsoleState tradingConsoleState = state as TradingConsoleState; ArrayList quotationCommands = new ArrayList(); foreach (var command in commandList) { QuotationCommand quotation = command as QuotationCommand; if (quotation != null) { quotationCommands.Add(quotation); } else { if (quotationCommands.Count > 0) { QuotationCommand mergingCommand = this.Merge(quotationCommands); quotationCommands.Clear(); this.AppendChild(commands, mergingCommand, token, state); } this.AppendChild(commands, command, token, state); } } if (quotationCommands.Count > 0) { QuotationCommand mergingCommand = this.Merge(quotationCommands); quotationCommands.Clear(); this.AppendChild(commands, mergingCommand, token, state); } commands.SetAttribute("FirstSequence", firstSequence.ToString()); commands.SetAttribute("LastSequence", lastSequence.ToString()); return(commands); } finally { this._ReaderWriterLock.ExitReadLock(); } }
public void Add(QuotationCommand quotation) { if (this._IsSendPriceImmediately) { this._Queue.Enqueue(quotation); this._WaitEvent.Set(); } else { lock (this._Lock) { this._QuotationQueue.Add(quotation); } } }
public void GetQuotation_CommandItemIsNull_ThrowException() { QuotationCommand command = new QuotationCommand() { WholesalerId = 1, TotalPrice = 100, Items = null }; using (var context = new BrasserieContext(ContextOptions)) { var wholesalerService = new WholesalerService(context); Action action = () => wholesalerService.GetQuotation(command); action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.COMMAND_IS_NULL); } }
public void SetQuotation(OriginQ[] originQs, OverridedQ[] overridedQs) { try { QuotationCommand command = new QuotationCommand { OriginQs = originQs, OverridedQs = overridedQs, IsQuotation = true }; Broadcaster.Default.AddCommand(command); } catch (Exception ex) { Logger.Error(ex); } }
private QuotationCommand Merge(ArrayList quotationCommands) { if (quotationCommands == null || quotationCommands.Count == 0) { return(null); } else if (quotationCommands.Count == 1) { return((QuotationCommand)quotationCommands[0]); } else { QuotationCommand lastQuotationCommand = (QuotationCommand)(quotationCommands[quotationCommands.Count - 1]); QuotationCommand mergingCommand = new QuotationCommand(lastQuotationCommand.Sequence); mergingCommand.Merge(quotationCommands); return(mergingCommand); } }
private static byte[] GetDataInGeneralFormat(Token token, TraderState state, QuotationCommand command, int?beginSequence = null, int?endSequence = null) { XmlDocument xmlDoc = new XmlDocument(); XmlElement commands = xmlDoc.CreateElement("Commands"); xmlDoc.AppendChild(commands); XmlNode commandNode = command.ToXmlNode(token, state); if (commandNode == null) { return(null); } XmlNode commandNode2 = commands.OwnerDocument.ImportNode(commandNode, true); commands.AppendChild(commandNode2); commands.SetAttribute("FirstSequence", (beginSequence ?? command.Sequence).ToString()); commands.SetAttribute("LastSequence", (endSequence ?? command.Sequence).ToString()); return(PacketConstants.ContentEncoding.GetBytes(commands.OuterXml)); }
private void ProcessCommand() { while (true) { if (this._IsStop) { break; } WaitHandle.WaitAny(this._Events); Command command; while (this._Queue.TryDequeue(out command)) { QuotationCommand quotation = command as QuotationCommand; if (quotation != null) { QuotationDispatcher.Default.Add(quotation); #if TEST QuotationTester.Default.Add(); #else #endif } else { CompositeCommand compositeCommand = command as CompositeCommand; if (compositeCommand != null) { foreach (var cmd in compositeCommand.Commands) { this._Commands.Add(cmd); Application.Default.AgentController.SendCommand(command: cmd); } } else { this._Commands.Add(command); Application.Default.AgentController.SendCommand(command: command); } } } } }
private void WriteForQuotation() { Token token; TraderState state = SessionManager.Default.GetTokenAndState(this._Id, out token); if (state == null || token == null) { Write(); return; } this._QuotationQueue.Clear(); this._QuotationQueue.Add(this._CurrentCommand.Quotation.QuotationCommand); while (this._Queue.Count > 0) { CommandForClient item = this._Queue.Peek(); if (item.CommandType != DataType.Quotation) { break; } item = this._Queue.Dequeue(); this._QuotationQueue.Add(item.Quotation.QuotationCommand); } byte[] content; if (this._QuotationQueue.Count == 1) { content = this._CurrentCommand.Quotation.GetPriceInBytes(token, state); } else { QuotationCommand command = new QuotationCommand(); command.Merge(this._QuotationQueue); content = QuotationTranslator.GetPriceInBytes(token, state, command, this._QuotationQueue[0].Sequence, this._QuotationQueue[this._QuotationQueue.Count - 1].Sequence); } if (content == null) { Write(); return; } this._CurrentPacket = SerializeManager.Default.SerializePrice(content); this.BeginWrite(this._CurrentPacket, 0, this._CurrentPacket.Length); }
private void Process() { while (true) { if (this._IsStoped) { break; } if (this._IsSendPriceImmediately) { WaitHandle.WaitAny(this._Events); QuotationCommand quotation; while (this._Queue.TryDequeue(out quotation)) { CommandManager.Default.AddQuotation(quotation); } } else { Thread.Sleep(_ProcessPeriodMilliseconds); var qotation = new QuotationCommand(); lock (this._Lock) { if (this._QuotationQueue.Count == 0) { continue; } qotation.Merge(this._QuotationQueue); this._QuotationQueue.Clear(); } if (qotation.OverridedQs == null || qotation.OverridedQs.Length == 0) { continue; } CommandManager.Default.AddQuotation(qotation); } } }
public void GetQuotation_Discount20_IsOk() { var expect = 44.00; QuotationCommand command = new QuotationCommand() { WholesalerId = 1, Items = new List <ItemCommand> { new ItemCommand() { BeerId = 1, Quantity = 25 } } }; using (var context = new BrasserieContext(ContextOptions)) { var wholesalerService = new WholesalerService(context); var actual = wholesalerService.GetQuotation(command); actual.Should().Be(expect); } }
public CommandForClient(UnmanagedMemory data = null, QuotationCommand quotationCommand = null, Command command = null) { if (command != null) { this._Command = command; this._Quotation = null; this._Data = null; this._CommandType = DataType.Command; } else if (quotationCommand != null) { this._Quotation = new QuotationTranslator(quotationCommand); this._Command = null; this._Data = null; this._CommandType = DataType.Quotation; } else { this._Data = data; this._Command = null; this._Quotation = null; this._CommandType = DataType.Response; } }
public void GetQuotation_StockNotEnough_ThrowException() { QuotationCommand command = new QuotationCommand() { WholesalerId = 1, TotalPrice = 100, Items = new List <ItemCommand> { new ItemCommand() { BeerId = 1, Quantity = 40 } } }; using (var context = new BrasserieContext(ContextOptions)) { var wholesalerService = new WholesalerService(context); Action action = () => wholesalerService.GetQuotation(command); action.Should().ThrowExactly <NotEnoughQuantityException>().WithMessage(ExceptionMessage.ENOUGH_STOCK); } }
public void AddQuotation(QuotationCommand quotation) { this._Commands.Add(quotation); Application.Default.AgentController.SendCommand(quotation); }
public void SendCommand(QuotationCommand quotationCommand=null,Command command =null) { if (_ClientsProcessor.IsEmpty()) return; var target = new CommandForClient(quotationCommand:quotationCommand,command:command); _CommandTransfer.Send(target); }
public QuotationTranslator(QuotationCommand command) { this._QuotationCommand = command; }
public void Initialize(QuotationCommand quotation, Command command, bool isQuotation) { this._QuotationCommand = quotation; this._Command = command; this._isQuotation = isQuotation; }
private byte[] GetQuotationWhenMobile(Token token, TraderState state,QuotationCommand command) { byte[] result = null; result = GetQuotationCommon(token, state); if (result == null) { XmlDocument xmlDoc = new XmlDocument(); XmlElement commands = xmlDoc.CreateElement("Commands"); xmlDoc.AppendChild(commands); XmlNode commandNode = command.ToXmlNode(token, state); if (commandNode == null) { return null; } XmlNode commandNode2 = commands.OwnerDocument.ImportNode(commandNode, true); commands.AppendChild(commandNode2); commands.SetAttribute("FirstSequence", command.Sequence.ToString()); commands.SetAttribute("LastSequence", command.Sequence.ToString()); result = Constants.ContentEncoding.GetBytes(commands.OuterXml); CacheQuotationCommon(token.AppType, state.SignMapping, result); } return result; }
private QuotationCommand Merge(ArrayList quotationCommands) { if (quotationCommands == null || quotationCommands.Count == 0) { return null; } else if (quotationCommands.Count == 1) { return (QuotationCommand)quotationCommands[0]; } else { QuotationCommand lastQuotationCommand = (QuotationCommand)(quotationCommands[quotationCommands.Count - 1]); QuotationCommand mergingCommand = new QuotationCommand(lastQuotationCommand.Sequence); mergingCommand.Merge(quotationCommands); return mergingCommand; } }
public double GetQuotation(QuotationCommand command) { if (command.Items == null) { throw new HttpBodyException(ExceptionMessage.COMMAND_IS_NULL); } var wholesaler = _brasserieContext.Wholesalers .Include(e => e.WholesalerBeers) .ThenInclude(e => e.Beer) .SingleOrDefault(e => e.Id == command.WholesalerId); if (wholesaler == null) { throw new NotFindObjectException(ExceptionMessage.WHOLESALER_NOT_EXIST); } if (command.Items .GroupBy(e => e.BeerId) .ToList() .Count() < command.Items.Count()) { throw new DuplicateItemException(ExceptionMessage.DUPLICATE_ITEM); } var totalPrice = 0.00; foreach (var item in command.Items) { var wb = wholesaler.WholesalerBeers .SingleOrDefault(wb => wb.BeerId == item.BeerId); if (wb == null) { throw new NotFindObjectException(ExceptionMessage.BEER_NOT_SELL); } if (wb.Stock >= item.Quantity) { totalPrice += wb.Beer.Price * item.Quantity; } else { throw new NotEnoughQuantityException(ExceptionMessage.ENOUGH_STOCK); } } var stockTotal = 0; foreach (var item in command.Items) { stockTotal += item.Quantity; } if (stockTotal > 10 && stockTotal < 21) { var discountPrice10 = totalPrice * 0.10; totalPrice -= discountPrice10; } if (stockTotal > 20) { var discountPrice20 = totalPrice * 0.20; totalPrice -= discountPrice20; } return(Math.Round(totalPrice, 2)); }
private static byte[] GetDataInGeneralFormat(Token token, TraderState state,QuotationCommand command,int? beginSequence=null,int? endSequence=null) { XmlDocument xmlDoc = new XmlDocument(); XmlElement commands = xmlDoc.CreateElement("Commands"); xmlDoc.AppendChild(commands); XmlNode commandNode = command.ToXmlNode(token, state); if (commandNode == null) { return null; } XmlNode commandNode2 = commands.OwnerDocument.ImportNode(commandNode, true); commands.AppendChild(commandNode2); commands.SetAttribute("FirstSequence", (beginSequence ?? command.Sequence).ToString()); commands.SetAttribute("LastSequence", (endSequence ?? command.Sequence).ToString()); return PacketConstants.ContentEncoding.GetBytes(commands.OuterXml); }
private static byte[] GetDataIn4BitFormat(TraderState state,QuotationCommand command,int? beginSequence=null,int? endSequence=null) { var stringBuilder = new StringBuilder(CAPACITY); string seqenceStr = string.Format("{0}{1}{2}", (beginSequence ?? command.Sequence), _SequenceSeparator, (endSequence ?? command.Sequence)); stringBuilder.Append(seqenceStr); stringBuilder.Append(_StartSeparator); OverridedQuotation[] overridedQuotations = command.OverridedQs; if (overridedQuotations != null && overridedQuotations.Length > 0) { for (int i = 0; i < overridedQuotations.Length; i++) { OverridedQuotation overridedQuotation = overridedQuotations[i]; if (!state.InstrumentsView.ContainsKey(overridedQuotation.InstrumentID)) { continue; } if (overridedQuotation.QuotePolicyID != state.InstrumentsView[overridedQuotation.InstrumentID]) { continue; } if (i != 0) { stringBuilder.Append(_OutterSeparator); } AppendQuotationParts(stringBuilder, overridedQuotation); } } stringBuilder.Append(_StartSeparator); return Quotation4BitEncoder.Encode(stringBuilder.ToString()); }