1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
6  SOPE is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOPE; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#import <Foundation/Foundation.h>
23#import <NGExtensions/NGExtensions.h>
24#import <NGExtensions/NSException+misc.h>
25#include "NGSocketExceptions.h"
26
27@implementation NGSocketException
28
29- (id)init {
30  return [self initWithReason:@"a socket exception occured" socket:nil];
31}
32- (id)initWithReason:(NSString *)_reason {
33  return [self initWithReason:_reason socket:nil];
34}
35
36- (id)initWithReason:(NSString *)_reason socket:(id<NGSocket>)_socket {
37  self = [super initWithName:NSStringFromClass([self class])
38                reason:_reason
39                userInfo:nil];
40  if (self) {
41    self->socket = _socket;
42  }
43  return self;
44}
45
46- (id<NGSocket>)socket {
47  return self->socket;
48}
49
50@end /* NGSocketException */
51
52@implementation NGCouldNotResolveHostNameException
53
54- (id)init {
55  return [self initWithHostName:@"<noname>" reason:nil];
56}
57
58- (id)initWithHostName:(NSString *)_name reason:(NSString *)_reason {
59  NSString *r;
60
61  r = [[NSString alloc] initWithFormat:@"Could not resolve host %@: %@",
62			  _name, _reason ? _reason : (NSString *)@"error"];
63  if ((self = [super initWithReason:r socket:nil]) != nil) {
64    self->hostName = [_name copy];
65  }
66  [r release]; r = nil;
67
68  return self;
69}
70
71- (void)dealloc {
72  [self->hostName  release];
73  [super dealloc];
74}
75
76/* accessors */
77
78- (NSString *)hostName {
79  return self->hostName;
80}
81
82@end /* NGCouldNotResolveHostNameException */
83
84@implementation NGDidNotFindServiceException
85
86- (id)init {
87  return [self initWithServiceName:nil];
88}
89- (id)initWithServiceName:(NSString *)_service {
90  self = [super initWithReason:
91                  [NSString stringWithFormat:@"did not find service %@", _service]
92                socket:nil];
93  if (self) {
94    self->serviceName = [_service copy];
95  }
96  return self;
97}
98
99- (void)dealloc {
100  [self->serviceName release];
101  [super dealloc];
102}
103
104- (NSString *)serviceName {
105  return self->serviceName;
106}
107
108@end /* NGDidNotFindServiceException */
109
110@implementation NGInvalidSocketDomainException
111
112- (id)initWithReason:(NSString *)_reason
113  socket:(id<NGSocket>)_socket
114  domain:(id<NGSocketDomain>)_domain {
115
116  if ((self = [super initWithReason:_reason socket:nil])) {
117    self->domain = [_domain retain];
118  }
119  return self;
120}
121
122- (void)dealloc {
123  [self->domain release];
124  [super dealloc];
125}
126
127- (id<NGSocketDomain>)domain {
128  return self->domain;
129}
130
131@end /* NGInvalidSocketDomainException */
132
133@implementation NGCouldNotCreateSocketException
134
135- (id)init {
136  return [self initWithReason:@"Could not create socket" domain:nil];
137}
138- (id)initWithReason:(NSString *)_reason domain:(id<NGSocketDomain>)_domain {
139  if ((self = [super initWithReason:_reason socket:nil])) {
140    self->domain = [_domain retain];
141  }
142  return self;
143}
144
145- (void)dealloc {
146  [self->domain release];
147  [super dealloc];
148}
149
150- (id<NGSocketDomain>)domain {
151  return self->domain;
152}
153
154@end /* NGCouldNotCreateSocketException */
155
156// ******************** bind ***********************
157
158@implementation NGSocketBindException
159@end /* NGSocketBindException */
160
161@implementation NGSocketAlreadyBoundException
162
163- (id)init {
164  return [self initWithReason:@"Socket is already bound"];
165}
166
167@end /* NGSocketAlreadyBoundException */
168
169@implementation NGCouldNotBindSocketException
170
171- (id)init {
172  return [self initWithReason:@"could not bind socket" socket:nil address:nil];
173}
174
175- (id)initWithReason:(NSString *)_reason
176  socket:(id<NGSocket>)_socket
177  address:(id<NGSocketAddress>)_address {
178
179  if ((self = [super initWithReason:_reason socket:_socket])) {
180    self->address = [_address retain];
181  }
182  return self;
183}
184
185- (void)dealloc {
186  [self->address release];
187  [super dealloc];
188}
189
190- (id<NGSocketAddress>)address {
191  return self->address;
192}
193
194@end /* NGCouldNotBindSocketException */
195
196// ******************** connect ********************
197
198@implementation NGSocketConnectException
199@end /* NGSocketConnectException */
200
201@implementation NGSocketNotConnectedException
202
203- (id)init {
204  return [self initWithReason:@"Socket is not connected"];
205}
206
207@end /* NGSocketNotConnectedException */
208
209@implementation NGSocketAlreadyConnectedException
210
211- (id)init {
212  return [self initWithReason:@"Socket is already connected"];
213}
214
215@end /* NGSocketAlreadyConnectedException */
216
217@implementation NGCouldNotConnectException
218
219- (id)init {
220  return [self initWithReason:@"could not connect socket" socket:nil address:nil];
221}
222
223- (id)initWithReason:(NSString *)_reason
224  socket:(id<NGActiveSocket>)_socket
225  address:(id<NGSocketAddress>)_address {
226
227  if ((self = [super initWithReason:_reason socket:_socket])) {
228    self->address = [_address retain];
229  }
230  return self;
231}
232
233- (void)dealloc {
234  [self->address release];
235  [super dealloc];
236}
237
238- (id<NGSocketAddress>)address {
239  return self->address;
240}
241
242@end /* NGCouldNotConnectException */
243
244// ******************** listen ********************
245
246@implementation NGSocketIsAlreadyListeningException
247
248- (id)init {
249  return [self initWithReason:@"Socket is already listening"];
250}
251
252@end /* NGSocketIsAlreadyListeningException */
253
254@implementation NGCouldNotListenException
255@end /* NGCouldNotListenException */
256
257// ******************** accept ********************
258
259@implementation NGCouldNotAcceptException
260@end /* NGCouldNotAcceptException */
261
262// ******************** options ********************
263
264@implementation NGSocketOptionException
265
266- (id)init {
267  return [self initWithReason:@"Could not get/set socket option" option:-1 level:-1];
268}
269- (id)initWithReason:(NSString *)_reason option:(int)_option level:(int)_level {
270  if ((self = [super initWithReason:_reason])) {
271    option = _option;
272    level  = _level;
273  }
274  return self;
275}
276
277- (int)option {
278  return option;
279}
280- (int)level {
281  return level;
282}
283
284@end /* NGSocketOptionException */
285
286@implementation NGCouldNotSetSocketOptionException
287@end /* NGCouldNotSetSocketOptionException */
288
289@implementation NGCouldNotGetSocketOptionException
290@end /* NGCouldNotGetSocketOptionException */
291
292// ******************** socket closed **************
293
294@implementation NGSocketShutdownException
295
296- (id)init {
297  return [self initWithStream:nil reason:@"the socket was shutdown"];
298}
299- (id)initWithReason:(NSString *)_reason {
300  return [self initWithStream:nil reason:_reason];
301}
302- (id)initWithReason:(NSString *)_reason socket:(id<NGActiveSocket>)_socket {
303  return [self initWithStream:_socket reason:_reason];
304}
305
306- (id)initWithSocket:(id<NGActiveSocket>)_socket {
307  return [self initWithStream:_socket reason:@"the socket was shutdown"];
308}
309
310/* accessors */
311
312- (id<NGActiveSocket>)socket {
313  return [self->streamPointer nonretainedObjectValue];
314}
315
316@end /* NGSocketShutdownException */
317
318@implementation NGSocketShutdownDuringReadException
319@end
320
321@implementation NGSocketShutdownDuringWriteException
322@end
323
324@implementation NGSocketTimedOutException
325@end
326
327@implementation NGSocketConnectionResetException
328@end
329