|
Wired Networking
2.0
Objective-C implementation of the Wired 2.0 protocol
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2006-2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredNetworking/NSString-WINetworking.h> 00030 #import <WiredNetworking/WIError.h> 00031 00032 NSString * const WIWiredNetworkingErrorDomain = @"WIWiredNetworkingErrorDomain"; 00033 NSString * const WILibWiredErrorDomain = @"WILibWiredErrorDomain"; 00034 NSString * const WILibWiredErrorKey = @"WILibWiredErrorKey"; 00035 00036 00037 @implementation WIError 00038 00039 + (id)errorWithDomain:(NSString *)domain code:(NSInteger)code argument:(id)argument { 00040 NSDictionary *userInfo; 00041 00042 userInfo = argument ? [NSDictionary dictionaryWithObject:argument forKey:WIArgumentErrorKey] : NULL; 00043 00044 return [self errorWithDomain:domain code:code userInfo:userInfo]; 00045 } 00046 00047 00048 00049 + (id)errorWithDomain:(NSString *)domain { 00050 return [self errorWithDomain:domain code:0 userInfo:NULL]; 00051 } 00052 00053 00054 00055 - (id)initWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)userInfo { 00056 NSString *description; 00057 wi_pool_t *pool; 00058 00059 if([domain isEqualToString:WILibWiredErrorDomain]) { 00060 pool = wi_pool_init(wi_pool_alloc()); 00061 00062 code = wi_error_code(); 00063 description = [NSString stringWithWiredString:wi_error_string()]; 00064 userInfo = userInfo ? [[userInfo mutableCopy] autorelease] : [NSMutableDictionary dictionary]; 00065 00066 if(description) 00067 [(NSMutableDictionary *) userInfo setObject:description forKey:NSLocalizedDescriptionKey]; 00068 00069 wi_release(pool); 00070 } 00071 00072 return [super initWithDomain:domain code:code userInfo:userInfo]; 00073 } 00074 00075 00076 00077 - (id)initWithDomain:(NSString *)domain { 00078 return [self initWithDomain:domain code:0 userInfo:NULL]; 00079 } 00080 00081 00082 00083 #pragma mark - 00084 00085 - (NSString *)localizedDescription { 00086 if([[self userInfo] objectForKey:NSLocalizedDescriptionKey]) 00087 return [[self userInfo] objectForKey:NSLocalizedDescriptionKey]; 00088 00089 if([[self domain] isEqualToString:WIWiredNetworkingErrorDomain]) { 00090 switch([self code]) { 00091 case WIAddressLookupFailed: 00092 return WILS(@"Address Lookup Failed", @"WIError: WIAddressLookupFailed title"); 00093 break; 00094 00095 case WIAddressNetServiceLookupFailed: 00096 return WILS(@"Address Lookup Failed", @"WIError: WIAddressNetServiceLookupFailed title"); 00097 break; 00098 00099 case WISocketConnectFailed: 00100 return WILS(@"Connect Failed", @"WIError: WISocketConnectFailed title"); 00101 break; 00102 00103 case WISocketListenFailed: 00104 return WILS(@"Listen Failed", @"WIError: WISocketListenFailed title"); 00105 break; 00106 00107 case WISocketAcceptFailed: 00108 return WILS(@"Accept Failed", @"WIError: WISocketAcceptFailed title"); 00109 break; 00110 00111 case WISocketWriteFailed: 00112 return WILS(@"Socket Write Failed", @"WIError: WISocketWriteFailed title"); 00113 break; 00114 00115 case WISocketReadFailed: 00116 return WILS(@"Socket Read Failed", @"WIError: WISocketReadFailed title"); 00117 break; 00118 00119 case WIP7SpecLoadFailed: 00120 return WILS(@"Protocol Specification Load Failed", @"WIError: WIP7SpecLoadFailed title"); 00121 break; 00122 00123 default: 00124 break; 00125 } 00126 } 00127 00128 return [super localizedDescription]; 00129 } 00130 00131 00132 00133 - (NSString *)localizedFailureReason { 00134 NSString *error; 00135 id argument; 00136 00137 if([[self userInfo] objectForKey:NSLocalizedFailureReasonErrorKey]) 00138 return [[self userInfo] objectForKey:NSLocalizedFailureReasonErrorKey]; 00139 00140 if([[self domain] isEqualToString:WIWiredNetworkingErrorDomain]) { 00141 error = [[[self userInfo] objectForKey:WILibWiredErrorKey] localizedFailureReason]; 00142 argument = [[self userInfo] objectForKey:WIArgumentErrorKey]; 00143 00144 switch([self code]) { 00145 case WIAddressLookupFailed: 00146 return [NSSWF:WILS(@"Could not resolve the address \u201c%@\u201d: %@.", @"WIError: WIAddressLookupFailed description (hostname, underlying error)"), 00147 argument, error]; 00148 break; 00149 00150 case WIAddressNetServiceLookupFailed: 00151 return WILS(@"Could not retrieve address for server via Bonjour.", @"WIError: WIAddressNetServiceLookupFailed description"); 00152 break; 00153 00154 case WISocketConnectFailed: 00155 return [NSSWF:WILS(@"Could not connect to %@: %@.", @"WIError: WISocketConnectFailed description (address, underlying error)"), 00156 argument, error]; 00157 break; 00158 00159 case WISocketListenFailed: 00160 return [NSSWF:WILS(@"Could not listen on %@: %@.", @"WIError: WISocketListenFailed description (address, underlying error)"), 00161 argument, error]; 00162 break; 00163 00164 case WISocketAcceptFailed: 00165 return [NSSWF:WILS(@"Could not accept a connection for %@: %@.", @"WIError: WISocketAcceptFailed description (address, underlying error)"), 00166 argument, error]; 00167 break; 00168 00169 case WISocketWriteFailed: 00170 return [NSSWF:WILS(@"Could not write to %@: %@.", @"WIError: WISocketWriteFailed description (address, underlying error)"), 00171 argument, error]; 00172 break; 00173 00174 case WISocketReadFailed: 00175 return [NSSWF:WILS(@"Could not read from %@: %@.", @"WIError: WISocketReadFailed description (address, underlying error)"), 00176 argument, error]; 00177 break; 00178 00179 case WIP7SpecLoadFailed: 00180 if(argument) { 00181 return [NSSWF:WILS(@"Could not load %@: %@.", @"WIError: WIP7SpecLoadFailed description (path, underlying error)"), 00182 [argument lastPathComponent], error]; 00183 } else { 00184 return [NSSWF:WILS(@"Could not load specification: %@.", @"WIError: WIP7SpecLoadFailed description (underlying error)"), 00185 error]; 00186 } 00187 break; 00188 00189 default: 00190 break; 00191 } 00192 } 00193 else if([[self domain] isEqualToString:WILibWiredErrorDomain]) { 00194 return [self localizedDescription]; 00195 } 00196 00197 return [super localizedFailureReason]; 00198 } 00199 00200 @end