-
Notifications
You must be signed in to change notification settings - Fork 41
/
AMServer.m
157 lines (120 loc) · 4.03 KB
/
AMServer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//Copyright (C) 2008 Antoine Mercadal
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#import "AMServer.h"
#import "AMResourcesHelper.h"
@implementation AMServer
@synthesize serverName;
@synthesize host;
@synthesize username;
@synthesize password;
@synthesize port;
@synthesize statusImagePath;
@synthesize standartOutput;
@synthesize standartInput;
#pragma mark -
#pragma mark Initializations
- (id) init
{
self = [super init];
[self setStandartOutput: [NSPipe pipe]];
[self pingHost];
return self;
}
- (void) dealloc
{
host = nil;
port = nil;
username = nil;
password = nil;
serverName = nil;
statusImagePath = nil;
stdOut = nil;
}
- (id) initWithCoder:(NSCoder *)coder
{
self = [super init];
host = [coder decodeObjectForKey:@"host"];
port = [coder decodeObjectForKey:@"port"];
username = [coder decodeObjectForKey:@"username"];
password = [coder decodeObjectForKey:@"password"];
serverName = [coder decodeObjectForKey:@"serverName"];
[self setStandartInput:[NSPipe pipe]];
[self pingHost];
return self;
}
- (void) encodeWithCoder:(NSCoder *) coder
{
[coder encodeObject:host forKey:@"host"];
[coder encodeObject:port forKey:@"port"];
[coder encodeObject:username forKey:@"username"];
[coder encodeObject:password forKey:@"password"];
[coder encodeObject:serverName forKey:@"serverName"];
}
#pragma mark -
#pragma mark Overloaded accessors
- (NSString *) description
{
return serverName;
}
#pragma mark -
#pragma mark Observers and delegates
- (void) handleEndOfPing:(NSNotification *) aNotification
{
NSData *data;
NSString *outputContent;
NSPredicate *checkSuccess;
data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem];
outputContent = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
checkSuccess = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] '0% packet loss'"];
if ([checkSuccess evaluateWithObject:outputContent] == YES)
[self setStatusImagePath:[AMResourcesHelper pathForImageNamed:@"statusGreen"]];
else
[self setStatusImagePath:[AMResourcesHelper pathForImageNamed:@"statusRed"]];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadCompletionNotification object:[stdOut fileHandleForReading]];
[ping terminate];
data = nil;
outputContent = nil;
checkSuccess = nil;
ping = nil;
}
#pragma mark -
#pragma mark Helper methods
- (void) pingHost
{
ping = [[NSTask alloc] init];
stdOut = [NSPipe pipe];
if ([self host] == nil)
return;
[self setStatusImagePath:[AMResourcesHelper pathForImageNamed:@"statusOrange"]];
[ping setLaunchPath:@"/sbin/ping"];
[ping setArguments:[NSArray arrayWithObjects:@"-c", @"1", @"-t", @"2", [self host], nil]];
[ping setStandardOutput:stdOut];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleEndOfPing:)
name:NSFileHandleReadToEndOfFileCompletionNotification
object:[[ping standardOutput] fileHandleForReading]];
[[stdOut fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
[ping launch];
}
- (void) openShellOnThisServer
{
NSTask *shellTask = [[NSTask alloc] init];
[shellTask setLaunchPath:[[NSBundle mainBundle] pathForResource:@"SSHShell" ofType:@"command"]];
[shellTask setArguments:[NSArray arrayWithObjects:[self username], [self password], nil]];
[shellTask setStandardOutput:[self standartOutput]];
[shellTask launch];
}
@end