2020-10-05 13:59:08 +00:00
|
|
|
module dnetd.dlink;
|
|
|
|
|
|
|
|
import dnetd.dconnection;
|
|
|
|
import core.sync.mutex : Mutex;
|
|
|
|
import std.stdio;
|
|
|
|
import std.conv;
|
|
|
|
import dnetd.dserver;
|
2021-01-28 21:43:22 +00:00
|
|
|
import dnetd.dconfig;
|
2021-01-29 12:28:52 +00:00
|
|
|
import std.socket : Address;
|
|
|
|
import core.thread : Thread;
|
2021-01-30 09:50:11 +00:00
|
|
|
import std.socket;
|
|
|
|
import gogga;
|
|
|
|
import core.thread;
|
|
|
|
import tristanable.encoding : DataMessage;
|
2021-01-30 10:49:39 +00:00
|
|
|
import bmessage : bSendMessage = sendMessage, bReceiveMessage = receiveMessage;
|
2020-10-05 13:59:08 +00:00
|
|
|
|
2021-01-29 17:49:39 +00: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 18:48:18 +00:00
|
|
|
|
|
|
|
this(DServer server)
|
2021-01-29 17:49:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:48:18 +00: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 17:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 12:28:52 +00: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 09:50:11 +00:00
|
|
|
/**
|
|
|
|
* Outbound utilities
|
|
|
|
*/
|
|
|
|
private Socket outboundSocket;
|
|
|
|
|
2021-01-29 12:28:52 +00:00
|
|
|
/**
|
|
|
|
* Constructs a DLink for an outbound peering
|
|
|
|
*/
|
|
|
|
this(DServer server, string name, Address address)
|
|
|
|
{
|
2021-01-29 13:00:56 +00:00
|
|
|
/* Set the worker thread for outbound connections */
|
|
|
|
super(&outboundWorker);
|
2021-01-29 13:04:08 +00:00
|
|
|
|
2021-01-30 10:50:44 +00:00
|
|
|
this.server = server;
|
2021-01-29 18:48:18 +00:00
|
|
|
this.name = name;
|
|
|
|
this.address = address;
|
|
|
|
|
2021-01-29 12:28:52 +00: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 19:12:27 +00:00
|
|
|
|
2021-01-29 12:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new outbound connection
|
|
|
|
*/
|
|
|
|
private void initializeOutboundConnection()
|
|
|
|
{
|
|
|
|
/* Open a connection to the server */
|
2021-01-30 09:50:11 +00:00
|
|
|
outboundSocket = new Socket(address.addressFamily, SocketType.STREAM, ProtocolType.TCP);
|
2021-01-29 19:12:27 +00:00
|
|
|
|
|
|
|
gprintln(address);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-01-30 09:50:11 +00:00
|
|
|
outboundSocket.connect(address);
|
2021-01-29 19:12:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch(SocketOSException)
|
|
|
|
{
|
|
|
|
gprintln("Could not link with server "~name~"!", DebugType.ERROR);
|
|
|
|
Thread.sleep(dur!("seconds")(3));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:48:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 12:28:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:00:56 +00:00
|
|
|
private void outboundWorker()
|
|
|
|
{
|
2021-01-29 19:12:27 +00:00
|
|
|
/* Initialize a new outbound connection */
|
|
|
|
initializeOutboundConnection();
|
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
bool status = linkHandshake();
|
|
|
|
|
|
|
|
/* TODO: Implement me */
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the server LINK command and makes sure
|
|
|
|
* the server accepts the linking and also that the
|
|
|
|
* links name on our local server match up
|
|
|
|
*/
|
|
|
|
private bool linkHandshake()
|
|
|
|
{
|
|
|
|
bool status = true;
|
|
|
|
|
2021-01-30 09:39:58 +00:00
|
|
|
/* TODO: Send LINK (1) command */
|
|
|
|
byte[] data;
|
|
|
|
data ~= [1];
|
|
|
|
|
2021-01-30 10:50:44 +00:00
|
|
|
gprintln("Here", DebugType.WARNING);
|
|
|
|
|
2021-01-30 10:10:48 +00:00
|
|
|
/* TODO: Encode [serverNameLen, serverName] */
|
2021-01-30 10:25:40 +00:00
|
|
|
string serverName = server.getGeneralConfig().getName();
|
2021-01-30 10:10:48 +00:00
|
|
|
data ~= [cast(byte)serverName.length];
|
|
|
|
data ~= serverName;
|
2021-01-30 09:39:58 +00:00
|
|
|
|
2021-01-30 09:50:11 +00:00
|
|
|
/* Encode and send LINK command */
|
|
|
|
DataMessage message = new DataMessage(0, data);
|
|
|
|
bSendMessage(outboundSocket, message.encode());
|
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
/* TODO: Await an acknowledgement [status] */
|
|
|
|
byte[] receivedResponseBytes;
|
2021-01-30 10:56:41 +00:00
|
|
|
bool recvStatus = bReceiveMessage(outboundSocket, receivedResponseBytes);
|
|
|
|
gprintln("Recev status: "~to!(string)(recvStatus), DebugType.WARNING);
|
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
DataMessage receivedResponse = DataMessage.decode(receivedResponseBytes);
|
2021-01-30 09:39:58 +00:00
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
ubyte[] dataReply = cast(ubyte[])receivedResponse.getData();
|
2021-01-30 09:39:58 +00:00
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
/* TODO: 0 is good, 1 is bad */
|
|
|
|
if(dataReply[0] == 0)
|
2021-01-29 16:29:05 +00:00
|
|
|
{
|
2021-01-30 10:49:39 +00:00
|
|
|
/* TODO: Get server name, makes sure it matches on in config file */
|
2021-01-29 16:29:05 +00:00
|
|
|
|
2021-01-30 10:50:44 +00:00
|
|
|
|
2021-01-30 10:49:39 +00:00
|
|
|
}
|
|
|
|
else if(dataReply[0] == 1)
|
|
|
|
{
|
|
|
|
/* TODO: Handle this case */
|
|
|
|
gprintln("Server linking rejected", DebugType.ERROR);
|
|
|
|
status = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO: Handle this case */
|
|
|
|
status = false;
|
2021-01-29 16:29:05 +00:00
|
|
|
}
|
2021-01-30 10:49:39 +00:00
|
|
|
/* TODO: If 0, then expect following [nameLen, name] */
|
|
|
|
|
|
|
|
|
|
|
|
return status;
|
2021-01-29 13:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 18:48:18 +00:00
|
|
|
override public string toString()
|
|
|
|
{
|
|
|
|
return "Server: "~name~", Address: "~to!(string)(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 12:28:52 +00:00
|
|
|
/**
|
|
|
|
* Constructs a DLink for an inbound peering
|
|
|
|
*/
|
|
|
|
this(DServer server, string name, Address address, DConnection connection)
|
|
|
|
{
|
2021-01-29 16:29:05 +00:00
|
|
|
/* Save the server, name and address */
|
|
|
|
|
|
|
|
/* Save the active connection */
|
2021-01-29 12:28:52 +00:00
|
|
|
/* Save name and address */
|
|
|
|
this(server, name, address);
|
|
|
|
|
|
|
|
/* Save connection */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 09:39:58 +00:00
|
|
|
/* TODO: Remove this from here and put it in DServer */
|
2020-10-05 13:59:08 +00:00
|
|
|
public final class DMeyer
|
|
|
|
{
|
2021-01-28 21:43:22 +00:00
|
|
|
/* Direct peers */
|
2021-01-29 19:38:50 +00:00
|
|
|
private DLink[] outboundPeers;
|
2020-10-05 13:59:08 +00:00
|
|
|
private Mutex linksMutex;
|
|
|
|
|
2021-01-28 21:43:22 +00:00
|
|
|
/* Associated server */
|
2020-10-05 13:59:08 +00:00
|
|
|
private DServer server;
|
|
|
|
|
2021-01-29 19:38:50 +00:00
|
|
|
this(DServer server, DLink[] outboundPeers)
|
2020-10-05 13:59:08 +00:00
|
|
|
{
|
|
|
|
this.server = server;
|
2021-01-29 09:08:24 +00:00
|
|
|
|
|
|
|
/* Initialize the locks */
|
|
|
|
initLocks();
|
2021-01-29 12:28:52 +00: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 19:21:59 +00:00
|
|
|
|
|
|
|
|
2021-01-29 19:38:50 +00:00
|
|
|
this.outboundPeers = outboundPeers;
|
2021-01-29 09:08:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:49:39 +00:00
|
|
|
|
2021-01-29 09:08:24 +00:00
|
|
|
/* Initialize locks */
|
|
|
|
private void initLocks()
|
|
|
|
{
|
2020-10-05 13:59:08 +00:00
|
|
|
linksMutex = new Mutex();
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:21:59 +00:00
|
|
|
public DLink[] getOutboundLinks()
|
2021-01-29 17:49:39 +00:00
|
|
|
{
|
2021-01-29 19:38:50 +00:00
|
|
|
return outboundPeers;
|
2021-01-29 17:49:39 +00:00
|
|
|
}
|
2020-10-05 13:59:08 +00:00
|
|
|
}
|
2021-01-30 09:39:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new inbound connection that is to be used for linking
|
|
|
|
*/
|
|
|
|
void initializeLink(DServer server, DConnection newConnection)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|