-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwesomeNetworkSyncer.mm
240 lines (175 loc) · 8.09 KB
/
AwesomeNetworkSyncer.mm
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// AwesomeNetworkSyncer.mm
// awesomesauce
//
// Created by Keegan Poppen on 3/17/11.
// Copyright 2011 Lord Keeganus Industries. All rights reserved.
//
#import "AwesomeNetworkSyncer.h"
#import "awesomesauceAppDelegate.h"
@implementation SquareChangeSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
NSString *matrix = (NSString*)[data objectForKey:@"matrix"];
int row = [[data objectForKey:@"row"] intValue];
int column = [[data objectForKey:@"column"] intValue];
BOOL value = [[data objectForKey:@"value"] boolValue];
int track_id = [[data objectForKey:@"track_id"] intValue];
NSLog(@"Square at (%d,%d) in matrix %@ should change to value: %d on track: %d", row, column, matrix, value, track_id);
if ([matrix caseInsensitiveCompare:@"future"] == NSOrderedSame) {
matrixHandler->getMatrix(track_id)->setFutureSquare(row, column, value);
//TODO: set the square in the future matrix. This also implies the JITness of creating a future matrix if !exists
} else {
matrixHandler->getMatrix(track_id)->setSquare(row, column, value);
//TODO: set square in the current matrix
}
}
-(void)presentMatrixSquareChangedAtRow:(int)row andColumn:(int)column toValue:(BOOL)squareValue withTrackId:(int)trackId {
[self sendSquareChangedInMatrix:@"present" AtRow:row andColumn:column toValue:squareValue withTrackId:trackId];
}
-(void)futureMatrixSquareChangedAtRow:(int)row andColumn:(int)column toValue:(BOOL)squareValue withTrackId:(int)trackId {
[self sendSquareChangedInMatrix:@"future" AtRow:row andColumn:column toValue:squareValue withTrackId:(int)trackId];
}
-(void)sendSquareChangedInMatrix:(NSString*)matrix AtRow:(int)row andColumn:(int)column toValue:(BOOL)squareValue withTrackId:(int)trackId{
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:row],@"row",
[NSNumber numberWithInt:column], @"column",
[NSNumber numberWithBool:squareValue], @"value",
[NSNumber numberWithInt:trackId], @"track_id",
matrix, @"matrix", nil]
withEventName:@"square_change"];
}
@end
//handler for track add messages
@implementation TrackAddSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
matrixHandler->addNewMatrix(false);
[(awesomesauceAppDelegate *)[[UIApplication sharedApplication] delegate] addMatrixInterface];
NSLog(@"received addTrack");
}
-(NSTimeInterval)sendTrackAddedWithId:(int)trackId {
return [networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:@"id",[NSNumber numberWithInt:trackId],nil] withEventName:@"track_add"];
NSLog(@"sending addTrack");
}
@end
//handler for track remove messages
@implementation TrackRemoveSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
//TODO: actually remove said track
}
-(void)sendTrackRemovedWithId:(int)trackId {
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:@"id",[NSNumber numberWithInt:trackId],nil] withEventName:@"track_remove"];
}
@end
//ASDataSyncee for track removing
@implementation TrackClearSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
int trackId = [[data objectForKey:@"id"] intValue];
NSLog(@"track id: %d", trackId);
matrixHandler->getMatrix(trackId)->clear();
NSLog(@"received clearTrack");
}
-(void)sendTrackClearedWithId:(int)trackId {
int num = matrixHandler->currentMatrix;
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:num],@"id",nil] withEventName:@"track_clear"];
NSLog(@"sending clearTrack %d", num);
}
@end
//handler for track add messages
@implementation FutureStartSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
int futureLength = [[data objectForKey:@"length"] intValue];
int mode = [[data objectForKey:@"mode"] intValue];
int trackId = [[data objectForKey:@"track_id"] intValue];
matrixHandler->getMatrix(trackId)->startFuture(futureLength, mode);
NSLog(@"received future");
}
-(void)sendFutureStartWithLength:(int)length withMode:(int)mode withTrackId:(int)trackId {
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:length],@"length",
[NSNumber numberWithInt:mode],@"mode",
[NSNumber numberWithInt:trackId], @"track_id",nil] withEventName:@"future_start"];
NSLog(@"sending future");
}
@end
//handler for instrument change messages
@implementation InstrumentChangeSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
int intrument_id = [[data objectForKey:@"instrument_id"] intValue];
int index = [[data objectForKey:@"index"] intValue];
int track_id = [[data objectForKey:@"track_id"] intValue];
//matrixHandler->getMatrix(track_id)->setOscillator(intrument_id, index); //TODO!!!
[(awesomesauceAppDelegate *)[[UIApplication sharedApplication] delegate] matrixChanged];
}
-(void)sendInstrumentChanged:(int)instrument withIndex:(int)index onTrack:(int)trackId {
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:instrument], @"instrument_id",
[NSNumber numberWithInt:index], @"index",
[NSNumber numberWithInt:trackId], @"track_id", nil] withEventName:@"instrument_change"];
}
@end
//handler for bpm change messages
@implementation BPMChangeSync
@synthesize networker;
@synthesize matrixHandler;
-(void)receiveData:(NSDictionary*)data fromTime:(NSTimeInterval)updateTime {
float bpm = [[data objectForKey:@"bpm"] floatValue];
matrixHandler->setBpm(bpm, false);
NSLog(@"receiving bpm changed");
[(awesomesauceAppDelegate *)[[UIApplication sharedApplication] delegate] updateBpmSlider:bpm];
}
-(void)sendBPMChanged:(float)bpm {
//NSLog(@"sending bpm changed1: %f", bpm);
float tempBpm = matrixHandler->bpm / 4.0;
[networker sendData:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:tempBpm],@"bpm",nil] withEventName:@"bpm_change"];
//NSLog(@"sending bpm changed2: %f", bpm);
}
@end
@implementation AwesomeNetworkSyncer
@synthesize networker;
@synthesize squareSync;
@synthesize trackAddSync, trackRemoveSync, trackClearSync, futureStartSync, instrumentChangeSync, bpmChangeSync;
- (id)initWithNetworker:(AwesomeNetworker*)awesomeNetworker andMatrixHandler:(MatrixHandler*)handler {
self = [super init];
if (self) {
networker = awesomeNetworker;
squareSync = [[SquareChangeSync alloc] init];
[networker registerEventHandler:@"square_change" withSyncee:squareSync];
squareSync.networker = networker;
squareSync.matrixHandler = handler;
trackAddSync = [[TrackAddSync alloc] init];
[networker registerEventHandler:@"track_add" withSyncee:trackAddSync];
trackAddSync.networker = networker;
trackAddSync.matrixHandler = handler;
trackRemoveSync = [[TrackRemoveSync alloc] init];
[networker registerEventHandler:@"track_remove" withSyncee:trackRemoveSync];
trackRemoveSync.networker = networker;
trackRemoveSync.matrixHandler = handler;
trackClearSync = [[TrackClearSync alloc] init];
[networker registerEventHandler:@"track_clear" withSyncee:trackClearSync];
trackClearSync.networker = networker;
trackClearSync.matrixHandler = handler;
futureStartSync = [[FutureStartSync alloc] init];
[networker registerEventHandler:@"future_start" withSyncee:futureStartSync];
futureStartSync.networker = networker;
futureStartSync.matrixHandler = handler;
instrumentChangeSync = [[InstrumentChangeSync alloc] init];
[networker registerEventHandler:@"instrument_change" withSyncee:instrumentChangeSync];
instrumentChangeSync.networker = networker;
instrumentChangeSync.matrixHandler = handler;
bpmChangeSync = [[BPMChangeSync alloc] init];
[networker registerEventHandler:@"bpm_change" withSyncee:bpmChangeSync];
bpmChangeSync.networker = networker;
bpmChangeSync.matrixHandler = handler;
}
return self;
}
@end