// MyBWRSimulator.Core/Components/Abstract/Port.cs
namespace MyBWRSimulator.Core.Components.Abstract
{
///
/// Abstract base class for all types of connection points on components.
///
public abstract class Port
{
///
/// Unique ID for the port (e.g., "Inlet1", "OutletA").
///
public string ID { get; protected set; }
///
/// A reference to the Port it's connected to. Null if not connected.
///
public Port ConnectedPort { get; protected set; }
///
/// A reference back to the component this port belongs to.
///
public Component ParentComponent { get; internal set; } // Internal set by AddPort methods
///
/// Constructor for the Port base class.
///
/// Unique identifier for the port.
protected Port(string id)
{
ID = id;
}
///
/// Establishes a connection between this port and another port.
///
/// The port to connect to.
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}.");
}
///
/// Breaks the connection of this port.
///
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}.");
}
}
}
}