-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMissingTopicHandlerExample.m
More file actions
109 lines (88 loc) · 3.85 KB
/
Copy pathMissingTopicHandlerExample.m
File metadata and controls
109 lines (88 loc) · 3.85 KB
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
// Diffusion Client Library for iOS, tvOS and OS X / macOS - Examples
//
// Copyright (C) 2016 - 2023 DiffusionData Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "MissingTopicHandlerExample.h"
@import Diffusion;
@interface MissingTopicHandlerExample (PTDiffusionMissingTopicHandler) <PTDiffusionMissingTopicHandler>
@end
@implementation MissingTopicHandlerExample {
PTDiffusionSession* _session;
}
-(void)startWithURL:(NSURL*)url {
PTDiffusionCredentials *const credentials =
[[PTDiffusionCredentials alloc] initWithPassword:@"password"];
PTDiffusionSessionConfiguration *const sessionConfiguration =
[[PTDiffusionSessionConfiguration alloc] initWithPrincipal:@"control"
credentials:credentials];
NSLog(@"Connecting...");
[PTDiffusionSession openWithURL:url
configuration:sessionConfiguration
completionHandler:^(PTDiffusionSession *session, NSError *error)
{
if (!session) {
NSLog(@"Failed to open session: %@", error);
return;
}
// At this point we now have a connected session.
NSLog(@"Connected.");
// Set ivar to maintain a strong reference to the session.
self->_session = session;
// Register as missing topic handler for a branch of the topic tree.
[self registerAsMissingTopicHandlerForSession:session];
}];
}
-(void)registerAsMissingTopicHandlerForSession:(PTDiffusionSession *const)session {
[session.topicControl addMissingTopicHandler:self
forTopicPath:@"Example/Control Client Handler"
completionHandler:^(PTDiffusionTopicTreeRegistration *const registration, NSError *const error)
{
if (registration) {
NSLog(@"Registered as missing topic handler.");
} else {
NSLog(@"Failed to register as missing topic handler. Error: %@", error);
}
}];
}
@end
@implementation MissingTopicHandlerExample (PTDiffusionMissingTopicHandler)
-(void)diffusionTopicTreeRegistration:(PTDiffusionTopicTreeRegistration *const)registration
hadMissingTopicNotification:(PTDiffusionMissingTopicNotification *const)notification {
NSString *const expression = notification.topicSelectorExpression;
NSLog(@"Received Missing Topic Notification: %@", expression);
// Expect a path pattern expression.
if (![expression hasPrefix:@">"]) {
NSLog(@"Topic selector expression is not a path pattern.");
return;
}
// Extract topic path from path pattern expression.
NSString *const topicPath = [expression substringFromIndex:1];
// Add a topic at this path.
[_session.topicControl addTopicWithPath:topicPath
type:PTDiffusionTopicType_JSON
completionHandler:^(PTDiffusionAddTopicResult *const result, NSError *const error)
{
if (error) {
NSLog(@"Failed to add topic.");
return;
}
}];
}
-(void)diffusionTopicTreeRegistrationDidClose:(PTDiffusionTopicTreeRegistration *)registration {
NSLog(@"Closed");
}
-(void)diffusionTopicTreeRegistration:(PTDiffusionTopicTreeRegistration *)registration
didFailWithError:(NSError *)error {
NSLog(@"Failed: %@", error);
}
@end