blob: d0ca6acd34c913af9b18c1f2d4eb942d8f59a3ee (
plain)
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
|
// SDLProtocolReceivedMessageRouter.m
//
// This class gets handed the SDLProtocol messages as they are received and decides what happens to them and where they are sent on to.
#import "SDLProtocolReceivedMessageRouter.h"
#import "SDLDebugTool.h"
#import "SDLProtocolMessage.h"
#import "SDLProtocolMessageAssembler.h"
@interface SDLProtocolReceivedMessageRouter ()
@property (assign) BOOL alreadyDestructed;
@property (strong) NSMutableDictionary *messageAssemblers;
@end
@implementation SDLProtocolReceivedMessageRouter
- (instancetype)init {
if (self = [super init]) {
_alreadyDestructed = NO;
self.messageAssemblers = [NSMutableDictionary dictionaryWithCapacity:2];
}
return self;
}
- (void)handleReceivedMessage:(SDLProtocolMessage *)message {
SDLFrameType frameType = message.header.frameType;
switch (frameType) {
case SDLFrameType_Single: {
[self sdl_dispatchProtocolMessage:message];
} break;
case SDLFrameType_Control: {
[self sdl_dispatchControlMessage:message];
} break;
case SDLFrameType_First: // fallthrough
case SDLFrameType_Consecutive: {
[self sdl_dispatchMultiPartMessage:message];
} break;
default: break;
}
}
- (void)sdl_dispatchProtocolMessage:(SDLProtocolMessage *)message {
if ([self.delegate respondsToSelector:@selector(onProtocolMessageReceived:)]) {
[self.delegate onProtocolMessageReceived:message];
}
}
- (void)sdl_dispatchControlMessage:(SDLProtocolMessage *)message {
// VERSION DEPENDENT CODE
switch (message.header.version) {
case 1: // fallthrough
case 2: // fallthrough
case 3: // fallthrough
case 4: {
[self sdl_dispatchV1ControlMessage:message];
} break;
case 5: {
// TODO: New delegates
[self sdl_dispatchV5ControlMessage:message];
} break;
}
}
- (void)sdl_dispatchV1ControlMessage:(SDLProtocolMessage *)message {
switch (message.header.frameData) {
case SDLFrameData_StartSessionACK: {
if ([self.delegate respondsToSelector:@selector(handleProtocolStartServiceACKMessage:)]) {
[self.delegate handleProtocolStartServiceACKMessage:message];
}
} break;
case SDLFrameData_StartSessionNACK: {
if ([self.delegate respondsToSelector:@selector(handleProtocolStartServiceNAKMessage:)]) {
[self.delegate handleProtocolStartServiceNAKMessage:message];
}
} break;
case SDLFrameData_EndSessionACK: {
if ([self.delegate respondsToSelector:@selector(handleProtocolEndServiceACKMessage:)]) {
[self.delegate handleProtocolEndServiceACKMessage:message];
}
} break;
case SDLFrameData_EndSessionNACK: {
if ([self.delegate respondsToSelector:@selector(handleProtocolStartServiceNAKMessage:)]) {
[self.delegate handleProtocolEndServiceNAKMessage:message];
}
} break;
case SDLFrameData_Heartbeat: {
if ([self.delegate respondsToSelector:@selector(handleHeartbeatForSession:)]) {
[self.delegate handleHeartbeatForSession:message.header.sessionID];
}
} break;
case SDLFrameData_HeartbeatACK: {
if ([self.delegate respondsToSelector:@selector(handleHeartbeatACK)]) {
[self.delegate handleHeartbeatACK];
}
} break;
default: break; // Other frame data is possible, but we don't care about them
}
}
- (void)sdl_dispatchV5ControlMessage:(SDLProtocolMessage *)message {
// TODO
}
- (void)sdl_dispatchMultiPartMessage:(SDLProtocolMessage *)message {
// Pass multipart messages to an assembler and call delegate when done.
NSNumber *sessionID = [NSNumber numberWithUnsignedChar:message.header.sessionID];
SDLProtocolMessageAssembler *assembler = self.messageAssemblers[sessionID];
if (assembler == nil) {
assembler = [[SDLProtocolMessageAssembler alloc] initWithSessionID:message.header.sessionID];
self.messageAssemblers[sessionID] = assembler;
}
SDLMessageAssemblyCompletionHandler completionHandler = ^void(BOOL done, SDLProtocolMessage *assembledMessage) {
if (done) {
[self sdl_dispatchProtocolMessage:assembledMessage];
}
};
[assembler handleMessage:message withCompletionHandler:completionHandler];
}
#pragma mark - Lifecycle
- (void)sdl_destructObjects {
if (!self.alreadyDestructed) {
self.alreadyDestructed = YES;
self.delegate = nil;
}
}
- (void)sdl_dispose {
[self sdl_destructObjects];
}
- (void)dealloc {
[self sdl_destructObjects];
}
@end
|