diff --git a/source/birchwood/client/core.d b/source/birchwood/client/client.d similarity index 90% rename from source/birchwood/client/core.d rename to source/birchwood/client/client.d index 78c66ed..cb1d975 100644 --- a/source/birchwood/client/core.d +++ b/source/birchwood/client/client.d @@ -1,4 +1,4 @@ -module birchwood.client.core; +module birchwood.client.client; import std.socket : Socket, SocketException, Address, getAddress, SocketType, ProtocolType, SocketOSException; import std.socket : SocketFlags; @@ -8,6 +8,8 @@ import core.sync.mutex : Mutex; import core.thread : Thread, dur; import std.string; import eventy; +import birchwood.client.conninfo : ConnectionInfo; +import birchwood.client.exceptions : BirchwoodException; import birchwood.messages : Message, encodeMessage, decodeMessage, isValidText; import birchwood.constants : ReplyType; @@ -22,143 +24,7 @@ __gshared static this() } -public class BirchwoodException : Exception -{ - public enum ErrorType - { - INVALID_CONN_INFO, - ALREADY_CONNECTED, - CONNECT_ERROR, - EMPTY_PARAMS, - INVALID_CHANNEL_NAME, - INVALID_NICK_NAME, - ILLEGAL_CHARACTERS - } - private ErrorType errType; - - /* Auxillary error information */ - /* TODO: Make these actually Object */ - private string auxInfo; - - this(ErrorType errType) - { - super("BirchwoodError("~to!(string)(errType)~")"~(auxInfo.length == 0 ? "" : " "~auxInfo)); - this.errType = errType; - } - - this(ErrorType errType, string auxInfo) - { - this(errType); - this.auxInfo = auxInfo; - } - - public ErrorType getType() - { - return errType; - } -} - -public struct ConnectionInfo -{ - /* Server address information */ - private Address addrInfo; - private string nickname; - - /* Misc. */ - /* TODO: Make final/const (find out difference) */ - private ulong bulkReadSize; - - /* Client behaviour (TODO: what is sleep(0), like nothing) */ - private ulong fakeLag = 0; - - /* The quit message */ - public const string quitMessage; - - /* TODO: before publishing change this bulk size */ - private this(Address addrInfo, string nickname, ulong bulkReadSize = 20, string quitMessage = "birchwood client disconnecting...") - { - this.addrInfo = addrInfo; - this.nickname = nickname; - this.bulkReadSize = bulkReadSize; - this.quitMessage = quitMessage; - } - - public ulong getBulkReadSize() - { - return this.bulkReadSize; - } - - public Address getAddr() - { - return addrInfo; - } - - /** - * Creates a ConnectionInfo struct representing a client configuration which - * can be provided to the Client class to create a new connection based on its - * parameters - * - * Params: - * hostname = hostname of the server - * port = server port - * nickname = nickname to use - * Returns: ConnectionInfo for this server - */ - public static ConnectionInfo newConnection(string hostname, ushort port, string nickname) - { - try - { - /* Attempt to resolve the address (may throw SocketException) */ - Address[] addrInfo = getAddress(hostname, port); - - /* Username check */ - if(!nickname.length) - { - throw new BirchwoodException(BirchwoodException.ErrorType.INVALID_CONN_INFO); - } - - /* TODO: Add feature to choose which address to use, prefer v4 or v6 type of thing */ - Address chosenAddress = addrInfo[0]; - - return ConnectionInfo(chosenAddress, nickname); - } - catch(SocketException e) - { - throw new BirchwoodException(BirchwoodException.ErrorType.INVALID_CONN_INFO); - } - } - - /** - * Tests invalid conneciton information - * - * 1. Invalid hostnames - * 2. Invalid usernames - */ - unittest - { - try - { - newConnection("1.", 21, "deavmi"); - assert(false); - } - catch(BirchwoodException e) - { - assert(e.getType() == BirchwoodException.ErrorType.INVALID_CONN_INFO); - } - - try - { - newConnection("1.1.1.1", 21, ""); - assert(false); - } - catch(BirchwoodException e) - { - assert(e.getType() == BirchwoodException.ErrorType.INVALID_CONN_INFO); - } - - } -} // TODO: Make abstract and for unit tests make a `DefaultClient` // ... which logs outputs for the `onX()` handler functions diff --git a/source/birchwood/client/conninfo.d b/source/birchwood/client/conninfo.d new file mode 100644 index 0000000..cf1e871 --- /dev/null +++ b/source/birchwood/client/conninfo.d @@ -0,0 +1,105 @@ +module birchwood.client.conninfo; + +import std.socket : SocketException, Address, getAddress; +import birchwood.client.exceptions : BirchwoodException; + +public struct ConnectionInfo +{ + /* Server address information */ + private Address addrInfo; + private string nickname; + + /* Misc. */ + /* TODO: Make final/const (find out difference) */ + private ulong bulkReadSize; + + /* Client behaviour (TODO: what is sleep(0), like nothing) */ + private ulong fakeLag = 0; + + /* The quit message */ + public const string quitMessage; + + /* TODO: before publishing change this bulk size */ + private this(Address addrInfo, string nickname, ulong bulkReadSize = 20, string quitMessage = "birchwood client disconnecting...") + { + this.addrInfo = addrInfo; + this.nickname = nickname; + this.bulkReadSize = bulkReadSize; + this.quitMessage = quitMessage; + } + + public ulong getBulkReadSize() + { + return this.bulkReadSize; + } + + public Address getAddr() + { + return addrInfo; + } + + /** + * Creates a ConnectionInfo struct representing a client configuration which + * can be provided to the Client class to create a new connection based on its + * parameters + * + * Params: + * hostname = hostname of the server + * port = server port + * nickname = nickname to use + * Returns: ConnectionInfo for this server + */ + public static ConnectionInfo newConnection(string hostname, ushort port, string nickname) + { + try + { + /* Attempt to resolve the address (may throw SocketException) */ + Address[] addrInfo = getAddress(hostname, port); + + /* Username check */ + if(!nickname.length) + { + throw new BirchwoodException(BirchwoodException.ErrorType.INVALID_CONN_INFO); + } + + /* TODO: Add feature to choose which address to use, prefer v4 or v6 type of thing */ + Address chosenAddress = addrInfo[0]; + + return ConnectionInfo(chosenAddress, nickname); + } + catch(SocketException e) + { + throw new BirchwoodException(BirchwoodException.ErrorType.INVALID_CONN_INFO); + } + } + + /** + * Tests invalid conneciton information + * + * 1. Invalid hostnames + * 2. Invalid usernames + */ + unittest + { + try + { + newConnection("1.", 21, "deavmi"); + assert(false); + } + catch(BirchwoodException e) + { + assert(e.getType() == BirchwoodException.ErrorType.INVALID_CONN_INFO); + } + + try + { + newConnection("1.1.1.1", 21, ""); + assert(false); + } + catch(BirchwoodException e) + { + assert(e.getType() == BirchwoodException.ErrorType.INVALID_CONN_INFO); + } + + } +} \ No newline at end of file diff --git a/source/birchwood/client/exceptions.d b/source/birchwood/client/exceptions.d new file mode 100644 index 0000000..fd0c088 --- /dev/null +++ b/source/birchwood/client/exceptions.d @@ -0,0 +1,40 @@ +module birchwood.client.exceptions; + +import std.conv : to; + +public class BirchwoodException : Exception +{ + public enum ErrorType + { + INVALID_CONN_INFO, + ALREADY_CONNECTED, + CONNECT_ERROR, + EMPTY_PARAMS, + INVALID_CHANNEL_NAME, + INVALID_NICK_NAME, + ILLEGAL_CHARACTERS + } + + private ErrorType errType; + + /* Auxillary error information */ + /* TODO: Make these actually Object */ + private string auxInfo; + + this(ErrorType errType) + { + super("BirchwoodError("~to!(string)(errType)~")"~(auxInfo.length == 0 ? "" : " "~auxInfo)); + this.errType = errType; + } + + this(ErrorType errType, string auxInfo) + { + this(errType); + this.auxInfo = auxInfo; + } + + public ErrorType getType() + { + return errType; + } +} \ No newline at end of file diff --git a/source/birchwood/client/package.d b/source/birchwood/client/package.d index 46ce979..f1d7e89 100644 --- a/source/birchwood/client/package.d +++ b/source/birchwood/client/package.d @@ -1,3 +1,5 @@ module birchwood.client; -public import birchwood.client.core; \ No newline at end of file +public import birchwood.client.client; +public import birchwood.client.exceptions : BirchwoodException; +public import birchwood.client.conninfo : ConnectionInfo; \ No newline at end of file diff --git a/source/birchwood/client/receiver.d b/source/birchwood/client/receiver.d index 18ec02c..fcc9fb0 100644 --- a/source/birchwood/client/receiver.d +++ b/source/birchwood/client/receiver.d @@ -1,6 +1,6 @@ module birchwood.client.receiver; -import core.thread : Thread; +import core.thread : Thread, dur; import std.container.slist : SList; import core.sync.mutex : Mutex; diff --git a/source/birchwood/client/sender.d b/source/birchwood/client/sender.d index 886194d..4bbe45c 100644 --- a/source/birchwood/client/sender.d +++ b/source/birchwood/client/sender.d @@ -1,6 +1,6 @@ module birchwood.client.sender; -import core.thread : Thread; +import core.thread : Thread, dur; import std.container.slist : SList; import core.sync.mutex : Mutex;