Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -353,5 +353,52 @@ /** * \return The service of the node for which translation was requested */ - (const char*)service; @end + +/** + * An OFException indicating that the connection could not be established. + */ +@interface OFConnectionFailedException: OFException +{ + char *host; + uint16_t port; +} + +/** + * \param h The host to which the connection failed + * \param p The port on the host to which the connection failed + * \return A new connection failed exception + */ ++ newWithObject: (id)obj + andHost: (const char*)h + andPort: (uint16_t)p; + +/** + * Initializes an already allocated connection failed exception. + * + * \param h The host to which the connection failed + * \param p The port on the host to which the connection failed + * \return An initialized connection failed exception + */ +- initWithObject: (id)obj + andHost: (const char*)h + andPort: (uint16_t)p; + +- free; + +/** + * \return An error message for the exception as a C string. + */ +- (const char*)cString; + +/** + * \return The host to which the connection failed + */ +- (const char*)host; + +/** + * \return The port on the host to which the connection failed + */ +- (uint16_t)port; +@end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -380,16 +380,16 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "The specified node with the specified service could " - "not be translated to an address for an object of type %s. This " - "means that either the node was not found, there is no such " - "service on the specified node, there was a problem with the name " - "server, there was a problem with your network connection or you " - "specified an invalid node or service.", [object name]); + asprintf(&string, "The service %s on %s could not be translated to an " + "address for an object of type %s. This means that either the node " + "was not found, there is no such service on the node, there was a " + "problem with the name server, there was a problem with your " + "network connection or you specified an invalid node or service.", + service, node, [object name]); return string; } - (const char*)node @@ -400,5 +400,57 @@ - (const char*)service { return service; } @end + +@implementation OFConnectionFailedException ++ newWithObject: (id)obj + andHost: (const char*)h + andPort: (uint16_t)p +{ + return [self newWithObject: obj + andHost: h + andPort: p]; +} + +- initWithObject: (id)obj + andHost: (const char*)h + andPort: (uint16_t)p +{ + if ((self = [super initWithObject: obj])) { + host = (h != NULL ? strdup(h) : NULL); + port = p; + } + + return self; +} + +- free +{ + if (host != NULL) + free(host); + + return [super free]; +} + +- (const char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "A connection to %s:%d could not be established in " + "object of type %s!", host, port, [object name]); + + return string; +} + +- (const char*)host +{ + return host; +} + +- (uint16_t)port +{ + return port; +} +@end Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -93,14 +93,14 @@ break; } freeaddrinfo(res0); - if (sock < 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock < 0) + @throw [OFConnectionFailedException newWithObject: self + andHost: host + andPort: port]; return self; } - bindOn: (const char*)host