1
0
Fork 0
mirror of https://github.com/besterprotocol/besterd synced 2023-12-13 21:00:32 +01:00

Merge branch 'seperate_unix_connections_handlers'

This commit is contained in:
Tristan B. Kildaire 2020-04-26 13:19:27 +02:00
commit 0ac8e56837
4 changed files with 93 additions and 12 deletions

View file

@ -20,12 +20,18 @@ public final class BesterConnection : Thread
private Socket clientConnection;
/* The server backend */
private BesterServer server;
public BesterServer server;
/* The client's credentials */
private string authUsername;
private string authPassword;
/* Returns true if the client is a user (false if a server/unauthenticated user) */
public bool isUser()
{
return authUsername != null && authPassword != null;
}
this(Socket clientConnection, BesterServer server)
{
/* Save socket and set thread worker function pointer */
@ -36,6 +42,23 @@ public final class BesterConnection : Thread
debugPrint("New client handler spawned for " ~ clientConnection.remoteAddress().toAddrString());
}
override public string toString()
{
writeln("oof", clientConnection.remoteAddress().toAddrString());
return clientConnection.remoteAddress().toAddrString();
}
public string[] getCredentials()
{
return [authUsername, authPassword];
}
/* Send a message to the user/server */
public void sendMessage(JSONValue replyMessage)
{
/* TODO: Implement me */
}
/* Read/send loop */
private void run()
{
@ -182,8 +205,10 @@ public final class BesterConnection : Thread
/* TODO: Send and receive data here */
/* Handler's UNIX domain socket */
Socket handlerSocket = chosenHandler.getSocket();
/* TODO: Change this call here below (also remove startup connection) */
Socket handlerSocket = chosenHandler.getNewSocket();
//writeln(handlerSocket == null);
debugPrint("chosenHandler.socketPath: " ~ chosenHandler.socketPath);
/* Get the payload as a string */
string payloadString = toJSON(payload);
@ -343,15 +368,12 @@ public final class BesterConnection : Thread
private JSONValue handlerRun(MessageHandler chosenHandler, JSONValue payload)
{
/* TODO: Send and receive data here */
/* Handler's UNIX domain socket */
Socket handlerSocket = chosenHandler.getSocket();
Socket handlerSocket = chosenHandler.getNewSocket();
/* Get the payload as a string */
string payloadString = toJSON(payload);
/* Construct the data to send */
byte[] sendBuffer;

View file

@ -16,6 +16,9 @@ public final class MessageHandler
/* The pluginName/type */
private string pluginName;
/* The UNIX domain socket path */
public string socketPath;
public Socket getSocket()
{
return domainSocket;
@ -27,7 +30,10 @@ public final class MessageHandler
this.pluginName = pluginName;
/* Initialize the socket */
initializeUNIXSocket(socketPath);
//initializeUNIXSocket(socketPath);
/* Set the socket path */
this.socketPath = socketPath;
}
public string getPluginName()
@ -35,6 +41,17 @@ public final class MessageHandler
return pluginName;
}
public Socket getNewSocket()
{
/* Create the UNIX domain socket */
Socket domainSocket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
/* Connect to the socket at the socket path */
domainSocket.connect(new UnixAddress(socketPath));
return domainSocket;
}
private void initializeUNIXSocket(string socketPath)
{
/* Create the UNIX domain socket */

View file

@ -83,6 +83,13 @@ public final class HandlerResponse
/* TODO: Error check and do accesses JSON that would be done in `.execute` */
}
else if(cmp(serverCommand, "sendHandler") == 0)
{
/* Set the command type to SEND_HAANDLER */
commandType = CommandType.SEND_HANDLER;
/* TODO: Error check and do accesses JSON that would be done in `.execute` */
}
else
{
/* TODO: Error handling */
@ -124,9 +131,16 @@ public final class HandlerResponse
{
clients ~= clientList[i].str();
}
/* TODO: Implement me */
writeln("Users wanting to send to ", clients);
/* Find the users that are wanting to be sent to */
BesterConnection[] connectionList = originalRequester.server.getClients(clients);
writeln(connectionList);
/* TODO: Implement me */
writeln("sdafdfasd", originalRequester.server.clients[0].toString());
}
else if (commandType == CommandType.SEND_SERVERS)
{
@ -143,6 +157,10 @@ public final class HandlerResponse
}
else if (commandType == CommandType.SEND_HANDLER)
{
/* Name of the handler to send the message to */
string handler = messageResponse["header"]["command"]["data"]["handler"].str();
debugPrint("Handler to forward to: " ~ handler);
/* TODO: Add me */
}
}

View file

@ -31,6 +31,30 @@ public final class BesterServer
/* Connected clients */
public BesterConnection[] clients;
public BesterConnection[] getClients(string[] usernames)
{
/* List of authenticated users matching `usernames` */
BesterConnection[] matchedUsers;
/* Search through the provided usernames */
for(ulong i = 0; i < usernames.length; i++)
{
for(ulong k = 0; k < clients.length; k++)
{
/* The potentially-matched user */
BesterConnection potentialMatch = clients[k];
/* Check if the user is authenticated */
if(potentialMatch.isUser() && cmp(potentialMatch.getCredentials()[0], usernames[i]))
{
matchedUsers ~= potentialMatch;
}
}
}
return matchedUsers;
}
public void addListener(BesterListener listener)
{
this.listeners ~= listener;