using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.PMR;
using Tango.PMR.Common;
using Tango.PMR.Integration;
namespace Tango.Transport
{
///
/// Represents a base transport message.
///
internal abstract class TransportMessageBase : ExtendedObject
{
public bool AtLeastOneResponseReceived { get; set; }
public String TransportComponentName { get; set; }
public QueuePriority Priority { get; set; }
public TransportThreadingMode ThreadingMode { get; set; }
///
/// Gets or sets a value indicating whether this instance is multi response.
///
public bool IsContinuous { get; set; }
///
/// Gets or sets the message token.
///
public String Token { get; set; }
///
/// Gets or sets the message direction.
///
public TransportMessageDirection Direction { get; set; }
///
/// Gets or sets method to serial the message to byte array.
///
public Func Serialize { get; set; }
///
/// Gets or sets the activate timeout callback.
///
public Action ActivateTimeout { get; set; }
///
/// Gets or sets the message.
///
public Object Message { get; set; }
///
/// Gets or sets a value indicating whether log the message before sending.
///
public bool ShouldLog { get; set; }
public bool Immidiate { get; set; }
///
/// Notifies the message observer of the new result.
///
/// The result.
public abstract void SetResult(object result, bool completed);
///
/// Notifies the message observer of an exception.
///
/// The ex.
public abstract void SetException(Exception ex);
public abstract String GetActualMessageTypeName();
public abstract Object GetActualMessage();
///
/// Initializes a new instance of the class.
///
/// The token.
/// The message.
/// The direction.
/// To bytes.
public TransportMessageBase(String token, object message, TransportMessageDirection direction, Func toBytes)
{
Token = token;
Message = message;
Direction = direction;
Serialize = toBytes;
}
public static String GetActualMessageTypeName(Object obj)
{
String name = String.Empty;
if (obj is ITangoMessage)
{
var message = obj.GetType().GetProperty("Message").GetValue(obj);
if (message.GetType() == typeof(GenericRequest))
{
try
{
name = (message as GenericRequest).GetTypeName();
}
catch
{
name = message.GetType().Name;
}
}
else if (message.GetType() == typeof(GenericResponse))
{
try
{
name = (message as GenericResponse).GetTypeName();
}
catch
{
name = message.GetType().Name;
}
}
else
{
name = message.GetType().Name;
}
}
else if (obj is MessageContainer)
{
name = (obj as MessageContainer).Type.ToString();
}
else
{
name = obj.GetType().Name;
}
return name;
}
}
}