Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
805 - 1
// ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (C) 2010, Frank Blumenberg
3
//
4
// See License.txt for complete licensing and attribution information.
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//
23
// ///////////////////////////////////////////////////////////////////////////////
24
 
25
 
26
#import "MKIpConnection.h"
27
#import "AsyncSocket.h"
28
 
29
static NSString * const MKIpConnectionException = @"MKIpConnectionException";
30
 
31
@implementation MKIpConnection
32
 
33
#pragma mark Properties
34
 
35
@synthesize delegate;
36
 
37
#pragma mark Initialization
38
 
39
- (id) init {
40
  return [self initWithDelegate:nil];
41
}
42
 
43
- (id) initWithDelegate:(id<MKConnectionDelegate>)theDelegate;
44
{
45
  if (self = [super init]) {
46
 
47
    asyncSocket = [[AsyncSocket alloc] init];
48
    [asyncSocket setDelegate:self];  
49
 
50
    self.delegate = theDelegate;
51
  }
52
  return self;
53
}
54
 
55
- (void) dealloc {
56
  DLog("dealloc");
57
  [asyncSocket release];
58
  [super dealloc];
59
}
60
 
61
#pragma mark -
62
#pragma mark MKInput
63
 
64
- (BOOL) connectTo:(NSString *)hostOrDevice error:(NSError **)err;
65
{
66
  if (delegate == nil) {
67
    [NSException raise:MKIpConnectionException
68
                format:@"Attempting to connect without a delegate. Set a delegate first."];
69
  }
70
 
71
  NSArray * hostItems = [hostOrDevice componentsSeparatedByString:@":"];
72
  if ( [hostItems count] != 2 ) {
73
    [NSException raise:MKIpConnectionException
74
                format:@"Attempting to connect without a port. Set a port first."];
75
  }
76
 
77
  int port = [[hostItems objectAtIndex:1] intValue];
78
  NSString * host = [hostItems objectAtIndex:0];
79
 
80
  DLog(@"Try to connect to %@ on port %d", host, port);
81
  return [asyncSocket connectToHost:host onPort:port withTimeout:30 error:err];
82
}
83
 
84
- (BOOL) isConnected;
85
{
86
  return [asyncSocket isConnected];
87
}
88
 
89
- (void) disconnect;
90
{
91
  DLog(@"Try to disconnect from %@ on port %d", [asyncSocket connectedHost], [asyncSocket connectedPort]);
92
  [asyncSocket disconnect];
93
}
94
 
95
- (void) writeMkData:(NSData *)data;
96
{
97
//  NSMutableData * newData = [data mutableCopy];
98
//
99
//  [newData appendBytes:"\n" length:1];
100
 
101
  [asyncSocket writeData:data withTimeout:-1 tag:0];
102
 
103
//  [newData release];
104
}
105
 
106
#pragma mark -
107
#pragma mark AsyncSocketDelegate
108
 
109
- (BOOL) onSocketWillConnect:(AsyncSocket *)sock {
110
  DLog(@"About to connect to %S", [sock connectedHost]);
111
  return TRUE;
112
}
113
 
114
- (void) onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port;
115
{
116
  DLog(@"Did connect to %@ on port %d", host, port);
117
 
118
  if ( [delegate respondsToSelector:@selector(didConnectTo:)] ) {
119
    [delegate didConnectTo:host];
120
  }
121
 
122
 
123
  DLog(@"Start reading the first data frame");
124
  [sock readDataToData:[AsyncSocket CRData] withTimeout:-1 tag:0];
125
}
126
 
127
- (void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
128
{
129
  if ( [delegate respondsToSelector:@selector(didReadMkData:)] ) {
130
    [delegate didReadMkData:data];
131
  }
132
//  DLog(@"Did read data %@",data);
133
//
134
//  DLog(@"Start reading the next data frame");
135
  [sock readDataToData:[AsyncSocket CRData] withTimeout:-1 tag:0];
136
}
137
 
138
- (void) onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;
139
{
140
  DLog(@"Finished writing the next data frame");
141
}
142
 
143
- (void) onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
144
{
145
  ALog(@"Disconnet with an error %@", err);
146
  if ( [delegate respondsToSelector:@selector(willDisconnectWithError:)] ) {
147
    [delegate willDisconnectWithError:err];
148
  }
149
}
150
 
151
- (void) onSocketDidDisconnect:(AsyncSocket *)sock;
152
{
153
  DLog(@"Disconnect from %@ on port %d", [asyncSocket connectedHost], [asyncSocket connectedPort]);
154
 
155
  if ( [delegate respondsToSelector:@selector(didDisconnect)] ) {
156
    [delegate didDisconnect];
157
  }
158
}
159
 
160
@end