2015-08-01 19:58:53 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Drawing ;
using System.Xml ;
2015-08-22 19:57:27 +02:00
using System.Windows.Forms ;
2015-08-01 19:58:53 +02:00
namespace EduNetworkBuilder
{
[Serializable]
public class NetworkLink : NetworkComponent
{
HostNicID SrcNic ;
HostNicID DstNic ;
public LinkType theLinkType = LinkType . normal ;
public bool isVisibleLink = true ; //False for wireless. Skip drawing a line if it is there
public NetworkLink ( HostNicID source , HostNicID dest , LinkType type = LinkType . normal )
{
SrcNic = source ;
DstNic = dest ;
Network myNet = NB . GetNetwork ( ) ;
myNet . MarkAsLinked ( source , GetUniqueIdentifier ) ;
myNet . MarkAsLinked ( dest , GetUniqueIdentifier ) ;
theLinkType = type ;
2015-08-22 19:57:27 +02:00
NetworkDevice sDev = myNet . GetDeviceFromID ( source ) ;
NetworkDevice dDev = myNet . GetDeviceFromID ( dest ) ;
if ( sDev = = null | | dDev = = null ) return ;
NetworkCard sNic = sDev . NicFromID ( source ) ;
NetworkCard dNic = dDev . NicFromID ( dest ) ;
NetworkCard AccessPoint = null ;
NetworkCard Client = null ;
bool IsWireless = false ;
if ( sNic ! = null & & dNic ! = null )
{
if ( sNic . GetNicType = = NicType . wport )
{
AccessPoint = sNic ;
Client = dNic ;
IsWireless = true ;
}
if ( dNic . GetNicType = = NicType . wport )
{
AccessPoint = dNic ;
Client = sNic ;
IsWireless = true ;
}
if ( IsWireless & & AccessPoint ! = null & & Client ! = null )
{
bool donesomething = false ;
if ( AccessPoint . SSID ! = Client . SSID )
{
donesomething = true ;
Client . SSID = AccessPoint . SSID ;
}
if ( AccessPoint . WirelessKey ! = Client . WirelessKey )
{
donesomething = true ;
Client . WirelessKey = AccessPoint . WirelessKey ;
}
if ( donesomething )
{
MessageBox . Show ( "The SSID and Key have been updated on the client." ) ;
}
}
}
2015-08-01 19:58:53 +02:00
}
public NetworkLink ( XmlNode theNode )
{
IsDirty = true ;
Load ( theNode ) ;
}
public HostNicID Src
{
get { return SrcNic ; }
}
public HostNicID Dst
{
get { return DstNic ; }
}
public override void Load ( XmlNode theNode )
{
foreach ( XmlNode Individual in theNode . ChildNodes )
{
XmlNodeType myNodetype = Individual . NodeType ;
if ( myNodetype = = XmlNodeType . Element )
{
switch ( Individual . Name . ToLower ( ) )
{
case "srcnic" :
SrcNic = new HostNicID ( Individual ) ;
break ;
case "dstnic" :
DstNic = new HostNicID ( Individual ) ;
break ;
case "hostname" :
hostname = Individual . InnerText ;
break ;
case "linktype" :
theLinkType = NB . ParseEnum < LinkType > ( Individual . InnerText ) ;
break ;
case "uniqueidentifier" :
int . TryParse ( Individual . InnerText , out UniqueIdentifier ) ;
break ;
}
}
}
Network myNet = NB . GetNetwork ( ) ;
myNet . MarkAsLinked ( SrcNic , GetUniqueIdentifier ) ;
myNet . MarkAsLinked ( DstNic , GetUniqueIdentifier ) ;
}
public override void Save ( XmlWriter writer )
{
writer . WriteStartElement ( "link" ) ;
SrcNic . Save ( writer , "SrcNic" ) ;
DstNic . Save ( writer , "DstNic" ) ;
writer . WriteElementString ( "hostname" , hostname ) ;
writer . WriteElementString ( "linktype" , theLinkType . ToString ( ) ) ;
writer . WriteElementString ( "uniqueidentifier" , UniqueIdentifier . ToString ( ) ) ;
writer . WriteEndElement ( ) ;
}
public override void Destroy ( )
{
Network myNet = NB . GetNetwork ( ) ;
if ( myNet ! = null )
{
myNet . MarkAsUnlinked ( SrcNic , GetUniqueIdentifier ) ;
myNet . MarkAsUnlinked ( DstNic , GetUniqueIdentifier ) ;
}
}
public bool IsSource ( int ID )
{
if ( SrcNic . HostID = = ID )
return true ;
else return false ;
}
public bool IsDest ( int ID )
{
if ( DstNic . HostID = = ID )
return true ;
else return false ;
}
//Have func to verify both ends are powered on
public bool isLive ( )
{
return false ;
}
/// <summary>
/// Check to see if the link is connected to the specified nic at either end
/// </summary>
/// <param name="toFind">The unique identifier of the link</param>
/// <returns>True if it has it</returns>
public bool HasLink ( HostNicID toFind )
{
if ( SrcNic . Equals ( toFind ) | | DstNic . Equals ( toFind ) )
return true ;
return false ;
}
2015-08-22 19:57:27 +02:00
/// <summary>
/// Check that the link works. If not, drop the link. It usually only
/// fails in wireless if the ssid and key do not match
/// </summary>
public bool VerifyLinkIntegrity ( )
{
Network myNet = NB . GetNetwork ( ) ;
if ( myNet = = null ) return false ;
NetworkDevice sDev = myNet . GetDeviceFromID ( SrcNic ) ;
NetworkDevice dDev = myNet . GetDeviceFromID ( DstNic ) ;
if ( sDev = = null | | dDev = = null ) return false ;
NetworkCard sNic = sDev . NicFromID ( SrcNic ) ;
NetworkCard dNic = dDev . NicFromID ( DstNic ) ;
bool deleteme = false ;
if ( sNic ! = null & & dNic ! = null )
{
if ( sNic . GetNicType = = NicType . wport | | dNic . GetNicType = = NicType . wport )
{
if ( sNic . WirelessKey ! = dNic . WirelessKey )
deleteme = true ;
if ( sNic . SSID ! = dNic . SSID )
deleteme = true ;
}
}
if ( sNic . isWireless ( ) ! = dNic . isWireless ( ) )
deleteme = true ;
if ( deleteme )
{
sDev . RemoveLinkTo ( dDev . hostname ) ; //this removes this link
return true ;
}
return false ;
}
2015-08-01 19:58:53 +02:00
public override void Print ( Image BaseImage , bool DrawTitle )
{
//Find the XY of the connected items
Network myNet = NB . GetNetwork ( ) ;
NetworkDevice Src = myNet . HostMatchingHostNicID ( SrcNic ) ;
NetworkDevice Dst = myNet . HostMatchingHostNicID ( DstNic ) ;
//Draw a line between them
if ( Src = = null | | Dst = = null ) return ;
Point sPoint = Src . GetCenter ( ) ;
Point dPoint = Dst . GetCenter ( ) ;
Pen tPen = new Pen ( Color . Black , 4 ) ;
2015-08-10 00:33:00 +02:00
if ( theLinkType = = LinkType . wireless )
{
//tPen = new Pen(Color.DarkSlateGray);
tPen . DashCap = System . Drawing . Drawing2D . DashCap . Round ;
tPen . DashPattern = new float [ ] { 8.0F , 2.0F } ;
}
2015-08-01 19:58:53 +02:00
Graphics . FromImage ( BaseImage ) . DrawLine ( tPen , sPoint , dPoint ) ;
IsDirty = false ; //we have printed, we are no longer dirty.
}
public Point PositionOnLine ( nb_direction direction , int Percentage )
{
Network myNet = NB . GetNetwork ( ) ;
if ( Percentage < 0 ) Percentage = 0 ;
if ( Percentage > 100 ) Percentage = 100 ;
NetworkDevice Src = myNet . HostMatchingHostNicID ( SrcNic ) ;
NetworkDevice Dst = myNet . HostMatchingHostNicID ( DstNic ) ;
//Draw a line between them
if ( Src = = null | | Dst = = null )
return new Point ( - 1 , - 1 ) ;
Point sPoint = Src . GetCenter ( ) ;
Point dPoint = Dst . GetCenter ( ) ;
double deltax = ( sPoint . X - dPoint . X ) / 100.0 ;
double deltay = ( sPoint . Y - dPoint . Y ) / 100.0 ;
//wearehere; //We need to start at an end. (we need to add sPoint.x, spoint.y depending on which dir we are going)
Point answer ;
if ( direction = = nb_direction . to_dst )
answer = new Point ( sPoint . X - ( int ) ( deltax * Percentage ) , sPoint . Y - ( int ) ( deltay * Percentage ) ) ;
else
answer = new Point ( ( int ) ( deltax * Percentage ) + dPoint . X , ( int ) ( deltay * Percentage ) + dPoint . Y ) ;
return answer ;
}
public List < string > UsedNicIDStrings ( )
{
List < string > usedLinks = new List < string > ( ) ;
//If the nic is a wireless end point, we should not count it. We can have multiple Nics linked to one AP
usedLinks . Add ( SrcNic . HostNicIDString ) ;
usedLinks . Add ( DstNic . HostNicIDString ) ;
return usedLinks ;
}
/// <summary>
/// Have the packet traverse the network
/// </summary>
/// <param name="tPacket"></param>
public override void DoMoving ( Packet tPacket )
{
NetworkDevice movingTo = null ;
Network myNet = NB . GetNetwork ( ) ;
HostNicID target ;
tPacket . IncrementDistance ( ) ;
2015-08-15 05:47:56 +02:00
Point newLocation = PositionOnLine ( tPacket . myDirection , tPacket . myLinkPercent ) ;
if ( myNet . DeviceIsOverDamaging ( theLinkType , newLocation ) )
{
tPacket . health - = 10 ;
//Console.WriteLine(" health=" + tPacket.health.ToString());
}
if ( tPacket . health < = 0 )
{
//The link is broken. Drop the packet
tPacket . Tracking . AddMessage ( DebugLevel . info , this , "The packet was corrupted. The network wire may run too close to electricity or fluorescent lighting, or the wireless path is being interfered by a microwave, wireless phone, or other radio device." ) ;
tPacket . Tracking . Status = "The packet got corrupted and was dropped." ;
tPacket . MyStatus = PacketStatus . finished_failed ;
}
2015-08-01 19:58:53 +02:00
if ( theLinkType = = LinkType . broken & & tPacket . myLinkPercent > 50 )
{
//The link is broken. Drop the packet
tPacket . Tracking . AddMessage ( DebugLevel . info , this , "The packet tried to use a broken network wire and was corrupted." ) ;
tPacket . Tracking . Status = "The packet got corrupted and was dropped." ;
tPacket . MyStatus = PacketStatus . finished_failed ;
}
if ( tPacket . Arrived ( ) )
{
//We need to pass it to the recieving device
if ( tPacket . myDirection = = nb_direction . to_dst )
{
movingTo = myNet . GetDeviceFromID ( DstNic . HostID ) ;
target = DstNic ;
}
else
{
movingTo = myNet . GetDeviceFromID ( SrcNic . HostID ) ;
target = SrcNic ;
}
if ( movingTo = = null )
{
tPacket . AddMessage ( DebugLevel . info , "Oops! We do not have anything at the end of the network wire. This should not happen!" ) ;
tPacket . Tracking . Status = hostname + " Nothing at far end of the wire. This should never happen.." ;
tPacket . MyStatus = PacketStatus . finished_failed ;
}
else
{
movingTo . DoInputFromLink ( tPacket , target ) ;
}
}
}
}
}