Statistics
| Branch: | Tag: | Revision:

root / Classes / AddLoadBalancerAlgorithmViewController.m @ 72744ed1

History | View | Annotate | Download (9 kB)

1
//
2
//  AddLoadBalancerAlgorithmViewController.m
3
//  OpenStack
4
//
5
//  Created by Michael Mayo on 4/8/11.
6
//  Copyright 2011 __MyCompanyName__. All rights reserved.
7
//
8

    
9
#import "AddLoadBalancerAlgorithmViewController.h"
10
#import "OpenStackAccount.h"
11
#import "UIViewController+Conveniences.h"
12
#import <QuartzCore/QuartzCore.h>
13
#import "LoadBalancer.h"
14

    
15
#define kRandom 0
16
#define kRoundRobin 1
17
#define kWeightedRoundRobin 2
18
#define kLeastConnections 3
19
#define kWeightedLeastConnections 4
20

    
21
#define kNodes 5
22
#define kAnimationTime 0.5
23

    
24

    
25
@implementation AddLoadBalancerAlgorithmViewController
26

    
27
@synthesize account, loadBalancer, tableView, pickerView;
28

    
29
- (id)initWithAccount:(OpenStackAccount *)a {
30
    self = [super initWithNibName:@"AddLoadBalancerAlgorithmViewController" bundle:nil];
31
    if (self) {
32
        self.account = a;
33
    }
34
    return self;
35
}
36

    
37
- (void)dealloc {
38
    [account release];
39
    [loadBalancer release];
40
    [tableView release];
41
    [pickerView release];
42
    [loadBalancerIcon release];
43
    [serverIcons release];
44
    [dots release];
45
    [super dealloc];
46
}
47

    
48
#pragma mark - View lifecycle
49

    
50
- (void)viewDidLoad {
51
    [super viewDidLoad];
52
    self.navigationItem.title = @"Algorithm";
53
}
54

    
55
#pragma mark - Table view data source
56

    
57
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
58
    return 1;
59
}
60

    
61
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
62
    return 1;
63
}
64

    
65
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
66
    switch ([self.pickerView selectedRowInComponent:0]) {
67
        case kRandom:
68
            return @"Directs traffic to a randomly selected node.";
69
        case kRoundRobin:
70
            return @"Directs traffic in a circular pattern to each node of a load balancer in succession.";
71
        case kWeightedRoundRobin:
72
            return @"Directs traffic in a circular pattern to each node of a load balancer in succession with a larger proportion of requests being serviced by nodes with a greater weight.";
73
        case kLeastConnections:
74
            return @"Directs traffic to the node with the fewest open connections to the load balancer.";
75
        case kWeightedLeastConnections:
76
            return @"Directs traffic to the node with the fewest open connections between the load balancer.  Nodes with a larger weight will service more connections at any one time.";
77
        default:
78
            return @"";
79
    }
80
}
81

    
82
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
83
    return 132;
84
}
85

    
86
- (void)animateDot:(NSInteger)dotIndex toServer:(NSInteger)serverIndex {
87
    UIView *dot = [dots objectAtIndex:dotIndex];
88
    dot.frame = CGRectMake(10, 30, 6, 6);
89
    dot.alpha = 0;
90
    [dot setNeedsDisplay];
91

    
92
    [UIView animateWithDuration:kAnimationTime delay:dotIndex * kAnimationTime options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
93
        CGRect goal = dot.frame;
94
        goal.origin.x += 140;
95
        dot.frame = goal;
96
        dot.alpha = 1;
97
    } completion:^(BOOL finished) {
98
        [UIView animateWithDuration:kAnimationTime delay:0 options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
99
            
100
            if ([self.pickerView selectedRowInComponent:0] == kRandom) {                
101
                dot.center = [[serverIcons objectAtIndex:arc4random() % kNodes] center];
102
            } else if ([self.pickerView selectedRowInComponent:0] == kRoundRobin || [self.pickerView selectedRowInComponent:0] == kWeightedRoundRobin) {
103
                dot.center = [[serverIcons objectAtIndex:serverIndex] center];
104
            } else if ([self.pickerView selectedRowInComponent:0] == kLeastConnections || [self.pickerView selectedRowInComponent:0] == kWeightedLeastConnections) {
105
                if (dotIndex < kNodes - 2) {
106
                    dot.center = [[serverIcons objectAtIndex:0] center];
107
                } else {
108
                    dot.center = [[serverIcons objectAtIndex:1] center];
109
                }
110
            }
111
            
112
        } completion:^(BOOL finished) {
113
            if (dotIndex == kNodes - 1) {
114
                [self animateDots];
115
            }
116
        }];
117
    }];
