2020-10-05 15:59:08 +02:00
|
|
|
module dnetd.dlink;
|
|
|
|
|
|
|
|
import dnetd.dconnection;
|
|
|
|
import core.sync.mutex : Mutex;
|
|
|
|
import std.stdio;
|
|
|
|
import std.conv;
|
|
|
|
import dnetd.dserver;
|
2021-01-28 23:43:22 +02:00
|
|
|
import dnetd.dconfig;
|
2021-01-29 14:28:52 +02:00
|
|
|
import std.socket : Address;
|
|
|
|
import core.thread : Thread;
|
2020-10-05 15:59:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DLink
|
|
|
|
*
|
|
|
|
* Couples a DConneciton (direct peer)
|
|
|
|
* with information about what this link
|
|
|
|
* knows and can tell us
|
|
|
|
*/
|
2021-01-29 14:28:52 +02:00
|
|
|
public final class DLidnk
|
2020-10-05 15:59:08 +02:00
|
|
|
{
|
|
|
|
/* The directly attached peer */
|
|
|
|
private DConnection directPeer;
|
|
|
|
|
|
|
|
/* Servers (by name) this server is aware of */
|
|
|
|
private string[] knowledgeList;
|
|
|
|
|
|
|
|
this(DConnection directPeer)
|
|
|
|
{
|
|
|
|
this.directPeer = directPeer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call this to update list */
|
|
|
|
public void updateKB()
|
|
|
|
{
|
|
|
|
/* TODO: Ask DConneciton here for the servers he knows */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/**
|
|
|
|
* Represents a server link
|
|
|
|
*
|
|
|
|
* Either used for inbound or outbound
|
|
|
|
*/
|
|
|
|
public final class DLink : Thread
|
|
|
|
{
|
|
|
|
/* Associated server */
|
|
|
|
private DServer server;
|
|
|
|
|
|
|
|
/* The connection */
|
|
|
|
private DConnection connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Links details
|
|
|
|
*/
|
|
|
|
private string name;
|
|
|
|
private Address address;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a DLink for an outbound peering
|
|
|
|
*/
|
|
|
|
this(DServer server, string name, Address address)
|
|
|
|
{
|
2021-01-29 15:00:56 +02:00
|
|
|
/* Set the worker thread for outbound connections */
|
|
|
|
super(&outboundWorker);
|
2021-01-29 15:04:08 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/* Create an outbound connection */
|
|
|
|
/* TODO: Fuuuuuuuuuuuuuuuuuuuck handling of shit here bababooey and not in dconnection.d as we would have done below */
|
|
|
|
|
|
|
|
/* Initialize a new outbound connection */
|
|
|
|
initializeOutboundConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new outbound connection
|
|
|
|
*/
|
|
|
|
private void initializeOutboundConnection()
|
|
|
|
{
|
|
|
|
/* Open a connection to the server */
|
|
|
|
// connection = new DConnection();
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:00:56 +02:00
|
|
|
private void outboundWorker()
|
|
|
|
{
|
|
|
|
/* TODO: Implement me */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/**
|
|
|
|
* Constructs a DLink for an inbound peering
|
|
|
|
*/
|
|
|
|
this(DServer server, string name, Address address, DConnection connection)
|
|
|
|
{
|
|
|
|
/* Save name and address */
|
|
|
|
this(server, name, address);
|
|
|
|
|
|
|
|
/* Save connection */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:59:08 +02:00
|
|
|
public final class DMeyer
|
|
|
|
{
|
2021-01-28 23:43:22 +02:00
|
|
|
/* Direct peers */
|
2020-10-05 15:59:08 +02:00
|
|
|
private DLink[] links;
|
|
|
|
private Mutex linksMutex;
|
|
|
|
|
2021-01-28 23:43:22 +02:00
|
|
|
/* Associated server */
|
2020-10-05 15:59:08 +02:00
|
|
|
private DServer server;
|
|
|
|
|
2021-01-28 23:43:22 +02:00
|
|
|
this(DServer server, DLinkConfig linkConfig)
|
2020-10-05 15:59:08 +02:00
|
|
|
{
|
|
|
|
this.server = server;
|
2021-01-29 11:08:24 +02:00
|
|
|
|
|
|
|
/* Initialize the locks */
|
|
|
|
initLocks();
|
2021-01-29 14:28:52 +02:00
|
|
|
|
|
|
|
/* Open a connection to the server */
|
|
|
|
|
|
|
|
/* TODO: Open connections to all servers we are yet to open a connection to (check the `links` array) */
|
2021-01-29 11:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize locks */
|
|
|
|
private void initLocks()
|
|
|
|
{
|
2020-10-05 15:59:08 +02:00
|
|
|
linksMutex = new Mutex();
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// /* Attach a direct peer */
|
|
|
|
// public void attachDirectPeer(DConnection peer)
|
|
|
|
// {
|
|
|
|
// /* TODO: Add to `directPeers` */
|
|
|
|
// linksMutex.lock();
|
2020-10-05 15:59:08 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// links ~= new DLink(peer);
|
|
|
|
// writeln("Attached direct peer: "~to!(string)(peer));
|
2020-10-05 15:59:08 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// linksMutex.unlock();
|
|
|
|
// }
|
2020-10-05 15:59:08 +02:00
|
|
|
|
|
|
|
/* Get a list of all servers we know of */
|
|
|
|
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// public DLink getLink(DConnection peer)
|
|
|
|
// {
|
|
|
|
// DLink link;
|
2020-10-05 15:59:08 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// linksMutex.lock();
|
2020-10-05 15:59:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// linksMutex.unlock();
|
2020-10-05 15:59:08 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
// return link;
|
|
|
|
// }
|
2020-10-05 15:59:08 +02:00
|
|
|
|
|
|
|
}
|