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;
|
2021-01-30 11:50:11 +02:00
|
|
|
import std.socket;
|
|
|
|
import gogga;
|
|
|
|
import core.thread;
|
|
|
|
import tristanable.encoding : DataMessage;
|
|
|
|
import bmessage : bSendMessage = sendMessage;
|
2020-10-05 15:59:08 +02:00
|
|
|
|
2021-01-29 19:49:39 +02:00
|
|
|
/**
|
|
|
|
* Link manager
|
|
|
|
*
|
|
|
|
* Given a set of original DLink objects, it will open connections to them
|
|
|
|
* It also facilitates DConnection making a call to `.addLink` here when an
|
|
|
|
* inbound peering request comes in
|
|
|
|
*/
|
|
|
|
public final class DLinkManager
|
|
|
|
{
|
2021-01-29 20:48:18 +02:00
|
|
|
|
|
|
|
this(DServer server)
|
2021-01-29 19:49:39 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:48:18 +02:00
|
|
|
/**
|
|
|
|
* Goes through the DConnection[] array in DServer and returns
|
|
|
|
* all connections that are SERVER connections
|
|
|
|
*/
|
|
|
|
public DConnection[] getLinkedServers()
|
|
|
|
{
|
|
|
|
DConnection[] links;
|
|
|
|
|
|
|
|
/* TODO: Implement me */
|
|
|
|
|
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-01-30 11:50:11 +02:00
|
|
|
/**
|
|
|
|
* Outbound utilities
|
|
|
|
*/
|
|
|
|
private Socket outboundSocket;
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/**
|
|
|
|
* 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 20:48:18 +02:00
|
|
|
this.name = name;
|
|
|
|
this.address = address;
|
|
|
|
|
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 */
|
|
|
|
|
2021-01-29 21:12:27 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new outbound connection
|
|
|
|
*/
|
|
|
|
private void initializeOutboundConnection()
|
|
|
|
{
|
|
|
|
/* Open a connection to the server */
|
2021-01-30 11:50:11 +02:00
|
|
|
outboundSocket = new Socket(address.addressFamily, SocketType.STREAM, ProtocolType.TCP);
|
2021-01-29 21:12:27 +02:00
|
|
|
|
|
|
|
gprintln(address);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-01-30 11:50:11 +02:00
|
|
|
outboundSocket.connect(address);
|
2021-01-29 21:12:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch(SocketOSException)
|
|
|
|
{
|
|
|
|
gprintln("Could not link with server "~name~"!", DebugType.ERROR);
|
|
|
|
Thread.sleep(dur!("seconds")(3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:48:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:00:56 +02:00
|
|
|
private void outboundWorker()
|
|
|
|
{
|
2021-01-29 21:12:27 +02:00
|
|
|
/* Initialize a new outbound connection */
|
|
|
|
initializeOutboundConnection();
|
|
|
|
|
2021-01-30 11:39:58 +02:00
|
|
|
/* TODO: Send LINK (1) command */
|
|
|
|
byte[] data;
|
|
|
|
data ~= [1];
|
|
|
|
|
2021-01-30 12:10:48 +02:00
|
|
|
/* TODO: Encode [serverNameLen, serverName] */
|
2021-01-30 12:25:40 +02:00
|
|
|
string serverName = server.getGeneralConfig().getName();
|
2021-01-30 12:10:48 +02:00
|
|
|
data ~= [cast(byte)serverName.length];
|
|
|
|
data ~= serverName;
|
2021-01-30 11:39:58 +02:00
|
|
|
|
2021-01-30 11:50:11 +02:00
|
|
|
/* Encode and send LINK command */
|
|
|
|
DataMessage message = new DataMessage(0, data);
|
|
|
|
bSendMessage(outboundSocket, message.encode());
|
|
|
|
|
2021-01-30 11:39:58 +02:00
|
|
|
|
|
|
|
|
2021-01-29 15:00:56 +02:00
|
|
|
/* TODO: Implement me */
|
2021-01-29 18:29:05 +02:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2021-01-29 15:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 20:48:18 +02:00
|
|
|
override public string toString()
|
|
|
|
{
|
|
|
|
return "Server: "~name~", Address: "~to!(string)(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/**
|
|
|
|
* Constructs a DLink for an inbound peering
|
|
|
|
*/
|
|
|
|
this(DServer server, string name, Address address, DConnection connection)
|
|
|
|
{
|
2021-01-29 18:29:05 +02:00
|
|
|
/* Save the server, name and address */
|
|
|
|
|
|
|
|
/* Save the active connection */
|
2021-01-29 14:28:52 +02:00
|
|
|
/* Save name and address */
|
|
|
|
this(server, name, address);
|
|
|
|
|
|
|
|
/* Save connection */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:39:58 +02:00
|
|
|
/* TODO: Remove this from here and put it in DServer */
|
2020-10-05 15:59:08 +02:00
|
|
|
public final class DMeyer
|
|
|
|
{
|
2021-01-28 23:43:22 +02:00
|
|
|
/* Direct peers */
|
2021-01-29 21:38:50 +02:00
|
|
|
private DLink[] outboundPeers;
|
2020-10-05 15:59:08 +02:00
|
|
|
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-29 21:38:50 +02:00
|
|
|
this(DServer server, DLink[] outboundPeers)
|
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 21:21:59 +02:00
|
|
|
|
|
|
|
|
2021-01-29 21:38:50 +02:00
|
|
|
this.outboundPeers = outboundPeers;
|
2021-01-29 11:08:24 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:49:39 +02:00
|
|
|
|
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 21:21:59 +02:00
|
|
|
public DLink[] getOutboundLinks()
|
2021-01-29 19:49:39 +02:00
|
|
|
{
|
2021-01-29 21:38:50 +02:00
|
|
|
return outboundPeers;
|
2021-01-29 19:49:39 +02:00
|
|
|
}
|
2020-10-05 15:59:08 +02:00
|
|
|
}
|
2021-01-30 11:39:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new inbound connection that is to be used for linking
|
|
|
|
*/
|
|
|
|
void initializeLink(DServer server, DConnection newConnection)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|