1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 09:43:37 +02:00

Refactored code to increase re-use

This commit is contained in:
Tristan B. Kildaire 2020-09-26 12:24:04 +02:00
parent fb5cd4f045
commit 71ec17140f

View File

@ -26,7 +26,20 @@ public class DConnection : Thread
/* The connection type */ /* The connection type */
public enum ConnectionType public enum ConnectionType
{ {
CLIENT, SERVER CLIENT, SERVER, UNSPEC
}
/* Command types */
public enum Command
{
JOIN,
PART,
AUTH,
LINK,
REGISTER,
LIST,
MSG,
UNKNOWN
} }
/** /**
@ -59,6 +72,9 @@ public class DConnection : Thread
/* Set the socket */ /* Set the socket */
this.socket = socket; this.socket = socket;
/* Set the default state */
connType = ConnectionType.UNSPEC;
/* Initialize locks */ /* Initialize locks */
initLocks(); initLocks();
@ -146,23 +162,6 @@ public class DConnection : Thread
return status; return status;
} }
private void commandLog(byte commandByte)
{
}
public enum Command
{
JOIN,
PART,
AUTH,
LINK,
REGISTER,
LIST,
MSG,
UNKNOWN
}
private Command getCommand(byte commandByte) private Command getCommand(byte commandByte)
{ {
Command command = Command.UNKNOWN; Command command = Command.UNKNOWN;
@ -214,12 +213,11 @@ public class DConnection : Thread
*/ */
long tag = message.tag; long tag = message.tag;
/* Get the command byte */ /* The reply */
byte[] reply;
/* Get the command */
byte commandByte = message.data[0]; byte commandByte = message.data[0];
/* Print command info */
commandLog(commandByte);
Command command = getCommand(commandByte); Command command = getCommand(commandByte);
writeln(to!(string)(this)~" ~> "~to!(string)(command)); writeln(to!(string)(this)~" ~> "~to!(string)(command));
@ -246,10 +244,7 @@ public class DConnection : Thread
hasAuthed = true; hasAuthed = true;
/* Encode the reply */ /* Encode the reply */
byte[] reply = [status]; reply = [status];
/* TODO: Implement me, use return value */
writeSocket(tag, reply);
} }
/* If `link` command (requires: unauthed) */ /* If `link` command (requires: unauthed) */
else if(command == Command.LINK && !hasAuthed) else if(command == Command.LINK && !hasAuthed)
@ -259,6 +254,7 @@ public class DConnection : Thread
/* Set the type of this connection to `server` */ /* Set the type of this connection to `server` */
connType = ConnectionType.SERVER; connType = ConnectionType.SERVER;
hasAuthed = true;
} }
/* If `register` command (requires: unauthed, client) */ /* If `register` command (requires: unauthed, client) */
else if(command == Command.REGISTER && !hasAuthed && connType == ConnectionType.CLIENT) else if(command == Command.REGISTER && !hasAuthed && connType == ConnectionType.CLIENT)
@ -298,10 +294,7 @@ public class DConnection : Thread
/* TODO: Do reply */ /* TODO: Do reply */
/* Encode the reply */ /* Encode the reply */
byte[] reply = [isPresentInfo]; reply = [isPresentInfo];
/* TODO: Implement me, use return value */
writeSocket(tag, reply);
} }
/* If `part` command (requires: authed, client) */ /* If `part` command (requires: authed, client) */
else if(command == Command.PART && hasAuthed && connType == ConnectionType.CLIENT) else if(command == Command.PART && hasAuthed && connType == ConnectionType.CLIENT)
@ -328,10 +321,7 @@ public class DConnection : Thread
/* TODO: Do reply */ /* TODO: Do reply */
/* Encode the reply */ /* Encode the reply */
byte[] reply = [true]; reply = [true];
/* TODO: Implement me, use return value */
writeSocket(tag, reply);
} }
/* If `list` command (requires: authed, client) */ /* If `list` command (requires: authed, client) */
else if(command == Command.LIST && hasAuthed && connType == ConnectionType.CLIENT) else if(command == Command.LIST && hasAuthed && connType == ConnectionType.CLIENT)
@ -355,11 +345,8 @@ public class DConnection : Thread
/* TODO: Reply */ /* TODO: Reply */
/* Encode the reply */ /* Encode the reply */
byte[] reply = [true]; reply = [true];
reply ~= channelList; reply ~= channelList;
/* TODO: Implement me, use return value */
writeSocket(tag, reply);
} }
/* If `msg` command (requires: authed, client) */ /* If `msg` command (requires: authed, client) */
else if(command == Command.MSG && hasAuthed && connType == ConnectionType.CLIENT) else if(command == Command.MSG && hasAuthed && connType == ConnectionType.CLIENT)
@ -415,8 +402,7 @@ public class DConnection : Thread
/* Encode the reply */ /* Encode the reply */
/* TODO: */ /* TODO: */
byte[] reply = [status]; reply = [status];
writeSocket(tag, reply);
} }
/* If no matching built-in command was found */ /* If no matching built-in command was found */
else else
@ -433,11 +419,13 @@ public class DConnection : Thread
else else
{ {
/* Write error message */ /* Write error message */
byte[] reply = [2]; reply = [2];
}
}
/* Write the response */
writeSocket(tag, reply); writeSocket(tag, reply);
} }
}
}
/** /**
* Authenticate * Authenticate