118
}
119

    
120
- (void)animateDot:(NSTimer *)timer {
121
    [self animateDot:[[timer.userInfo objectForKey:@"dotIndex"] intValue] toServer:[[timer.userInfo objectForKey:@"serverIndex"] intValue]];
122
}
123

    
124
- (void)animateDots {
125
    for (int i = 0; i < kNodes; i++) {
126
        [self animateDot:i toServer:i];
127
    }
128
}
129

    
130
- (UITableViewCell *)algorithmCell:(UITableView *)tableView {
131
    static NSString *CellIdentifier = @"Cell";
132
    
133
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
134
    if (cell == nil) {
135
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
136
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
137
        
138
        loadBalancerIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"load-balancers-icon.png"]];
139
        loadBalancerIcon.center = cell.center;
140
        CGRect r = loadBalancerIcon.frame;
141
        r.origin.y += 10;
142
        loadBalancerIcon.frame = r;
143
//        loadBalancerIcon.clipsToBounds = NO;
144
//        [loadBalancerIcon.layer setShadowColor:[[UIColor blackColor] CGColor]];
145
//        [loadBalancerIcon.layer setShadowRadius:1.0f];
146
//        [loadBalancerIcon.layer setShadowOffset:CGSizeMake(1, 1)];
147
//        [loadBalancerIcon.layer setShadowOpacity:0.8f];
148
        
149
        [cell addSubview:loadBalancerIcon];
150
        
151
        serverIcons = [[NSMutableArray alloc] initWithCapacity:5];
152
        dots = [[NSMutableArray alloc] initWithCapacity:5];
153
        
154
        for (int i = 0; i < kNodes; i++) {
155
            UIImageView *server = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"custom-icon.png"]];
156
            UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 6, 6)];
157
            dot.backgroundColor = [UIColor redColor];
158
            dot.layer.cornerRadius = 3.5;
159
            server.frame = CGRectMake(20 + (61 * i), 80, 35, 35);
160
//            server.clipsToBounds = NO;
161
//            [server.layer setShadowColor:[[UIColor blackColor] CGColor]];
162
//            [server.layer setShadowRadius:1.0f];
163
//            [server.layer setShadowOffset:CGSizeMake(1, 1)];
164
//            [server.layer setShadowOpacity:0.8f];
165
            [cell addSubview:server];
166
            [cell addSubview:dot];
167
            [cell sendSubviewToBack:dot];
168
            [serverIcons addObject:server];
169
            [dots addObject:dot];
170
            [server release];
171
            [dot release];
172
        }
173
        
174
        //[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(animateDots) userInfo:nil repeats:NO];
175
        [self animateDots];
176
        
177
    }
178
    
179
    return cell;
180
}
181

    
182
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
183
    
184
    return [self algorithmCell:self.tableView];
185
    
186
    static NSString *CellIdentifier = @"Cell";
187
    
188
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
189
    if (cell == nil) {
190
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
191
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
192
    }
193
    
194
    switch (indexPath.section) {
195
        case kRandom:
196
            cell.textLabel.text = @"Random";
197
            break;
198
        case kRoundRobin:
199
            cell.textLabel.text = @"Round Robin";
200
            break;
201
        case kWeightedRoundRobin:
202
            cell.textLabel.text = @"Weighted Round Robin";
203
            break;
204
        case kLeastConnections:
205
            cell.textLabel.text = @"Least Connections";
206
            break;
207
        case kWeightedLeastConnections:
208
            cell.textLabel.text = @"Weighted Least Connections";
209
            break;
210
        default:
211
            break;
212
    }
213
    return cell;
214
}
215

    
216
#pragma mark - Table view delegate
217

    
218
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
219
}
220

    
221
#pragma mark - Picker data source
222

    
223
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
224
    return 1;
225
}
226

    
227
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
228
    return 5;
229
}
230

    
231
#pragma mark - Picker delegate
232

    
233
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
234
    switch (row) {
235
        case kRandom:
236
            return @"Random";
237
        case kRoundRobin:
238
            return @"Round Robin";
239
        case kWeightedRoundRobin:
240
            return @"Weighted Round Robin";
241
        case kLeastConnections:
242
            return @"Least Connections";
243
        case kWeightedLeastConnections:
244
            return @"Weighted Least Connections";
245
        default:
246
            return @"";
247
    }
248
}
249

    
250
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
251
    // change the animation
252
    [self.tableView reloadData];
253
    //[self animateDots];
254
}
255

    
256
@end