This commit is contained in:
Tristan B. Kildaire 2020-05-01 09:13:42 +02:00
parent 7ab34cb188
commit e1b0d1fe85
3 changed files with 32 additions and 35 deletions

View File

@ -28,6 +28,9 @@ public final class BesterConnection : Thread
private string username;
private string password;
/* If the connection is active */
private bool isActive = true;
/* The connection scope */
public enum Scope
{
@ -73,7 +76,7 @@ public final class BesterConnection : Thread
private void run()
{
debugPrint("<<< Begin read/send loop >>>");
while(clientConnection.isAlive()) /*TODO: Remove and also make the stting of this kak not be closing socket */
while(isActive) /*TODO: Remove and also make the stting of this kak not be closing socket */
{
/* Received JSON message */
JSONValue receivedMessage;
@ -93,8 +96,7 @@ public final class BesterConnection : Thread
if(connectionType == Scope.SERVER)
{
debugPrint("Server connection done, closing BesterConnection.");
clientConnection.close();
break;
isActive = false;
}
}
catch(BesterException exception)
@ -105,6 +107,7 @@ public final class BesterConnection : Thread
}
debugPrint("<<< End read/send loop >>>");
clientConnection.close();
/* TODO: Remove myself from the connections array */
}
@ -133,8 +136,6 @@ public final class BesterConnection : Thread
debugPrint("Destructor finished");
}
/* TODO: Comment [], rename [] */
private bool dispatchMessage(Scope scopeField, JSONValue payloadBlock)
{
@ -171,7 +172,7 @@ public final class BesterConnection : Thread
if(cmp(commandType, "close") == 0)
{
debugPrint("Closing socket...");
this.clientConnection.close();
isActive = false;
}
else
{
@ -187,15 +188,13 @@ public final class BesterConnection : Thread
try
{
/* TODO: Collect return value */
/* Provide the handler the message and let it process it and send us a reply */
HandlerResponse handlerResponse = chosenHandler.handleMessage(payloadData);
/* TODO: Continue here, we will make all error handling do on construction as to make this all more compact */
debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ handlerResponse.toString());
/* Execute the message handler's command */
/* Execute the message handler's command (as per its reply) */
handlerResponse.execute(this);
}
catch(ResponseError e)
@ -225,7 +224,6 @@ public final class BesterConnection : Thread
return dispatchStatus;
}
/**
* Given the headerBlock, this returns the requested scope
* of the connection.
@ -287,8 +285,8 @@ public final class BesterConnection : Thread
/* TODO: Send message back about an invalid scope */
/* Close the connection */
clientConnection.close();
/* TODO: End this here */
isActive = false;
}
else if(scopeField == Scope.CLIENT)
{
@ -298,6 +296,7 @@ public final class BesterConnection : Thread
* send the client a message back and then close the
* connection.
*/
debugPrint("Client scope enabled");
/* Get the authentication block */
JSONValue authenticationBlock = headerBlock["authentication"];
@ -329,9 +328,7 @@ public final class BesterConnection : Thread
/* TODO : Send error message to client */
/* Close the connection */
clientConnection.close();
/* TODO: Throw exception here */
isActive = false;
}
}
else if(scopeField == Scope.SERVER)
@ -355,7 +352,8 @@ public final class BesterConnection : Thread
/* Dispatch the message */
bool dispatchStatus = dispatchMessage(connectionType, payloadBlock);
/* TODO: Catch error here and not inside dispatchMessage, gets rid of the need for this if statement */
if(dispatchStatus)
{
debugPrint("Dispatch succeeded");
@ -366,7 +364,7 @@ public final class BesterConnection : Thread
debugPrint("Dispatching failed...");
}
}
/* If thr attempt to convert the message to JSON fails */
/* If the attempt to convert the message to JSON fails */
catch(JSONException exception)
{
debugPrint("<<< There was an error whilst parsing the JSON message >>>\n\n"~exception.toString());

View File

@ -6,7 +6,11 @@ import utils.debugging : debugPrint;
import std.stdio : writeln;
import base.net : NetworkException;
/* TODO: Use exception handling here */
/**
* Generalized socket receive function which will read into the
* variable pointed to by `receiveMessage` by reading from the
* socket `originator`.
*/
public void receiveMessage(Socket originator, ref JSONValue receiveMessage)
{
if(!receiveMessage_internal(originator, receiveMessage))
@ -15,6 +19,15 @@ public void receiveMessage(Socket originator, ref JSONValue receiveMessage)
}
}
/**
* Generalized socket send function which will send the JSON
* encoded message, `jsonMessage`, over to the client at the
* other end of the socket, `recipient`.
*
* It gets the length of `jsonMessage` and encodes a 4 byte
* message header in little-endian containing the message's
* length.
*/
public void sendMessage(Socket recipient, JSONValue jsonMessage)
{
if(!sendMessage_internal(recipient, jsonMessage))
@ -23,11 +36,6 @@ public void sendMessage(Socket recipient, JSONValue jsonMessage)
}
}
/**
* Generalized socket receive function which will read into the
* variable pointed to by `receiveMessage` by reading from the
* socket `originator`.
*/
private bool receiveMessage_internal(Socket originator, ref JSONValue receiveMessage)
{
/* TODO: Implement me */
@ -138,15 +146,6 @@ private bool receiveMessage_internal(Socket originator, ref JSONValue receiveMes
return true;
}
/**
* Generalized socket send function which will send the JSON
* encoded message, `jsonMessage`, over to the client at the
* other end of the socket, `recipient`.
*
* It gets the length of `jsonMessage` and encodes a 4 byte
* message header in little-endian containing the message's
* length.
*/
private bool sendMessage_internal(Socket recipient, JSONValue jsonMessage)
{
/* The message buffer */

View File

@ -8,14 +8,14 @@ def sendAs(username):
d.connect(("127.0.0.1",2223))
# First do it and authenticate
bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type2"}})
bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}})
print(len(bys), bys)
d.send(len(bys).to_bytes(4, "little"))
d.send(bys.encode())
# Loop prompt and sending of message to tbk
while True:
bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type2"}})
bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}})
print(len(bys), bys)
d.send(len(bys).to_bytes(4, "little"))
d.send(bys.encode())