77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
// MyBWRSimulator.Core/Components/Abstract/Port.cs
|
|
namespace MyBWRSimulator.Core.Components.Abstract
|
|
{
|
|
/// <summary>
|
|
/// Abstract base class for all types of connection points on components.
|
|
/// </summary>
|
|
public abstract class Port
|
|
{
|
|
/// <summary>
|
|
/// Unique ID for the port (e.g., "Inlet1", "OutletA").
|
|
/// </summary>
|
|
public string ID { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// A reference to the Port it's connected to. Null if not connected.
|
|
/// </summary>
|
|
public Port ConnectedPort { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// A reference back to the component this port belongs to.
|
|
/// </summary>
|
|
public Component ParentComponent { get; internal set; } // Internal set by AddPort methods
|
|
|
|
/// <summary>
|
|
/// Constructor for the Port base class.
|
|
/// </summary>
|
|
/// <param name="id">Unique identifier for the port.</param>
|
|
protected Port(string id)
|
|
{
|
|
ID = id;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Establishes a connection between this port and another port.
|
|
/// </summary>
|
|
/// <param name="otherPort">The port to connect to.</param>
|
|
public virtual void Connect(Port otherPort)
|
|
{
|
|
if (otherPort == null)
|
|
{
|
|
Console.WriteLine($"Error: Cannot connect {ID} to null port.");
|
|
return;
|
|
}
|
|
if (ConnectedPort != null && ConnectedPort != otherPort)
|
|
{
|
|
Console.WriteLine($"Warning: {ID} is already connected to {ConnectedPort.ID}. Disconnecting first.");
|
|
Disconnect();
|
|
}
|
|
|
|
ConnectedPort = otherPort;
|
|
// For bidirectional connection, the other port should also connect to this one
|
|
if (otherPort.ConnectedPort != this)
|
|
{
|
|
otherPort.Connect(this);
|
|
}
|
|
Console.WriteLine($"Port {ID} connected to {otherPort.ID}.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Breaks the connection of this port.
|
|
/// </summary>
|
|
public virtual void Disconnect()
|
|
{
|
|
if (ConnectedPort != null)
|
|
{
|
|
Port oldConnectedPort = ConnectedPort;
|
|
ConnectedPort = null;
|
|
// Ensure the other port also disconnects from this one
|
|
if (oldConnectedPort.ConnectedPort == this)
|
|
{
|
|
oldConnectedPort.Disconnect();
|
|
}
|
|
Console.WriteLine($"Port {ID} disconnected from {oldConnectedPort.ID}.");
|
|
}
|
|
}
|
|
}
|
|
} |