summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLRPCMessage.m
blob: d5762c0ce0230aba4223fbb934556798c1a55527 (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
//  SDLRPCMessage.m
//


#import "SDLRPCMessage.h"

#import "SDLNames.h"

NS_ASSUME_NONNULL_BEGIN

@implementation SDLRPCMessage

@synthesize messageType;

- (instancetype)initWithName:(NSString *)name {
    if (self = [super init]) {
        function = [[NSMutableDictionary alloc] initWithCapacity:3];
        parameters = [[NSMutableDictionary alloc] init];
        messageType = SDLNameRequest;
        [store setObject:function forKey:messageType];
        [function setObject:parameters forKey:SDLNameParameters];
        [function setObject:name forKey:SDLNameOperationName];
    }
    return self;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    if (self = [super initWithDictionary:dict]) {
        NSEnumerator *enumerator = [store keyEnumerator];
        while (messageType = [enumerator nextObject]) {
            if ([messageType isEqualToString:SDLNameBulkData] == FALSE) {
                break;
            }
        }
        if (messageType != nil) {
            function = [store objectForKey:messageType];
            parameters = [function objectForKey:SDLNameParameters];
        }
        self.bulkData = [dict objectForKey:SDLNameBulkData];
    }
    return self;
}

- (nullable NSString *)getFunctionName {
    return [function objectForKey:SDLNameOperationName];
}

- (void)setFunctionName:(nullable NSString *)functionName {
    if (functionName != nil) {
        [function setObject:functionName forKey:SDLNameOperationName];
    } else {
        [function removeObjectForKey:SDLNameOperationName];
    }
}

- (nullable NSObject *)getParameters:(NSString *)functionName {
    return [parameters objectForKey:functionName];
}

- (void)setParameters:(NSString *)functionName value:(nullable NSObject *)value {
    if (value != nil) {
        [parameters setObject:value forKey:functionName];
    } else {
        [parameters removeObjectForKey:functionName];
    }
}

- (void)dealloc {
    function = nil;
    parameters = nil;
}

- (NSString *)name {
    return [function objectForKey:SDLNameOperationName];
}

- (NSString *)description {
    NSMutableString *description = [NSMutableString stringWithFormat:@"%@ (%@)\n%@", self.name, self.messageType, self->parameters];

    return description;
}

@end

NS_ASSUME_NONNULL_END