JD2022-TU1/main/tools/extern/Bloomberg/Code/Libraries/Bloomberg.Message/Writer/DataInfoWriter.cs

151 lines
No EOL
4.9 KiB
C#

using System;
using System.IO;
using Bloomberg.Message.Components;
using Bloomberg.Message.Entities;
namespace Bloomberg.Message.Writer
{
public class DataInfoWriter : DataWriterBase
{
private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(DataInfoWriter));
private readonly DataInfo _dataInfo;
private readonly string _messagePath;
public DataInfoWriter(DataInfo dataInfo, string messagePath)
{
_dataInfo = dataInfo;
_messagePath = messagePath;
}
public override void Write(DataSender currentSender)
{
WriteDataInfoStartMarker(currentSender);
if (_dataInfo != null && _dataInfo.DataElements != null)
{
foreach (var data in _dataInfo.DataElements)
{
if (data.Include)
Write(data, currentSender);
}
}
WriteDataInfoEndMarker(currentSender);
}
public override void StartAsync(DataSender currentSender)
{
WriteDataInfoStartMarker(currentSender);
if (_dataInfo != null && _dataInfo.DataElements != null)
{
foreach (var data in _dataInfo.DataElements)
{
if (data.Include && !data.IsAsync)
Write(data, currentSender);
}
}
}
public override void FinishAsync(DataSender currentSender)
{
if (_dataInfo != null && _dataInfo.DataElements != null)
{
foreach (var data in _dataInfo.DataElements)
{
if (data.Include && data.IsAsync)
Write(data, currentSender);
}
}
WriteDataInfoEndMarker(currentSender);
}
private void Write(DataElement elem, DataSender currentSender)
{
WriteDataStartMarker(currentSender);
SendString(elem.Name, currentSender);
SendString(elem.Type, currentSender);
SendString(elem.Filename, currentSender);
SendString(elem.Error, currentSender);
currentSender.SendOnStream(BitConverter.GetBytes(elem.Enable ? 1 : 0));
currentSender.SendOnStream(BitConverter.GetBytes(elem.Include ? 1 : 0));
if (elem.Enable)
WriteDataAttributes(elem, currentSender);
WriteDataEndMarker(currentSender);
}
private void WriteDataAttributes(DataElement elem, DataSender currentSender)
{
//check binary file path
string binaryFilePath = null;
bool binaryFileExists = false;
bool hasFile = false;
if (elem.HasBinary && !string.IsNullOrEmpty(elem.Filename))
{
hasFile = true;
binaryFilePath = Path.Combine(_messagePath, elem.Filename);
binaryFileExists = File.Exists(binaryFilePath);
}
//Attribute Pairs
var aw = new AttributeWriter(currentSender);
aw.Start();
if (elem.DataAttributes != null)
{
foreach (DataAttribute attr in elem.DataAttributes)
{
aw.WriteKeyValue(attr.Name, attr.Value);
}
}
//add an error attribute if the file doesn't exists
if(elem.HasBinary && hasFile && !binaryFileExists)
{
aw.WriteKeyValue("Error", $"Writer could not find file '{binaryFilePath}'.");
}
aw.End();
//Bin content
var vlmw = new VariableLengthWriter(currentSender);
vlmw.Start();
if (elem.HasBinary && !String.IsNullOrEmpty(elem.Filename))
{
binaryFilePath = Path.Combine(_messagePath, elem.Filename);
if (File.Exists(binaryFilePath))
{
using (var fileStream = new FileStream(binaryFilePath, FileMode.Open, FileAccess.Read))
{
vlmw.Write(fileStream);
}
}
else
{
// TODO: Raise/log error when file doesnt exists
_log.Error("Could not find file " + binaryFilePath);
}
}
vlmw.End();
}
private static void SendString(string str, DataSender currentSender)
{
if (!String.IsNullOrEmpty(str))
{
currentSender.SendOnStream(BitConverter.GetBytes((uint)str.Length));
currentSender.SendOnStream(Utils.MessageConstants.MessageEncoding.GetBytes(str));
}
else
{
currentSender.SendOnStream(BitConverter.GetBytes((uint)0));
}
}
}
}