|
/// -----------------------------------------------------------------------------------------------------------
/// Module : ABAS400AdapterConnection.cs
/// Description : Defines the connection to the target system.
/// -----------------------------------------------------------------------------------------------------------
#region Using Directives
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.ServiceModel.Channels.Common;
using System.Net.Sockets;
using System.Net;
#endregion
namespace LOBAdapters.TCP
{
public class AdapterConnection : IConnection
{
#region Private Fields
private const int ResponseSubCodeLength = 50;
private const int ResponseDescriptionLength = 500;
private AdapterConnectionFactory connectionFactory;
private string connectionId;
TcpClient client = new TcpClient();
NetworkStream clientStream = null;
IPEndPoint serverEndPoint = null;
#endregion Private Fields
/// <summary>
/// Initializes a new instance of the ABAS400AdapterConnection class with the ABAS400AdapterConnectionFactory
/// </summary>
public AdapterConnection(AdapterConnectionFactory connectionFactory)
{
this.connectionFactory = connectionFactory;
this.connectionId = Guid.NewGuid().ToString();
}
#region Public Properties
/// <summary>
/// Gets the ConnectionFactory
/// </summary>
public AdapterConnectionFactory ConnectionFactory
{
get
{
return this.connectionFactory;
}
}
#endregion Public Properties
#region IConnection Members
/// <summary>
/// Closes the connection to the target system
/// </summary>
public void Close(TimeSpan timeout)
{
if (clientStream != null)
{
clientStream.Flush();
clientStream.Close();
}
if (client.Connected)
client.Close();
}
/// <summary>
/// Returns a value indicating whether the connection is still valid
/// </summary>
public bool IsValid(TimeSpan timeout)
{
if (client.Connected && clientStream != null)
return true;
return false;
}
/// <summary>
/// Opens the connection to the target system.
/// </summary>
public void Open(TimeSpan timeout)
{
serverEndPoint =
new IPEndPoint(IPAddress.Parse(this.connectionFactory.Connection.Uri.Host),
this.connectionFactory.Connection.Uri.Port);
}
/// <summary>
/// Clears the context of the Connection. This method is called when the connection is set back to the connection pool
/// </summary>
public void ClearContext()
{
AdapterUtilities.Trace.Trace(System.Diagnostics.TraceEventType.Verbose,
"Context Clear", "Context Was Cleared");
}
/// <summary>
/// Builds a new instance of the specified IConnectionHandler type
/// </summary>
public TConnectionHandler BuildHandler<TConnectionHandler>(MetadataLookup metadataLookup)
where TConnectionHandler : class, IConnectionHandler
{
if (typeof(IOutboundHandler).IsAssignableFrom(typeof(TConnectionHandler)))
{
return new AdapterOutboundHandler(this, metadataLookup) as TConnectionHandler;
}
if (typeof(IMetadataResolverHandler).IsAssignableFrom(typeof(TConnectionHandler)))
{
return new AdapterMetadataResolverHandler(this, metadataLookup) as TConnectionHandler;
}
if (typeof(IMetadataBrowseHandler).IsAssignableFrom(typeof(TConnectionHandler)))
{
return new AdapterMetadataBrowseHandler(this, metadataLookup) as TConnectionHandler;
}
if (typeof(IMetadataSearchHandler).IsAssignableFrom(typeof(TConnectionHandler)))
{
return new AdapterMetadataSearchHandler(this, metadataLookup) as TConnectionHandler;
}
return default(TConnectionHandler);
}
/// <summary>
/// Aborts the connection to the target system
/// </summary>
public void Abort()
{
if (clientStream != null)
{
clientStream.Flush();
clientStream.Close();
}
if (client.Connected)
client.Close();
}
/// <summary>
/// Gets the Id of the Connection
/// </summary>
public String ConnectionId
{
get
{
return connectionId;
}
}
#endregion IConnection Members
#region Custom Functions
public string Send(string request)
{
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(request);
string responseString = string.Empty;
byte[] message = new byte[7168];
try
{
client.Connect(serverEndPoint);
clientStream = client.GetStream();
clientStream.Write(requestBytes, 0, requestBytes.Length);
clientStream.Read(message, 0, message.Length);
responseString = System.Text.Encoding.ASCII.GetString(message);
}
catch (Exception ex)
{
Abort();
responseString = "<Error>" + ex.ToString() + "</Error>";
}
return responseString;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Self-motivated, creative and results-driven technology executive who is well versed and experienced in leveraging an end-to-end, holistic vision of business objectives to drive full technology / business alignment.
Skilled in grasping business needs and sudden market demand’s shifts by diving into latest business / technology trends, selecting the best fit business model / technology to create a positive reflection on revenue. His multifaceted approach has enabled him to deliver key solutions across a wide range of disciplines including design, development, UX / UI, Business Intelligence.
Technical Specialties are in .Net, Java, Spring Boot, Maven, MS SQL, Oracle, Postgesql, Redis, Javascript, Bootstrap, Angular 2.